| 1 | import unittest |
| 2 | import json |
| 3 | from unittest import mock |
| 4 | |
| 5 | from auth0.authentication.pushed_authorization_requests import PushedAuthorizationRequests |
| 6 | |
| 7 | |
| 8 | class TestRevokeToken(unittest.TestCase): |
| 9 | @mock.patch("auth0.authentication.rest.RestClient.post") |
| 10 | def test_par(self, mock_post): |
| 11 | a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!") |
| 12 | a.pushed_authorization_request( |
| 13 | response_type="code", redirect_uri="https://example.com/callback" |
| 14 | ) |
| 15 | |
| 16 | args, kwargs = mock_post.call_args |
| 17 | |
| 18 | self.assertEqual(args[0], "https://my.domain.com/oauth/par") |
| 19 | self.assertEqual( |
| 20 | kwargs["data"], |
| 21 | { |
| 22 | "client_id": "cid", |
| 23 | "client_secret": "sh!", |
| 24 | "response_type": "code", |
| 25 | "redirect_uri": "https://example.com/callback", |
| 26 | }, |
| 27 | ) |
| 28 | |
| 29 | @mock.patch("auth0.authentication.rest.RestClient.post") |
| 30 | def test_par_custom_params(self, mock_post): |
| 31 | a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!") |
| 32 | a.pushed_authorization_request( |
| 33 | response_type="code", redirect_uri="https://example.com/callback", foo="bar" |
| 34 | ) |
| 35 | |
| 36 | args, kwargs = mock_post.call_args |
| 37 | |
| 38 | self.assertEqual(args[0], "https://my.domain.com/oauth/par") |
| 39 | self.assertEqual( |
| 40 | kwargs["data"], |
| 41 | { |
| 42 | "client_id": "cid", |
| 43 | "client_secret": "sh!", |
| 44 | "response_type": "code", |
| 45 | "redirect_uri": "https://example.com/callback", |
| 46 | "foo": "bar", |
| 47 | }, |
| 48 | ) |
| 49 | |
| 50 | @mock.patch("auth0.authentication.rest.RestClient.post") |
| 51 | def test_with_authorization_details(self, mock_post): |
| 52 | a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!") |
| 53 | a.pushed_authorization_request( |
| 54 | response_type="code", |
| 55 | redirect_uri="https://example.com/callback", |
| 56 | authorization_details=[{"type": "money_transfer", "instructedAmount": {"amount": 2500, "currency": "USD"}}], |
| 57 | ) |
| 58 | |
| 59 | args, kwargs = mock_post.call_args |
| 60 | |
| 61 | expected_data = { |
| 62 | "client_id": "cid", |
| 63 | "client_secret": "sh!", |
| 64 | "response_type": "code", |
| 65 | "redirect_uri": "https://example.com/callback", |
| 66 | "authorization_details": [{"type": "money_transfer", "instructedAmount": {"amount": 2500, "currency": "USD"}}], |
| 67 | } |
| 68 | |
| 69 | actual_data = kwargs["data"] |
| 70 | |
| 71 | self.assertEqual(args[0], "https://my.domain.com/oauth/par") |
| 72 | |
| 73 | self.assertEqual( |
| 74 | json.dumps(actual_data, sort_keys=True), |
| 75 | json.dumps(expected_data, sort_keys=True) |
| 76 | ) |
| 77 | |
| 78 | @mock.patch("auth0.authentication.rest.RestClient.post") |
| 79 | def test_jar(self, mock_post): |
| 80 | a = PushedAuthorizationRequests("my.domain.com", "cid", client_secret="sh!") |
| 81 | a.pushed_authorization_request( |
| 82 | response_type="code", |
| 83 | redirect_uri="https://example.com/callback", |
| 84 | request="my-jwt-request", |
| 85 | ) |
| 86 | |
| 87 | args, kwargs = mock_post.call_args |
| 88 | |
| 89 | expected_data = { |
| 90 | "client_id": "cid", |
| 91 | "client_secret": "sh!", |
| 92 | "response_type": "code", |
| 93 | "redirect_uri": "https://example.com/callback", |
| 94 | "request": 'my-jwt-request', |
| 95 | } |
| 96 | |
| 97 | actual_data = kwargs["data"] |
| 98 | |
| 99 | self.assertEqual(args[0], "https://my.domain.com/oauth/par") |
| 100 | |
| 101 | self.assertEqual( |
| 102 | json.dumps(actual_data, sort_keys=True), |
| 103 | json.dumps(expected_data, sort_keys=True) |
| 104 | ) |