|
| 1 | +import datetime |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from py42.exceptions import Py42BadRequestError |
3 | 5 | from py42.response import Py42Response |
|
6 | 8 |
|
7 | 9 | from code42cli import PRODUCT_NAME |
8 | 10 | from code42cli.cmds.legal_hold import _check_matter_is_accessible |
| 11 | +from code42cli.date_helper import convert_datetime_to_timestamp |
9 | 12 | from code42cli.main import cli |
10 | 13 |
|
11 | | - |
12 | 14 | _NAMESPACE = "{}.cmds.legal_hold".format(PRODUCT_NAME) |
13 | 15 | TEST_MATTER_ID = "99999" |
14 | 16 | TEST_LEGAL_HOLD_MEMBERSHIP_UID = "88888" |
|
18 | 20 | INACTIVE_TEST_USERNAME = "inactive@example.com" |
19 | 21 | INACTIVE_TEST_USER_ID = "54321" |
20 | 22 | TEST_POLICY_UID = "66666" |
| 23 | +_CREATE_EVENT_ID = "564564654566" |
| 24 | +_MEMBERSHIP_EVENT_ID = "74533457745" |
21 | 25 | TEST_PRESERVATION_POLICY_UID = "1010101010" |
22 | 26 | MATTER_RESPONSE = """ |
23 | 27 | { |
|
169 | 173 | ] |
170 | 174 | } |
171 | 175 | """ |
| 176 | +TEST_EVENT_PAGE = { |
| 177 | + "legalHoldEvents": [ |
| 178 | + { |
| 179 | + "eventUid": "564564654566", |
| 180 | + "eventType": "HoldCreated", |
| 181 | + "eventDate": "2015-05-16T15:07:44.820Z", |
| 182 | + "legalHoldUid": "88888", |
| 183 | + "actorUserUid": "12345", |
| 184 | + "actorUsername": "holdcreator@example.com", |
| 185 | + "actorFirstName": "john", |
| 186 | + "actorLastName": "doe", |
| 187 | + "actorUserExtRef": None, |
| 188 | + "actorEmail": "holdcreatorr@example.com", |
| 189 | + }, |
| 190 | + { |
| 191 | + "eventUid": "74533457745", |
| 192 | + "eventType": "MembershipCreated", |
| 193 | + "eventDate": "2019-05-17T15:07:44.820Z", |
| 194 | + "legalHoldUid": "88888", |
| 195 | + "legalHoldMembershipUid": "645576514441664433", |
| 196 | + "custodianUserUid": "12345", |
| 197 | + "custodianUsername": "kim.jones@code42.com", |
| 198 | + "custodianFirstName": "kim", |
| 199 | + "custodianLastName": "jones", |
| 200 | + "custodianUserExtRef": None, |
| 201 | + "custodianEmail": "user@example.com", |
| 202 | + "actorUserUid": "1234512345", |
| 203 | + "actorUsername": "creator@example.com", |
| 204 | + "actorFirstName": "john", |
| 205 | + "actorLastName": "doe", |
| 206 | + "actorUserExtRef": None, |
| 207 | + "actorEmail": "user@example.com", |
| 208 | + }, |
| 209 | + ] |
| 210 | +} |
| 211 | +EMPTY_EVENTS_RESPONSE = """{"legalHoldEvents": []}""" |
172 | 212 | EMPTY_MATTERS_RESPONSE = """{"legalHolds": []}""" |
173 | 213 | ALL_MATTERS_RESPONSE = """{{"legalHolds": [{}]}}""".format(MATTER_RESPONSE) |
174 | 214 | LEGAL_HOLD_COMMAND = "legal-hold" |
@@ -212,6 +252,15 @@ def active_and_inactive_legal_hold_memberships_response(mocker): |
212 | 252 | return [_create_py42_response(mocker, ALL_ACTIVE_AND_INACTIVE_CUSTODIANS_RESPONSE)] |
213 | 253 |
|
214 | 254 |
|
| 255 | +@pytest.fixture |
| 256 | +def empty_events_response(mocker): |
| 257 | + return _create_py42_response(mocker, EMPTY_EVENTS_RESPONSE) |
| 258 | + |
| 259 | + |
| 260 | +def events_list_generator(): |
| 261 | + yield TEST_EVENT_PAGE |
| 262 | + |
| 263 | + |
215 | 264 | @pytest.fixture |
216 | 265 | def get_user_id_success(cli_state): |
217 | 266 | cli_state.sdk.users.get_by_username.return_value = { |
@@ -246,6 +295,11 @@ def check_matter_accessible_failure(cli_state, custom_error): |
246 | 295 | ) |
247 | 296 |
|
248 | 297 |
|
| 298 | +@pytest.fixture |
| 299 | +def get_all_events_success(cli_state): |
| 300 | + cli_state.sdk.legalhold.get_all_events.return_value = events_list_generator() |
| 301 | + |
| 302 | + |
249 | 303 | @pytest.fixture |
250 | 304 | def user_already_added_response(mocker): |
251 | 305 | mock_response = mocker.MagicMock(spec=Response) |
@@ -575,6 +629,44 @@ def test_list_with_csv_format_returns_no_response_when_response_is_empty( |
575 | 629 | assert "Matter ID,Name,Description,Creator,Creation Date" not in result.output |
576 | 630 |
|
577 | 631 |
|
| 632 | +def test_search_events_shows_events_that_respect_type_filters( |
| 633 | + runner, cli_state, get_all_events_success |
| 634 | +): |
| 635 | + |
| 636 | + result = runner.invoke( |
| 637 | + cli, |
| 638 | + ["legal-hold", "search-events", "--event-type", "HoldCreated"], |
| 639 | + obj=cli_state, |
| 640 | + ) |
| 641 | + |
| 642 | + assert _CREATE_EVENT_ID in result.output |
| 643 | + assert _MEMBERSHIP_EVENT_ID not in result.output |
| 644 | + |
| 645 | + |
| 646 | +def test_search_events_with_csv_returns_no_events_when_response_is_empty( |
| 647 | + runner, cli_state, get_all_events_success, empty_events_response |
| 648 | +): |
| 649 | + cli_state.sdk.legalhold.get_all_events.return_value = empty_events_response |
| 650 | + result = runner.invoke(cli, ["legal-hold", "events", "-f", "csv"], obj=cli_state) |
| 651 | + |
| 652 | + assert ( |
| 653 | + "actorEmail,actorUsername,actorLastName,actorUserUid,actorUserExtRef" |
| 654 | + not in result.output |
| 655 | + ) |
| 656 | + |
| 657 | + |
| 658 | +def test_search_events_is_called_with_expected_begin_timestamp(runner, cli_state): |
| 659 | + expected_timestamp = convert_datetime_to_timestamp( |
| 660 | + datetime.datetime.strptime("2017-01-01", "%Y-%m-%d") |
| 661 | + ) |
| 662 | + command = ["legal-hold", "search-events", "--begin", "2017-01-01T00:00:00"] |
| 663 | + runner.invoke(cli, command, obj=cli_state) |
| 664 | + |
| 665 | + cli_state.sdk.legalhold.get_all_events.assert_called_once_with( |
| 666 | + None, expected_timestamp, None |
| 667 | + ) |
| 668 | + |
| 669 | + |
578 | 670 | @pytest.mark.parametrize( |
579 | 671 | "command, error_msg", |
580 | 672 | [ |
|
0 commit comments