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
|
import re
import unittest
from unittest.mock import ANY, MagicMock
import pytest
from aioresponses import CallbackResult, aioresponses
from yarl import URL
from auth0.management.async_auth0 import AsyncAuth0 as Auth0
clients = re.compile(r"^https://example\.com/api/v2/clients.*")
factors = re.compile(r"^https://example\.com/api/v2/guardian/factors.*")
payload = {"foo": "bar"}
def get_callback(status=200):
mock = MagicMock(return_value=CallbackResult(status=status, payload=payload))
def callback(url, **kwargs):
return mock(url, **kwargs)
return callback, mock
class TestAuth0(unittest.IsolatedAsyncioTestCase):
@pytest.mark.asyncio
@aioresponses()
async def test_get(self, mocked):
callback, mock = get_callback()
mocked.get(clients, callback=callback)
auth0 = Auth0(domain="example.com", token="jwt")
self.assertEqual(await auth0.clients.all_async(), payload)
mock.assert_called_with(
URL("https://example.com/api/v2/clients?include_fields=true"),
allow_redirects=True,
params={"include_fields": "true"},
headers=ANY,
timeout=ANY,
)
@pytest.mark.asyncio
@aioresponses()
async def test_shared_session(self, mocked):
callback, mock = get_callback()
callback2, mock2 = get_callback()
mocked.get(clients, callback=callback)
mocked.put(factors, callback=callback2)
async with Auth0(domain="example.com", token="jwt") as auth0:
self.assertEqual(await auth0.clients.all_async(), payload)
self.assertEqual(
await auth0.guardian.update_factor_async("factor-1", {"factor": 1}),
payload,
)
mock.assert_called_with(
URL("https://example.com/api/v2/clients?include_fields=true"),
allow_redirects=True,
params={"include_fields": "true"},
headers=ANY,
timeout=ANY,
)
mock2.assert_called_with(
URL("https://example.com/api/v2/guardian/factors/factor-1"),
allow_redirects=True,
json={"factor": 1},
headers=ANY,
timeout=ANY,
)
|