11import base64
22import re
33import uuid
4- from os import environ
54from os .path import exists
65
6+ import pytest
7+
78from anaconda_anon_usage import tokens , utils
89
10+ try :
11+ import anaconda_auth
12+ except ImportError :
13+ anaconda_auth = None
14+
915
1016def test_client_token (aau_token_path ):
1117 assert not exists (aau_token_path )
@@ -150,26 +156,26 @@ def test_token_string_with_two_dotted_org_tokens(two_dotted_org_tokens):
150156 assert token_string .count (" o/" ) == 2
151157
152158
153- def test_token_string_with_env_org_token (no_system_tokens ):
159+ def test_token_string_with_env_org_token (monkeypatch , no_system_tokens ):
154160 org_token_e = utils ._random_token ()
155161 mch_token_e = utils ._random_token ()
156162 ins_token_e = utils ._random_token ()
157- environ [ "ANACONDA_ANON_USAGE_ORG_TOKEN" ] = org_token_e
158- environ [ "ANACONDA_ANON_USAGE_MACHINE_TOKEN" ] = mch_token_e
159- environ [ "ANACONDA_ANON_USAGE_INSTALLER_TOKEN" ] = ins_token_e
163+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_ORG_TOKEN" , org_token_e )
164+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_MACHINE_TOKEN" , mch_token_e )
165+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_INSTALLER_TOKEN" , ins_token_e )
160166 token_string = tokens .token_string ()
161167 assert "o/" + org_token_e in token_string
162168 assert "m/" + mch_token_e in token_string
163169
164170
165- def test_token_string_with_system_and_env (system_tokens ):
171+ def test_token_string_with_system_and_env (monkeypatch , system_tokens ):
166172 org_token , mch_token , ins_token = system_tokens
167173 org_token_e = utils ._random_token ()
168174 mch_token_e = utils ._random_token ()
169175 ins_token_e = utils ._random_token ()
170- environ [ "ANACONDA_ANON_USAGE_ORG_TOKEN" ] = org_token_e
171- environ [ "ANACONDA_ANON_USAGE_MACHINE_TOKEN" ] = mch_token_e
172- environ [ "ANACONDA_ANON_USAGE_INSTALLER_TOKEN" ] = ins_token_e
176+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_ORG_TOKEN" , org_token_e )
177+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_MACHINE_TOKEN" , mch_token_e )
178+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_INSTALLER_TOKEN" , ins_token_e )
173179 token_string = tokens .token_string ()
174180 assert "i/" + ins_token in token_string
175181 assert "i/" + ins_token_e in token_string
@@ -183,13 +189,13 @@ def test_token_string_with_system_and_env(system_tokens):
183189 assert token_string .count (" i/" ) == 2
184190
185191
186- def test_token_string_with_invalid_tokens (no_system_tokens ):
192+ def test_token_string_with_invalid_tokens (monkeypatch , no_system_tokens ):
187193 org_token_e = "invalid token"
188194 mch_token_e = "superlongtokenthathasnobusinessbeinganactualtoken"
189195 ins_token_e = "fake installer"
190- environ [ "ANACONDA_ANON_USAGE_ORG_TOKEN" ] = org_token_e
191- environ [ "ANACONDA_ANON_USAGE_MACHINE_TOKEN" ] = mch_token_e
192- environ [ "ANACONDA_ANON_USAGE_MACHINE_TOKEN" ] = ins_token_e
196+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_ORG_TOKEN" , org_token_e )
197+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_MACHINE_TOKEN" , mch_token_e )
198+ monkeypatch . setenv ( "ANACONDA_ANON_USAGE_INSTALLER_TOKEN" , ins_token_e )
193199 token_string = tokens .token_string ()
194200 assert "o/" not in token_string
195201 assert "m/" not in token_string
@@ -249,19 +255,11 @@ def test_token_string_env_readonly(monkeypatch, no_system_tokens):
249255 assert "m/" not in token_string
250256
251257
252- def test_anaconda_string_keyring (anaconda_uid ):
253- token_string = tokens .token_string ()
254- assert "a/" in token_string
255- expected = uuid .UUID (anaconda_uid ).bytes
256- expected = base64 .urlsafe_b64encode (expected ).decode ("ascii" ).rstrip ("=" )
257- aval = re .sub ("^.*a/" , "" , token_string ).split (" " , 1 )[0 ]
258- assert aval == expected
259-
260-
261- def test_anaconda_string_env (anaconda_uid_env ):
258+ @pytest .mark .skipif (anaconda_auth is None , reason = "Requires the anaconda_auth module" )
259+ def test_keyring_in_module (api_key_sub ):
262260 token_string = tokens .token_string ()
263261 assert "a/" in token_string
264- expected = uuid .UUID (anaconda_uid_env ).bytes
262+ expected = uuid .UUID (api_key_sub ).bytes
265263 expected = base64 .urlsafe_b64encode (expected ).decode ("ascii" ).rstrip ("=" )
266264 aval = re .sub ("^.*a/" , "" , token_string ).split (" " , 1 )[0 ]
267265 assert aval == expected
0 commit comments