class DataSetUpToTests(BaseModelTestCase):
    model_class = Report
    required_input = {
        "date": datetime.datetime.now().date(),
        "description": "Some description",
        "author": None,
        "project": None,
        "work_hours": datetime.timedelta(hours=8),
    }

    def setUp(self):
        task_type = TaskActivityType(pk=1, name="Other")
        task_type.save()
        self.SAMPLE_STRING_FOR_TYPE_VALIDATION_TESTS = "This is a string"
        self.author = CustomUser(email="*****@*****.**",
                                 password="******",
                                 first_name="John",
                                 last_name="Doe")
        self.author.save()

        self.project = Project(name="Test Project",
                               start_date=datetime.datetime.now())
        self.project.save()

        self.required_input["author"] = self.author
        self.required_input["project"] = self.project

        self.REPORT_MODEL_DATA = self.required_input.copy()
 def test_project_report_list_view_should_display_message_if_project_has_no_reports(self):
     other_project = Project(name="Other Project", start_date=datetime.datetime.now())
     other_project.save()
     response = self.client.get(
         reverse("project-report-list", kwargs={"pk": other_project.pk, "year": self.year, "month": self.month})
     )
     self.assertEqual(response.status_code, 200)
     self.assertContains(response, ProjectReportListStrings.NO_REPORTS_MESSAGE.value)
class TestCustomUserSignals(TestCase):
    def setUp(self):
        self.user = ManagerUserFactory()
        self.user.full_clean()
        self.user.save()
        self.user_admin = AdminUserFactory()
        self.client.force_login(self.user_admin)
        self.project = Project(name="TEST", start_date=timezone.now())
        self.project.save()
        self.project.managers.add(self.user)
        self.project.members.add(self.user)

    def test_user_should_not_be_in_project_as_manager_when_he_is_no_longer_manager(
            self):
        response = self.client.post(
            path=reverse("custom-user-update-by-admin", args=(self.user.pk, )),
            data={
                "email": self.user.email,
                "password": self.user.password,
                "user_type": CustomUser.UserType.EMPLOYEE.name,
            },
        )
        self.assertEqual(response.status_code, 302)
        self.user.refresh_from_db()
        self.assertEqual(self.user.user_type,
                         CustomUser.UserType.EMPLOYEE.name)
        self.assertFalse(self.project in self.user.manager_projects.all())

    def test_user_should_not_be_a_project_member_when_he_is_no_longer_active(
            self):
        superuser = AdminUserFactory(is_staff=True, is_superuser=True)
        self.client.force_login(superuser)
        response = self.client.post(
            path=f"/admin/users/customuser/{self.user.pk}/change/",
            data={
                "email": self.user.email,
                "password": self.user.password,
                "user_type": self.user.user_type,
                "is_active": False,
            },
        )
        self.assertEqual(response.status_code, 302)
        self.user.refresh_from_db()
        self.assertEqual(self.user.user_type,
                         CustomUser.UserType.EMPLOYEE.name)
        self.assertFalse(self.project in self.user.projects.all())

    def test_custom_user_preferences_object_should_be_created_along_with_new_user_create(
            self):
        new_user = CustomUser(email="*****@*****.**",
                              password="******")
        new_user.save()

        custom_user_preference_query = CustomUserPreferences.objects.filter(
            user=new_user)

        self.assertTrue(custom_user_preference_query.exists())
        self.assertEqual(custom_user_preference_query.count(), 1)
Example #4
0
 def test_project_name_should_be_shorter_than_or_equal_set_max_length(self):
     project = Project(start_date=datetime.now().date(),
                       name="X" * ProjectConstants.MAX_NAME_LENGTH.value)
     try:
         project.full_clean()
     except ValidationError:
         self.fail("Ensure this value has at most " +
                   str(ProjectConstants.MAX_NAME_LENGTH.value) +
                   " characters")
