def test_get_projects_work_hours_and_percentage_should_return_dictionary_containing_with_work_time_and_time_percent_per_project( self): ReportFactory(author=self.user, project=self.project_1, work_hours=timezone.timedelta(hours=8)) ReportFactory(author=self.user, project=self.project_2, work_hours=timezone.timedelta(hours=4)) ReportFactory(author=self.user, project=self.project_2, work_hours=timezone.timedelta(hours=8)) ReportFactory(project=self.project_1, work_hours=timezone.timedelta(hours=4)) result = ProjectsWorkPercentageMixin( )._get_projects_work_hours_and_percentage(self.user.report_set.all()) self.assertEqual(len(result), 2) self.assertEqual( result, { self.project_1.name: (timezone.timedelta(hours=8), 40.0), self.project_2.name: (timezone.timedelta(hours=12), 60.0), }, )
def test_project_report_list_view_should_display_inactive_members_reports(self): current_date = datetime.datetime.now().date() previous_date = current_date + relativedelta(months=-1) inactive_user = UserFactory(is_active=False) project_report = ReportFactory( author=inactive_user, date=current_date, project=self.project, description="This is for current project." ) other_project_report = ReportFactory( author=inactive_user, date=current_date, description="This is for other project." ) project_report_from_other_month = ReportFactory( author=inactive_user, date=previous_date, project=self.project, description="This is for another month." ) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertContains(response, project_report.description) self.assertNotContains(response, other_project_report.description) self.assertNotContains(response, project_report_from_other_month.description) response = self.client.get( reverse( "project-report-list", kwargs={"pk": self.project.pk, "year": previous_date.year, "month": previous_date.month}, ) ) self.assertEqual(response.status_code, 200) self.assertNotContains(response, project_report.description) self.assertNotContains(response, other_project_report.description) self.assertContains(response, project_report_from_other_month.description)
def setUp(self): super().setUp() self.date_1 = datetime.datetime.now().date() self.date_2 = self.date_1 - datetime.timedelta(days=1) ReportFactory(work_hours=datetime.timedelta(hours=6), date=self.date_1) ReportFactory(work_hours=datetime.timedelta(hours=7), date=self.date_1) ReportFactory(work_hours=datetime.timedelta(hours=5), date=self.date_2)
def _generate_user_reports_for_current_and_previous_month_with_uneven_total_hours( self): reports_per_project, remaining_hours = self._strip_hours_between_projects( ) months_and_reported_days = self._get_number_of_reported_days_for_each_month( reports_per_project) for start_date, total_reported_days in months_and_reported_days.items( ): total_reported_days = reports_per_project report_date = start_date for _ in range(total_reported_days): work_hours = timezone.timedelta(hours=self.hours_per_report) for project in self.projects: ReportFactory(project=project, author=self.user, work_hours=work_hours, date=report_date) report_date += relativedelta(days=1) if remaining_hours != 0: ReportFactory( project=self.projects[0], author=self.user, work_hours=timezone.timedelta(hours=remaining_hours), date=report_date, )
def setUp(self): super().setUp() self.author_1 = UserFactory() self.author_2 = UserFactory() ReportFactory(work_hours=datetime.timedelta(hours=6), author=self.author_1) ReportFactory(work_hours=datetime.timedelta(hours=7), author=self.author_2) ReportFactory(work_hours=datetime.timedelta(hours=5), author=self.author_2)
def test_view_should_display_author_reports_only_one_project(self): admin = AdminUserFactory() report_from_project1 = ReportFactory(author=self.user, project=self.project, date=self.report.date) report_from_other_project = ReportFactory(author=self.user) self.client.force_login(admin) response = self.client.get(self.url) self.assertContains(response, self.report.description) self.assertContains(response, report_from_project1.description) self.assertContains(response, report_from_project1.project.name) self.assertNotContains(response, report_from_other_project.description) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, AuthorReportProjectView.template_name)
def test_custom_report_list_view_should_not_display_reports_from_different_month_than_selected(self): current_date = datetime.datetime.now().date() other_report = ReportFactory( date=current_date + relativedelta(months=+1), author=self.user, project=self.report.project ) yet_another_report = ReportFactory( date=current_date + relativedelta(years=-1), author=self.user, project=self.report.project ) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertNotContains(response, other_report.description) self.assertNotContains(response, yet_another_report.description)
def test_edited_report_would_not_be_summed_twice_for_work_hours_sum(self): user = UserFactory() today = timezone.now().date() work_hours = datetime.timedelta(hours=20) ReportFactory(work_hours=work_hours, date=today, author=user) edited_report = ReportFactory(work_hours=datetime.timedelta(hours=3), date=today, author=user) edited_report.work_hours = datetime.timedelta(hours=4) edited_report.save() self.assertEqual( user.report_set.get_report_work_hours_sum_for_date(today), datetime.timedelta(hours=24))
def test_user_is_author_of_current_report_mixin_should_not_limit_view_report_queryset_if_user_is_not_employee( self): user = UserFactory(user_type=CustomUser.UserType.MANAGER.name) ReportFactory(author=user) # Other author report. ReportFactory() assert Report.objects.count() == 2 request = self.request_factory.get("anything") request.user = user response = self.view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context_data["object_list"]), 2)
def setUp(self): super().setUp() self.user = AdminUserFactory() self.task_activity = TaskActivityTypeFactory() self.project = ProjectFactory() self.project.members.add(self.user) self.client.force_login(self.user) self.report = ReportFactory(author=self.user, project=self.project, task_activity=self.task_activity) self.url = reverse("project-report-detail", args=(self.report.pk, )) self.data = { "date": self.report.date, "description": self.report.description, "project": self.report.project.pk, "author": self.report.author.pk, "task_activity": self.task_activity.pk, "work_hours": self.report.work_hours_str, "current-project-pk": self.report.project.pk, } self.expected_success_message = SuccessPopUpMessages.REPORT_UPDATE_BY_ADMIN.value.format( report_author_email=self.report.author.email, report_date=self.report.date, project_name=self.report.project.name, )
def setUp(self): super().setUp() self.task_type = TaskActivityType(pk=1, name="Other") self.task_type.save() self.user = AdminUserFactory() self.project = ProjectFactory() self.project.members.add(self.user) self.client.force_login(self.user) current_date = datetime.datetime.now().date() self.report = ReportFactory( author=self.user, project=self.project, date=current_date, task_activity=self.task_type, work_hours=datetime.timedelta(hours=8), ) self.data = { "date": timezone.now().date(), "description": "Some other description", "project": self.report.project.pk, "author": self.user.pk, "task_activity": self.report.task_activity.pk, "work_hours": "8.00", } self.year = current_date.year self.month = current_date.month self.url = reverse( "project-report-list", kwargs={"pk": self.project.pk, "year": self.year, "month": self.month} )
def setUp(self): super().setUp() self.user = AdminUserFactory() self.new_user = AdminUserFactory() self.other_project = ProjectFactory() self.project_start_date = timezone.now() + timezone.timedelta(days=-1) self.project = ProjectFactory( name="aaa", start_date=self.project_start_date, stop_date=timezone.now() + timezone.timedelta(days=6) ) self.report = ReportFactory(author=self.user, project=self.project) self.project.members.add(self.user) self.data = {"project": self.project.pk, "author": self.user.pk} self.url_single_user = reverse( "export-data", kwargs={ "pk": self.user.pk, "year": str(self.report.creation_date.year), "month": str(self.report.creation_date.month), }, ) self.url_project = reverse( "export-project-reports", kwargs={ "pk": self.data["project"], "year": str(self.report.creation_date.year), "month": str(self.report.creation_date.month), }, ) self.workbook_for_project = generate_xlsx_for_project(self.project) self.workbook_for_user = generate_xlsx_for_single_user(self.user)
def test_user_is_manager_of_current_project_or_author_of_current_report_mixin_should_not_limit_view_project_queryset_if_user_is_not_manager( self): user = UserFactory(user_type=CustomUser.UserType.EMPLOYEE.name) report = ReportFactory() report.project.managers.add(user) # Report with project without current user as manager. ReportFactory() assert Report.objects.count() == 2 request = self.request_factory.get("anything") request.user = user response = self.view(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context_data["object_list"]), 2)
def setUp(self): self.user = UserFactory() self.client.force_login(self.user) self.task_activity = TaskActivityTypeFactory() self.report = ReportFactory( author=self.user, date=datetime.datetime.now().date(), task_activity=self.task_activity, work_hours=datetime.timedelta(hours=8), ) self.report.project.members.add(self.user) self.url = reverse( "join-project", kwargs={ "year": datetime.datetime.now().date().year, "month": datetime.datetime.now().date().month }, ) self.success_url = reverse( "custom-report-list", kwargs={ "year": datetime.datetime.now().date().year, "month": datetime.datetime.now().date().month }, ) self.new_project = ProjectFactory(name="New Project", start_date=datetime.datetime.now())
def test_manager_should_get_notification_about_missing_reports(self, test_date, missing_reports): with freeze_time("2019-07-08"): ReportFactory(author=self.employee, project=self.project, date="2019-07-08") self.client.force_login(self.manager) with freeze_time(test_date): response = self.client.get(self.url) self._check_response(response, 200, [self.employee.email, missing_reports])
def test_initial_date_should_be_highest_report_date_for_user_for_last_report_date_preference( self): user_preferences = CustomUserPreferences.objects.get(user=self.user) user_preferences.default_report_date = DatePreferences.LAST_REPORT_DATE.name user_preferences.save() highest_date = timezone.now().date() + relativedelta(days=+100) ReportFactory(author=self.user, date=timezone.now().date() + relativedelta(days=+10)) ReportFactory(author=self.user, date=highest_date) ReportFactory(author=self.user, date=timezone.now().date() + relativedelta(days=-100)) response = self.client.get(self.url) initial_date = response.context["form"].initial["date"] self.assertEqual(initial_date, highest_date)
def test_manager_should_only_get_notifications_about_employees_from_his_projects(self): with freeze_time("2019-07-08"): ReportFactory(date="2019-07-08") self.client.force_login(self.manager) with freeze_time("2019-07-15"): response = self.client.get(self.url) self._check_response(response, 200, [UserNotificationsText.NO_MORE_NOTIFICATIONS.value])
def test_get_project_ordered_by_last_report_creation_date_should_not_include_other_users_reports( self): # Create report from other user in the same project ReportFactory(project=self.project_3) result = list( self.user.get_project_ordered_by_last_report_creation_date()) self.assertEqual(result, [self.project_2, self.project_1, self.project_3])
def test_manager_should_not_get_any_notification_about_missing_reports(self, test_date): with freeze_time("2019-07-05"): ReportFactory(author=self.employee, project=self.project, date="2019-07-05") self.client.force_login(self.manager) with freeze_time(test_date): response = self.client.get(self.url) self._check_response(response, 200, [UserNotificationsText.NO_MORE_NOTIFICATIONS.value])
def test_project_work_percentage_mixin_should_pass_work_hours_summary_to_report_list_view( self): class TestView(ProjectsWorkPercentageMixin, ListView): model = Report report_1 = ReportFactory() report_2 = ReportFactory() response = TestView.as_view()(self.request) self.assertEqual(response.status_code, 200) self.assertTrue( "projects_work_percentage" in response.context_data.keys()) projects_work_percentage = response.context_data[ "projects_work_percentage"] self.assertTrue(report_1.project.name in projects_work_percentage) self.assertTrue(report_2.project.name in projects_work_percentage)
def create_report(self, user: Any, report_publication_date: timezone.datetime, number_of_reports: int) -> None: ReportFactory( date=report_publication_date, description=FuzzyText(length=random.randrange(20, 100)), author=user, project=self._pick_random_user_project(user), work_hours=self._get_random_work_hours(number_of_reports), task_activity=self.pick_random_task_activity(), )
def test_initial_date_should_be_today_for_always_today_preference( self, report_date): user_preferences = CustomUserPreferences.objects.get(user=self.user) user_preferences.default_report_date = DatePreferences.TODAY.name user_preferences.save() ReportFactory(author=self.user, date=report_date) self._assert_initial_date_equal_today()
def test_manager_should_not_get_get_notifications_about_inactive_employees(self): inactive_user = UserFactory(is_active=False) with freeze_time("2019-07-08"): ReportFactory(author=inactive_user, project=self.project, date="2019-07-08") self.client.force_login(self.manager) with freeze_time("2019-07-15"): response = self.client.get(self.url) self._check_response(response, 200, [UserNotificationsText.NO_MORE_NOTIFICATIONS.value])
def test_editing_report_work_hours_sum_for_given_day_for_single_user_should_not_exceed_24( self): user = UserFactory() today = timezone.now().date() ReportFactory(work_hours=datetime.timedelta(hours=23), date=today, author=user) edited_report = ReportFactory(work_hours=datetime.timedelta(hours=1), date=today, author=user) with self.assertRaises(ValidationError) as exception: edited_report.work_hours = datetime.timedelta(hours=2) edited_report.full_clean() self.assertEqual( exception.exception.messages[0], ReportValidationStrings. WORK_HOURS_SUM_FOR_GIVEN_DATE_FOR_SINGLE_AUTHOR_EXCEEDED.value, )
def test_manager_should_not_get_any_notifications_if_they_are_disabled_for_project(self): self.project.is_notification_enabled = False self.project.save() with freeze_time("2019-07-08"): ReportFactory(author=self.employee, project=self.project, date="2019-07-08") self.client.force_login(self.manager) with freeze_time("2019-07-15"): response = self.client.get(self.url) self._check_response(response, 200, [UserNotificationsText.NO_MORE_NOTIFICATIONS.value])
def test_initial_date_should_be_date_of_recently_created_report_for_recently_created_report_date_preference( self, report_date): user_preferences = CustomUserPreferences.objects.get(user=self.user) user_preferences.default_report_date = DatePreferences.RECENTLY_CREATED_REPORT_DATE.name user_preferences.save() ReportFactory(author=self.user, date=report_date) response = self.client.get(self.url) initial_date = response.context["form"].initial["date"] self.assertEqual(initial_date, report_date)
def test_author_report_list_view_should_link_to_admin_report_detail_if_user_is_author(self): report = ReportFactory(date=self.current_time) response = self.client.get( reverse( "author-report-list", kwargs={"pk": report.author.pk, "year": self.current_time.year, "month": self.current_time.month}, ) ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, AuthorReportView.template_name) self.assertContains(response, reverse("admin-report-detail", kwargs={"pk": report.pk}))
def test_custom_report_list_view_should_display_monthly_hour_sum_in_hour_format(self): report_date = timezone.datetime(year=timezone.now().year, month=timezone.now().month, day=1) for i in range(3): ReportFactory( author=self.user, date=report_date + timezone.timedelta(days=i), work_hours=timezone.timedelta(hours=8) ) response = self.client.get(self.url) self.assertContains(response, "32:00") self.assertNotContains(response, str(timezone.timedelta(hours=32)))
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_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)