Skip to content

Commit 8bce20a

Browse files
Merge pull request #429 from adamtheturtle/split-unexpected-headers
Split unexpected headers
2 parents 7ed8714 + 406e9d3 commit 8bce20a

File tree

3 files changed

+113
-78
lines changed

3 files changed

+113
-78
lines changed

.travis.yml

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,36 @@ python:
44
- '3.6'
55
env:
66
matrix:
7+
- TEST_FILENAME=test_add_target.py::TestActiveFlag
8+
- TEST_FILENAME=test_add_target.py::TestApplicationMetadata
79
- TEST_FILENAME=test_add_target.py::TestContentTypes
10+
- TEST_FILENAME=test_add_target.py::TestImage
811
- TEST_FILENAME=test_add_target.py::TestMissingData
9-
- TEST_FILENAME=test_add_target.py::TestWidth
1012
- TEST_FILENAME=test_add_target.py::TestTargetName
11-
- TEST_FILENAME=test_add_target.py::TestImage
12-
- TEST_FILENAME=test_add_target.py::TestActiveFlag
1313
- TEST_FILENAME=test_add_target.py::TestUnexpectedData
14-
- TEST_FILENAME=test_add_target.py::TestApplicationMetadata
14+
- TEST_FILENAME=test_add_target.py::TestWidth
15+
- TEST_FILENAME=test_authorization_header.py
1516
- TEST_FILENAME=test_database_summary.py
17+
- TEST_FILENAME=test_date_header.py::TestFormat
18+
- TEST_FILENAME=test_date_header.py::TestMissing
19+
- TEST_FILENAME=test_date_header.py::TestSkewedTime
1620
- TEST_FILENAME=test_delete_target.py
1721
- TEST_FILENAME=test_get_duplicates.py
1822
- TEST_FILENAME=test_get_target.py
23+
- TEST_FILENAME=test_invalid_given_id.py
24+
- TEST_FILENAME=test_invalid_json.py
1925
- TEST_FILENAME=test_query.py
2026
- TEST_FILENAME=test_target_list.py
2127
- TEST_FILENAME=test_target_summary.py
22-
- TEST_FILENAME=test_update_target.py::TestUpdate
23-
- TEST_FILENAME=test_update_target.py::TestUnexpectedData
24-
- TEST_FILENAME=test_update_target.py::TestWidth
28+
- TEST_FILENAME=test_unexpected_json.py
2529
- TEST_FILENAME=test_update_target.py::TestActiveFlag
2630
- TEST_FILENAME=test_update_target.py::TestApplicationMetadata
27-
- TEST_FILENAME=test_update_target.py::TestTargetName
2831
- TEST_FILENAME=test_update_target.py::TestImage
32+
- TEST_FILENAME=test_update_target.py::TestTargetName
33+
- TEST_FILENAME=test_update_target.py::TestUnexpectedData
34+
- TEST_FILENAME=test_update_target.py::TestUpdate
35+
- TEST_FILENAME=test_update_target.py::TestWidth
2936
- TEST_FILENAME=test_usage.py
30-
- TEST_FILENAME=test_invalid_given_id.py
31-
- TEST_FILENAME=test_invalid_json.py
32-
- TEST_FILENAME=test_unexpected_headers.py
33-
- TEST_FILENAME=test_unexpected_json.py
3437
addons:
3538
apt:
3639
packages:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
Tests for the `Authorization` header.
3+
"""
4+
5+
from typing import Dict, Union
6+
7+
import pytest
8+
import requests
9+
from requests import codes
10+
11+
from mock_vws._constants import ResultCodes
12+
from tests.mock_vws.utils import (
13+
TargetAPIEndpoint,
14+
assert_vws_failure,
15+
rfc_1123_date,
16+
)
17+
18+
19+
@pytest.mark.usefixtures('verify_mock_vuforia')
20+
class TestAuthorizationHeader:
21+
"""
22+
Tests for what happens when the `Authorization` header is not as expected.
23+
"""
24+
25+
def test_missing(self, endpoint: TargetAPIEndpoint) -> None:
26+
"""
27+
An `UNAUTHORIZED` response is returned when no `Authorization` header
28+
is given.
29+
"""
30+
date = rfc_1123_date()
31+
endpoint_headers = dict(endpoint.prepared_request.headers)
32+
33+
headers: Dict[str, Union[str, bytes]] = {
34+
**endpoint_headers,
35+
'Date': date,
36+
}
37+
38+
headers.pop('Authorization', None)
39+
40+
endpoint.prepared_request.prepare_headers( # type: ignore
41+
headers=headers,
42+
)
43+
session = requests.Session()
44+
response = session.send( # type: ignore
45+
request=endpoint.prepared_request,
46+
)
47+
48+
assert_vws_failure(
49+
response=response,
50+
status_code=codes.UNAUTHORIZED,
51+
result_code=ResultCodes.AUTHENTICATION_FAILURE,
52+
)
53+
54+
def test_incorrect(self, endpoint: TargetAPIEndpoint) -> None:
55+
"""
56+
If an incorrect `Authorization` header is given, a `BAD_REQUEST`
57+
response is given.
58+
"""
59+
date = rfc_1123_date()
60+
61+
headers: Dict[str, Union[str, bytes]] = {
62+
**endpoint.prepared_request.headers,
63+
'Authorization': 'gibberish',
64+
'Date': date,
65+
}
66+
67+
endpoint.prepared_request.prepare_headers( # type: ignore
68+
headers=headers,
69+
)
70+
session = requests.Session()
71+
response = session.send( # type: ignore
72+
request=endpoint.prepared_request,
73+
)
74+
75+
assert_vws_failure(
76+
response=response,
77+
status_code=codes.BAD_REQUEST,
78+
result_code=ResultCodes.FAIL,
79+
)
Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Tests for when endpoints are called with unexpected header data.
2+
Tests for the `Date` header.
33
"""
44