Example #5
0
 def test_project_name_should_not_be_longer_than_set_max_length(self):
     project = Project(start_date=datetime.now().date(),
                       name="X" *
                       (ProjectConstants.MAX_NAME_LENGTH.value + 1))
     with self.assertRaises(ValidationError) as exception:
         project.full_clean()
     self.assertTrue(
         "Ensure this value has at most " + str(ProjectConstants.MAX_NAME_LENGTH.value) + " characters"
         in str(exception.exception)
     )  # pylint: disable=line-too-long # noqa E501
 def setUp(self):
     self.user = ManagerUserFactory()
     self.user.full_clean()
     self.user.save()
     self.user_admin = AdminUserFactory()
     self.client.force_login(self.user_admin)
     self.project = Project(name="TEST", start_date=timezone.now())
     self.project.save()
     self.project.managers.add(self.user)
     self.project.members.add(self.user)
Example #7
0
 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_project_report_list_view_should_not_display_other_projects_reports(self):
        other_project = Project(name="Other Project", start_date=datetime.datetime.now())
        other_project.save()

        other_report = Report(
            date=datetime.datetime.now().date(),
            description="Some other description",
            author=self.user,
            project=other_project,
            work_hours=datetime.timedelta(hours=8),
        )
        other_report.save()

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, other_report.description)
    def test_that_project_report_list_should_return_list_of_all_reports_assigned_to_project(self):
        other_user = CustomUser(
            email="*****@*****.**",
            password="******",
            first_name="Jane",
            last_name="Doe",
            is_active=True,
        )
        other_user.save()
        self.project.members.add(other_user)

        other_project = Project(name="Project test", start_date=datetime.datetime.now())
        other_project.save()
        other_project.members.add(self.user)
        other_project.members.add(other_user)

        other_project_report = Report(
            date=datetime.datetime.now().date(),
            description="Some other description",
            author=self.user,
            project=other_project,
            work_hours=datetime.timedelta(hours=8),
        )
        other_project_report.save()

        other_report_1 = Report(
            date=datetime.datetime.now().date(),
            description="Some other description",
            author=other_user,
            project=self.project,
            work_hours=datetime.timedelta(hours=8),
        )
        other_report_1.save()

        other_report_2 = Report(
            date=datetime.date(2001, 1, 1),
            description="Some other description",
            author=self.user,
            project=self.project,
            work_hours=datetime.timedelta(hours=8),
        )
        other_report_2.save()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, ProjectReportList.template_name)
        self.assertContains(response, self.project.name)
        self._assert_response_contain_report(response, [self.report, other_report_1, other_report_2])
    def setUp(self):
        task_type = TaskActivityType(pk=1, name="Other")
        task_type.save()
        self.SAMPLE_STRING_FOR_TYPE_VALIDATION_TESTS = "This is a string"
        self.author = CustomUser(email="*****@*****.**",
                                 password="******",
                                 first_name="John",
                                 last_name="Doe")
        self.author.save()

        self.project = Project(name="Test Project",
                               start_date=datetime.datetime.now())
        self.project.save()

        self.required_input["author"] = self.author
        self.required_input["project"] = self.project

        self.REPORT_MODEL_DATA = self.required_input.copy()
 def setUp(self):
     self.employee = CustomUser(email="*****@*****.**",
                                password="******",
                                user_type=CustomUser.UserType.EMPLOYEE.name)
     self.employee.save()
     self.manager = CustomUser(email="*****@*****.**",
                               password="******",
                               user_type=CustomUser.UserType.MANAGER.name)
     self.manager.save()
     self.admin = CustomUser(email="*****@*****.**",
                             password="******",
                             user_type=CustomUser.UserType.ADMIN.name)
     self.admin.save()
     self.project = Project(name="Test Project",
                            start_date=timezone.now() -
                            timezone.timedelta(days=2))
     self.project.save()
     self.project.managers.add(self.manager)
