File: test_vpc_instance_authenticator.py

package info (click to toggle)
python-ibm-cloud-sdk-core 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 944 kB
  • sloc: python: 5,750; makefile: 40; xml: 7; sh: 7
file content (68 lines) | stat: -rw-r--r-- 2,739 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# pylint: disable=missing-docstring
import logging
import pytest

from ibm_cloud_sdk_core.authenticators import VPCInstanceAuthenticator, Authenticator
from .utils.logger_utils import setup_test_logger

setup_test_logger(logging.ERROR)


TEST_IAM_PROFILE_CRN = 'crn:iam-profile:123'
TEST_IAM_PROFILE_ID = 'iam-id-123'


def test_constructor():
    authenticator = VPCInstanceAuthenticator(iam_profile_id=TEST_IAM_PROFILE_ID, url='someurl.com')
    assert authenticator is not None
    assert authenticator.authentication_type() == Authenticator.AUTHTYPE_VPC
    assert authenticator.token_manager.iam_profile_crn is None
    assert authenticator.token_manager.iam_profile_id == TEST_IAM_PROFILE_ID
    assert authenticator.token_manager.url == 'someurl.com'


def test_setters():
    authenticator = VPCInstanceAuthenticator(iam_profile_id=TEST_IAM_PROFILE_ID, url='someurl.com')
    assert authenticator is not None
    assert authenticator.authentication_type() == Authenticator.AUTHTYPE_VPC
    assert authenticator.token_manager.iam_profile_crn is None
    assert authenticator.token_manager.iam_profile_id == TEST_IAM_PROFILE_ID
    assert authenticator.token_manager.url == 'someurl.com'

    # Set the IAM profile CRN to trigger a validation which will fail,
    # because at most one of iam_profile_crn or iam_profile_id may be specified.
    with pytest.raises(ValueError) as err:
        authenticator.set_iam_profile_crn(TEST_IAM_PROFILE_CRN)
    assert str(err.value) == 'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.'

    authenticator.set_iam_profile_id(None)
    assert authenticator.token_manager.iam_profile_id is None

    authenticator.set_iam_profile_crn(TEST_IAM_PROFILE_CRN)
    assert authenticator.token_manager.iam_profile_crn == TEST_IAM_PROFILE_CRN


def test_constructor_validate_failed():
    with pytest.raises(ValueError) as err:
        VPCInstanceAuthenticator(
            iam_profile_crn=TEST_IAM_PROFILE_CRN,
            iam_profile_id=TEST_IAM_PROFILE_ID,
        )
    assert str(err.value) == 'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.'


def test_authenticate():
    def mock_get_token():
        return 'mock_token'

    authenticator = VPCInstanceAuthenticator(iam_profile_crn=TEST_IAM_PROFILE_CRN)
    authenticator.token_manager.get_token = mock_get_token

    # Simulate an SDK API request that needs to be authenticated.
    request = {'headers': {}}

    # Trigger the "get token" processing to obtain the access token and add to the "SDK request".
    authenticator.authenticate(request)

    # Verify that the "authenticate()" method added the Authorization header
    assert request['headers']['Authorization'] == 'Bearer mock_token'