Skip to content

Commit

Permalink
Merge pull request #2257 from uktrade/set-unique-email-for-user
Browse files Browse the repository at this point in the history
Set unique email address for base user
  • Loading branch information
kevincarrogan authored Oct 28, 2024
2 parents 23cf8aa + b9e9859 commit c3e9f79
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/organisations/caseworker/views/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUp(self):
)
self.data = {
"role": Roles.EXPORTER_ADMINISTRATOR_ROLE_ID,
"email": self.faker.email(),
"email": self.faker.unique.email(),
"sites": [self.organisation.primary_site.id],
"phone_number": "+441234567895",
}
Expand Down
6 changes: 2 additions & 4 deletions api/users/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import factory

from faker import Faker


from api.organisations.tests.factories import OrganisationFactory
from api.users import models
from api.users.enums import UserType, UserStatuses
from api.users.models import Role, UserOrganisationRelationship
from api.teams.tests.factories import TeamFactory

faker = Faker()
from test_helpers.faker import faker


class BaseUserFactory(factory.django.DjangoModelFactory):
first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
email = factory.LazyAttribute(lambda n: faker.email())
email = factory.LazyAttribute(lambda n: faker.unique.email())

class Meta:
model = models.BaseUser
Expand Down
7 changes: 1 addition & 6 deletions test_helpers/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import django.utils.timezone
from django.db import connection
from django.test import override_settings
from faker import Faker
from rest_framework.test import APITestCase, URLPatternsTestCase, APIClient
import pytest

Expand Down Expand Up @@ -84,6 +83,7 @@
from api.teams.models import Team
from api.users.tests.factories import GovUserFactory
from test_helpers import colours
from test_helpers.faker import faker
from api.users.enums import SystemUser, UserType
from api.users.libraries.user_to_token import user_to_token
from api.users.models import ExporterUser, UserOrganisationRelationship, BaseUser, GovUser, Role
Expand All @@ -96,11 +96,6 @@ class Static:
seeded = False


# Instantiating this once so that we have a single instance across all tests allowing us to use things like .unique
# and we can guarantee that we will always have unique values even if we use things like `setUpClass`.
faker = Faker()


class DataTestClient(APITestCase, URLPatternsTestCase):
"""
Test client which creates seeds the database with system data and sets up an initial organisation and user
Expand Down
6 changes: 6 additions & 0 deletions test_helpers/faker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from faker import Faker


# Instantiating this once so that we have a single instance across all tests allowing us to use things like .unique
# and we can guarantee that we will always have unique values even if we use things like `setUpClass`.
faker = Faker()
8 changes: 3 additions & 5 deletions test_helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

from importlib import import_module, reload

from faker import Faker

from django.conf import settings
from django.urls import clear_url_caches

from test_helpers.faker import faker

from api.core.constants import Roles
from api.flags.enums import SystemFlags
from api.staticdata.countries.models import Country
from api.users.models import ExporterUser, UserOrganisationRelationship

faker = Faker()


def generate_key_value_pair(key, choices):
"""
Expand All @@ -37,7 +35,7 @@ def create_exporter_users(organisation, quantity=1, role_id=Roles.EXPORTER_EXPOR
users = []

for i in range(quantity):
user, created = ExporterUser.objects.get_or_create(email=faker.email())
user, created = ExporterUser.objects.get_or_create(email=faker.unique.email())
if created:
user.first_name = faker.first_name()
user.last_name = faker.last_name()
Expand Down

0 comments on commit c3e9f79

Please sign in to comment.