22import datetime
33import pytest
44import json
5+ import pyotp
6+ import os
57
68from tapisservice .auth import validate_token
79from tapisservice .config import conf as tapisconf
1921TEST_CLIENT_REDIRECT_URI = 'http://localhost:5000/testsuite'
2022TEST_USERNAME = 'testuser1'
2123TEST_PASSWORD = 'testuser1'
24+ MFA_USERNAME = 'cicsvc'
25+ MFA_GEN_CODE = os .environ .get ('MFA_GEN_CODE' )
2226
2327@pytest .fixture
2428def client ():
@@ -244,13 +248,19 @@ def get_jwt(client):
244248 return access_token_str
245249
246250
247- def gen_mfa_token (username , tokencode = None ):
251+ @pytest .fixture
252+ def mfa_token (tokencode = None ):
248253 """
249254 Generate a OTP mfa code using pyotp given a username and token code.
250255 If a token code is not provided, a random one will be used.
251256 """
252- pass
253-
257+ if tokencode is None :
258+ tokencode = MFA_GEN_CODE
259+ print (f'DEBUG:: generating MFA token with tokencode: { tokencode } ' )
260+ if tokencode is None :
261+ print (f'ERROR! tokencode should not be None! Env: { os .environ .items ()} ' )
262+ totp = pyotp .TOTP (tokencode )
263+ return totp .now ()
254264
255265# =====================
256266# Actual test functions
@@ -272,7 +282,6 @@ def test_get_jwt(client):
272282def test_get_mfa_config (client ):
273283 print ('top of get mfa config' )
274284 try :
275- print (f'what the heck is going on here' )
276285 tenant_config = tenant_configs_cache .get_config (TEST_TENANT_ID )
277286 print (f'after tenant config get:: { tenant_config } ' )
278287 mfa_config = json .loads (tenant_config .mfa_config )
@@ -295,6 +304,9 @@ def test_get_mfa_config(client):
295304 print (f'got { e } while trying to get mfa config for tenant { TEST_TENANT_ID } ' )
296305 raise Exception ()
297306
307+ def test_get_mfa_code (client , mfa_token ):
308+ print (f'got mfa tken:: { mfa_token } ' )
309+ assert mfa_token is not None
298310
299311## Health Check
300312# hello
@@ -318,7 +330,9 @@ def test_get_metadata(client):
318330
319331## Admin
320332# get_config
333+ # TODO
321334# update_config
335+ # TODO
322336
323337## Clients
324338
@@ -361,8 +375,10 @@ def test_authenticator_create_clients(client, capsys): ## TODO: this works, but
361375 check_clients_table (TEST_CLIENT_ID , TEST_CLIENT_REDIRECT_URI , 'A Test Client' , "This is a client just for testing" )
362376
363377# Get client details
378+ # TODO
364379
365380# Update client details
381+ # TODO
366382
367383# Permanantly set a client to inactive
368384def test_authenticator_delete_clients (client ):
@@ -540,6 +556,8 @@ def test_password_grant_no_client(client, init_db):
540556 assert 'refresh_token' not in response .json ['result' ]
541557
542558# Create a v2 bearer token from a Tapis v3 JWT
559+ # TODO
560+
543561# Revoke a token
544562def test_revoke_token (client , init_db ):
545563 """
@@ -591,10 +609,15 @@ def test_revoke_token(client, init_db):
591609
592610 check_refresh_token_table (refresh_token_claims , "password" , True , TEST_CLIENT_ID )
593611
612+ # Note: Device code checks are below
613+
594614## Profiles
595615# get_userinfo
616+ # TODO
596617# list_profiles
618+ # TODO
597619# get_profile
620+ # TODO
598621
599622## grant type tests
600623
@@ -846,7 +869,19 @@ def test_exchange_device_code(client):
846869 validate_access_token (response )
847870
848871## MFA tests
872+ # TODO
873+ def test_mfa_valid_code (mfa_token ):
874+ # uses the cicsvc creds to auth.
875+ response = mfa .call_mfa (mfa_token , TEST_TENANT_ID , MFA_USERNAME )
876+ print (f'DEBUG:: mfa response: { response } ' )
877+ assert response is True
878+
879+ def test_mfa_invalid_code (mfa_token ):
880+ response = mfa .call_mfa ('123456' , TEST_TENANT_ID , MFA_USERNAME )
881+ print (f'DEBUG:: mfa response: { response } ' )
882+ assert response is False
849883
850884## OAuth2ProviderExtCallback tests
885+ # TODO
851886
852887
0 commit comments