Skip to content

Commit 071831e

Browse files
author
Juliya Smith
authored
Use temp profile context when executing integration tests (#206)
1 parent 1eb69c1 commit 071831e

File tree

7 files changed

+98
-64
lines changed

7 files changed

+98
-64
lines changed

tests/cmds/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def sdk(mocker):
3535
return mocker.MagicMock(spec=SDKClient)
3636

3737

38-
@pytest.fixture()
38+
@pytest.fixture
3939
def mock_42(mocker):
4040
return mocker.patch("py42.sdk.from_local_account")
4141

tests/integration/__init__.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +0,0 @@
1-
import os
2-
3-
import pexpect
4-
5-
6-
LINE_FEED = b"\r\n"
7-
PASSWORD_PROMPT = b"Password: "
8-
ENCODING_TYPE = "utf-8"
9-
10-
11-
def encode_response(line, encoding_type=ENCODING_TYPE):
12-
return line.decode(encoding_type)
13-
14-
15-
def run_command(command):
16-
17-
process = pexpect.spawn(command)
18-
response = []
19-
try:
20-
expected = process.expect([PASSWORD_PROMPT, pexpect.EOF])
21-
if expected == 0:
22-
process.sendline(os.environ["C42_PW"])
23-
process.expect(LINE_FEED)
24-
output = process.readlines()
25-
response = [encode_response(line) for line in output]
26-
else:
27-
output = process.before
28-
response = encode_response(output).splitlines()
29-
except pexpect.TIMEOUT:
30-
process.close()
31-
return process.exitstatus, response
32-
process.close()
33-
return process.exitstatus, response
34-
35-
36-
__all__ = [run_command]

tests/integration/conftest.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
from contextlib import contextmanager
3+
4+
import pexpect
5+
import pytest
6+
7+
from code42cli.errors import Code42CLIError
8+
from code42cli.profile import create_profile
9+
from code42cli.profile import delete_profile
10+
from code42cli.profile import get_profile
11+
from code42cli.profile import switch_default_profile
12+
13+
TEST_PROFILE_NAME = "TEMP-INTEGRATION-TEST"
14+
_LINE_FEED = b"\r\n"
15+
_PASSWORD_PROMPT = b"Password: "
16+
_ENCODING_TYPE = "utf-8"
17+
18+
19+
@contextmanager
20+
def use_temp_profile():
21+
"""Creates a temporary profile to use for executing integration tests."""
22+
host = os.environ.get("C42_HOST") or "http://127.0.0.1:4200"
23+
username = os.environ.get("C42_USER") or "test_username@example.com"
24+
password = os.environ.get("C42_PW") or "test_password"
25+
current_profile_name = _get_current_profile_name()
26+
create_profile(TEST_PROFILE_NAME, host, username, True)
27+
switch_default_profile(TEST_PROFILE_NAME)
28+
yield password
29+
delete_profile(TEST_PROFILE_NAME)
30+
31+
# Switch back to the original profile if there was one
32+
if current_profile_name:
33+
switch_default_profile(current_profile_name)
34+
35+
36+
def _get_current_profile_name():
37+
try:
38+
profile = get_profile()
39+
return profile.name
40+
except Code42CLIError:
41+
return None
42+
43+
44+
@pytest.fixture
45+
def command_runner():
46+
def run_command(command):
47+
with use_temp_profile() as pw:
48+
process = pexpect.spawn(command)
49+
response = []
50+
try:
51+
expected = process.expect([_PASSWORD_PROMPT, pexpect.EOF])
52+
if expected == 0:
53+
process.sendline(pw)
54+
process.expect(_LINE_FEED)
55+
output = process.readlines()
56+
response = [_encode_response(line) for line in output]
57+
else:
58+
output = process.before
59+
response = _encode_response(output).splitlines()
60+
except pexpect.TIMEOUT:
61+
process.close()
62+
return process.exitstatus, response
63+
process.close()
64+
return process.exitstatus, response
65+
66+
return run_command
67+
68+
69+
def _encode_response(line, encoding_type=_ENCODING_TYPE):
70+
return line.decode(encoding_type)

tests/integration/test_alert_rules.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from tests.integration import run_command
32

43
ALERT_RULES_COMMAND = "code42 alert-rules"
54

@@ -20,6 +19,6 @@
2019
"{} list --format RAW-JSON".format(ALERT_RULES_COMMAND),
2120
],
2221
)
23-
def test_alert_rules_command_returns_success_return_code(command):
24-
return_code, response = run_command(command)
22+
def test_alert_rules_command_returns_success_return_code(command, command_runner):
23+
return_code, response = command_runner(command)
2524
assert return_code == 0

tests/integration/test_alerts.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
from datetime import timedelta
33

44
import pytest
5-
from tests.integration import run_command
65

76

87
begin_date = datetime.utcnow() - timedelta(days=20)
98
end_date = datetime.utcnow() - timedelta(days=10)
109
begin_date_str = begin_date.strftime("%Y-%m-%d")
1110
end_date_str = end_date.strftime("%Y-%m-%d")
1211

