def test_event_is_updated_with_context_and_context_cleared_if_set(self): with mock_response(): cased.publish_key = "cs_test_001" cased.clear_context_after_publishing = True cased.Context.update({"country": "Austria"}) Event.publish({"user": "******"}) cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", { "user": "******", "country": "Austria", "cased_id": ANY, "timestamp": ANY, }, ) Event.publish({"user": "******"}) cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", {"user": "******", "cased_id": ANY, "timestamp": ANY}, )
def test_policy_can_create(self): with mock_response(): cased.api_key = "cs_test_001" assert (Policy.create( name="my-policy", description="policy description").status_code == 200)
def test_requestor_has_policy_api_key_on_404(self): with mock_response(status_code=404): cased.policy_key = "cs_test_001" requestor = Requestor.policy_requestor() assert requestor.api_key == "cs_test_001" assert requestor.request("get", "/policies").status_code == 404
def test_requestor_fails_without_publish_api_key_on_post(self): with mock_response(): cased.publish_key = None with pytest.raises(Exception): requestor = Requestor.publish_requestor() requestor.request("post", "/publish").status_code
def test_event_local_publish_data_handles_nested_dict_updates_gracefully(self): with mock_response(): cased.publish_key = "cs_test_001" cased.Context.update( { "country": "Austria", "mappings": {"user-one": "123", "user-two": "456"}, } ) Event.publish({"mappings": {"user-three": "789"}}) cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", { "mappings": { "user-one": "123", "user-three": "789", "user-two": "456", }, "country": "Austria", "cased_id": ANY, "timestamp": ANY, }, )
def test_requestor_has_publish_api_key(self): with mock_response(): cased.publish_key = "cs_test_001" requestor = Requestor.publish_requestor() assert requestor.api_key == "cs_test_001" assert requestor.request("post", "/publish").status_code == 200
def test_event_can_still_publish_with_reliability_engine_misconfigured_per_request( self, ): with mock_response(): cased.publish_key = "cs_test_001" cased.reliability_backend = None assert Event.publish({"user": "******"}, backend="nothere").status_code == 200
def test_export_create_request_has_correct_paramaters(self): with mock_response(): cased.api_key = "cs_test_001" Export.create() cased.http.HTTPClient.make_request.assert_called_with( "post", "https://api.cased.com/exports/", "cs_test_001", {})
def test_event_publish_raised_if_publisher_does_not_implement_publish(self): with mock_response(): cased.publish_key = "cs_test_001" empty = EmptyClass() cased.add_publisher(empty) with pytest.raises(PublisherException): Event.publish({"user": "******"})
def test_event_list_request_is_called_with_correct_url(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list() cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", {"per_page": 25} )
def test_policy_delete_request_has_correct_paramaters(self): with mock_response(status_code=204): cased.api_key = "cs_test_001" assert Policy.delete("12345").status_code == 204 cased.http.HTTPClient.make_request.assert_called_with( "delete", "https://api.cased.com/policies/12345", "cs_test_001", None)
def test_export_request_is_called_with_an_event_id(self): with mock_response(): cased.api_key = "cs_test_001" assert Export.fetch("1234") cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/exports/1234", "cs_test_001", None)
def test_event_list_request_can_set_page_limit(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list(limit=10) cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", {"per_page": 10} )
def test_fetch_next(self): from cased.tests.util import mock_response data = {"total_pages": 40, "total_count": 500, "results": results_2} with mock_response(headers=headers_2, json_data=data): cased.policy_key = "cs_test_001" assert list_obj.fetch_next().prev_page == 4 assert list_obj.fetch_next().next_page == 6 assert list_obj.fetch_next().results == results_2
def test_event_publish_does_not_publish_to_additional_unless_given(self): with mock_response(): cased.publish_key = "cs_test_001" pub = MockPublisher() with patch.object(pub, "publish", wraps=pub.publish) as wrapped_publish: Event.publish({"user": "******"}) # Confirm publish is not called on the added publisher assert not wrapped_publish.called
def test_event_can_publish_with_reliability_engine_configured_per_request(self): with mock_response(): cased.publish_key = "cs_test_001" cased.reliability_backend = None assert ( Event.publish( {"user": "******"}, backend=SimpleReliabilityBackend ).status_code == 200 )
def test_event_list_by_action_request_has_correct_paramaters(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list_by_action("test-event") cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", {"per_page": 25, "phrase": "(action:test-event)"}, )
def test_export_download_is_called_correctly(self): with mock_response(): cased.api_key = "cs_test_001" assert Export.download("1234") cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/exports/1234/download", "cs_test_001", None, )
def test_events_can_be_listed_and_filtered_with_variables(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list(variables={"team": "team_1"}) cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", {"per_page": 25, "variables[team]": "team_1"}, )
def test_event_list_by_actor_request_api_key_can_be_overrode(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list_by_actor("test", api_key="alt_key") cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "alt_key", {"per_page": 25, "phrase": "(actor:test)"}, )
def test_event_is_extended_with_cased_id_and_timestamp(self): with mock_response(): cased.publish_key = "cs_test_001" Event.publish({"user": "******"}) # Confirm that "cased_id" and "timestamp" have been added to the request cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", {"user": "******", "cased_id": ANY, "timestamp": ANY}, )
def test_event_publish_publishes_to_additional(self): with mock_response(): cased.publish_key = "cs_test_001" pub = MockPublisher() cased.add_publisher(pub) with patch.object(pub, "publish", wraps=pub.publish) as wrapped_publish: Event.publish({"user": "******"}) # Confirm publish is called on the added publisher wrapped_publish.assert_called_with( {"user": "******", "cased_id": ANY, "timestamp": ANY} )
def test_policy_create_request_has_correct_paramaters(self): with mock_response(): cased.api_key = "cs_test_001" Policy.create(name="my-policy", description="policy description") cased.http.HTTPClient.make_request.assert_called_with( "post", "https://api.cased.com/policies/", "cs_test_001", { "name": "my-policy", "description": "policy description" }, )
def test_event_local_publish_data_handles_deeply_nested_updates(self): with mock_response(): cased.publish_key = "cs_test_001" cased.Context.update( { "mappings": {"user-one": "123", "user-two": "456"}, "location": { "country": "test-country-1", "divisions": [ "test-division-1", "test-division-2", "test-division-3", ], }, } ) Event.publish( { "mappings": {"user-three": "789"}, "location": {"city": "test-city-1"}, } ) cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", { "mappings": { "user-one": "123", "user-three": "789", "user-two": "456", }, "location": { "city": "test-city-1", "country": "test-country-1", "divisions": [ "test-division-1", "test-division-2", "test-division-3", ], }, "cased_id": ANY, "timestamp": ANY, }, )
def test_events_can_be_filtered_with_multiple_variables(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list(limit=10, variables={"team": "team_1", "organization": "org_1"}) cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", { "per_page": 10, "variables[team]": "team_1", "variables[organization]": "org_1", }, )
def test_event_list_request_has_correct_paramaters_with_a_long_phrase(self): with mock_response(): cased.policy_key = "cs_test_001" Event.list( search=Query.make_phrase_from_dict( {"actor": "test", "event": "user.login"} ) ) cased.http.HTTPClient.make_request.assert_called_with( "get", "https://api.cased.com/events/", "cs_test_001", {"per_page": 25, "phrase": "(actor:test AND event:user.login)"}, )
def test_page_iter_works_with_next(self): from cased.tests.util import mock_response data = { "total_pages": 2, "total_count": 8, "results": results_2, } # use the actual test values here since we're testing the iterator with mock_response(headers=headers_3, json_data=data): cased.policy_key = "cs_test_001" iterator = list_obj.page_iter() next_result = next(iterator) assert next_result == results_2
def test_page_iter_can_iterate_through_results_of_single_page(self): from cased.tests.util import mock_response data = { "total_pages": 1, "total_count": 4, "results": results_1, } # use the actual test values here since we're testing the iterator with mock_response(headers=headers_4, json_data=data): cased.policy_key = "cs_test_001" iterator = list_obj.page_iter() next_result = iterator.__next__() assert next_result == results_1
def test_event_local_publish_data_takes_precedence_over_context(self): with mock_response(): cased.publish_key = "cs_test_001" cased.Context.update({"country": "Austria", "user": "******"}) Event.publish({"user": "******"}) cased.http.HTTPClient.make_request.assert_called_with( "post", ANY, "cs_test_001", { "user": "******", "country": "Austria", "cased_id": ANY, "timestamp": ANY, }, )
def test_fetch_fails_on_abstract_class(self): with mock_response(): cased.policy_key = "cs_test_001" with pytest.raises(NotImplementedError): CasedAPIResource.fetch("1234").status_code