33Tests for the cortex CLI config file
44"""
55
6- # These tests are all marked to run in serial order because they make modifications to the
7- # cortex config file and/or CORTEX_API_KEY value and would potentially impact other tests
8- # that are running in parallel (with poetry run pytest -n auto), so they are run separately.
9-
10- # Additionally, order is VERY IMPORTANT in this file because of the way CORTEX_API key is
11- # deleted, set to invalid values, etc. Moving test order could impact the overall success
12- # of pytest. Tread carefully here.
13-
146import io
157import os
168import pytest
179import sys
1810from string import Template
1911
20- # Requires user input, so use monkeypatch to set it.
21- @pytest .fixture (scope = "session" )
22- def delete_cortex_api_key ():
23- if "CORTEX_API_KEY" in os .environ :
24- del os .environ ['CORTEX_API_KEY' ]
25-
26- @pytest .mark .serial
2712def test_config_file_api_key_quotes (tmp_path ):
2813 cortex_api_key = os .getenv ('CORTEX_API_KEY' )
2914 f = tmp_path / "cortex_config_api_key_quotes"
@@ -35,22 +20,53 @@ def test_config_file_api_key_quotes(tmp_path):
3520 f .write_text (content )
3621 cli (["-c" , str (f ), "entity-types" , "list" ])
3722
38- @pytest .mark .serial
3923def test_config_file_create (monkeypatch , tmp_path ):
4024 monkeypatch .setattr ('sys.stdin' , io .StringIO ('y' ))
4125 f = tmp_path / "test-config.txt"
4226 response = cli (["-c" , str (f ), "-k" , os .getenv ('CORTEX_API_KEY' ), "scorecards" , "list" ])
4327 assert any (scorecard ['tag' ] == 'cli-test-scorecard' for scorecard in response ['scorecards' ]), "Should find scorecard with tag cli-test-scorecard"
4428
45- @ pytest . mark . serial
46- def test_config_file_bad_api_key ( monkeypatch , tmp_path , delete_cortex_api_key ):
29+ def test_config_file_bad_api_key ( monkeypatch , tmp_path ):
30+ monkeypatch . delenv ( "CORTEX_API_KEY" )
4731 monkeypatch .setattr ('sys.stdin' , io .StringIO ('y' ))
4832 f = tmp_path / "test-config-bad-api-key.txt"
4933 response = cli (["-c" , str (f ), "-k" , "invalidApiKey" , "scorecards" , "list" ], return_type = ReturnType .RAW )
5034 assert "401 Client Error: Unauthorized" in str (response ), "should get Unauthorized error"
5135
52- @pytest .mark .serial
53- def test_environment_variable_invalid_key ():
54- os .environ ["CORTEX_API_KEY" ] = "invalidKey"
36+ def test_environment_variable_invalid_key (monkeypatch ):
37+ monkeypatch .setenv ("CORTEX_API_KEY" , "invalidKey" )
5538 response = cli (["scorecards" , "list" ], return_type = ReturnType .RAW )
5639 assert "401 Client Error: Unauthorized" in str (response ), "should get Unauthorized error"
40+
41+ def test_config_file_bad_url (monkeypatch , tmp_path ):
42+ monkeypatch .delenv ("CORTEX_BASE_URL" )
43+ cortex_api_key = os .getenv ('CORTEX_API_KEY' )
44+ f = tmp_path / "cortex_config_api_key_quotes"
45+ template = Template ("""
46+ [default]
47+ api_key = "${cortex_api_key}"
48+
49+ [mySection]
50+ api_key = "${cortex_api_key}"
51+ base_url = https://bogus.url
52+ """ )
53+ content = template .substitute (cortex_api_key = cortex_api_key )
54+ f .write_text (content )
55+ response = cli (["-c" , str (f ), "-l" , "DEBUG" , "-t" , "mySection" , "entity-types" , "list" ], return_type = ReturnType .RAW )
56+ assert "Max retries exceeded with url" in str (response ), "should get max retries error"
57+
58+ def test_config_file_base_url_env_var (monkeypatch , tmp_path ):
59+ cortex_api_key = os .getenv ('CORTEX_API_KEY' )
60+ f = tmp_path / "cortex_config_api_key_quotes"
61+ template = Template ("""
62+ [default]
63+ api_key = "${cortex_api_key}"
64+
65+ [mySection]
66+ api_key = "${cortex_api_key}"
67+ base_url = https://bogus.url
68+ """ )
69+ content = template .substitute (cortex_api_key = cortex_api_key )
70+ f .write_text (content )
71+ monkeypatch .setenv ("CORTEX_BASE_URL" , "https://api.getcortexapp.com" )
72+ cli (["-c" , str (f ), "-t" , "mySection" , "entity-types" , "list" ])
0 commit comments