def test_change_owner__history_items(self): user1 = create_test_user() user2 = create_test_user() barrier = BarrierFactory(created_by=user1) TeamMemberFactory(barrier=barrier, user=user1, role="Reporter", default=True) member = TeamMemberFactory(barrier=barrier, user=user1, role="Owner", default=True) url = reverse("get-member", kwargs={"pk": member.id}) assert user1 == member.user items = TeamMemberHistoryFactory.get_history_items( barrier_id=barrier.pk) assert 2 == len(items) payload = {"user": user2.profile.sso_user_id} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code items = TeamMemberHistoryFactory.get_history_items( barrier_id=barrier.pk) assert 3 == len(items) assert user1 == items[-1].old_record.user assert user2 == items[-1].new_record.user assert "Owner" == items[-1].old_record.role assert "Owner" == items[-1].new_record.role
def test_change_contributor_forbidden(self): user1 = create_test_user() user2 = create_test_user() barrier = BarrierFactory(created_by=user1) member = TeamMemberFactory(barrier=barrier, user=user1, role="Contributor") url = reverse("get-member", kwargs={"pk": member.id}) assert user1 == member.user payload = {"user": user2.id} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_403_FORBIDDEN == response.status_code member.refresh_from_db() assert user1 == member.user
def test_change_owner(self): user1 = create_test_user() user2 = create_test_user() barrier = BarrierFactory(created_by=user1) TeamMemberFactory(barrier=barrier, user=user1, role="Reporter", default=True) member = TeamMemberFactory( barrier=barrier, user=user1, role="Owner", default=True ) url = reverse("get-member", kwargs={"pk": member.id}) assert user1 == member.user payload = {"user": user2.profile.sso_user_id} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code member.refresh_from_db() assert user2 == member.user
def test_member_filter(self): user1 = create_test_user() _barrier0 = BarrierFactory() barrier1 = BarrierFactory(created_by=user1) member1 = TeamMemberFactory(barrier=barrier1, user=user1, role="Reporter", default=True) barrier2 = BarrierFactory(created_by=user1) TeamMemberFactory(barrier=barrier2, user=user1, role="Contributor") assert 3 == Barrier.objects.count() url = f'{reverse("list-barriers")}?member={member1.id}' response = self.api_client.get(url) assert status.HTTP_200_OK == response.status_code assert 2 == response.data["count"] barrier_ids = [b["id"] for b in response.data["results"]] assert {str(barrier1.id), str(barrier2.id)} == set(barrier_ids)
def test_delete_member(self): user1 = create_test_user() barrier = BarrierFactory(created_by=user1) member = TeamMemberFactory(barrier=barrier, user=user1, role="Contributor") url = reverse("get-member", kwargs={"pk": member.id}) assert 1 == TeamMember.objects.filter(barrier=barrier).count() response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert 0 == TeamMember.objects.filter(barrier=barrier).count()
def test_member_filter__distinct_records(self): """ Only include the barrier once even if the user is listed multiple times as a member for a barrier. """ user1 = create_test_user() barrier1 = BarrierFactory(created_by=user1) member1 = TeamMemberFactory(barrier=barrier1, user=user1, role="Reporter", default=True) member2 = TeamMemberFactory(barrier=barrier1, user=user1, role="Owner", default=True) assert 1 == Barrier.objects.count() url = f'{reverse("list-barriers")}?member={member1.id}' response = self.api_client.get(url) assert status.HTTP_200_OK == response.status_code assert 1 == response.data["count"]
def test_delete_default_member(self): """ Cannot delete a member if default is set to True. """ user1 = create_test_user() barrier = BarrierFactory(created_by=user1) member = TeamMemberFactory( barrier=barrier, user=user1, role="Protected role", default=True ) url = reverse("get-member", kwargs={"pk": member.id}) assert 1 == TeamMember.objects.filter(barrier=barrier).count() response = self.api_client.delete(url) assert response.status_code == status.HTTP_403_FORBIDDEN assert 1 == TeamMember.objects.filter(barrier=barrier).count()
def test_individual_barrier_reminder_email(self): test_user = create_test_user() inactivity_theshold_date = timezone.now() - timedelta( days=settings.BARRIER_INACTIVITY_THESHOLD_DAYS) # test individual call params test_barrier = BarrierFactory() TeamMemberFactory(barrier=test_barrier, user=test_user, role="Owner", default=True) Barrier.objects.filter(id=test_barrier.id).update( modified_on=inactivity_theshold_date - timedelta(days=1), activity_reminder_sent=None, ) with patch.object(NotificationsAPIClient, "send_email_notification") as mock: send_barrier_inactivity_reminders() email_personalisation = { "barrier_title": test_barrier.title, "barrier_url": f"{settings.DMAS_BASE_URL}/barriers/{test_barrier.id}/", "full_name": f"{test_user.first_name} {test_user.last_name}", "barrier_created_date": test_barrier.created_on.strftime("%d %B %Y"), } assert mock.call_count == 1 mock.assert_called_once_with( email_address=test_user.email, template_id=settings. BARRIER_INACTIVITY_REMINDER_NOTIFICATION_ID, personalisation=email_personalisation, ) mock.stop()
def test_inactive_barrier_reminder_email(self): test_user = create_test_user() inactivity_theshold_date = timezone.now() - timedelta( days=settings.BARRIER_INACTIVITY_THESHOLD_DAYS) BarrierFactory.create_batch(size=10) for barrier in Barrier.objects.all(): TeamMemberFactory(barrier=barrier, user=test_user, role="Owner", default=True) # modified_on can't be updated directly, so we need to update the barrier queryset Barrier.objects.all().update(modified_on=inactivity_theshold_date - timedelta(days=1)) # create recent barries that should not be sent a reminder BarrierFactory.create_batch(size=15) with patch.object(NotificationsAPIClient, "send_email_notification", return_value=None) as mock: send_barrier_inactivity_reminders() assert mock.call_count == 10 assert (Barrier.objects.filter( activity_reminder_sent__isnull=False).count() == 10) mock.stop() # when called a second time, no new reminders should be sent with patch.object(NotificationsAPIClient, "send_email_notification", return_value=None) as mock: send_barrier_inactivity_reminders() assert mock.call_count == 0 assert (Barrier.objects.filter( activity_reminder_sent__isnull=False).count() == 10) mock.stop()