diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index 07a13ac..a5e64f0 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -44,8 +44,11 @@ jobs: run: | python -m poetry install + - name: Set django settings path + run: $env:DJANGO_SETTINGS_MODULE="config.settings" + - name: Run tests - run: poetry run manage.py test apps + run: poetry run pytest . - name: Run tests with pytest run: poetry run pytest --cov=./ --cov-report=xml diff --git a/backend/config/tests/conftest.py b/backend/config/tests/conftest.py index e58a09e..809b00d 100644 --- a/backend/config/tests/conftest.py +++ b/backend/config/tests/conftest.py @@ -7,7 +7,7 @@ register(factories.UserFactory) -#! api_client +# !api_client @pytest.fixture def api_client(): return APIClient diff --git a/backend/config/tests/factories.py b/backend/config/tests/factories.py index 8f79b37..7f71ec4 100644 --- a/backend/config/tests/factories.py +++ b/backend/config/tests/factories.py @@ -6,7 +6,7 @@ User = get_user_model() -#! UserFactory +# !UserFactory class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User @@ -14,5 +14,18 @@ class Meta: email = factory.Sequence(lambda n: "normal_user{}@gmail.com".format(n)) password = "foo12345" is_active = True - is_staff = False - is_superuser = False + + @classmethod + def create_user(cls, **kwargs): + kwargs["is_staff"] = False + kwargs["is_superuser"] = False + return super().create(**kwargs) + + @classmethod + def create_superuser(cls, **kwargs): + if not kwargs["is_superuser"] or not kwargs["is_staff"]: + raise ValueError("Superuser must have is_superuser=True and is_staff=True.") + else: + kwargs["is_staff"] = True + kwargs["is_superuser"] = True + return super().create(**kwargs) diff --git a/backend/config/tests/users/test_users.py b/backend/config/tests/users/test_users.py index bc7a050..da31a26 100644 --- a/backend/config/tests/users/test_users.py +++ b/backend/config/tests/users/test_users.py @@ -6,11 +6,46 @@ pytestmark = pytest.mark.django_db -#! TestUsersManagers +# !TestUsersManagers class TestUsersManagers: def test_create_user(self, user_factory): - user = user_factory.create() - assert user.email == "normal_user{}@gmail.com".format(0) - assert user.is_active + user = user_factory.create_user(email="normal_user@gmail.com", password="foo") + assert user.email == "normal_user@gmail.com" + assert user.is_active is True assert user.is_staff is False assert user.is_superuser is False + try: + # username is None for the AbstractUser option + # username does not exist for the AbstractBaseUser option + assert user.username is None + except ( + AttributeError + ): # Raised when an attribute reference (see Attribute references) or assignment fails + pass + + def test_create_superuser(self, user_factory): + admin_user = user_factory.create_superuser( + email="super_user@gmail.com", + password="foo", + is_superuser=True, + is_staff=True, + ) + assert admin_user.email == "super_user@gmail.com" + assert admin_user.is_active is True + assert admin_user.is_staff is True + assert admin_user.is_superuser is True + try: + # username is None for the AbstractUser option + # username does not exist for the AbstractBaseUser option + assert admin_user.username is None + except ( + AttributeError + ): # Raised when an attribute reference (see Attribute references) or assignment fails + pass + with pytest.raises(ValueError): + user_factory.create_superuser( + email="super_user@gmail.com", + password="foo", + is_superuser=False, + is_staff=False, + )