def test_can_successfully_rename_a_list(self): """Test that a list can be renamed.""" company_list = CompanyListFactory(adviser=self.user, name='old name') url = _get_list_detail_url(company_list.pk) new_name = 'new name' response = self.api_client.patch( url, data={ 'name': new_name, }, ) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data == { 'id': str(company_list.pk), 'item_count': 0, 'name': new_name, 'created_on': format_date_or_datetime(company_list.created_on), } company_list.refresh_from_db() assert company_list.name == new_name
def test_can_filter_by_valid_company_id(self): """Test that a user's lists can be filtered to those containing a particular company.""" company_to_filter_by = CompanyFactory() # Create some lists containing `company_to_filter_by` expected_lists = CompanyListFactory.create_batch(3, adviser=self.user) CompanyListItemFactory.create_batch( len(expected_lists), list=factory.Iterator(expected_lists), company=company_to_filter_by, ) # Create some lists without `company_to_filter_by` that should not be returned CompanyListFactory.create_batch(3, adviser=self.user) response = self.api_client.get( list_collection_url, data={ 'items__company_id': company_to_filter_by.pk, }, ) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] assert len(results) == len(expected_lists) expected_list_ids = {str(list_.pk) for list_ in expected_lists} actual_list_ids = {result['id'] for result in results} assert actual_list_ids == expected_list_ids
def test_doesnt_return_other_users_lists(self): """Test that other users' company lists are not returned.""" # Create some lists belonging to other users CompanyListFactory.create_batch(5) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data['results'] == []
def test_with_non_existent_list(self): """Test that you cannot add an item to a list that does not exist.""" # Existing company lists for other users should not matter CompanyListFactory.create_batch(5) company = CompanyFactory() url = _get_list_item_url(uuid4(), company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert not CompanyListItem.objects.filter( list__adviser=self.user, company=company, ).exists()
def test_with_existing_item(self): """ Test that no error is returned if the specified company is already on the authenticated user's list. """ creation_date = datetime(2018, 1, 2, tzinfo=utc) modified_date = datetime(2018, 1, 5, tzinfo=utc) company = CompanyFactory() company_list = CompanyListFactory(adviser=self.user) with freeze_time(creation_date): CompanyListItemFactory(list=company_list, company=company) url = _get_list_item_url(company_list.pk, company.pk) with freeze_time(modified_date): response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' company_list_item = _get_queryset_for_list_and_company( company_list, company, ).first() assert company_list_item.created_on == creation_date assert company_list_item.modified_on == creation_date
def test_with_multiple_user_lists(self): """Test that user who owns multiple lists can list all their contents.""" lists = CompanyListFactory.create_batch(5, adviser=self.user) company_list_companies = {} # add multiple companies to user's lists for company_list in lists: companies = CompanyFactory.create_batch(5) company_list_companies[company_list.pk] = companies CompanyListItemFactory.create_batch( len(companies), list=company_list, company=factory.Iterator(companies), ) # check if contents of each user's list can be listed for company_list in lists: url = _get_list_item_collection_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() result_company_ids = { result['company']['id'] for result in response_data['results'] } assert result_company_ids == { str(company.id) for company in company_list_companies[company_list.pk] }
def test_with_multiple_lists(self): """Test that deleting company from one list will not delete it from the other lists.""" company = CompanyFactory() company_lists = CompanyListFactory.create_batch(5, adviser=self.user) CompanyListItemFactory.create_batch( 5, list=factory.Iterator(company_lists), company=company, ) # company exists on 5 user's lists assert CompanyListItem.objects.filter(list__in=company_lists, company=company).count() == 5 url = _get_list_item_url(company_lists[0].pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert not CompanyListItem.objects.filter(list=company_lists[0], company=company).exists() # The company exists on 4 other lists assert CompanyListItem.objects.filter(list__in=company_lists, company=company).count() == 4
def test_cannot_delete_another_users_list(self): """Test that another user's list can't be deleted.""" company_list = CompanyListFactory() url = _get_list_detail_url(company_list.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_with_list_that_does_not_belong_to_user(self): """Test that 404 status is returned if selected list does not belong to user.""" company_list = CompanyListFactory() url = _get_list_item_collection_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_returns_401_if_unauthenticated(self, api_client): """Test that a 401 is returned for an unauthenticated user.""" company_list = CompanyListFactory() url = _get_list_item_collection_url(company_list.pk) response = api_client.get(url) assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_with_company_that_does_not_exist(self): """Test that 404 status code is returned if the specified company does not exist.""" company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, uuid4()) response = self.api_client.delete(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert response.content == b'{"detail":"Not found."}'
def test_with_non_existent_company(self): """Test that a 404 is returned if the specified company ID is invalid.""" company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, uuid4()) response = self.api_client.put(url) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_validation(self, request_data, expected_errors): """Test validation.""" company_list = CompanyListFactory(adviser=self.user) url = _get_list_detail_url(company_list.pk) response = self.api_client.patch(url, data=request_data) assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.json() == expected_errors
def test_returns_401_if_unauthenticated(self, api_client, http_method): """Test that a 401 is returned for an unauthenticated user.""" company = CompanyFactory() company_list = CompanyListFactory() url = _get_list_item_url(company_list.pk, company.pk) response = api_client.generic(http_method, url) assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_adviser_can_have_the_same_company_on_multiple_lists(self): """Tests that adviser can have the same company on multiple lists.""" company = CompanyFactory() other_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=other_list, company=company) company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert CompanyListItem.objects.filter( list__in=(other_list, company_list), company=company, ).count() == 2
def test_returns_403_if_without_permissions(self, api_client): """Test that a 403 is returned for a user with no permissions.""" user = create_test_user(dit_team=TeamFactory()) company_list = CompanyListFactory(adviser=user) api_client = self.create_api_client(user=user) url = _get_list_item_collection_url(company_list.pk) response = api_client.get(url) assert response.status_code == status.HTTP_403_FORBIDDEN
def test_other_lists_are_not_affected(self): """Test that other lists are not affected when a list is deleted.""" list_to_delete = CompanyListFactory(adviser=self.user) list_item = CompanyListItemFactory(list=list_to_delete) other_list = CompanyListFactory(adviser=self.user) other_list_item = CompanyListItemFactory(company=list_item.company) url = _get_list_detail_url(list_to_delete.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' try: other_list.refresh_from_db() other_list_item.refresh_from_db() except (CompanyList.DoesNotExist, CompanyListItem.DoesNotExist): pytest.fail('Other lists should not be affected.')
def test_permission_checking(self, permission_codenames, expected_status, api_client): """Test that the expected status is returned for various user permissions.""" user = create_test_user(permission_codenames=permission_codenames, dit_team=None) company_list = CompanyListFactory(adviser=user) url = _get_list_detail_url(company_list.pk) api_client = self.create_api_client(user=user) response = api_client.delete(url) assert response.status_code == expected_status
def test_lists_are_sorted_by_name(self): """Test that returned lists are sorted by name.""" expected_list_names = ['A list', 'B list', 'C list', 'D list'] shuffled_list_names = sample(expected_list_names, len(expected_list_names)) CompanyListFactory.create_batch( len(expected_list_names), adviser=self.user, name=factory.Iterator(shuffled_list_names), ) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK response_data = response.json() actual_list_names = [ result['name'] for result in response_data['results'] ] assert actual_list_names == expected_list_names
def test_with_no_list_items(self): """ Test that an empty list is returned if the user does not have any companies on the selected list. """ company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_collection_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data['results'] == []
def test_with_archived_company(self): """Test that an archived company can't be added to the authenticated user's list.""" company_list = CompanyListFactory(adviser=self.user) company = ArchivedCompanyFactory() url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.json() == { api_settings.NON_FIELD_ERRORS_KEY: CANT_ADD_ARCHIVED_COMPANY_MESSAGE, }
def test_cannot_rename_another_users_list(self): """Test that another user's list can't be renamed.""" company_list = CompanyListFactory(name='old name') url = _get_list_detail_url(company_list.pk) new_name = 'new name' response = self.api_client.patch( url, data={ 'name': new_name, }, ) assert response.status_code == status.HTTP_404_NOT_FOUND
def test_with_company_not_on_the_list(self): """ Test that 204 status is returned if company is not on the authenticated user's selected list. """ company = CompanyFactory() company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert not CompanyListItem.objects.filter(list=company_list, company=company).exists()
def test_returns_items_in_expected_format(self): """Test that a user's list is returned in the expected format.""" company_list = CompanyListFactory(adviser=self.user) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] assert len(results) == 1 assert results[0] == { 'id': str(company_list.pk), 'item_count': 0, 'name': company_list.name, 'created_on': format_date_or_datetime(company_list.created_on), }
def test_can_get_a_list(self): """Test that details of a single list can be retrieved.""" company_list = CompanyListFactory(adviser=self.user) url = _get_list_detail_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data == { 'id': str(company_list.pk), 'item_count': 0, 'name': company_list.name, 'created_on': format_date_or_datetime(company_list.created_on), }
def test_with_only_items_on_other_users_lists(self): """ Test that an empty list is returned if the user has no companies on their selected list, but other users have companies on theirs. """ CompanyListItemFactory.create_batch(5) company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_collection_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data['results'] == []
def test_with_list_that_does_not_belong_to_user(self): """Test that a company cannot be removed from another user's lists.""" company = CompanyFactory() company_list = CompanyListFactory() CompanyListItemFactory(list=company_list, company=company) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert response.content == b'{"detail":"Not found."}' # company has not been deleted from another user list assert CompanyListItem.objects.filter(list=company_list, company=company).exists()
def test_includes_accurate_item_count(self, num_items): """Test that the correct item count is returned.""" company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory.create_batch(num_items, list=company_list) # Create some unrelated items – should not affect the count CompanyListItemFactory.create_batch(7) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] result = next(result for result in results if result['id'] == str(company_list.pk)) assert result['item_count'] == num_items
def test_with_archived_company(self): """Test that no error is returned when removing an archived company.""" company = ArchivedCompanyFactory() company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=company_list, company=company) assert CompanyListItem.objects.filter(list=company_list, company=company).exists() url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert not CompanyListItem.objects.filter(list=company_list, company=company).exists()
def test_two_advisers_can_have_the_same_company(self): """Test that two advisers can have the same company on their list.""" other_user_item = CompanyListItemFactory() company = other_user_item.company company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert _get_queryset_for_list_and_company(other_user_item.list, company).exists() assert _get_queryset_for_list_and_company(company_list, company).exists()