13-
ALERT_COMMAND = "code42 alerts search -b {} -e {}".format(begin_date_str, end_date_str)
12+
ALERT_SEARCH_COMMAND = "code42 alerts search -b {} -e {}".format(
13+
begin_date_str, end_date_str
14+
)
1415
ADVANCED_QUERY = """{"groupClause":"AND", "groups":[{"filterClause":"AND",
1516
"filters":[{"operator":"ON_OR_AFTER", "term":"eventTimestamp", "value":"2020-09-13T00:00:00.000Z"},
1617
{"operator":"ON_OR_BEFORE", "term":"eventTimestamp", "value":"2020-12-07T13:20:15.195Z"}]}],
@@ -25,25 +26,27 @@
2526
@pytest.mark.parametrize(
2627
"command",
2728
[
28-
ALERT_COMMAND,
29-
"{} --state OPEN".format(ALERT_COMMAND),
30-
"{} --state RESOLVED".format(ALERT_COMMAND),
31-
"{} --actor user@code42.com".format(ALERT_COMMAND),
32-
"{} --rule-name 'File Upload Alert'".format(ALERT_COMMAND),
33-
"{} --rule-id 962a6a1c-54f6-4477-90bd-a08cc74cbf71".format(ALERT_COMMAND),
34-
"{} --rule-type FedEndpointExfiltration".format(ALERT_COMMAND),
35-
"{} --description 'Alert on any file upload'".format(ALERT_COMMAND),
36-
"{} --exclude-rule-type 'FedEndpointExfiltration'".format(ALERT_COMMAND),
29+
ALERT_SEARCH_COMMAND,
30+
"{} --state OPEN".format(ALERT_SEARCH_COMMAND),
31+
"{} --state RESOLVED".format(ALERT_SEARCH_COMMAND),
32+
"{} --actor user@code42.com".format(ALERT_SEARCH_COMMAND),
33+
"{} --rule-name 'File Upload Alert'".format(ALERT_SEARCH_COMMAND),
34+
"{} --rule-id 962a6a1c-54f6-4477-90bd-a08cc74cbf71".format(
35+
ALERT_SEARCH_COMMAND
36+
),
37+
"{} --rule-type FedEndpointExfiltration".format(ALERT_SEARCH_COMMAND),
38+
"{} --description 'Alert on any file upload'".format(ALERT_SEARCH_COMMAND),
39+
"{} --exclude-rule-type 'FedEndpointExfiltration'".format(ALERT_SEARCH_COMMAND),
3740
"{} --exclude-rule-id '962a6a1c-54f6-4477-90bd-a08cc74cbf71'".format(
38-
ALERT_COMMAND
41+
ALERT_SEARCH_COMMAND
3942
),
40-
"{} --exclude-rule-name 'File Upload Alert'".format(ALERT_COMMAND),
41-
"{} --exclude-actor-contains 'user@code42.com'".format(ALERT_COMMAND),
42-
"{} --exclude-actor 'user@code42.com'".format(ALERT_COMMAND),
43-
"{} --actor-contains 'user@code42.com'".format(ALERT_COMMAND),
43+
"{} --exclude-rule-name 'File Upload Alert'".format(ALERT_SEARCH_COMMAND),
44+
"{} --exclude-actor-contains 'user@code42.com'".format(ALERT_SEARCH_COMMAND),
45+
"{} --exclude-actor 'user@code42.com'".format(ALERT_SEARCH_COMMAND),
46+
"{} --actor-contains 'user@code42.com'".format(ALERT_SEARCH_COMMAND),
4447
ALERT_ADVANCED_QUERY_COMMAND,
4548
],
4649
)
47-
def test_alert_command_returns_success_return_code(command):
48-
return_code, response = run_command(command)
50+
def test_alert_command_returns_success_return_code(command, command_runner):
51+
return_code, response = command_runner(command)
4952
assert return_code == 0

tests/integration/test_auditlogs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import timedelta
33

44
import pytest
5-
from tests.integration import run_command
65

76
SEARCH_COMMAND = "code42 audit-logs search"
87
BASE_COMMAND = "{} -b".format(SEARCH_COMMAND)
@@ -42,6 +41,6 @@
4241
("{} '{}' --debug".format(BASE_COMMAND, begin_date_str)),
4342
],
4443
)
45-
def test_auditlogs_search_command_returns_success_return_code(command):
46-
return_code, response = run_command(command)
44+
def test_auditlogs_search_command_returns_success_return_code(command, command_runner):
45+
return_code, response = command_runner(command)
4746
assert return_code == 0

tests/integration/test_legal_hold.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from tests.integration import run_command
32

43
LEGAL_HOLD_COMMAND = "code42 legal-hold"
54

@@ -20,6 +19,6 @@
2019
"{} list --format RAW-JSON".format(LEGAL_HOLD_COMMAND),
2120
],
2221
)
23-
def test_alert_rules_command_returns_success_return_code(command):
24-
return_code, response = run_command(command)
22+
def test_alert_rules_command_returns_success_return_code(command, command_runner):
23+
return_code, response = command_runner(command)
2524
assert return_code == 0

0 commit comments

Comments
 (0)