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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
import unittest
from unittest import mock
import json
import requests
from ...exceptions import Auth0Error, RateLimitError
from ...authentication.back_channel_login import BackChannelLogin
class TestBackChannelLogin(unittest.TestCase):
@mock.patch("auth0.rest.RestClient.post")
def test_ciba(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
g.back_channel_login(
binding_message="This is a binding message",
login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
scope="openid",
)
args, kwargs = mock_post.call_args
self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
self.assertEqual(
kwargs["data"],
{
"client_id": "cid",
"client_secret": "clsec",
"binding_message": "This is a binding message",
"login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
"scope": "openid",
},
)
@mock.patch("requests.request")
def test_server_error(self, mock_requests_request):
response = requests.Response()
response.status_code = 400
response._content = b'{"error":"foo"}'
mock_requests_request.return_value = response
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
with self.assertRaises(Auth0Error) as context:
g.back_channel_login(
binding_message="msg",
login_hint="hint",
scope="openid"
)
self.assertEqual(context.exception.status_code, 400)
self.assertEqual(context.exception.message, 'foo')
@mock.patch("auth0.rest.RestClient.post")
def test_should_require_binding_message(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
# Expecting an exception to be raised when binding_message is missing
with self.assertRaises(Exception) as context:
g.back_channel_login(
login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }',
scope="openid",
)
# Assert the error message is correct
self.assertIn("missing 1 required positional argument: \'binding_message\'", str(context.exception))
@mock.patch("auth0.rest.RestClient.post")
def test_should_require_login_hint(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
# Expecting an exception to be raised when login_hint is missing
with self.assertRaises(Exception) as context:
g.back_channel_login(
binding_message="This is a binding message.",
scope="openid",
)
# Assert the error message is correct
self.assertIn("missing 1 required positional argument: \'login_hint\'", str(context.exception))
@mock.patch("auth0.rest.RestClient.post")
def test_should_require_scope(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
# Expecting an exception to be raised when scope is missing
with self.assertRaises(Exception) as context:
g.back_channel_login(
binding_message="This is a binding message.",
login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }',
)
# Assert the error message is correct
self.assertIn("missing 1 required positional argument: \'scope\'", str(context.exception))
@mock.patch("auth0.rest.RestClient.post")
def test_with_authorization_details(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
g.back_channel_login(
binding_message="This is a binding message.",
login_hint= json.dumps({"format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID"}),
scope="openid",
authorization_details=[
{
"type":"payment_initiation","locations":["https://example.com/payments"],
"instructedAmount":
{
"currency":"EUR","amount":"123.50"
},
"creditorName":"Merchant A",
"creditorAccount":
{
"bic":"ABCIDEFFXXX",
"iban":"DE021001001093071118603"
},
"remittanceInformationUnstructured":"Ref Number Merchant"
}
],
)
args, kwargs = mock_post.call_args
expected_data = {
"client_id": "cid",
"client_secret": "clsec",
"binding_message": "This is a binding message.",
"login_hint": json.dumps({"format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID"}),
"scope": "openid",
"authorization_details": json.dumps([
{
"type":"payment_initiation","locations":["https://example.com/payments"],
"instructedAmount":
{
"currency":"EUR","amount":"123.50"
},
"creditorName":"Merchant A",
"creditorAccount":
{
"bic":"ABCIDEFFXXX",
"iban":"DE021001001093071118603"
},
"remittanceInformationUnstructured":"Ref Number Merchant"
}
]),
}
actual_data = kwargs["data"]
self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
self.assertEqual(
actual_data,
expected_data,
"Request data does not match expected data after JSON serialization."
)
@mock.patch("auth0.rest.RestClient.post")
def test_with_request_expiry(self, mock_post):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
g.back_channel_login(
binding_message="This is a binding message",
login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
scope="openid",
requested_expiry=100
)
args, kwargs = mock_post.call_args
self.assertEqual(args[0], "https://my.domain.com/bc-authorize")
self.assertEqual(
kwargs["data"],
{
"client_id": "cid",
"client_secret": "clsec",
"binding_message": "This is a binding message",
"login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }",
"scope": "openid",
"requested_expiry": "100",
},
)
def test_requested_expiry_negative_raises(self):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
with self.assertRaises(ValueError):
g.back_channel_login(
binding_message="msg",
login_hint="hint",
scope="openid",
requested_expiry=-10
)
def test_requested_expiry_zero_raises(self):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
with self.assertRaises(ValueError):
g.back_channel_login(
binding_message="msg",
login_hint="hint",
scope="openid",
requested_expiry=0
)
def test_requested_non_int_raises(self):
g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec")
with self.assertRaises(ValueError):
g.back_channel_login(
binding_message="msg",
login_hint="hint",
scope="openid",
requested_expiry="string_instead_of_int"
)
|