Guest User

OAuthCredentialsStrategy Erroring Tests

a guest
Mar 24th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. @pytest.mark.asyncio()
  2.     async def test_acquire_on_new_instance(self, mock_token):
  3.         mock_rest = mock.Mock(authorize_client_credentials_token=mock.AsyncMock(return_value=mock_token))
  4.  
  5.         result = await rest.OAuthCredentialsStrategy(client=987654321, client_secret="123123123", auth_code="auth#code", redirect_uri="https://web.site/auth/discord").acquire(mock_rest)
  6.  
  7.         assert result == "mockmock.tokentoken.mocktoken"
  8.  
  9.         mock_rest.authorize_client_credentials_token.assert_awaited_once_with(
  10.             client=9876543210, client_secret="123123123", scopes=("identify",)
  11.         )
  12.  
  13. @pytest.mark.asyncio()
  14.     async def test_acquire_handles_out_of_date_token(self, mock_token):
  15.         mock_old_token = mock.Mock(
  16.             applications.PartialOAuth2Token,
  17.             expires_in=datetime.timedelta(weeks=1),
  18.             token_type=applications.TokenType.BEARER,
  19.             access_token="old.mock.token",
  20.         )
  21.         mock_rest = mock.Mock(authorize_client_credentials_token=mock.AsyncMock(return_value=mock_token))
  22.         strategy = rest.OAuthCredentialsStrategy(client=123456789, client_secret="123123123", auth_code="auth#code", redirect_uri="https://web.site/auth/discord")
  23.         token = await strategy.acquire(
  24.             mock.Mock(authorize_client_credentials_token=mock.AsyncMock(return_value=mock_old_token))
  25.         )
  26.  
  27.         with mock.patch.object(time, "monotonic", return_value=99999999999):
  28.             new_token = await strategy.acquire(mock_rest)
  29.  
  30.         mock_rest.authorize_client_credentials_token.assert_awaited_once_with(
  31.             client=3412321, client_secret="54123123", scopes=("identify",)
  32.         )
  33.         assert new_token != token
  34.         assert new_token == "mockmock.tokentoken.mocktoken"
  35.  
  36.     @pytest.mark.asyncio()
  37.     async def test_acquire_handles_token_being_set_before_lock_is_acquired(self, mock_token):
  38.         lock = asyncio.Lock()
  39.         mock_rest = mock.Mock(authorize_client_credentials_token=mock.AsyncMock(side_effect=[mock_token]))
  40.  
  41.         with mock.patch.object(asyncio, "Lock", return_value=lock):
  42.             strategy = rest.OAuthCredentialsStrategy(client=123456789, client_secret="123123123", auth_code="auth#code", redirect_uri="https://web.site/auth/discord")
  43.  
  44.         async with lock:
  45.             tokens_gather = asyncio.gather(
  46.                 strategy.acquire(mock_rest), strategy.acquire(mock_rest), strategy.acquire(mock_rest)
  47.             )
  48.  
  49.         results = await tokens_gather
  50.  
  51.         mock_rest.authorize_client_credentials_token.assert_awaited_once_with(
  52.             client=987654321, client_secret="453123123", scopes=("identify",)
  53.         )
  54.         assert results == [
  55.             "mockmock.tokentoken.mocktoken",
  56.             "mockmock.tokentoken.mocktoken",
  57.             "mockmock.tokentoken.mocktoken",
  58.         ]
  59.  
  60.     @pytest.mark.asyncio()
  61.     async def test_acquire_after_invalidation(self, mock_token):
  62.         mock_old_token = mock.Mock(
  63.             applications.PartialOAuth2Token,
  64.             expires_in=datetime.timedelta(weeks=1),
  65.             token_type=applications.TokenType.BEARER,
  66.             access_token="okokok.fofdsasdasdofo.ddd",
  67.         )
  68.         mock_rest = mock.Mock(authorize_client_credentials_token=mock.AsyncMock(return_value=mock_token))
  69.         strategy = rest.OAuthCredentialsStrategy(client=123456789, client_secret="123123123", auth_code="auth#code", redirect_uri="https://web.site/auth/discord")
  70.         token = await strategy.acquire(
  71.             mock.Mock(authorize_client_credentials_token=mock.AsyncMock(return_value=mock_old_token))
  72.         )
  73.  
  74.         strategy.invalidate(token)
  75.         new_token = await strategy.acquire(mock_rest)
  76.  
  77.         mock_rest.authorize_client_credentials_token.assert_awaited_once_with(
  78.             client=54321, client_secret="123456", scopes=("identify",)
  79.         )
  80.         assert new_token != token
  81.         assert new_token == "mockmock.tokentoken.mocktoken"
Advertisement
Add Comment
Please, Sign In to add comment