def test_authenticated_get(self): summon_type = baker.make(SummonType) url = reverse("teams-summon-types", kwargs={"pk": summon_type.team.pk}) client = get_authenticated_client() response = client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_authenticated_post_create_author(self): """ The author of the case should automatically be set to the authenticated user who made the POST request """ self.assertEquals(Case.objects.count(), 0) team = baker.make(CaseTeam) reason = baker.make(CaseReason, team=team) url = reverse("cases-list") client = get_authenticated_client() response = client.post( url, { "description": "Foo", "team": team.pk, "reason": reason.pk, "address": { "bag_id": "foo bag ID" }, }, format="json", ) test_user = get_test_user() case = Case.objects.get(id=response.data["id"]) self.assertEquals(case.author, test_user)
def test_authenticated_post_update(self): schedule = baker.make(Schedule) new_priority = baker.make(Priority) new_week_segment = baker.make(WeekSegment) new_day_segment = baker.make(DaySegment) new_priority = baker.make(Priority) client = get_authenticated_client() url = reverse("schedules-list") client.post( url, { # existing schedule fields "action": schedule.action.id, "case": schedule.case.id, # update with new fields: "week_segment": new_week_segment.id, "day_segment": new_day_segment.id, "priority": new_priority.id, }, format="json", ) schedule = Schedule.objects.get(id=schedule.id) self.assertEquals(schedule.week_segment.id, new_week_segment.id) self.assertEquals(schedule.day_segment.id, new_day_segment.id) self.assertEquals(schedule.priority.id, new_priority.id)
def test_itinerary_update_team(self): """ Upate a team to an itinerary through the API """ itinerary = Itinerary.objects.create() user_a = User.objects.create(email="*****@*****.**") user_b = User.objects.create(email="*****@*****.**") itinerary.add_team_members([user_b.id]) url = reverse("v1:itinerary-team", kwargs={"pk": itinerary.id}) client = get_authenticated_client() data = { "team_members": [ {"user": {"id": user_a.id}}, {"user": {"id": user_b.id}}, ] } response = client.put(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) team_members = [ team_member.user for team_member in itinerary.team_members.all() ] self.assertEquals([user_a, user_b], team_members)
def test_authenticated_post_create_wrong_team_reason_relation(self): """ Request should fail if the CaseReason is not one of the given teams CaseReasons """ self.assertEquals(Case.objects.count(), 0) team_a = baker.make(CaseTeam) team_b = baker.make(CaseTeam) reason = baker.make(CaseReason, team=team_a) url = reverse("cases-list") client = get_authenticated_client() response = client.post( url, { "description": "Foo", "team": team_b.pk, "reason": reason.pk, "address": { "bag_id": "foo bag ID" }, }, format="json", ) self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEquals(Case.objects.count(), 0)
def test_search_without_suffix(self, mock_q): """ An authenticated search works without optional suffix """ url = reverse("v1:search-list") client = get_authenticated_client() # Mock search function FOO_SEARCH_RESULTS = [] mock_q.get_search_results = Mock(return_value=FOO_SEARCH_RESULTS) MOCK_SEARCH_QUERY_PARAMETERS = self.MOCK_SEARCH_QUERY_PARAMETERS.copy() MOCK_SEARCH_QUERY_PARAMETERS.pop("suffix") response = client.get(url, MOCK_SEARCH_QUERY_PARAMETERS) # Tests if the search function was called with all the given parameters MOCK_SEARCH_QUERY_PARAMETERS_ASSERT = self.MOCK_SEARCH_QUERY_PARAMETERS.copy( ) MOCK_SEARCH_QUERY_PARAMETERS_ASSERT["suffix"] = "" mock_q.get_search_results.assert_called_with( *MOCK_SEARCH_QUERY_PARAMETERS_ASSERT.values()) # Tests if a success response code is given self.assertEqual(response.status_code, status.HTTP_200_OK) # Tests if the response contains the mock data self.assertEqual(response.json(), {"cases": FOO_SEARCH_RESULTS})
def test_authenticated_post_invalid_case(self): """ A post should fail if the given Case doesn't exist """ self.assertEquals(Summon.objects.count(), 0) case = baker.make(Case) summon_type = baker.make(SummonType, team=case.team) data = { "description": "foo_description", "case": 99, "type": summon_type.id, "persons": [{ "first_name": "foo_first_name", "preposition": "foo_preposition", "last_name": "foo_last_name", }], } url = reverse("summons-list") client = get_authenticated_client() response = client.post(url, data, format="json") self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEquals(Summon.objects.count(), 0)
def test_authenticated_post_create(self): self.assertEqual(Schedule.objects.count(), 0) action = baker.make(Action) week_segment = baker.make(WeekSegment) day_segment = baker.make(DaySegment) priority = baker.make(Priority) case = baker.make(Case) client = get_authenticated_client() url = reverse("schedules-list") response = client.post( url, { "action": action.id, "week_segment": week_segment.id, "day_segment": day_segment.id, "priority": priority.id, "case": case.id, }, format="json", ) self.assertEqual(Schedule.objects.count(), 1) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_fail(self, mock_get_cases_from_settings): """ Should fail and create no Itinerary if no cases are available """ self.assertEqual(Itinerary.objects.count(), 0) mock_get_cases_from_settings.return_value = [] url = reverse("v1:itinerary-list") client = get_authenticated_client() user = get_test_user() response = client.post( url, { "team_members": [{ "user": { "id": user.id } }], "settings": { "opening_date": "2020-04-24", "target_length": 8, }, }, format="json", ) self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertEqual(Itinerary.objects.count(), 0)
def test_authenticated_get(self): team = baker.make(CaseTeam) url = reverse("teams-reasons", kwargs={"pk": team.pk}) client = get_authenticated_client() response = client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_results_team_filter(self): """ Should only returns cases for the given team """ url = reverse("cases-search") client = get_authenticated_client() MOCK_STREET_NAME = "FOO STREET NAME" MOCK_STREET_NUMBER = 5 MOCK_TEAM = "The A-Team" address = baker.make(Address, street_name=MOCK_STREET_NAME, number=MOCK_STREET_NUMBER) team_a = baker.make(CaseTeam, name=MOCK_TEAM) team_b = baker.make(CaseTeam) baker.make(Case, team=team_a, address=address) baker.make(Case, team=team_b, address=address) SEARCH_QUERY_PARAMETERS = { "streetName": MOCK_STREET_NAME, "streetNumber": MOCK_STREET_NUMBER, "team": team_a.id, } response = client.get(url, SEARCH_QUERY_PARAMETERS) data = response.json() self.assertEquals(len(data["results"]), 1)
def test_results_open_cases(self): """ Should only returns cases which haven't ended yet """ url = reverse("cases-search") client = get_authenticated_client() MOCK_STREET_NAME = "FOO STREET NAME" MOCK_STREET_NUMBER = 5 address = baker.make(Address, street_name=MOCK_STREET_NAME, number=MOCK_STREET_NUMBER) baker.make(Case, address=address) # Closed cases baker.make(Case, address=address, end_date=datetime.datetime.now(), _quantity=10) SEARCH_QUERY_PARAMETERS = { "streetName": MOCK_STREET_NAME, "streetNumber": MOCK_STREET_NUMBER, } response = client.get(url, SEARCH_QUERY_PARAMETERS) data = response.json() self.assertEquals(len(data["results"]), 1)
def test_multiple_address_result(self): url = reverse("cases-search") client = get_authenticated_client() MOCK_STREET_NAME = "FOO STREET NAME" MOCK_STREET_NUMBER = 5 address_a = baker.make( Address, street_name=MOCK_STREET_NAME, number=MOCK_STREET_NUMBER, suffix="A", ) address_b = baker.make( Address, street_name=MOCK_STREET_NAME, number=MOCK_STREET_NUMBER, suffix="B", ) baker.make(Case, address=address_a) baker.make(Case, address=address_b) SEARCH_QUERY_PARAMETERS = { "streetName": MOCK_STREET_NAME, "streetNumber": MOCK_STREET_NUMBER, } response = client.get(url, SEARCH_QUERY_PARAMETERS) data = response.json() self.assertEquals(len(data["results"]), 2)
def test_search_with_fraud_prediction(self, mock_q): """ The cases in a search result should contain a fraud_prediction if it's available """ url = reverse("v1:search-list") client = get_authenticated_client() CASE_ID = "FOO-ID" # Mock search function FOO_SEARCH_RESULTS = [{"id": CASE_ID}] mock_q.get_search_results = Mock(return_value=FOO_SEARCH_RESULTS) # Create a fraud prediction object with the same CASE_ID fraud_prediction = FraudPrediction.objects.create( case_id=CASE_ID, fraud_probability=0.6, fraud_prediction=True, business_rules={}, shap_values={}, ) response = client.get(url, self.MOCK_SEARCH_QUERY_PARAMETERS) expected_fraud_prediction = FraudPredictionSerializer( fraud_prediction).data fraud_prediction_response = ( response.json().get("cases")[0].get("fraud_prediction")) self.assertEqual(expected_fraud_prediction, fraud_prediction_response)
def test_authenticated_get_empty(self): url = reverse("visits-list") client = get_authenticated_client() response = client.get(url) data = response.json() self.assertEquals(data["results"], [])
def test_without_stadium(self): """ An authenticated request should fail if no stadium is given """ url = reverse("v1:case-unplanned") client = get_authenticated_client() response = client.get(url, {"date": "2020-04-05"}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_fail_street_name_no_number(self): url = reverse("cases-search") client = get_authenticated_client() SEARCH_QUERY_PARAMETERS = { "streetName": "FOO_STREET_NUMBER", } response = client.get(url, SEARCH_QUERY_PARAMETERS) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_authenticated_get_empty(self): team = baker.make(CaseTeam) url = reverse("teams-reasons", kwargs={"pk": team.pk}) client = get_authenticated_client() response = client.get(url) data = response.json() self.assertEqual(data["results"], [])
def test_with_wrong_date_format(self): """ An authenticated request should fail if unknown stadium is given """ url = reverse("v1:case-unplanned") client = get_authenticated_client() response = client.get(url, {"date": "FOO", "stadium": ISSUEMELDING}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_results(self): QUANTITY = 10 baker.make(Case, _quantity=QUANTITY) url = reverse("cases-list") client = get_authenticated_client() response = client.get(url) results = response.data["results"] self.assertEqual(len(results), QUANTITY)
def test_fail_postal_code_no_number(self): url = reverse("cases-search") client = get_authenticated_client() SEARCH_QUERY_PARAMETERS = { "postalCode": "FOO_POSTAL_CODE", } response = client.get(url, SEARCH_QUERY_PARAMETERS) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_authenticated_get_filled(self): baker.make(Visit, _quantity=2) url = reverse("visits-list") client = get_authenticated_client() response = client.get(url) data = response.json() self.assertEquals(len(data["results"]), 2)
def test_authenticated_requests(self): """ An authenticated request should be possible """ client = get_authenticated_client() response = client.get(self.get_url()) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.json().get("results")), 0)
def test_itinerary_without_team(self): """ Should return an empty response if no team is associated to itinerary """ itinerary = Itinerary.objects.create() url = reverse("v1:itinerary-team", kwargs={"pk": itinerary.id}) client = get_authenticated_client() response = client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json()["team_members"], [])
def test_authenticated_get_list(self): team = baker.make(CaseTeam) baker.make(CaseReason, team=team, _quantity=2) url = reverse("teams-reasons", kwargs={"pk": team.pk}) client = get_authenticated_client() response = client.get(url) data = response.json() self.assertEqual(len(data["results"]), 2)
def test_authenticated_requests_no_bag_id(self): """ An authenticated request fails if the requested id's doesn't have a bag_id """ url = reverse(self.PERMIT_URL_NAME) client = get_authenticated_client() response = client.get(url) # The response returns a 404 self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_authenticated_requests(self): """ is_authorized is true when user is not logged in """ url = reverse("is-authorized") client = get_authenticated_client() response = client.get(url) expected_response = {"is_authorized": True} self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEquals(response.json(), expected_response)
def test_no_itinerary_get(self): """ Should succeed and return no itineraries (none have been created) """ url = reverse("v1:itinerary-list") client = get_authenticated_client() response = client.get(url) expected_response_json = {"itineraries": []} self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json(), expected_response_json)
def test_delete(self): """ Removes a itinerary object using a delete request """ itinerary = Itinerary.objects.create() url = reverse("v1:itinerary-detail", kwargs={"pk": itinerary.id}) client = get_authenticated_client() response = client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Itinerary.objects.count(), 0)
def test_with_empty_list(self): """ Should return an empty list if no cases are found """ url = reverse("v1:case-unplanned") client = get_authenticated_client() response = client.get(url, { "date": "2020-04-05", "stadium": ISSUEMELDING }) self.assertEqual(response.json(), {"cases": []})