-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_authenticate.py
175 lines (162 loc) · 6.71 KB
/
test_authenticate.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
import json
import requests
import testtools
from testtools.matchers import (
Equals,
KeysEqual,
)
from .config import (
OPENAM_URI,
PASSWORD,
USERNAME,
)
AUTHENTICATE_URI = OPENAM_URI + "/authenticate"
OPENAM_USERNAME_HEADER = "X-OpenAM-Username"
OPENAM_PASSWORD_HEADER = "X-OpenAM-Password"
class TestAuthenticateZeroPage(testtools.TestCase):
def test_authenticate_default(self):
"""
Verify that the correct username and password combination
gives a succesful response with the correct fields.
"""
headers = {
OPENAM_USERNAME_HEADER: USERNAME,
OPENAM_PASSWORD_HEADER: PASSWORD,
"Content-Type": "application/json",
}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(200))
# verify that the response has the right fields
self.assertThat(response.json(), KeysEqual(
{
"successUrl": "",
"tokenId": "",
}
))
def test_authenticate_invalid_username(self):
"""
Verify that when an invalid username is provided, the response
is 401 Unauthorised.
"""
headers = {
OPENAM_USERNAME_HEADER: "invalid",
OPENAM_PASSWORD_HEADER: PASSWORD,
"Content-Type": "application/json",
}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(401))
def test_authenticate_invalid_password(self):
"""
Verify that when an invalid password is provided, the response
is 401 Unauthorised.
"""
headers = {
OPENAM_USERNAME_HEADER: USERNAME,
OPENAM_PASSWORD_HEADER: "invalid",
"Content-Type": "application/json",
}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(401))
class TestAuthenticateCallbacks(testtools.TestCase):
def test_authenticate_default(self):
"""
Verify that when we send a POST request to the authenticate endpoint
with no username and password specified, we get a callback response,
then when we respond to that with the callback filled in with the
correct username and password, a response is returned with the tokenId
"""
headers = {"Content-Type": "application/json"}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(200))
data = response.json()
for idx, callback in enumerate(data['callbacks']):
if callback['type'] == 'NameCallback':
data['callbacks'][idx]['input'][0]['value'] = USERNAME
if callback['type'] == 'PasswordCallback':
data['callbacks'][idx]['input'][0]['value'] = PASSWORD
response = requests.post(
AUTHENTICATE_URI,
data=json.dumps(data),
headers=headers,
)
self.assertThat(response.status_code, Equals(200))
self.assertThat(response.json(), KeysEqual(
{
"successUrl": "",
"tokenId": "",
}
))
def test_authenticate_invalid_username(self):
"""
Verify that when we send a POST request to the authenticate endpoint
with no username and password specified, we get a callback response,
then when we respond to that with the callback filled in with the
incorrect username and correct password, a 401 response is returned.
"""
headers = {"Content-Type": "application/json"}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(200))
data = response.json()
for idx, callback in enumerate(data['callbacks']):
if callback['type'] == 'NameCallback':
data['callbacks'][idx]['input'][0]['value'] = 'invalid'
if callback['type'] == 'PasswordCallback':
data['callbacks'][idx]['input'][0]['value'] = PASSWORD
response = requests.post(
AUTHENTICATE_URI,
data=json.dumps(data),
headers=headers,
)
self.assertThat(response.status_code, Equals(401))
def test_authenticate_invalid_password(self):
"""
Verify that when we send a POST request to the authenticate endpoint
with no username and password specified, we get a callback response,
then when we respond to that with the callback filled in with the
correct username and incorrect password, a 401 response is returned.
"""
headers = {"Content-Type": "application/json"}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(200))
data = response.json()
for idx, callback in enumerate(data['callbacks']):
if callback['type'] == 'NameCallback':
data['callbacks'][idx]['input'][0]['value'] = USERNAME
if callback['type'] == 'PasswordCallback':
data['callbacks'][idx]['input'][0]['value'] = 'invalid'
response = requests.post(
AUTHENTICATE_URI,
data=json.dumps(data),
headers=headers,
)
self.assertThat(response.status_code, Equals(401))
def test_authenticate_no_session(self):
"""
Verify that when we send a POST request to the authenticate endpoint
with no username and password specified, we get a callback response,
then when we respond to that with the callback filled in with the
correct username and correct password and the noSession parameter set
to true, then a response is returned without the tokenId
"""
headers = {"Content-Type": "application/json"}
response = requests.post(AUTHENTICATE_URI, headers=headers)
self.assertThat(response.status_code, Equals(200))
data = response.json()
for idx, callback in enumerate(data['callbacks']):
if callback['type'] == 'NameCallback':
data['callbacks'][idx]['input'][0]['value'] = USERNAME
if callback['type'] == 'PasswordCallback':
data['callbacks'][idx]['input'][0]['value'] = PASSWORD
response = requests.post(
AUTHENTICATE_URI,
data=json.dumps(data),
headers=headers,
params={'noSession': True},
)
self.assertThat(response.status_code, Equals(200))
self.assertThat(response.json(), KeysEqual(
{
"successUrl": "",
"message": "",
}
))