This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_auth.py
195 lines (147 loc) · 6.63 KB
/
test_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Tests for the objectrocket.auth module."""
import base64
import json
import mock
import pytest
import responses
from objectrocket import errors
from objectrocket.auth import Auth
from objectrocket.client import Client
@pytest.fixture
def auth_url(mongodb_sharded_instance):
return "https://sjc-api.objectrocket.com/v2/tokens/"
@pytest.yield_fixture(autouse=True)
def ensure_auth_production_url(auth_url):
"""Fixture that ensures that the proper production URLs are used in tests,
instead of the potentially overridden ones from environment variables.
See objectrocket.constants.OR_DEFAULT_API_URL
"""
with mock.patch.object(Auth, '_url', new_callable=mock.PropertyMock) as mock_auth_url:
type(mock_auth_url).return_value = auth_url
with mock.patch.object(Client, '_url', new_callable=mock.PropertyMock) as mock_client_url:
type(mock_client_url).return_value = auth_url.replace('tokens/', '')
yield
@pytest.fixture()
def base64_basic_auth_header():
"""Just returns a properly formatted basic author header for testing"""
user_passwd = '{}:{}'.format('tester', 'testpass').encode()
b64string = base64.encodestring(user_passwd).decode().replace('\n', '')
return 'Basic {}'.format(b64string)
####################################
# Tests for Auth public interface. #
####################################
@responses.activate
def test_authenticate_makes_expected_request(client, mocked_response, auth_url,
base64_basic_auth_header):
username, password, return_token = 'tester', 'testpass', 'return_token'
responses.add(responses.GET, auth_url, status=200,
body=json.dumps({'data': {'token': return_token}}),
content_type="application/json")
output = client.auth.authenticate(username, password)
assert output == return_token
assert responses.calls[0].request.headers.get('Authorization') == base64_basic_auth_header
assert responses.calls[0].request.headers.get('Content-Type') == 'application/json'
@responses.activate
def test_authenticate_binds_given_credentials(client, mocked_response, auth_url):
username, password, return_token = 'tester', 'testpass', 'return_token'
responses.add(responses.GET, auth_url, status=200,
body=json.dumps({'data': {'token': return_token}}),
content_type="application/json")
orig_username, orig_password = client.auth._username, client.auth._password
client.auth.authenticate(username, password)
assert orig_username is None
assert orig_password is None
assert client.auth._username == username
assert client.auth._password == password
@responses.activate
def test_authenticate_binds_auth_token_properly(client, mocked_response, auth_url):
username, password, return_token = 'tester', 'testpass', 'return_token'
responses.add(responses.GET, auth_url, status=200,
body=json.dumps({'data': {'token': return_token}}),
content_type="application/json")
orig_token = client.auth._token
client.auth.authenticate(username, password)
assert orig_token is None
assert client.auth._token == return_token
@responses.activate
def test_authenticate_raises_when_no_data_returned(client, mocked_response, auth_url):
username, password = 'tester', 'testpass'
auth = Auth(base_client=client)
responses.add(responses.GET, auth_url, status=200,
body=json.dumps({}),
content_type="application/json")
with pytest.raises(errors.AuthFailure) as exinfo:
auth.authenticate(username, password)
assert exinfo.value.args == ("KeyError: 'data'",)
@responses.activate
def test_authenticate_raises_when_no_token_returned(client, mocked_response, auth_url):
username, password = 'tester', 'testpass'
auth = Auth(base_client=client)
responses.add(responses.GET, auth_url, status=200,
body=json.dumps({'data': {}}),
content_type="application/json")
with pytest.raises(errors.AuthFailure) as exinfo:
auth.authenticate(username, password)
assert exinfo.value.args == ("KeyError: 'token'",)
#####################################
# Tests for Auth private interface. #
#####################################
def test_default_request_kwargs_match_base(client):
auth = Auth(base_client=client)
auth_kwargs = auth._default_request_kwargs
base_kwargs = super(Auth, auth)._default_request_kwargs
assert auth_kwargs == base_kwargs
def test_auth_url_points_to_expected_endpoint(client):
auth = Auth(base_client=client)
assert auth._url == client._url + 'tokens/'
def test_auth_password_setter(client):
orig_val = client.auth._password
testval = 'testing-password'
client.auth._password = testval
assert client.auth._password is testval
assert orig_val is not testval
@responses.activate
def test_auth_refresh_simply_invokes_authenticate_with_current_creds(client, mocked_response,
auth_url):
# Assemble.
username, password, return_token = 'tester', 'testpass', 'return_token'
responses.add(
responses.GET, auth_url,
status=200,
body=json.dumps({'data': {'token': return_token}}),
content_type="application/json"
)
auth_output = client.auth.authenticate(username, password)
bound_username, bound_password = client.auth._username, client.auth._password
# Action.
with mock.patch.object(client.auth, 'authenticate', return_value=return_token) as patched_auth:
refresh_output = client.auth._refresh()
# Assert.
assert auth_output == refresh_output
patched_auth.assert_called_once_with(bound_username, bound_password)
def test_auth_token_setter(client):
orig_val = client.auth._token
testval = 'testing-token'
client.auth._token = testval
assert client.auth._token is testval
assert orig_val is not testval
def test_auth_username_setter(client):
orig_val = client.auth._username
testval = 'testing-username'
client.auth._username = testval
assert client.auth._username is testval
assert orig_val is not testval
@responses.activate
def test_auth_verify_makes_expected_call(client):
token = 'testing'
expected_url = 'https://sjc-api.objectrocket.com/v2/tokens/verify/'
expected_user_data = {'testing': 'testing'}
responses.add(
responses.POST,
expected_url,
status=200,
body=json.dumps({'data': expected_user_data}),
content_type="application/json"
)
output = client.auth._verify(token)
assert output == expected_user_data