|
| 1 | + |
| 2 | +import unittest |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import requests |
| 6 | +from ...exceptions import Auth0Error, RateLimitError |
| 7 | + |
| 8 | +from ...authentication.back_channel_login import BackChannelLogin |
| 9 | + |
| 10 | +class TestBackChannelLogin(unittest.TestCase): |
| 11 | + @mock.patch("auth0.rest.RestClient.post") |
| 12 | + def test_ciba(self, mock_post): |
| 13 | + g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") |
| 14 | + |
| 15 | + g.back_channel_login( |
| 16 | + binding_message="This is a binding message", |
| 17 | + login_hint="{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }", |
| 18 | + scope="openid", |
| 19 | + ) |
| 20 | + |
| 21 | + args, kwargs = mock_post.call_args |
| 22 | + |
| 23 | + self.assertEqual(args[0], "https://my.domain.com/bc-authorize") |
| 24 | + self.assertEqual( |
| 25 | + kwargs["data"], |
| 26 | + { |
| 27 | + "client_id": "cid", |
| 28 | + "client_secret": "clsec", |
| 29 | + "binding_message": "This is a binding message", |
| 30 | + "login_hint": "{ \"format\": \"iss_sub\", \"iss\": \"https://my.domain.auth0.com/\", \"sub\": \"auth0|[USER ID]\" }", |
| 31 | + "scope": "openid", |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + @mock.patch("auth0.rest.RestClient.post") |
| 36 | + def test_should_require_binding_message(self, mock_post): |
| 37 | + g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") |
| 38 | + |
| 39 | + # Expecting an exception to be raised when binding_message is missing |
| 40 | + with self.assertRaises(Exception) as context: |
| 41 | + g.back_channel_login( |
| 42 | + login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }', |
| 43 | + scope="openid", |
| 44 | + ) |
| 45 | + |
| 46 | + # Assert the error message is correct |
| 47 | + self.assertIn("missing 1 required positional argument: \'binding_message\'", str(context.exception)) |
| 48 | + |
| 49 | + @mock.patch("auth0.rest.RestClient.post") |
| 50 | + def test_should_require_login_hint(self, mock_post): |
| 51 | + g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") |
| 52 | + |
| 53 | + # Expecting an exception to be raised when login_hint is missing |
| 54 | + with self.assertRaises(Exception) as context: |
| 55 | + g.back_channel_login( |
| 56 | + binding_message="This is a binding message.", |
| 57 | + scope="openid", |
| 58 | + ) |
| 59 | + |
| 60 | + # Assert the error message is correct |
| 61 | + self.assertIn("missing 1 required positional argument: \'login_hint\'", str(context.exception)) |
| 62 | + |
| 63 | + @mock.patch("auth0.rest.RestClient.post") |
| 64 | + def test_should_require_scope(self, mock_post): |
| 65 | + g = BackChannelLogin("my.domain.com", "cid", client_secret="clsec") |
| 66 | + |
| 67 | + # Expecting an exception to be raised when scope is missing |
| 68 | + with self.assertRaises(Exception) as context: |
| 69 | + g.back_channel_login( |
| 70 | + binding_message="This is a binding message.", |
| 71 | + login_hint='{ "format": "iss_sub", "iss": "https://my.domain.auth0.com/", "sub": "auth0|USER_ID" }', |
| 72 | + ) |
| 73 | + |
| 74 | + # Assert the error message is correct |
| 75 | + self.assertIn("missing 1 required positional argument: \'scope\'", str(context.exception)) |
| 76 | + |
| 77 | + |
| 78 | + |
0 commit comments