-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_v1_auth_unit.py
193 lines (155 loc) · 6.57 KB
/
test_v1_auth_unit.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
import string
import random
try:
from unittest import mock
except ImportError:
import mock # python 2 compatibility
from hubstaff.client_v1 import HubstaffClient
from hubstaff.exceptions import HubstaffError, HubstaffAuthError
class TestCase(unittest.TestCase):
def setUp(self):
self.app_token = ''.join([random.choice(string.ascii_letters)
for _ in range(43)])
self.auth_token = ''.join([random.choice(string.ascii_letters)
for _ in range(43)])
def test_init_raises_error_on_no_auth_token_no_username(self):
with self.assertRaises(ValueError) as err_ctx:
HubstaffClient(app_token=self.app_token,
password='ValidPasswordHere')
self.assertEqual(err_ctx.exception.args[0],
'auth_token or (username, password) pair must be set')
def test_init_raises_error_on_no_auth_token_no_password(self):
with self.assertRaises(ValueError) as err_ctx:
HubstaffClient(app_token=self.app_token,
username='[email protected]')
self.assertEqual(err_ctx.exception.args[0],
'auth_token or (username, password) pair must be set')
@mock.patch('requests.post')
def test_authenticate_method_returns_auth_token_no_password(self, m_post):
m_post.return_value.status_code = 401
m_post.return_value.json.return_value = {
'error': 'Invalid email and/or password'
}
client = HubstaffClient(
app_token=self.app_token,
auth_token=self.auth_token)
auth_token = client.authenticate()
self.assertEqual(auth_token, self.auth_token)
@mock.patch('requests.post')
def test_authenticate_method_no_password_doesnt_call_api(self, m_post):
client = HubstaffClient(
app_token=self.app_token,
auth_token=self.auth_token)
client.authenticate()
m_post.assert_not_called()
@mock.patch('requests.post')
def test_authenticate_method_returns_auth_token(self, m_post):
m_post.return_value.status_code = 200
m_post.return_value.json.return_value = {
'user': {'auth_token': self.auth_token}
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='ValidPasswordHere')
auth_token = client.authenticate()
self.assertEqual(auth_token, self.auth_token)
@mock.patch('requests.post')
def test_authenticate_method_setups_auth_token(self, m_post):
m_post.return_value.status_code = 200
m_post.return_value.json.return_value = {
'user': {'auth_token': self.auth_token}
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='ValidPasswordHere')
client.authenticate()
self.assertEqual(client._auth_token, self.auth_token)
@mock.patch('requests.post')
def test_authenticate_method_calls_post_with_right_params(self, m_post):
m_post.return_value.status_code = 200
m_post.return_value.json.return_value = {
'user': {'auth_token': self.auth_token}
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='ValidPasswordHere')
client.authenticate()
self.assertEqual(m_post.call_count, 1)
call_args, call_kwargs = m_post.call_args
self.assertEqual(call_args, (
'https://api.hubstaff.com/v1/auth',
))
self.assertDictEqual(call_kwargs, {
'headers': {
'App-Token': self.app_token
},
'data': {
'email': '[email protected]',
'password': 'ValidPasswordHere'
},
})
@mock.patch('requests.post')
def test_authenticate_method_raises_too_many_requests_error(self, m_post):
m_post.return_value.status_code = 429
m_post.return_value.json.return_value = {
'error': 'Rate limit has been reached. '
'Please wait before making your next request.'
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='ValidPasswordHere')
with self.assertRaises(HubstaffError) as err_ctx:
client.authenticate()
self.assertEqual(err_ctx.exception.message,
'Rate limit has been reached. '
'Please wait before making your next request.')
@mock.patch('requests.post')
def test_authenticate_method_raises_invalid_app_token_error(self, m_post):
m_post.return_value.status_code = 401
m_post.return_value.json.return_value = {
'error': 'Invalid app_token'
}
client = HubstaffClient(
app_token='bad_token',
username='[email protected]',
password='ValidPasswordHere')
with self.assertRaises(HubstaffAuthError) as err_ctx:
client.authenticate()
self.assertEqual(err_ctx.exception.message, 'Invalid app_token')
@mock.patch('requests.post')
def test_authenticate_method_raises_invalid_username_error(self, m_post):
m_post.return_value.status_code = 401
m_post.return_value.json.return_value = {
'error': 'Invalid email and/or password'
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='ValidPasswordHere')
with self.assertRaises(HubstaffAuthError) as err_ctx:
client.authenticate()
self.assertEqual(err_ctx.exception.message,
'Invalid email and/or password')
@mock.patch('requests.post')
def test_authenticate_method_raises_invalid_password_error(self, m_post):
m_post.return_value.status_code = 401
m_post.return_value.json.return_value = {
'error': 'Invalid email and/or password'
}
client = HubstaffClient(
app_token=self.app_token,
username='[email protected]',
password='?' * 16)
with self.assertRaises(HubstaffAuthError) as err_ctx:
client.authenticate()
self.assertEqual(err_ctx.exception.message,
'Invalid email and/or password')
if __name__ == '__main__':
unittest.main()