Skip to content

Commit

Permalink
Get rid of workaround for response header access
Browse files Browse the repository at this point in the history
We no longer need support Django older than 2.2, so switch to the
header access method introduced in Django 1.11.
  • Loading branch information
hmpf committed Oct 22, 2021
1 parent 86e598b commit d1adb0e
Showing 1 changed file with 18 additions and 35 deletions.
53 changes: 18 additions & 35 deletions tests/integration/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@
}


# Django newer than 1.9 can send a response that lacks the Content-Type header,
# like for a delete. The __repr__ in django 1.9 and 1.10 directly looks up the
# Content-Type header, leading to a KeyError. Therefore, don't print()
# responses, which depends on __repr__.
#
# See django bug #27640. Fixed in Django 1.11
def print_response(response):
print(
'<%(cls)s status_code=%(status_code)d%(content_type)s>'
% {
'cls': response.__class__.__name__,
'status_code': response.status_code,
'content_type': response._headers.get('Content-Type', ''),
}
)


# Generic tests


Expand Down Expand Up @@ -85,18 +68,18 @@ def test_delete(db, api_client, token, endpoint):
response_delete = delete(api_client, endpoint, res.get('id'))
response_get = get(api_client, endpoint, res.get('id'))

print_response(response_delete)
print(response_delete)
assert response_delete.status_code == 204

print_response(response_get)
print(response_get)
assert response_get.status_code == 404


@pytest.mark.parametrize("endpoint", ['account', 'netbox', 'location', 'room', 'vlan'])
def test_create(db, api_client, token, endpoint):
create_token_endpoint(token, endpoint)
response = create(api_client, endpoint, TEST_DATA.get(endpoint))
print_response(response)
print(response)
assert response.status_code == 201


Expand Down Expand Up @@ -126,12 +109,12 @@ def test_update_org_on_account(db, api_client, token):
create_token_endpoint(token, endpoint)
data = {"organizations": ["myorg"]}
response = update(api_client, endpoint, 1, data)
print_response(response)
print(response)
assert response.status_code == 200

data = {"organizations": []}
response = update(api_client, endpoint, 1, data)
print_response(response)
print(response)
assert response.status_code == 200


Expand All @@ -141,7 +124,7 @@ def test_update_group_on_org(db, api_client, token):
# Only admin group
data = {"accountgroups": [1]}
response = update(api_client, endpoint, 1, data)
print_response(response)
print(response)
assert response.status_code == 200


Expand All @@ -155,7 +138,7 @@ def test_update_netbox(db, api_client, token):
res = json.loads(response_create.content.decode('utf-8'))
data = {'categoryid': 'GW'}
response_update = update(api_client, endpoint, res['id'], data)
print_response(response_update)
print(response_update)
assert response_update.status_code == 200


Expand All @@ -168,7 +151,7 @@ def test_delete_netbox(db, api_client, token):
response_get = get(api_client, endpoint, json_create['id'])
json_get = json.loads(response_get.content.decode('utf-8'))

print_response(response_delete)
print(response_delete)
print(json_get['deleted_at'])

assert response_delete.status_code == 204
Expand All @@ -181,7 +164,7 @@ def test_delete_netbox(db, api_client, token):
def test_get_wrong_room(db, api_client, token):
create_token_endpoint(token, 'room')
response = api_client.get('{}blapp/'.format(ENDPOINTS['room']))
print_response(response)
print(response)
assert response.status_code == 404


Expand All @@ -190,15 +173,15 @@ def test_get_new_room(db, api_client, token):
create_token_endpoint(token, endpoint)
create(api_client, endpoint, TEST_DATA.get(endpoint))
response = api_client.get('/api/1/room/blapp/')
print_response(response)
print(response)
assert response.status_code == 200


def test_patch_room_not_found(db, api_client, token):
create_token_endpoint(token, 'room')
data = {'location': 'mylocation'}
response = api_client.patch('/api/1/room/blapp/', data, format='json')
print_response(response)
print(response)
assert response.status_code == 404


Expand All @@ -208,7 +191,7 @@ def test_patch_room_wrong_location(db, api_client, token):
create(api_client, endpoint, TEST_DATA.get(endpoint))
data = {'location': 'mylocatio'}
response = api_client.patch('/api/1/room/blapp/', data, format='json')
print_response(response)
print(response)
assert response.status_code == 400


Expand All @@ -219,7 +202,7 @@ def test_patch_room(db, api_client, token):
data = {'location': 'mylocation'}
response = api_client.patch('/api/1/room/blapp/', data, format='json')

print_response(response)
print(response)
assert response.status_code == 200


Expand All @@ -229,7 +212,7 @@ def test_delete_room_wrong_room(db, api_client, token):
create(api_client, endpoint, TEST_DATA.get(endpoint))
response = api_client.delete('/api/1/room/blap/')

print_response(response)
print(response)
assert response.status_code == 404


Expand All @@ -240,7 +223,7 @@ def test_validate_vlan(db, api_client, token):
testdata.update({'net_type': 'core'})
response = create(api_client, endpoint, testdata)

print_response(response)
print(response)
assert response.status_code == 400


Expand All @@ -260,7 +243,7 @@ def test_create_prefix(db, api_client, token):
testdata = prepare_prefix_test(db, api_client, token)
response = create(api_client, endpoint, testdata)

print_response(response)
print(response)
assert response.status_code == 201


Expand Down Expand Up @@ -293,15 +276,15 @@ def test_update_prefix_remove_usage(db, api_client, token, serializer_models):
def test_nonexistent_alert_should_give_404(db, api_client, token):
create_token_endpoint(token, 'alert')
response = api_client.get('{}9999/'.format(ENDPOINTS['alert']))
print_response(response)
print(response)
assert response.status_code == 404


def test_alert_should_be_visible_in_api(db, api_client, token, serializer_models):
create_token_endpoint(token, 'alert')
alert = AlertHistory.objects.all()[0]
response = api_client.get('{url}{id}/'.format(url=ENDPOINTS['alert'], id=alert.id))
print_response(response)
print(response)
assert response.status_code == 200
content = response.content.decode('utf-8')
# Simple string tests, but they might just as well parse the JSON structure
Expand Down

0 comments on commit d1adb0e

Please sign in to comment.