Skip to content

Commit b62d293

Browse files
Merge pull request #109 from tapis-project/authenticator-tests
add tests - full coverage
2 parents 97ba25f + 3fe5d16 commit b62d293

File tree

1 file changed

+155
-82
lines changed

1 file changed

+155
-82
lines changed

service/tests/basic_test.py

Lines changed: 155 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def init_db():
4747
'active': True
4848
}
4949
models.delete_tenant_from_db(TEST_TENANT_ID)
50-
print(f'got tapisconf:: {tapisconf}')
50+
# print(f'got tapisconf:: {tapisconf}')
5151
config = {
5252
"tenant_id":TEST_TENANT_ID,
5353
"allowable_grant_types":json.dumps(["password", "implicit", "authorization_code", "refresh_token", "device_code"]),
@@ -356,52 +356,94 @@ def test_get_metadata(client):
356356

357357
## Admin
358358
# get_config
359-
# def test_get_admin_config(client, tapis_service_jwt):
360-
# with client:
361-
# header = {
362-
# 'X-Tapis-Token': tapis_service_jwt,
363-
# 'X-Tapis-Tenant': TEST_TENANT_ID,
364-
# 'X-Tapis-User': 'authenticator'
365-
# }
366-
# response = client.get('http://localhost:5000/v3/oauth2/admin/config', headers=header)
367-
# print(f'got response:: {response.json}')
368-
# assert response.status_code == 200
369-
# # TODO: this doesn't seem to work.
370-
# # retrieved_config = response.json['result']
371-
# # tenant_config = tenant_configs_cache.get_config(TEST_TENANT_ID).serialize
372-
# # print(f'got config:: {retrieved_config}')
373-
# # print(f'checking against cached config: {tenant_config}')
374-
# # assert retrieved_config == tenant_config
375-
376-
# # update_config
377-
# def test_update_admin_config(client, tapis_service_jwt):
378-
# with client:
379-
# # get the config first, so we can compare after the change
380-
# current_config = tenant_configs_cache.get_config(TEST_TENANT_ID).serialize
381-
# # just change one thing
382-
# payload = {
383-
# "impers_oauth_client_id": "TEST"
384-
# }
385-
# # make request
386-
# header = {
387-
# 'X-Tapis-Token': tapis_service_jwt,
388-
# 'X-Tapis-Tenant': TEST_TENANT_ID,
389-
# 'X-Tapis-User': 'authenticator'
390-
# }
391-
# response = client.put(
392-
# 'http://localhost:5000/v3/oauth2/admin/config',
393-
# data=json.dumps(payload),
394-
# headers=header,
395-
# content_type="application/json"
396-
# )
397-
# print(f'DEBUG: got response:: {response}')
398-
# assert response.status_code == 200
399-
400-
359+
def test_get_admin_config(client, tapis_service_jwt, init_db):
360+
with client:
361+
header = {
362+
'X-Tapis-Token': tapis_service_jwt,
363+
'X-Tapis-Tenant': TEST_TENANT_ID,
364+
'X-Tapis-User': 'authenticator'
365+
}
366+
response = client.get('http://localhost:5000/v3/oauth2/admin/config', headers=header)
367+
print(f'got response:: {response.json}')
368+
assert response.status_code == 200
369+
# TODO: this doesn't seem to work.
370+
retrieved_config = response.json['result']
371+
print(f'got config:: {retrieved_config}')
372+
# tenant_config = tenant_configs_cache.get_config(TEST_TENANT_ID)
373+
tenant_configs = tenant_configs_cache.load_tenant_config_cache()
374+
tenant_config = [conf for conf in tenant_configs if conf.tenant_id == TEST_TENANT_ID][0]
375+
tenant_config_data = tenant_config.serialize
376+
assert retrieved_config == tenant_config_data
377+
378+
# # # update_config
379+
def test_update_admin_config(client, tapis_service_jwt):
380+
with client:
381+
# get current config
382+
current_config = [d for d in tenant_configs_cache.load_tenant_config_cache() if d.tenant_id == TEST_TENANT_ID][0].serialize
383+
384+
# just change one thing
385+
payload = {
386+
"impers_oauth_client_id": "TEST"
387+
}
388+
# make request
389+
header = {
390+
'X-Tapis-Token': tapis_service_jwt,
391+
'X-Tapis-Tenant': TEST_TENANT_ID,
392+
'X-Tapis-User': 'authenticator'
393+
}
394+
response = client.put(
395+
'http://localhost:5000/v3/oauth2/admin/config',
396+
data=json.dumps(payload),
397+
headers=header,
398+
content_type="application/json"
399+
)
400+
print(f'got response:: {response.json}')
401+
assert response.status_code == 200
402+
# TODO: compare the change to the original
403+
updated_config = response.json['result']
404+
print(f'DEBUG:: Comparing \n\t{updated_config}\n\t against \n\t{current_config}')
405+
assert response.json['result'] != current_config
406+
407+
# change it back
408+
payload = {"impers_oauth_client_id": current_config["impers_oauth_client_id"]}
409+
response = client.put(
410+
'http://localhost:5000/v3/oauth2/admin/config',
411+
data=json.dumps(payload),
412+
headers=header,
413+
content_type="application/json"
414+
)
415+
print(f'got response:: {response.json}')
416+
assert response.status_code == 200
401417

402418

403419
## Clients
404420

421+
# utility setup / teardown
422+
def insert_test_client(client):
423+
# first insert a new client into the db so there's no intersections
424+
new_client_id = f'{TEST_CLIENT_ID}__update_test'
425+
models.add_client_to_db({
426+
'tenant_id': TEST_TENANT_ID,
427+
"username": TEST_USERNAME,
428+
'client_id': new_client_id,
429+
'client_key': TEST_CLIENT_KEY,
430+
"display_name": "Tapis Authenticator Testsuite",
431+
"callback_url": TEST_CLIENT_REDIRECT_URI,
432+
'create_time': datetime.datetime.utcnow(),
433+
'last_update_time': datetime.datetime.utcnow(),
434+
'active': True
435+
})
436+
new_client = models.Client.query.filter_by(
437+
tenant_id=TEST_TENANT_ID,
438+
client_id=new_client_id,
439+
client_key=TEST_CLIENT_KEY
440+
).first()
441+
assert new_client is not None # fail the test if we don't have the test client
442+
return new_client
443+
444+
def remove_test_client(client, to_delete):
445+
models.db.session.delete(to_delete)
446+
models.db.session.commit()
405447

406448
def test_invalid_post(client):
407449
with client:
@@ -421,69 +463,99 @@ def test_authenticator_list_clients(client):
421463
# create_client
422464
def test_authenticator_create_clients(client, tapis_jwt): ## TODO: this works, but doing it twice violates uniqueness constraint. Need to find a way to reliably erase it without using another endpoint
423465
# result = client.authenticator.create_client(client_id=TEST_CLIENT_ID, callback_url='https://foo.example.com/oauth2/callback')
424-
header = {'X-Tapis-Token': tapis_jwt}
425-
payload = {
426-
"client_id": TEST_CLIENT_ID,
427-
"client_key": TEST_CLIENT_KEY,
428-
"callback_url": TEST_CLIENT_REDIRECT_URI,
429-
"display_name": "A Test Client",
430-
"description": "This is a client just for testing"
431-
}
432-
result = client.post(
433-
'http://localhost:5000/v3/oauth2/clients',
434-
headers=header,
435-
data=json.dumps(payload),
436-
content_type='application/json'
437-
)
438-
439-
assert result.status_code == 200
440-
# check the clients table to make sure it was created in the DB
441-
check_clients_table(TEST_CLIENT_ID, TEST_CLIENT_REDIRECT_URI, 'A Test Client', "This is a client just for testing")
466+
with client:
467+
new_client_id = f'{TEST_CLIENT_ID}__create_test'
468+
header = {'X-Tapis-Token': tapis_jwt}
469+
payload = {
470+
"client_id": new_client_id,
471+
"client_key": TEST_CLIENT_KEY,
472+
"callback_url": TEST_CLIENT_REDIRECT_URI,
473+
"display_name": "A Test Client",
474+
"description": "This is a client just for testing"
475+
}
476+
result = client.post(
477+
'http://localhost:5000/v3/oauth2/clients',
478+
headers=header,
479+
data=json.dumps(payload),
480+
content_type='application/json'
481+
)
482+
483+
assert result.status_code == 200
484+
# check the clients table to make sure it was created in the DB
485+
check_clients_table(new_client_id, TEST_CLIENT_REDIRECT_URI, 'A Test Client', "This is a client just for testing")
486+
# cleanup
487+
got_client = models.Client.query.filter_by(
488+
tenant_id=TEST_TENANT_ID,
489+
client_id=new_client_id,
490+
client_key=TEST_CLIENT_KEY
491+
).first()
492+
assert got_client is not None # fail if we can't find the client. This means the test didn't work
493+
494+
# delete the added client
495+
models.db.session.delete(got_client)
496+
models.db.session.commit()
497+
442498

443499
# Get client details
444500
def test_authenticator_get_client(client, tapis_jwt):
501+
# create a new client so there's no collisions
502+
new_client = insert_test_client(client)
503+
445504
with client:
446505
header = {'X-Tapis-Token': tapis_jwt}
447-
url = f'http://localhost:5000/v3/oauth2/clients/{TEST_CLIENT_ID}'
448-
result = client.get(f'http://localhost:5000/v3/oauth2/clients/{TEST_CLIENT_ID}', headers=header)
506+
url = f'http://localhost:5000/v3/oauth2/clients/{new_client.client_id}'
507+
508+
result = client.get(
509+
url,
510+
headers=header
511+
)
512+
513+
print(f'DEBUG:: got response getting client: {result.json}')
449514
assert result.status_code == 200
450515
check_clients_table(TEST_CLIENT_ID)
516+
517+
# cleanup
518+
remove_test_client(client, new_client)
451519

452520
# Update client details
453521
def test_authenticator_update_client(client, tapis_jwt):
522+
# first insert a new client into the db so there's no intersections
523+
new_client = insert_test_client(client)
524+
525+
# now update it
454526
header = {'X-Tapis-Token': tapis_jwt}
455527
payload = json.dumps({
456528
"callback_url": "http://localhost:5000/testsuite/update_client_test"
457529
})
458530
result = client.put(
459-
f'http://localhost:5000/v3/oauth2/clients/{TEST_CLIENT_ID}',
531+
f'http://localhost:5000/v3/oauth2/clients/{new_client.client_id}',
460532
headers=header,
461533
data=payload,
462534
content_type='application/json'
463535
)
464-
print(f'DEBUG: got result of update client:: {result}')
536+
print(f'DEBUG: got result of update client:: {result.json}')
465537
assert result.status_code == 200
466-
check_clients_table(TEST_CLIENT_ID)
467-
# TODO: we should create a better setup / teardown for these tests
468-
payload = json.dumps({
469-
"callback_url": TEST_CLIENT_REDIRECT_URI
470-
})
471-
result = client.put(
472-
f'http://localhost:5000/v3/oauth2/clients/{TEST_CLIENT_ID}',
473-
headers=header,
474-
data=payload,
475-
content_type='application/json'
476-
)
477-
assert result.status_code == 200 # fail if we can't put it back correctly
538+
check_clients_table(new_client.client_id, callback_url='http://localhost:5000/testsuite/update_client_test')
539+
# cleanup
540+
remove_test_client(client, new_client)
478541

479542

480543
# Permanantly set a client to inactive
481-
def test_authenticator_delete_clients(client):
482-
header = {'X-Tapis-Token': get_jwt(client)}
483-
result = client.delete(f'http://localhost:5000/v3/oauth2/clients/{TEST_CLIENT_ID}', headers=header)
544+
def test_authenticator_delete_clients(client, tapis_jwt):
545+
# insert a new client to avoid collision
546+
new_client = insert_test_client(client)
547+
548+
header = {'X-Tapis-Token': tapis_jwt}
549+
result = client.delete(
550+
f'http://localhost:5000/v3/oauth2/clients/{new_client.client_id}',
551+
headers=header
552+
)
484553
print(f'DEBUG: got result of delete call: {result.json}')
485554
assert result.status_code == 200
486-
check_clients_table(TEST_CLIENT_ID, negative=True)
555+
check_clients_table(new_client.client_id, negative=True)
556+
557+
# cleanup
558+
remove_test_client(client, new_client)
487559

488560
## Tokens
489561
# Generate a Tapis JWT
@@ -952,7 +1024,8 @@ def test_get_device_code(client):
9521024
def test_authorize_device_code(client):
9531025
# TODO: not sure how to do this one yet, since it tyically requires manually going to the verification url and signing in.
9541026
# the test_exchange_device_code func directly inserts the "Entered" status in the device_codes table to simulate this.
955-
# Skipping this one for now
1027+
# Skipping this one for now
1028+
# Maybe look more into modifying context, like https://flask.palletsprojects.com/en/stable/testing/#tests-that-depend-on-an-active-context
9561029
pass
9571030

9581031
def test_exchange_device_code(client):

0 commit comments

Comments
 (0)