refactored to build access and refresh tokens independently for re-usability in other tests.
parent
97cefc9ce1
commit
b98f667f30
|
@ -4,5 +4,5 @@ Feature: Manage account profiles
|
||||||
|
|
||||||
Scenario: Retrieve authenticated user's account
|
Scenario: Retrieve authenticated user's account
|
||||||
Given an authenticated user
|
Given an authenticated user
|
||||||
When account endpoint is called to get user profile
|
When a user requests their profile
|
||||||
Then user profile is returned
|
Then user profile is returned
|
||||||
|
|
|
@ -5,16 +5,17 @@ import json
|
||||||
from behave import given, then, when
|
from behave import given, then, when
|
||||||
from hamcrest import assert_that, equal_to, has_item
|
from hamcrest import assert_that, equal_to, has_item
|
||||||
|
|
||||||
from selene.api.testing import generate_auth_tokens
|
from selene.api.testing import generate_access_token, generate_refresh_token
|
||||||
from selene.data.account import PRIVACY_POLICY
|
from selene.data.account import PRIVACY_POLICY
|
||||||
|
|
||||||
|
|
||||||
@given('an authenticated user')
|
@given('an authenticated user')
|
||||||
def setup_authenticated_user(context):
|
def setup_authenticated_user(context):
|
||||||
generate_auth_tokens(context)
|
generate_access_token(context)
|
||||||
|
generate_refresh_token(context)
|
||||||
|
|
||||||
|
|
||||||
@when('account endpoint is called to get user profile')
|
@when('a user requests their profile')
|
||||||
def call_account_endpoint(context):
|
def call_account_endpoint(context):
|
||||||
context.response = context.client.get('/api/account')
|
context.response = context.client.get('/api/account')
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from .authentication import (
|
from .authentication import (
|
||||||
ACCESS_TOKEN_COOKIE_KEY,
|
ACCESS_TOKEN_COOKIE_KEY,
|
||||||
generate_auth_tokens,
|
generate_access_token,
|
||||||
|
generate_refresh_token,
|
||||||
get_account,
|
get_account,
|
||||||
REFRESH_TOKEN_COOKIE_KEY,
|
REFRESH_TOKEN_COOKIE_KEY,
|
||||||
validate_token_cookies
|
validate_token_cookies
|
||||||
|
|
|
@ -5,34 +5,54 @@ from selene.data.account import (
|
||||||
AccountRepository,
|
AccountRepository,
|
||||||
RefreshTokenRepository
|
RefreshTokenRepository
|
||||||
)
|
)
|
||||||
from selene.util.auth import AuthenticationTokenGenerator
|
from selene.util.auth import AuthenticationToken
|
||||||
from selene.util.db import get_db_connection
|
from selene.util.db import get_db_connection
|
||||||
|
|
||||||
ACCESS_TOKEN_COOKIE_KEY = 'seleneAccess'
|
ACCESS_TOKEN_COOKIE_KEY = 'seleneAccess'
|
||||||
|
ONE_MINUTE = 60
|
||||||
|
TWO_MINUTES = 120
|
||||||
REFRESH_TOKEN_COOKIE_KEY = 'seleneRefresh'
|
REFRESH_TOKEN_COOKIE_KEY = 'seleneRefresh'
|
||||||
|
|
||||||
|
|
||||||
def generate_auth_tokens(context):
|
def generate_access_token(context, expire=False):
|
||||||
token_generator = AuthenticationTokenGenerator(
|
access_token = AuthenticationToken(
|
||||||
context.account.id,
|
|
||||||
context.client_config['ACCESS_SECRET'],
|
context.client_config['ACCESS_SECRET'],
|
||||||
context.client_config['REFRESH_SECRET']
|
ONE_MINUTE
|
||||||
)
|
)
|
||||||
|
access_token.account_id = context.account.id
|
||||||
|
if not expire:
|
||||||
|
access_token.generate()
|
||||||
|
context.access_token = access_token
|
||||||
|
|
||||||
context.client.set_cookie(
|
context.client.set_cookie(
|
||||||
context.client_config['DOMAIN'],
|
context.client_config['DOMAIN'],
|
||||||
ACCESS_TOKEN_COOKIE_KEY,
|
ACCESS_TOKEN_COOKIE_KEY,
|
||||||
token_generator.access_token
|
access_token.jwt,
|
||||||
|
max_age=0 if expire else ONE_MINUTE
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_refresh_token(context, expire=False):
|
||||||
|
account_id = context.account.id
|
||||||
|
refresh_token = AuthenticationToken(
|
||||||
|
context.client_config['REFRESH_SECRET'],
|
||||||
|
TWO_MINUTES
|
||||||
|
)
|
||||||
|
refresh_token.account_id = account_id
|
||||||
|
if not expire:
|
||||||
|
refresh_token.generate()
|
||||||
|
context.refresh_token = refresh_token
|
||||||
|
|
||||||
context.client.set_cookie(
|
context.client.set_cookie(
|
||||||
context.client_config['DOMAIN'],
|
context.client_config['DOMAIN'],
|
||||||
REFRESH_TOKEN_COOKIE_KEY,
|
REFRESH_TOKEN_COOKIE_KEY,
|
||||||
token_generator.refresh_token
|
refresh_token.jwt,
|
||||||
|
max_age=0 if expire else TWO_MINUTES
|
||||||
)
|
)
|
||||||
context.request_refresh_token = token_generator.refresh_token
|
|
||||||
|
|
||||||
with get_db_connection(context.client_config['DB_CONNECTION_POOL']) as db:
|
with get_db_connection(context.client_config['DB_CONNECTION_POOL']) as db:
|
||||||
token_repository = RefreshTokenRepository(db, context.account.id)
|
token_repository = RefreshTokenRepository(db, account_id)
|
||||||
token_repository.add_refresh_token(token_generator.refresh_token)
|
token_repository.add_refresh_token(refresh_token.jwt)
|
||||||
|
|
||||||
|
|
||||||
def validate_token_cookies(context, expired=False):
|
def validate_token_cookies(context, expired=False):
|
||||||
|
|
Loading…
Reference in New Issue