-
Notifications
You must be signed in to change notification settings - Fork 32
/
test_flask_cognito.py
136 lines (106 loc) · 4.94 KB
/
test_flask_cognito.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
import flask_cognito
from unittest import TestCase
from unittest.mock import Mock
class objectview(object):
def __init__(self, d):
self.__dict__ = d
class TestHeaderPrefix(TestCase):
def test_valid_header_prefix(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : 'Bearer'})
get_mock = Mock(return_value='Bearer Test')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
result = ca.get_token()
assert (result == 'Test')
def test_incorrect_header_prefix(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : 'Bearer'})
get_mock = Mock(return_value='Something Test')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
self.assertRaises(flask_cognito.CognitoAuthError, ca.get_token)
def test_malformed_header(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : 'Bearer'})
get_mock = Mock(return_value='Something To Fail')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
self.assertRaises(flask_cognito.CognitoAuthError, ca.get_token)
def test_with_prefix_empty_string(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : ''})
get_mock = Mock(return_value='Something')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
result = ca.get_token()
self.assertEqual('Something',result)
def test_with_prefix_none(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : None})
get_mock = Mock(return_value='Something')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
result = ca.get_token()
self.assertEqual('Something',result)
def test_without_prefix_malformed(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : None})
get_mock = Mock(return_value='Something Else')
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
self.assertRaises(flask_cognito.CognitoAuthError, ca.get_token)
def test_without_prefix_missing(self):
flask_cognito._cog = objectview({'jwt_header_name' :'Authorization',
'jwt_header_prefix' : None})
get_mock = Mock(return_value=None)
request_mock = objectview({'headers': objectview({'get': get_mock})})
flask_cognito.request = request_mock
ca = flask_cognito.CognitoAuth()
result = ca.get_token()
self.assertIsNone(result)
def test_group_permissions_decorator(self):
flask_cognito.current_cognito_jwt = {'cognito:groups': ['admin', 'other']}
@flask_cognito.cognito_group_permissions(['admin'])
def some_func():
return True
self.assertTrue(some_func())
def test_group_permissions_fail_if_not_in_group(self):
flask_cognito.current_cognito_jwt = {'cognito:groups': ['other']}
@flask_cognito.cognito_group_permissions(['admin'])
def some_func():
return True
self.assertRaises(flask_cognito.CognitoAuthError, some_func)
def test_group_permissions_fail_if_no_groups(self):
flask_cognito.current_cognito_jwt = {'cognito:groups': []}
@flask_cognito.cognito_group_permissions(['admin'])
def some_func():
return True
self.assertRaises(flask_cognito.CognitoAuthError, some_func)
def test_group_permissions_fail_if_groups_is_none(self):
flask_cognito.current_cognito_jwt = {'cognito:groups': None}
@flask_cognito.cognito_group_permissions(['admin'])
def some_func():
return True
self.assertRaises(flask_cognito.CognitoAuthError, some_func)
def test_group_permissions_fail_if_no_group_attribute(self):
flask_cognito.current_cognito_jwt = {'cognito:name': 'Something'}
@flask_cognito.cognito_group_permissions(['admin'])
def some_func():
return True
self.assertRaises(flask_cognito.CognitoAuthError, some_func)
def test_identity_handler_late_init(self):
ca = flask_cognito.CognitoAuth()
# This throws an exception if self.identity_callback is not defined yet,
# particularly if the property is defined by init_app which may not have
# been called yet.
@ca.identity_handler
def handler(payload):
return None
self.assertEqual(ca.identity_callback, handler)