55
from datetime import datetime, timedelta
@@ -22,72 +22,9 @@
2222

2323

2424
@pytest.mark.usefixtures('verify_mock_vuforia')
25-
class TestAuthorizationHeader:
25+
class TestMissing:
2626
"""
27-
Tests for what happens when the `Authorization` header isn't as expected.
28-
"""
29-
30-
def test_missing(self, endpoint: TargetAPIEndpoint) -> None:
31-
"""
32-
An `UNAUTHORIZED` response is returned when no `Authorization` header
33-
is given.
34-
"""
35-
date = rfc_1123_date()
36-
endpoint_headers = dict(endpoint.prepared_request.headers)
37-
38-
headers: Dict[str, Union[str, bytes]] = {
39-
**endpoint_headers,
40-
'Date': date,
41-
}
42-
43-
headers.pop('Authorization', None)
44-
45-
endpoint.prepared_request.prepare_headers( # type: ignore
46-
headers=headers,
47-
)
48-
session = requests.Session()
49-
response = session.send( # type: ignore
50-
request=endpoint.prepared_request,
51-
)
52-
53-
assert_vws_failure(
54-
response=response,
55-
status_code=codes.UNAUTHORIZED,
56-
result_code=ResultCodes.AUTHENTICATION_FAILURE,
57-
)
58-
59-
def test_incorrect(self, endpoint: TargetAPIEndpoint) -> None:
60-
"""
61-
If an incorrect `Authorization` header is given, a `BAD_REQUEST`
62-
response is given.
63-
"""
64-
date = rfc_1123_date()
65-
66-
headers: Dict[str, Union[str, bytes]] = {
67-
**endpoint.prepared_request.headers,
68-
'Authorization': 'gibberish',
69-
'Date': date,
70-
}
71-
72-
endpoint.prepared_request.prepare_headers( # type: ignore
73-
headers=headers,
74-
)
75-
session = requests.Session()
76-
response = session.send( # type: ignore
77-
request=endpoint.prepared_request,
78-
)
79-
80-
assert_vws_failure(
81-
response=response,
82-
status_code=codes.BAD_REQUEST,
83-
result_code=ResultCodes.FAIL,
84-
)
85-
86-
87-
@pytest.mark.usefixtures('verify_mock_vuforia')
88-
class TestDateHeader:
89-
"""
90-
Tests for what happens when the `Date` header isn't as expected.
27+
Tests for what happens when the `Date` header is missing.
9128
"""
9229

9330
def test_no_date_header(
@@ -134,6 +71,14 @@ def test_no_date_header(
13471
result_code=ResultCodes.FAIL,
13572
)
13673

74+
75+
@pytest.mark.usefixtures('verify_mock_vuforia')
76+
class TestFormat:
77+
"""
78+
Tests for what happens when the `Date` header is not in the
79+
expected format.
80+
"""
81+
13782
def test_incorrect_date_format(
13883
self,
13984
vuforia_database_keys: VuforiaDatabaseKeys,
@@ -183,6 +128,14 @@ def test_incorrect_date_format(
183128
result_code=ResultCodes.FAIL,
184129
)
185130

131+
132+
@pytest.mark.usefixtures('verify_mock_vuforia')
133+
class TestSkewedTime:
134+
"""
135+
Tests for what happens when the `Date` header is given with an
136+
unexpected time.
137+
"""
138+
186139
@pytest.mark.parametrize(
187140
'time_multiplier',
188141
[1, -1],

0 commit comments

Comments
 (0)