dump-things-service/dump_things_service/tests/test_token_endpoints.py
2026-11-11 10:09:38 +01:00

99 lines
2.9 KiB
Python

from dump_things_service import (
HTTP_201_CREATED,
HTTP_404_NOT_FOUND,
HTTP_406_NOT_ACCEPTABLE,
HTTP_409_CONFLICT,
HTTP_422_UNPROCESSABLE_CONTENT,
)
def test_token_creation(fastapi_client_simple):
test_client, _, admin_token = fastapi_client_simple
json_record = {
'name': 'a',
'user_id': 'u_a',
'representation': '8bb6805ff10bcb1c2ca49dcd4bfef94d',
'collections': {
'collection_1': {'mode': 'WRITE_COLLECTION', 'incoming_label': 'i_a'},
},
}
# Create a token with name 'a'
response = test_client.post(
'/tokens',
headers={'x-dumpthings-token': admin_token},
json=json_record,
)
assert response.status_code == HTTP_201_CREATED, response.text
# Try to create another token with name 'a', should result in a 4ß9-error
response = test_client.post(
'/tokens',
headers={'x-dumpthings-token': admin_token},
json=json_record,
)
assert response.status_code == HTTP_409_CONFLICT
# Try to create another token with name 'b' and the same representation
# as 'a', should result in a 4ß9-error
json_record['name'] = 'b'
response = test_client.post(
'/tokens',
headers={'x-dumpthings-token': admin_token},
json=json_record,
)
assert response.status_code == HTTP_409_CONFLICT
def test_default_token_removal(fastapi_client_simple):
test_client, _, admin_token = fastapi_client_simple
# Create a token with name 'a'
response = test_client.delete(
'/tokens/test_default_token',
headers={'x-dumpthings-token': admin_token},
)
assert response.status_code == HTTP_406_NOT_ACCEPTABLE
def test_token_mode_error_detection(fastapi_client_simple):
test_client, _, admin_token = fastapi_client_simple
json_record = {
'name': 'a/b/c',
'user_id': 'u_a',
'representation': '8bb6805ff10bcb1c2ca49dcd4bfef94d',
'collections': {
'collection_1': {'mode': 'ABC', 'incoming_label': 'i_a'},
},
}
# Create a token with name 'a'
response = test_client.post(
'/tokens',
headers={'x-dumpthings-token': admin_token},
json=json_record,
)
assert response.status_code == HTTP_422_UNPROCESSABLE_CONTENT
def test_unknown_collection_error_detection(fastapi_client_simple):
test_client, _, admin_token = fastapi_client_simple
json_record = {
'name': 'a/b/c',
'user_id': 'u_a',
'representation': '8bb6805ff10bcb1c2ca49dcd4bfef94d',
'collections': {
'collection_abc': {'mode': 'READ_CURATED', 'incoming_label': 'i_a'},
},
}
# Create a token with name 'a'
response = test_client.post(
'/tokens',
headers={'x-dumpthings-token': admin_token},
json=json_record,
)
assert response.status_code == HTTP_404_NOT_FOUND