Debian Codegraph @

tests/authentication/test_back_channel_login.py

raw symbols SCIP
1
2 import unittest
3 from unittest import mock
4 import json
5
6 import requests
7 from auth0.authentication.exceptions import Auth0Error, RateLimitError
8
9 from auth0.authentication.back_channel_login import BackChannelLogin
10
11 class TestBackChannelLogin(unittest.TestCase):
12 @mock.patch("auth0.authentication.rest.RestClient.post")
13 def test_ciba(self, mock_post):
14 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
15
16 g.back_channel_login(
17 binding_message="This is a binding message",
18 login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
19 scope="openid",
20 )
21
22 args, kwargs = mock_post.call_args
23
24 self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
25 self.assertEqual(
26 kwargs["data"],
27 {
28 "client_id": "cid",
29 "client_secret": "clsec",
30 "binding_message": "This is a binding message",
31 "login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
32 "scope": "openid",
33 },
34 )
35
36 @mock.patch("requests.request")
37 def test_server_error(self, mock_requests_request):
38 response = requests.Response()
39 response.status_code = 400
40 response._content = b'{"error":"foo"}'
41 mock_requests_request.return_value = response
42
43 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
44 with self.assertRaises(Auth0Error) as context:
45 g.back_channel_login(
46 binding_message="msg",
47 login_hint="hint",
48 scope="openid"
49 )
50 self.assertEqual(context.exception.status_code, 400)
51 self.assertEqual(context.exception.message, 'foo')
52
53 @mock.patch("auth0.authentication.rest.RestClient.post")
54 def test_should_require_binding_message(self, mock_post):
55 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
56
57 # Expecting an exception to be raised when binding_message is missing
58 with self.assertRaises(Exception) as context:
59 g.back_channel_login(
60 login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }',
61 scope="openid",
62 )
63
64 # Assert the error message is correct
65 self.assertIn("missing 1 required positional argument: \'binding_message\'", str(context.exception))
66
67 @mock.patch("auth0.authentication.rest.RestClient.post")
68 def test_should_require_login_hint(self, mock_post):
69 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
70
71 # Expecting an exception to be raised when login_hint is missing
72 with self.assertRaises(Exception) as context:
73 g.back_channel_login(
74 binding_message="This is a binding message.",
75 scope="openid",
76 )
77
78 # Assert the error message is correct
79 self.assertIn("missing 1 required positional argument: \'login_hint\'", str(context.exception))
80
81 @mock.patch("auth0.authentication.rest.RestClient.post")
82 def test_should_require_scope(self, mock_post):
83 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
84
85 # Expecting an exception to be raised when scope is missing
86 with self.assertRaises(Exception) as context:
87 g.back_channel_login(
88 binding_message="This is a binding message.",
89 login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }',
90 )
91
92 # Assert the error message is correct
93 self.assertIn("missing 1 required positional argument: \'scope\'", str(context.exception))
94
95 @mock.patch("auth0.authentication.rest.RestClient.post")
96 def test_with_authorization_details(self, mock_post):
97 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
98 g.back_channel_login(
99 binding_message="This is a binding message.",
100 login_hint= json.dumps({"format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID"}),
101 scope="openid",
102 authorization_details=[
103 {
104 "type":"payment_initiation","locations":["https://example.com/payments"],
105 "instructedAmount":
106 {
107 "currency":"EUR","amount":"123.50"
108 },
109 "creditorName":"Merchant A",
110 "creditorAccount":
111 {
112 "bic":"ABCIDEFFXXX",
113 "iban":"DE021001001093071118603"
114 },
115 "remittanceInformationUnstructured":"Ref Number Merchant"
116 }
117 ],
118 )
119
120 args, kwargs = mock_post.call_args
121
122 expected_data = {
123 "client_id": "cid",
124 "client_secret": "clsec",
125 "binding_message": "This is a binding message.",
126 "login_hint": json.dumps({"format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID"}),
127 "scope": "openid",
128 "authorization_details": json.dumps([
129 {
130 "type":"payment_initiation","locations":["https://example.com/payments"],
131 "instructedAmount":
132 {
133 "currency":"EUR","amount":"123.50"
134 },
135 "creditorName":"Merchant A",
136 "creditorAccount":
137 {
138 "bic":"ABCIDEFFXXX",
139 "iban":"DE021001001093071118603"
140 },
141 "remittanceInformationUnstructured":"Ref Number Merchant"
142 }
143 ]),
144 }
145
146 actual_data = kwargs["data"]
147
148 self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
149
150 self.assertEqual(
151 actual_data,
152 expected_data,
153 "Request data does not match expected data after JSON serialization."
154 )
155
156 @mock.patch("auth0.authentication.rest.RestClient.post")
157 def test_with_request_expiry(self, mock_post):
158 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
159
160 g.back_channel_login(
161 binding_message="This is a binding message",
162 login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
163 scope="openid",
164 requested_expiry=100
165 )
166
167 args, kwargs = mock_post.call_args
168
169 self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
170 self.assertEqual(
171 kwargs["data"],
172 {
173 "client_id": "cid",
174 "client_secret": "clsec",
175 "binding_message": "This is a binding message",
176 "login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
177 "scope": "openid",
178 "requested_expiry": "100",
179 },
180 )
181
182 def test_requested_expiry_negative_raises(self):
183 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
184 with self.assertRaises(ValueError):
185 g.back_channel_login(
186 binding_message="msg",
187 login_hint="hint",
188 scope="openid",
189 requested_expiry=-10
190 )
191
192 def test_requested_expiry_zero_raises(self):
193 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
194 with self.assertRaises(ValueError):
195 g.back_channel_login(
196 binding_message="msg",
197 login_hint="hint",
198 scope="openid",
199 requested_expiry=0
200 )
201
202 def test_requested_non_int_raises(self):
203 g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
204 with self.assertRaises(ValueError):
205 g.back_channel_login(
206 binding_message="msg",
207 login_hint="hint",
208 scope="openid",
209 requested_expiry="string_instead_of_int"
210 )