moved api testing library code to the testing library and minor refactor of account tooling

pull/183/head
Chris Veilleux 2019-06-12 15:19:50 -05:00
parent 869e7ece42
commit de0ca3d1bf
4 changed files with 40 additions and 54 deletions

View File

@ -1,8 +0,0 @@
from .authentication import (
ACCESS_TOKEN_COOKIE_KEY,
generate_access_token,
generate_refresh_token,
get_account,
REFRESH_TOKEN_COOKIE_KEY,
validate_token_cookies
)

View File

@ -1,5 +0,0 @@
from .account import add_account, remove_account
from .account_geography import add_account_geography
from .agreement import add_agreements, remove_agreements
from .membership import insert_memberships, delete_memberships
from .test_db import create_test_db, drop_test_db

View File

@ -9,36 +9,14 @@ from selene.data.account import (
)
def build_test_membership(**overrides):
return AccountMembership(
type=overrides.get('type') or 'Monthly Membership',
start_date=overrides.get('start_date') or date.today(),
payment_method=overrides.get('payment_method') or 'Stripe',
payment_account_id=overrides.get('payment_account_id') or 'foo',
payment_id=overrides.get('payment_id') or 'bar'
)
def build_test_account(**overrides):
test_agreements = [
AccountAgreement(type=PRIVACY_POLICY, accept_date=date.today()),
AccountAgreement(type=TERMS_OF_USE, accept_date=date.today())
]
if 'membership' in overrides:
membership_overrides = overrides['membership']
if membership_overrides is None:
# if the membership override is set to None, the assumed intent
# is to add an account that does not have a membership
test_membership = None
else:
test_membership = build_test_membership(**membership_overrides)
else:
test_membership = build_test_membership()
return Account(
email_address=overrides.get('email_address') or 'foo@mycroft.ai',
username=overrides.get('username') or 'foobar',
membership=test_membership,
agreements=overrides.get('agreements') or test_agreements
)
@ -46,7 +24,7 @@ def build_test_account(**overrides):
def add_account(db, **overrides):
acct_repository = AccountRepository(db)
account = build_test_account(**overrides)
account.id = acct_repository.add(account, 'foo')
account.id = acct_repository.add(account, 'test_password')
if account.membership is not None:
acct_repository.add_membership(account.id, account.membership)
@ -56,3 +34,22 @@ def add_account(db, **overrides):
def remove_account(db, account):
account_repository = AccountRepository(db)
account_repository.remove(account)
def build_test_membership(**overrides):
stripe_acct = 'test_stripe_acct_id'
return AccountMembership(
type=overrides.get('type') or 'Monthly Membership',
start_date=overrides.get('start_date') or date.today(),
payment_method=overrides.get('payment_method') or 'Stripe',
payment_account_id=overrides.get('payment_account_id') or stripe_acct,
payment_id=overrides.get('payment_id') or 'test_stripe_payment_id'
)
def add_account_membership(db, account_id, **overrides):
membership = build_test_membership(**overrides)
acct_repository = AccountRepository(db)
acct_repository.add_membership(account_id, membership)
return membership

View File

@ -10,41 +10,43 @@ TWO_MINUTES = 120
REFRESH_TOKEN_COOKIE_KEY = 'seleneRefresh'
def generate_access_token(context, username='foo', expire=False):
def generate_access_token(context, duration=ONE_MINUTE):
access_token = AuthenticationToken(
context.client_config['ACCESS_SECRET'],
ONE_MINUTE
duration
)
if not expire:
if username == 'bar':
account_id = context.bar_account.id
else:
account_id = context.foo_account.id
access_token.generate(account_id)
context.access_token = access_token
account = context.accounts[context.username]
access_token.generate(account.id)
return access_token
def set_access_token_cookie(context, duration=ONE_MINUTE):
context.client.set_cookie(
context.client_config['DOMAIN'],
ACCESS_TOKEN_COOKIE_KEY,
access_token.jwt,
max_age=0 if expire else ONE_MINUTE
context.access_token.jwt,
max_age=duration
)
def generate_refresh_token(context, expire=False):
def generate_refresh_token(context, duration=TWO_MINUTES):
refresh_token = AuthenticationToken(
context.client_config['REFRESH_SECRET'],
TWO_MINUTES
duration
)
if not expire:
refresh_token.generate(context.account.id)
context.refresh_token = refresh_token
account = context.accounts[context.username]
refresh_token.generate(account.id)
return refresh_token
def set_refresh_token_cookie(context, duration=TWO_MINUTES):
context.client.set_cookie(
context.client_config['DOMAIN'],
REFRESH_TOKEN_COOKIE_KEY,
refresh_token.jwt,
max_age=0 if expire else TWO_MINUTES
context.refresh_token.jwt,
max_age=duration
)