From 3921322839aacc7ba1458075c3c74e60ea4a44c1 Mon Sep 17 00:00:00 2001 From: IsaacDMz Date: Sun, 12 Nov 2023 19:35:23 -0300 Subject: [PATCH 1/4] feat(auth): create tests and factories for authentication --- backend/authentication/factories.py | 58 +++++++++++++++++++++++++++++ backend/authentication/tests.py | 19 +++++++++- backend/pyproject.toml | 3 +- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 backend/authentication/factories.py diff --git a/backend/authentication/factories.py b/backend/authentication/factories.py new file mode 100644 index 000000000..67f613322 --- /dev/null +++ b/backend/authentication/factories.py @@ -0,0 +1,58 @@ +# authentication/factories.py +import factory +from .models import SupportEntityType, Support, User, UserResource, UserTask, UserTopic + +class SupportEntityTypeFactory(factory.django.DjangoModelFactory): + class Meta: + model = SupportEntityType + + name = factory.Faker('word') + +class SupportFactory(factory.django.DjangoModelFactory): + class Meta: + model = Support + + supporter_type = factory.SubFactory(SupportEntityTypeFactory) + supporter_entity = factory.Faker('random_int', min=1, max=100) + supported_type = factory.SubFactory(SupportEntityTypeFactory) + supported_entity = factory.Faker('random_int', min=1, max=100) + +class UserFactory(factory.django.DjangoModelFactory): + class Meta: + model = User + + user_name = factory.Faker('user_name') + name = factory.Faker('name') + password = factory.Faker('password') + location = factory.Faker('city') + description = factory.Faker('text', max_nb_chars=500) + verified = factory.Faker('boolean') + verification_method = factory.Faker('word') + verification_partner = factory.SubFactory("content.UserFactory") + social_accounts = [factory.Faker('user_name') for _ in range(3)] + private = factory.Faker('boolean') + high_risk = factory.Faker('boolean') + total_flags = factory.Faker('random_int', min=0, max=100) + creation_date = factory.Faker('date_time_this_decade', before_now=True) + deletion_date = factory.Faker('date_time_this_decade', before_now=False) + +class UserResourceFactory(factory.django.DjangoModelFactory): + class Meta: + model = UserResource + + user_id = factory.SubFactory(UserFactory) + resource_id = factory.SubFactory("content.ResourceFactory") + +class UserTaskFactory(factory.django.DjangoModelFactory): + class Meta: + model = UserTask + + user_id = factory.SubFactory(UserFactory) + task_id = factory.SubFactory("content.TaskFactory") + +class UserTopicFactory(factory.django.DjangoModelFactory): + class Meta: + model = UserTopic + + user_id = factory.SubFactory(UserFactory) + topic_id = factory.SubFactory("content.TopicFactory") diff --git a/backend/authentication/tests.py b/backend/authentication/tests.py index 7ce503c2d..bada77a28 100644 --- a/backend/authentication/tests.py +++ b/backend/authentication/tests.py @@ -1,3 +1,18 @@ -from django.test import TestCase +import pytest +from .factories import SupportEntityTypeFactory, SupportFactory, UserFactory, UserResourceFactory, UserTaskFactory, UserTopicFactory -# Create your tests here. +@pytest.mark.django_db +def test_str_methods() -> None: + support_entity_type = SupportEntityTypeFactory.build() + support = SupportFactory.build() + user = UserFactory.build() + user_resource = UserResourceFactory.build() + user_task = UserTaskFactory.build() + user_topic = UserTopicFactory.build() + + assert str(support_entity_type) == support_entity_type.name + assert str(support) == str(support.id) + assert str(user) == user.username + assert str(user_resource) == str(user_resource.id) + assert str(user_task) == str(user_task.id) + assert str(user_topic) == str(user_topic.id) diff --git a/backend/pyproject.toml b/backend/pyproject.toml index fec91735a..8e76c7517 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -47,7 +47,8 @@ line_length = 88 [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "backend.settings" python_files = "tests.py test_*.py *_tests.py" -addopts = "-vv --nomigrations --cov=. --cov-report=html --cov-report=term" +addopts = "--nomigrations --cov=. --cov-report=html --cov-report=term" + [tool.coverage.run] omit = [ From 5f41ad1efea9560d6fbdb52173e9bf388ba081fa Mon Sep 17 00:00:00 2001 From: IsaacDMz Date: Sun, 12 Nov 2023 19:43:11 -0300 Subject: [PATCH 2/4] fix(settings): add default order verification for unordered queries --- backend/backend/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/backend/settings.py b/backend/backend/settings.py index a68bf93c3..691309c5a 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -162,6 +162,7 @@ "DEFAULT_THROTTLE_RATES": {"anon": "7/min", "user": "10/min"}, "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination", + 'DEFAULT_PAGINATION_ORDERS_OBJECTS': False, "PAGE_SIZE": 20, } From 73415ca3c118f41bca209eff4947f25f28403951 Mon Sep 17 00:00:00 2001 From: IsaacDMz Date: Sun, 12 Nov 2023 19:50:08 -0300 Subject: [PATCH 3/4] fix(authentication): remove comments on code --- backend/authentication/factories.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/authentication/factories.py b/backend/authentication/factories.py index 67f613322..0ce8b917f 100644 --- a/backend/authentication/factories.py +++ b/backend/authentication/factories.py @@ -1,4 +1,3 @@ -# authentication/factories.py import factory from .models import SupportEntityType, Support, User, UserResource, UserTask, UserTopic From 6962b2ac520609a43957a01855989b00122aafa6 Mon Sep 17 00:00:00 2001 From: tosta Date: Wed, 15 Nov 2023 22:46:11 +0100 Subject: [PATCH 4/4] minor adjustments --- backend/authentication/factories.py | 59 +++++++++++++++++---------- backend/authentication/tests.py | 11 +++-- backend/content/factories.py | 62 +++++++++++++++++++++++++++++ backend/content/tests.py | 2 +- 4 files changed, 106 insertions(+), 28 deletions(-) create mode 100644 backend/content/factories.py diff --git a/backend/authentication/factories.py b/backend/authentication/factories.py index 0ce8b917f..43e920bd7 100644 --- a/backend/authentication/factories.py +++ b/backend/authentication/factories.py @@ -1,57 +1,74 @@ import factory -from .models import SupportEntityType, Support, User, UserResource, UserTask, UserTopic + +from .models import Support, SupportEntityType, User, UserResource, UserTask, UserTopic + class SupportEntityTypeFactory(factory.django.DjangoModelFactory): class Meta: model = SupportEntityType - name = factory.Faker('word') + name = factory.Faker("word") + class SupportFactory(factory.django.DjangoModelFactory): class Meta: model = Support supporter_type = factory.SubFactory(SupportEntityTypeFactory) - supporter_entity = factory.Faker('random_int', min=1, max=100) + supporter_entity = factory.Faker("random_int", min=1, max=100) supported_type = factory.SubFactory(SupportEntityTypeFactory) - supported_entity = factory.Faker('random_int', min=1, max=100) + supported_entity = factory.Faker("random_int", min=1, max=100) + class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User - user_name = factory.Faker('user_name') - name = factory.Faker('name') - password = factory.Faker('password') - location = factory.Faker('city') - description = factory.Faker('text', max_nb_chars=500) - verified = factory.Faker('boolean') - verification_method = factory.Faker('word') - verification_partner = factory.SubFactory("content.UserFactory") - social_accounts = [factory.Faker('user_name') for _ in range(3)] - private = factory.Faker('boolean') - high_risk = factory.Faker('boolean') - total_flags = factory.Faker('random_int', min=0, max=100) - creation_date = factory.Faker('date_time_this_decade', before_now=True) - deletion_date = factory.Faker('date_time_this_decade', before_now=False) + user_name = factory.Faker("user_name") + name = factory.Faker("name") + password = factory.Faker("password") + location = factory.Faker("city") + description = factory.Faker("text", max_nb_chars=500) + verified = factory.Faker("boolean") + verification_method = factory.Faker("word") + social_accounts = factory.List([factory.Faker("user_name") for _ in range(3)]) + private = factory.Faker("boolean") + high_risk = factory.Faker("boolean") + total_flags = factory.Faker("random_int", min=0, max=100) + creation_date = factory.Faker("date_time_this_decade", before_now=True) + deletion_date = factory.Faker("date_time_this_decade", before_now=False) + + # Workaround for the build method + # Does not work with the create method at the moment + # verification_partner field references itself + @factory.post_generation + def verification_partner(self, create, extracted, **kwargs): + if not create: + # Simple build, do nothing. + return + if extracted: + pass + class UserResourceFactory(factory.django.DjangoModelFactory): class Meta: model = UserResource user_id = factory.SubFactory(UserFactory) - resource_id = factory.SubFactory("content.ResourceFactory") + resource_id = factory.SubFactory("content.factories.ResourceFactory") + class UserTaskFactory(factory.django.DjangoModelFactory): class Meta: model = UserTask user_id = factory.SubFactory(UserFactory) - task_id = factory.SubFactory("content.TaskFactory") + task_id = factory.SubFactory("content.factories.TaskFactory") + class UserTopicFactory(factory.django.DjangoModelFactory): class Meta: model = UserTopic user_id = factory.SubFactory(UserFactory) - topic_id = factory.SubFactory("content.TopicFactory") + topic_id = factory.SubFactory("content.factories.TopicFactory") diff --git a/backend/authentication/tests.py b/backend/authentication/tests.py index bada77a28..5710136c6 100644 --- a/backend/authentication/tests.py +++ b/backend/authentication/tests.py @@ -1,7 +1,6 @@ -import pytest from .factories import SupportEntityTypeFactory, SupportFactory, UserFactory, UserResourceFactory, UserTaskFactory, UserTopicFactory -@pytest.mark.django_db + def test_str_methods() -> None: support_entity_type = SupportEntityTypeFactory.build() support = SupportFactory.build() @@ -11,8 +10,8 @@ def test_str_methods() -> None: user_topic = UserTopicFactory.build() assert str(support_entity_type) == support_entity_type.name - assert str(support) == str(support.id) + assert str(support) == str(support.id) assert str(user) == user.username - assert str(user_resource) == str(user_resource.id) - assert str(user_task) == str(user_task.id) - assert str(user_topic) == str(user_topic.id) + assert str(user_resource) == str(user_resource.id) + assert str(user_task) == str(user_task.id) + assert str(user_topic) == str(user_topic.id) diff --git a/backend/content/factories.py b/backend/content/factories.py new file mode 100644 index 000000000..8996e90fe --- /dev/null +++ b/backend/content/factories.py @@ -0,0 +1,62 @@ +import datetime +import random + +import factory + +from .models import Resource, ResourceTopic, Task, Topic + + +class ResourceFactory(factory.django.DjangoModelFactory): + class Meta: + model = Resource + + name = factory.Faker("name") + description = factory.Faker("paragraph") + topics = factory.Faker("words", nb=1) + location = factory.Faker("address") + url = factory.Faker("url") + total_flags = random.randint(1, 100) + private = random.choice([True, False]) + creation_date = factory.LazyFunction(datetime.datetime.now) + deletion_date = factory.LazyFunction(datetime.datetime.now) + + +class TaskFactory(factory.django.DjangoModelFactory): + class Meta: + model = Task + + name = factory.Faker("word") + description = factory.Faker("paragraph") + location = factory.Faker("address") + tags = factory.List([factory.Faker("word") for _ in range(10)]) + creation_date = factory.LazyFunction(datetime.datetime.now) + deletion_date = factory.LazyFunction(datetime.datetime.now) + + +class TopicFactory(factory.django.DjangoModelFactory): + class Meta: + model = Topic + + name = factory.Faker("word") + active = random.choice([True, False]) + description = factory.Faker("paragraph") + creation_date = factory.LazyFunction(datetime.datetime.now) + creation_date = factory.LazyFunction(datetime.datetime.now) + deprecation_date = factory.Faker("date") + + +class ResourceTopicFactory(factory.django.DjangoModelFactory): + class Meta: + model = ResourceTopic + + resource_id = factory.SubFactory(ResourceFactory) + topic_id = factory.SubFactory(TopicFactory) + + +class TopicFormatFactory(factory.django.DjangoModelFactory): + """Placeholder + We do not have Factory for events.Format, therefore can not use a Subfactory yet. + format_id = models.ForeignKey("events.Format", on_delete=models.CASCADE) + """ + + pass diff --git a/backend/content/tests.py b/backend/content/tests.py index 848153b04..6319917f1 100644 --- a/backend/content/tests.py +++ b/backend/content/tests.py @@ -1,4 +1,4 @@ -from .factory import ResourceFactory, TaskFactory, TopicFactory, ResourceTopicFactory +from .factories import ResourceFactory, TaskFactory, TopicFactory, ResourceTopicFactory import pytest