forked from drweaver/py_garage_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarage_auth_test.py
executable file
·98 lines (77 loc) · 3.36 KB
/
garage_auth_test.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
import unittest
from mock import MagicMock
from garage_auth import GarageAuth, AuthError
from io import StringIO
file='garage_auth_test.json'
class GarageAuthTest(unittest.TestCase):
def setUp(self):
def getitem(key):
config = { "client_id": "12345", "authorised": ["persona","personb"], "distance_limit_miles": 10, "lat": 0, "lng": 0 }
return config[key]
self._config = MagicMock()
self._config.__getitem__.side_effect = getitem
self._config.reload = MagicMock()
def test_forwarded_user_nobody(self):
auth = GarageAuth(self._config)
with self.assertRaises(AuthError):
auth.user("nobody")
def test_forwarded_user_none(self):
auth = GarageAuth(self._config)
with self.assertRaises(AuthError):
auth.user(None)
def test_forwarded_user_persona(self):
auth = GarageAuth(self._config)
self.assertEqual("persona",auth.user("persona"))
def test_forwarded_user_persona(self):
auth = GarageAuth(self._config)
self.assertEqual("personb",auth.user("personb"))
def test_token_error(self):
urlopenmock = MagicMock(return_value='{ "error": "some error"} ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_error_with_response(self):
urlopenmock = MagicMock(return_value="<html><body>rubbish</body></html> ")
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_invalid_audience(self):
urlopenmock = MagicMock(return_value='{ "audience": "54321", "email": "persona" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_email_not_auth(self):
urlopenmock = MagicMock(return_value='{ "audience": "12345", "email": "personc" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_audience_not_exist(self):
urlopenmock = MagicMock(return_value='{ "audiences": "12345", "email": "persona" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_email_not_exist(self):
urlopenmock = MagicMock(return_value='{ "audience": "12345", "emails": "persona" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
with self.assertRaises(AuthError):
auth.token("mytoken")
def test_token_response_1(self):
urlopenmock = MagicMock(return_value='{ "audience": "12345", "email": "persona" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
self.assertEqual("persona", auth.token("mytoken"))
self._config.reload.assert_called_with()
def test_token_response_2(self):
urlopenmock = MagicMock(return_value='{ "audience": "12345", "email": "personb" } ')
auth = GarageAuth(self._config, urlopen=urlopenmock)
self.assertEqual("personb", auth.token("mytoken"))
self._config.reload.assert_called_with()
def test_location_too_far(self):
auth = GarageAuth(self._config)
with self.assertRaises(AuthError):
auth.location( (0,0.2) )
def test_location_ok(self):
auth = GarageAuth(self._config)
self.assertEqual(6, int(auth.location( (0,0.1) )))
self._config.reload.assert_called_with()
if __name__ == '__main__':
unittest.main()