Example #12
0
class TestProjectQuerySet(TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.project = Project(name="Test project",
                               start_date=timezone.datetime(year=2019,
                                                            month=8,
                                                            day=28))

    @parameterized.expand(["2019-08-29", "2019-08-30", (None, )])
    def test_filter_active_projects_should_return_project_with_dates(
            self, date):
        with freeze_time("2019-08-28"):
            self.project.stop_date = date
            self.project.save()

            self.assertIn(self.project, Project.objects.filter_active())

    def test_filter_active_projects_should_not_return_project_with_stop_date_less_than_current_date(
            self):
        self.project.stop_date = timezone.now() - timezone.timedelta(days=1)
        self.project.save()

        self.assertNotIn(self.project, Project.objects.filter_active())

    def test_filter_completed_projects_should_return_all_projects_with_stop_date_less_than_current_date(
            self):
        project_not_completed = ProjectFactory()
        self.project.stop_date = timezone.now() - timezone.timedelta(days=1)
        self.project.save()

        completed_projects = Project.objects.filter_completed()

        self.assertIn(self.project, completed_projects)
        self.assertNotIn(project_not_completed, completed_projects)
Example #13
0
 def test_project_name_should_not_be_blank(self):
     project = Project(start_date=datetime.now().date())
     with self.assertRaises(ValidationError) as exception:
         project.full_clean()
     self.assertTrue(
         "This field cannot be blank." in str(exception.exception))
Example #14
0
 def test_project_should_have_start_date(self):
     project = Project(name="X")
     with self.assertRaises(ValidationError) as exception:
         project.full_clean()
     self.assertTrue(
         "This field cannot be null." in str(exception.exception))
Example #15
0
 def setUp(self) -> None:
     super().setUp()
     self.project = Project(name="Test project",
                            start_date=timezone.datetime(year=2019,
                                                         month=8,
                                                         day=28))
class TestProjectSignals(TestCase):
    def setUp(self):
        self.employee = CustomUser(email="*****@*****.**",
                                   password="******",
                                   user_type=CustomUser.UserType.EMPLOYEE.name)
        self.employee.save()
        self.manager = CustomUser(email="*****@*****.**",
                                  password="******",
                                  user_type=CustomUser.UserType.MANAGER.name)
        self.manager.save()
        self.admin = CustomUser(email="*****@*****.**",
                                password="******",
                                user_type=CustomUser.UserType.ADMIN.name)
        self.admin.save()
        self.project = Project(name="Test Project",
                               start_date=timezone.now() -
                               timezone.timedelta(days=2))
        self.project.save()
        self.project.managers.add(self.manager)

    def test_user_should_be_updated_to_manager_when_he_is_assigned_as_a_project_manager(
            self):
        self.project.managers.add(self.employee)
        self.client.post(path="/admin/managers/project/add/",
                         args=self.project)
        self.employee.refresh_from_db()
        self.assertEqual(self.employee.user_type,
                         CustomUser.UserType.MANAGER.name)

    def test_user_should_be_updated_to_employee_when_he_is_no_longer_manager_of_any_project(
            self):
        self.project.managers.remove(self.manager)
        self.client.post(
            path=f"/admin/managers/project/{self.project.pk}/change/",
            args=self.project)
        self.manager.refresh_from_db()
        self.assertEqual(self.manager.user_type,
                         CustomUser.UserType.EMPLOYEE.name)

    def test_admin_should_not_be_updated_to_manager_when_he_is_assigne_as_a_project_manager(
            self):
        self.project.managers.add(self.admin)
        self.client.post(path="/admin/managers/project/add/",
                         args=self.project)
        self.admin.refresh_from_db()
        self.assertEqual(self.admin.user_type, CustomUser.UserType.ADMIN.name)

    def test_admin_should_not_be_updated_to_employee_when_he_is_no_longer_manager_of_any_project(
            self):
        self.project.managers.remove(self.admin)
        self.client.post(
            path=f"/admin/managers/project/{self.project.pk}/change/",
            args=self.project)
        self.admin.refresh_from_db()
        self.assertEqual(self.admin.user_type, CustomUser.UserType.ADMIN.name)

    def test_project_should_not_be_suspended_when_project_is_completed(self):
        date = timezone.now().date() - timezone.timedelta(days=1)

        self.project.suspended = True
        self.project.stop_date = date
        self.project.save()
        self.project.refresh_from_db()

        self.assertEqual(self.project.suspended, False)
        self.assertEqual(self.project.stop_date, date)

    @freeze_time("2019-08-08")
    def test_project_could_be_suspended_when_stop_date_is_null(self):
        self.project.suspended = True
        self.project.save()
        self.project.refresh_from_db()

        self.assertEqual(self.project.suspended, True)
        self.assertEqual(self.project.stop_date, None)