File: test_basic_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 (40 lines) | stat: -rw-r--r-- 1,593 bytes parent folder | download
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
# pylint: disable=missing-docstring
import pytest

from ibm_cloud_sdk_core.authenticators import BasicAuthenticator, Authenticator


def test_basic_authenticator():
    authenticator = BasicAuthenticator('my_username', 'my_password')
    assert authenticator is not None
    assert authenticator.authentication_type() == Authenticator.AUTHTYPE_BASIC
    assert authenticator.username == 'my_username'
    assert authenticator.password == 'my_password'

    request = {'headers': {}}
    authenticator.authenticate(request)
    assert request['headers']['Authorization'] == 'Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ='


def test_basic_authenticator_validate_failed():
    with pytest.raises(ValueError) as err:
        BasicAuthenticator('my_username', None)
    assert str(err.value) == 'The username and password shouldn\'t be None.'

    with pytest.raises(ValueError) as err:
        BasicAuthenticator(None, 'my_password')
    assert str(err.value) == 'The username and password shouldn\'t be None.'

    with pytest.raises(ValueError) as err:
        BasicAuthenticator('{my_username}', 'my_password')
    assert (
        str(err.value) == 'The username and password shouldn\'t start or end with curly brackets or quotes. '
        'Please remove any surrounding {, }, or \" characters.'
    )

    with pytest.raises(ValueError) as err:
        BasicAuthenticator('my_username', '{my_password}')
    assert (
        str(err.value) == 'The username and password shouldn\'t start or end with curly brackets or quotes. '
        'Please remove any surrounding {, }, or \" characters.'
    )