Debian Codegraph @

test/units/galaxy/test_token.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright: (c) 2019, Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import annotations
6
7 import os
8 import pytest
9 from unittest.mock import MagicMock
10
11 import ansible.constants as C
12 from ansible.cli.galaxy import GalaxyCLI
13 from ansible.config import manager
14 from ansible.galaxy.token import GalaxyToken, NoTokenSentinel
15 from ansible.module_utils.common.text.converters import to_bytes, to_text
16
17
18 @pytest.fixture()
19 def b_token_file(request, tmp_path_factory):
20 b_test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Token'))
21 b_token_path = os.path.join(b_test_dir, b"token.yml")
22
23 token = getattr(request, 'param', None)
24 if token:
25 with open(b_token_path, 'wb') as token_fd:
26 token_fd.write(b"token: %s" % to_bytes(token))
27
28 orig_token_path = C.GALAXY_TOKEN_PATH
29 C.GALAXY_TOKEN_PATH = to_text(b_token_path)
30 try:
31 yield b_token_path
32 finally:
33 C.GALAXY_TOKEN_PATH = orig_token_path
34
35
36 def test_client_id(monkeypatch):
37 monkeypatch.setattr(C, 'GALAXY_SERVER_LIST', ['server1', 'server2'])
38
39 test_server_config = {option[0]: None for option in manager.GALAXY_SERVER_DEF}
40 test_server_config.update(
41 {
42 'url': 'http://my_galaxy_ng:8000/api/automation-hub/',
43 'auth_url': 'http://my_keycloak:8080/auth/realms/myco/protocol/openid-connect/token',
44 'client_id': 'galaxy-ng',
45 'token': 'access_token',
46 }
47 )
48
49 test_server_default = {option[0]: None for option in manager.GALAXY_SERVER_DEF}
50 test_server_default.update(
51 {
52 'url': 'https://cloud.redhat.com/api/automation-hub/',
53 'auth_url': 'https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token',
54 'token': 'access_token',
55 }
56 )
57
58 get_plugin_options = MagicMock(side_effect=[test_server_config, test_server_default])
59 monkeypatch.setattr(C.config, 'get_plugin_options', get_plugin_options)
60
61 cli_args = [
62 'ansible-galaxy',
63 'collection',
64 'install',
65 'namespace.collection:1.0.0',
66 ]
67
68 galaxy_cli = GalaxyCLI(args=cli_args)
69 mock_execute_install = MagicMock()
70 monkeypatch.setattr(galaxy_cli, '_execute_install_collection', mock_execute_install)
71 galaxy_cli.run()
72
73 assert galaxy_cli.api_servers[0].token.client_id == 'galaxy-ng'
74 assert galaxy_cli.api_servers[1].token.client_id == 'cloud-services'
75
76
77 def test_token_explicit(b_token_file):
78 assert GalaxyToken(token="explicit").get() == "explicit"
79
80
81 @pytest.mark.parametrize('b_token_file', ['file'], indirect=True)
82 def test_token_explicit_override_file(b_token_file):
83 assert GalaxyToken(token="explicit").get() == "explicit"
84
85
86 @pytest.mark.parametrize('b_token_file', ['file'], indirect=True)
87 def test_token_from_file(b_token_file):
88 assert GalaxyToken().get() == "file"
89
90
91 def test_token_from_file_missing(b_token_file):
92 assert GalaxyToken().get() is None
93
94
95 @pytest.mark.parametrize('b_token_file', ['file'], indirect=True)
96 def test_token_none(b_token_file):
97 assert GalaxyToken(token=NoTokenSentinel).get() is None