def test_manager_should_be_able_to_update_his_reports_in_project_in_which_he_is_not_manager( self): user_manager = ManagerUserFactory() self.client.force_login(user_manager) report_manager = ReportFactory(author=user_manager, task_activity=self.task_activity) report_manager.project.members.add(user_manager) data = { "date": report_manager.date, "description": "report_manager other description", "project": report_manager.project.pk, "author": report_manager.author.pk, "task_activity": self.task_activity.pk, "work_hours": report_manager.work_hours_str, } response = self.client.post(path=reverse("custom-report-detail", args=(report_manager.pk, )), data=data) report_manager.refresh_from_db() self.assertEqual(response.status_code, 302) self.assertEqual(report_manager.description, data["description"])
class AdminReportViewTests(TestCase): def setUp(self): super().setUp() self.admin = AdminUserFactory() self.user = UserFactory() self.task_activity = TaskActivityTypeFactory() self.project = ProjectFactory() self.project.members.add(self.user) self.client.force_login(self.admin) self.report = ReportFactory(author=self.user, project=self.project, task_activity=self.task_activity) self.url = reverse("admin-report-detail", kwargs={"pk": self.report.pk}) self.data = { "date": timezone.now().date(), "description": "Some other description", "project": self.report.project.pk, "author": self.user.pk, "task_activity": self.task_activity.pk, "work_hours": "8:00", "current-project-pk": self.report.project.pk, } def test_admin_report_detail_view_should_display_report_details(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, AdminReportView.template_name) self.assertContains(response, self.report.project.name) def test_admin_report_detail_view_should_update_report_on_post(self): response = self.client.post(self.url, self.data) self.assertEqual(response.status_code, 302) self.report.refresh_from_db() self.assertEqual(self.report.description, self.data["description"]) self.assertEqual(self.report.author, self.user) self.assertTrue(self.report.editable)
class ReportDetailViewTests(TestCase): def setUp(self): super().setUp() self.user = UserFactory() self.client.force_login(self.user) self.task_activity = TaskActivityTypeFactory() self.project = ProjectFactory() self.project.members.add(self.user) self.report = ReportFactory(author=self.user, project=self.project, task_activity=self.task_activity) self.url = reverse("custom-report-detail", args=(self.report.pk, )) self.data = { "date": self.report.date, "description": self.report.description, "project": self.project.pk, "author": self.report.author.pk, "task_activity": self.task_activity.pk, "work_hours": self.report.work_hours_str, } self.expected_success_message = SuccessPopUpMessages.REPORT_UPDATE.value.format( report_date=self.report.date, project_name=self.project.name) def test_custom_report_detail_view_should_display_report_details_on_get( self): response = self.client.get( path=reverse("custom-report-detail", args=(self.report.pk, ))) self.assertEqual(response.status_code, 200) self.assertContains(response, self.report.description) def test_custom_report_list_view_should_not_be_accessible_for_unauthenticated_users( self): self.client.logout() response = self.client.get( path=reverse("custom-report-detail", args=(self.report.pk, ))) self.assertEqual(response.status_code, 302) def test_custom_report_detail_view_should_not_render_non_existing_report( self): response = self.client.get( path=reverse("custom-report-detail", args=(999, ))) self.assertEqual(response.status_code, 404) def test_custom_report_detail_view_should_update_report_on_post(self): self.data["description"] = "Some other description" response = self.client.post(path=self.url, data=self.data) self.report.refresh_from_db() self.assertEqual(response.status_code, 302) self.assertEqual(self.report.description, self.data["description"]) def test_custom_report_detail_view_should_not_update_report_on_post_if_form_is_invalid( self): old_description = self.data["description"] self.data["description"] = "Some other description" self.data["project"] = "" response = self.client.post(path=self.url, data=self.data) self.report.refresh_from_db() self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context_data["form"].errors) self.assertEqual(old_description, self.report.description) def test_custom_report_detail_view_should_not_update_report_if_author_is_not_a_member_of_selected_project( self): other_project = ProjectFactory() other_project.save() old_description = self.data["description"] self.data["description"] = "Some other description" old_project = self.data["project"] self.data["project"] = other_project.pk response = self.client.post(path=self.url, data=self.data) self.report.refresh_from_db() self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.context_data["form"].errors) self.assertEqual(old_description, self.report.description) self.assertEqual(old_project, self.report.project.pk) def test_custom_report_detail_view_project_field_should_not_display_projects_the_author_is_not_a_member_of( self): other_project = Project(name="Other Project", start_date=datetime.datetime.now()) other_project.save() response = self.client.get( path=reverse("custom-report-detail", args=(self.report.pk, ))) self.assertEqual(response.status_code, 200) self.assertTrue(other_project not in response.context_data["form"]. fields["project"].queryset) def test_custom_report_detail_view_should_show_data_from_current_report_not_from_latest_report( self): # Latest report ReportFactory(author=self.user) response = self.client.get( path=reverse("custom-report-detail", args=(self.report.pk, ))) self.assertEqual(response.status_code, 200) self.assertEqual( response.context_data["form"].initial["task_activity"], self.report.task_activity) self.assertEqual(response.context_data["form"].initial["project"], self.report.project) def test_manager_should_be_able_to_update_his_reports_in_project_in_which_he_is_not_manager( self): user_manager = ManagerUserFactory() self.client.force_login(user_manager) report_manager = ReportFactory(author=user_manager, task_activity=self.task_activity) report_manager.project.members.add(user_manager) data = { "date": report_manager.date, "description": "report_manager other description", "project": report_manager.project.pk, "author": report_manager.author.pk, "task_activity": self.task_activity.pk, "work_hours": report_manager.work_hours_str, } response = self.client.post(path=reverse("custom-report-detail", args=(report_manager.pk, )), data=data) report_manager.refresh_from_db() self.assertEqual(response.status_code, 302) self.assertEqual(report_manager.description, data["description"]) def test_custom_report_detail_view_should_return_only_one_report_when_are_more_than_one_managers_in_project( self): manager_1 = ManagerUserFactory() manager_2 = ManagerUserFactory() self.report.project.managers.add(manager_1) self.report.project.managers.add(manager_2) report_1 = ReportFactory(author=manager_1, project=self.report.project) self.client.force_login(manager_1) response = self.client.get( path=reverse("custom-report-detail", args=(report_1.pk, ))) self.assertEqual(response.status_code, 200) self.assertContains(response, report_1.description) def test_custom_report_detail_view_should_contains_in_project_field_all_project_related_with_user( self): other_user_project = ProjectFactory() other_user_project.members.add(self.user) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertCountEqual( self.user.projects.all(), response.context_data["form"].fields["project"].queryset) def test_custom_report_detail_view_should_contains_chosen_project_even_user_is_no_longer_in_project( self): other_user_project = ProjectFactory() other_user_project.members.add(self.user) self.user.projects.remove(self.report.project) response = self.client.get(self.url) self.assertIn(self.report.project, response.context_data["form"].fields["project"].queryset) def test_success_message_should_be_added_on_post_if_form_is_valid(self): self.data["description"] = "Some other description" response = self.client.post(self.url, self.data) messages = list(get_messages(response.wsgi_request)) self.assertEqual(str(messages[0]), self.expected_success_message) def test_that_success_message_should_be_displayed_in_template_on_get_after_report_create_if_form_is_valid( self): response = self.client.post(self.url, self.data, follow=True) self.assertContains(response, self.expected_success_message) def test_that_success_message_should_not_be_added_if_form_is_invalid(self): self.data["description"] = "" response = self.client.post(self.url, self.data) messages = list(get_messages(response.wsgi_request)) self.assertEqual(len(messages), 0)