| 1 |
"""Tests for CDSETool's credentials module.""" |
| 2 |
|
| 3 |
# pyright: reportAttributeAccessIssue=false |
| 4 |
|
| 5 |
import datetime |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
import pytest |
| 9 |
import requests |
| 10 |
|
| 11 |
from cdsetool.credentials import ( |
| 12 |
Credentials, |
| 13 |
InvalidCredentialsException, |
| 14 |
NoCredentialsException, |
| 15 |
TokenExchangeException, |
| 16 |
) |
| 17 |
|
| 18 |
from ..mock_auth import mock_jwks, mock_openid, mock_token |
| 19 |
|
| 20 |
|
| 21 |
def test_ensure_tokens(requests_mock: Any, mocker: Any) -> None: |
| 22 |
mock_openid(requests_mock) |
| 23 |
mock_token(requests_mock) |
| 24 |
mock_jwks(mocker) |
| 25 |
|
| 26 |
credentials = Credentials("username", "password") |
| 27 |
assert credentials._Credentials__access_token is not None |
| 28 |
assert credentials._Credentials__refresh_token is not None |
| 29 |
|
| 30 |
credentials._Credentials__access_token_expires = ( |
| 31 |
datetime.datetime.now() - datetime.timedelta(hours=100) |
| 32 |
) |
| 33 |
spy = mocker.spy(credentials, "_Credentials__token_exchange") |
| 34 |
credentials._Credentials__ensure_tokens() |
| 35 |
spy.assert_called_once() |
| 36 |
|
| 37 |
credentials._Credentials__access_token_expires = ( |
| 38 |
datetime.datetime.now() - datetime.timedelta(hours=100) |
| 39 |
) |
| 40 |
credentials._Credentials__refresh_token_expires = ( |
| 41 |
datetime.datetime.now() - datetime.timedelta(hours=100) |
| 42 |
) |
| 43 |
credentials._Credentials__ensure_tokens() |
| 44 |
assert spy.call_count == 2 |
| 45 |
|
| 46 |
|
| 47 |
def test_read_credentials(requests_mock: Any, mocker: Any) -> None: |
| 48 |
mock_openid(requests_mock) |
| 49 |
mock_token(requests_mock) |
| 50 |
mock_jwks(mocker) |
| 51 |
|
| 52 |
mocker.patch( |
| 53 |
"netrc.netrc", |
| 54 |
return_value=mocker.Mock( |
| 55 |
authenticators=lambda x: ("username", None, "password") |
| 56 |
), |
| 57 |
) |
| 58 |
|
| 59 |
credentials = Credentials() |
| 60 |
assert credentials._Credentials__username == "username" |
| 61 |
assert credentials._Credentials__password == "password" |
| 62 |
|
| 63 |
mocker.patch("netrc.netrc", return_value=mocker.Mock(authenticators=lambda x: None)) |
| 64 |
|
| 65 |
with pytest.raises(NoCredentialsException): |
| 66 |
credentials = Credentials() |
| 67 |
|
| 68 |
|
| 69 |
def test_refresh_token(requests_mock: Any, mocker: Any) -> None: |
| 70 |
mock_openid(requests_mock) |
| 71 |
mock_token(requests_mock) |
| 72 |
mock_jwks(mocker) |
| 73 |
|
| 74 |
credentials = Credentials("username", "password") |
| 75 |
assert credentials._Credentials__access_token is not None |
| 76 |
assert credentials._Credentials__refresh_token is not None |
| 77 |
|
| 78 |
mock_token(requests_mock) # mock again to return a new token |
| 79 |
|
| 80 |
prev_access_token = credentials._Credentials__access_token |
| 81 |
credentials._Credentials__access_token_expires = datetime.datetime.now() |
| 82 |
credentials._Credentials__refresh_token_expires = datetime.datetime.now() |
| 83 |
credentials._Credentials__ensure_tokens() |
| 84 |
|
| 85 |
assert credentials._Credentials__access_token is not None |
| 86 |
assert credentials._Credentials__refresh_token is not None |
| 87 |
assert prev_access_token != credentials._Credentials__access_token |
| 88 |
|
| 89 |
|
| 90 |
def test_get_session(requests_mock: Any, mocker: Any) -> None: |
| 91 |
mock_openid(requests_mock) |
| 92 |
mock_token(requests_mock) |
| 93 |
mock_jwks(mocker) |
| 94 |
|
| 95 |
credentials = Credentials("username", "password") |
| 96 |
session = credentials.get_session() |
| 97 |
|
| 98 |
assert isinstance(session, requests.Session) |
| 99 |
assert ( |
| 100 |
session.headers.get("Authorization") |
| 101 |
== f"Bearer {credentials._Credentials__access_token}" |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def test_token_exchange(requests_mock: Any, mocker: Any) -> None: |
| 106 |
mock_openid(requests_mock) |
| 107 |
mock_token(requests_mock) |
| 108 |
mock_jwks(mocker) |
| 109 |
|
| 110 |
credentials = Credentials("myuser123123", "password") |
| 111 |
|
| 112 |
data = { |
| 113 |
"grant_type": "password", |
| 114 |
"username": credentials._Credentials__username, |
| 115 |
"password": credentials._Credentials__password, |
| 116 |
"client_id": "cdse-public", |
| 117 |
} |
| 118 |
|
| 119 |
credentials._Credentials__token_exchange(data) |
| 120 |
|
| 121 |
requests_mock.post( |
| 122 |
"https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token", |
| 123 |
text="{}", |
| 124 |
status_code=401, |
| 125 |
) |
| 126 |
|
| 127 |
with pytest.raises( |
| 128 |
InvalidCredentialsException, match="with username: myuser123123" |
| 129 |
): |
| 130 |
credentials._Credentials__token_exchange(data) |
| 131 |
|
| 132 |
requests_mock.post( |
| 133 |
"https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token", |
| 134 |
text="Failure 123123", |
| 135 |
status_code=500, |
| 136 |
) |
| 137 |
|
| 138 |
with pytest.raises(TokenExchangeException, match="Failure 123123"): |
| 139 |
credentials._Credentials__token_exchange(data) |