Ejemplo n.º 1
0
def test_get_access_token(requests_mock, mock_datetime_now):

    requests_mock.post(
        ACCESS_TOKEN_URL,
        json={"access_token": "access-token", "expires_in": 30},
    )

    access_token = _get_access_token(PROFILE)

    assert requests_mock.last_request.json() == {
        "client_id": PROFILE.client_id,
        "client_secret": PROFILE.client_secret,
        "grant_type": "client_credentials",
    }
    assert access_token == AccessToken(
        token="access-token", expires_at=NOW + timedelta(seconds=30)
    )
Ejemplo n.º 2
0
def _get_access_token(profile):
    url = _service_url(profile, "hudson", "access_token")
    payload = {
        "client_id": profile.client_id,
        "client_secret": profile.client_secret,
        "grant_type": "client_credentials",
    }

    response = requests.post(url, json=payload)
    response.raise_for_status()

    body = response.json()

    token = body["access_token"]
    now = datetime.now(tz=pytz.utc)
    expires_at = now + timedelta(seconds=body["expires_in"])

    return AccessToken(token, expires_at)
Ejemplo n.º 3
0
from faculty.session.accesstoken import (
    AccessToken,
    AccessTokenFileSystemCache,
    AccessTokenMemoryCache,
    _AccessTokenStore,
    _default_token_cache_path,
)

PROFILE = faculty.config.Profile(
    domain="test.domain.com",
    protocol="https",
    client_id="test-client-id",
    client_secret="test-client-secret",
)
NOW = datetime.now(tz=pytz.utc)
VALID_ACCESS_TOKEN = AccessToken(token="access-token",
                                 expires_at=NOW + timedelta(minutes=10))
EXPIRED_ACCESS_TOKEN = AccessToken(token="access-token",
                                   expires_at=NOW - timedelta(seconds=1))


@pytest.fixture
def mock_datetime_now(mocker):
    datetime_mock = mocker.patch("faculty.session.accesstoken.datetime")
    datetime_mock.now.return_value = NOW
    return datetime_mock


def test_access_token_store():
    store = _AccessTokenStore()
    store[PROFILE] = VALID_ACCESS_TOKEN
    assert store[PROFILE] == VALID_ACCESS_TOKEN