fixed a bug with new account logic when a user opts out of membership

pull/54/head
Chris Veilleux 2019-02-20 14:45:06 -06:00
parent 75fe9eed96
commit 24faf0fbbd
5 changed files with 78 additions and 52 deletions

View File

@ -1,11 +1,22 @@
Feature: Add a new account
Test the API call to add an account to the database.
Scenario: Successful account addition
When a valid new account request is submitted
Then the request will be successful
And the account will be added to the system
Scenario: Successful account addition with membership
Given a user completes on-boarding
And user opts into a membership
When the new account request is submitted
Then the request will be successful
And the account will be added to the system with a membership
Scenario: Successful account addition without membership
Given a user completes on-boarding
And user opts out of membership
When the new account request is submitted
Then the account will be added to the system without a membership
Scenario: Request missing a required field
When a request is sent without an email address
Given a user completes on-boarding
And user does not specify an email address
When the new account request is submitted
Then the request will fail with a bad request error

View File

@ -1,9 +1,8 @@
from datetime import date
from http import HTTPStatus
from behave import then, when
from behave import given, then, when
from flask import json
from hamcrest import assert_that, equal_to, is_in, not_none
from hamcrest import assert_that, equal_to, is_in, none, not_none
from selene.data.account import AccountRepository, PRIVACY_POLICY, TERMS_OF_USE
from selene.util.db import get_db_connection
@ -17,17 +16,38 @@ new_account_request = dict(
userEnteredEmail='bar@mycroft.ai',
password='bar'
),
support=dict(
openDataset=True,
membership='MONTHLY SUPPORTER',
stripeCustomerId='barstripe'
)
support=dict(openDataset=True)
)
@when('a valid new account request is submitted')
@given('a user completes on-boarding')
def build_new_account_request(context):
context.new_account_request = new_account_request
@given('user opts out of membership')
def add_maybe_later_membership(context):
context.new_account_request['support'].update(
membership='MAYBE LATER',
stripeCustomerId=None
)
@given('user opts into a membership')
def change_membership_option(context):
context.new_account_request['support'].update(
membership='MONTHLY SUPPORTER',
stripeCustomerId='barstripe'
)
@given('user does not specify an email address')
def remove_email_from_request(context):
del(context.new_account_request['login']['userEnteredEmail'])
@when('the new account request is submitted')
def call_add_account_endpoint(context):
context.new_account_request = new_account_request
context.client.content_type = 'application/json'
context.response = context.client.post(
'/api/account',
@ -36,22 +56,8 @@ def call_add_account_endpoint(context):
)
@when('a request is sent without an email address')
def create_account_without_email(context):
context.new_account_request = new_account_request
login_data = context.new_account_request['login']
del(login_data['userEnteredEmail'])
context.new_account_request['login'] = login_data
context.client.content_type = 'application/json'
context.response = context.client.post(
'/api/account',
data=json.dumps(context.new_account_request),
content_type='application_json'
)
@then('the account will be added to the system')
def check_db_for_account(context):
@then('the account will be added to the system {membership_option}')
def check_db_for_account(context, membership_option):
with get_db_connection(context.client_config['DB_CONNECTION_POOL']) as db:
acct_repository = AccountRepository(db)
account = acct_repository.get_account_by_email('bar@mycroft.ai')
@ -60,11 +66,15 @@ def check_db_for_account(context):
account.email_address, equal_to('bar@mycroft.ai')
)
assert_that(account.username, equal_to('barfoo'))
assert_that(account.subscription.type, equal_to('Monthly Supporter'))
assert_that(
account.subscription.stripe_customer_id,
equal_to('barstripe')
)
if membership_option == 'with a membership':
assert_that(account.subscription.type, equal_to('Monthly Supporter'))
assert_that(
account.subscription.stripe_customer_id,
equal_to('barstripe')
)
elif membership_option == 'without a membership':
assert_that(account.subscription, none())
assert_that(len(account.agreements), equal_to(2))
for agreement in account.agreements:
assert_that(agreement.type, is_in((PRIVACY_POLICY, TERMS_OF_USE)))

View File

@ -19,7 +19,7 @@ from selene.data.account import (
from selene.util.db import get_db_connection
from ..base_endpoint import SeleneEndpoint
membeship_types = {
membership_types = {
'MONTHLY SUPPORTER': 'Monthly Supporter',
'YEARLY SUPPORTER': 'Yearly Supporter',
'MAYBE LATER': 'Maybe Later'
@ -59,11 +59,11 @@ class Support(Model):
)
stripe_customer_id = StringType()
# def validate_stripe_customer_id(self, data, value):
# if data['membership'] != 'Maybe Later':
# if not data['stripe_customer_id']:
# raise ValidationError('Membership requires a stripe ID')
# return value
def validate_stripe_customer_id(self, data, value):
if data['membership'] != 'MAYBE LATER':
if not data['stripe_customer_id']:
raise ValidationError('Membership requires a stripe ID')
return value
class AddAccountRequest(Model):
@ -143,9 +143,17 @@ class AccountEndpoint(SeleneEndpoint):
return email_address, password
def _add_account(self, email_address, password):
membership_type = membeship_types[
membership_type = membership_types[
self.request_data['support']['membership']
]
subscription = None
if membership_type != 'Maybe Later':
subscription = AccountSubscription(
type=membership_type,
start_date=date.today(),
stripe_customer_id=self.request_data['support'][
'stripeCustomerId']
)
account = Account(
email_address=email_address,
username=self.request_data['username'],
@ -153,11 +161,7 @@ class AccountEndpoint(SeleneEndpoint):
AccountAgreement(type=PRIVACY_POLICY, accept_date=date.today()),
AccountAgreement(type=TERMS_OF_USE, accept_date=date.today())
],
subscription=AccountSubscription(
type=membership_type,
start_date=date.today(),
stripe_customer_id=self.request_data['support']['stripeCustomerId']
)
subscription=subscription
)
with get_db_connection(self.config['DB_CONNECTION_POOL']) as db:
acct_repository = AccountRepository(db)

View File

@ -26,6 +26,6 @@ class Account(object):
email_address: str
username: str
agreements: List[AccountAgreement]
subscription: AccountSubscription
subscription: AccountSubscription = None
id: str = None
refresh_tokens: List[str] = None

View File

@ -157,9 +157,10 @@ class AccountRepository(object):
for agreement in result['account']['agreements']:
account_agreements.append(AccountAgreement(**agreement))
result['account']['agreements'] = account_agreements
result['account']['subscription'] = AccountSubscription(
**result['account']['subscription']
)
if result['account']['subscription'] is not None:
result['account']['subscription'] = AccountSubscription(
**result['account']['subscription']
)
account = Account(**result['account'])
return account