File: test_bearer_authenticator.py

package info (click to toggle)
python-ibm-cloud-sdk-core 3.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 4,428; makefile: 26; sh: 25
file content (28 lines) | stat: -rw-r--r-- 1,069 bytes parent folder | download | duplicates (2)
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
# pylint: disable=missing-docstring
import pytest

from ibm_cloud_sdk_core.authenticators import BearerTokenAuthenticator, Authenticator


def test_bearer_authenticator():
    authenticator = BearerTokenAuthenticator('my_bearer_token')
    assert authenticator is not None
    assert authenticator.authentication_type() == Authenticator.AUTHTYPE_BEARERTOKEN
    assert authenticator.bearer_token == 'my_bearer_token'

    authenticator.set_bearer_token('james bond')
    assert authenticator.bearer_token == 'james bond'

    request = {'headers': {}}
    authenticator.authenticate(request)
    assert request['headers']['Authorization'] == 'Bearer james bond'


def test_bearer_validate_failed():
    with pytest.raises(ValueError) as err:
        BearerTokenAuthenticator(None)
    assert str(err.value) == 'The bearer token shouldn\'t be None.'
    authenticator = BearerTokenAuthenticator('my_bearer_token')
    with pytest.raises(ValueError) as err:
        authenticator.set_bearer_token(None)
    assert str(err.value) == 'The bearer token shouldn\'t be None.'