diff --git a/tasks/migrations/0002_initial.py b/tasks/migrations/0002_initial.py index 01a67c4..68b65cb 100644 --- a/tasks/migrations/0002_initial.py +++ b/tasks/migrations/0002_initial.py @@ -27,14 +27,14 @@ class Migration(migrations.Migration): ), migrations.AddField( model_name='comment', - name='task_id', + name='task', field=models.ForeignKey( - on_delete=django.db.models.deletion.CASCADE, to='tasks.task'), + on_delete=django.db.models.deletion.CASCADE, to='tasks.task', related_name='task'), ), migrations.AddField( model_name='comment', - name='user_id', - field=models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='comments', + name='appUser', + field=models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='appUser', to='users.user'), ), ] diff --git a/tasks/models.py b/tasks/models.py index 492dafc..23a4461 100644 --- a/tasks/models.py +++ b/tasks/models.py @@ -19,8 +19,10 @@ class Priority(ChoiceEnum): class Task(models.Model): title = models.CharField(max_length=200) created_date = models.DateTimeField(auto_now_add=True) - assignee = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='assigneeTasks') - created_by = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='createdTasks') + assignee = models.ForeignKey( + User, on_delete=models.RESTRICT, related_name='assigneeTasks') + created_by = models.ForeignKey( + User, on_delete=models.RESTRICT, related_name='createdTasks') priority = EnumChoiceField(Priority, default=Priority.LOW, max_length=1) status = EnumChoiceField(Status, default=Status.BACKLOG, max_length=1) description = models.TextField(null=True, blank=True) @@ -30,8 +32,10 @@ def __str__(self) -> str: class Comment(models.Model): - user_id = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='comments') - task_id = models.ForeignKey(Task, on_delete=models.CASCADE) + appUser = models.ForeignKey( + User, on_delete=models.RESTRICT, related_name='appUser') + task = models.ForeignKey( + Task, on_delete=models.CASCADE, related_name='task') title = models.CharField(max_length=200) created_date = models.DateTimeField(auto_now_add=True) description = models.TextField(null=True, blank=True) diff --git a/tests.py b/tests.py deleted file mode 100644 index 0c4bd9c..0000000 --- a/tests.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_dummy(): - assert 1 == 1 diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py index e849c8f..8ac29d2 100644 --- a/users/migrations/0001_initial.py +++ b/users/migrations/0001_initial.py @@ -27,13 +27,10 @@ class Migration(migrations.Migration): ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='auth.user')), - ('email', models.EmailField(max_length=255)), - ('first_name', models.CharField(max_length=255)), - ('last_name', models.CharField(max_length=255)), ('role', enumchoicefield.fields.EnumChoiceField(default=users.models.Role.EMPLOYEE, enum_class=users.models.Role, max_length=1)), - ('team_id', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='users', - to='users.team')), + ('team', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='team', + to='users.team')), ], ), ] diff --git a/users/models.py b/users/models.py index a1bc204..a2f54e4 100644 --- a/users/models.py +++ b/users/models.py @@ -1,6 +1,6 @@ from django.db import models from enumchoicefield import ChoiceEnum, EnumChoiceField -from django.conf import settings +from django.contrib.auth.models import User as DjangoUser # Enum @@ -20,13 +20,28 @@ def __str__(self) -> str: class User(models.Model): user = models.OneToOneField( - settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True) - email = models.EmailField(max_length=255) - first_name = models.CharField(max_length=255) - last_name = models.CharField(max_length=255) + DjangoUser, on_delete=models.CASCADE, primary_key=True) role = EnumChoiceField(Role, default=Role.EMPLOYEE, max_length=1) - team_id = models.ForeignKey( - Team, on_delete=models.RESTRICT, related_name='users') + team = models.ForeignKey( + Team, on_delete=models.RESTRICT, related_name='team') + + @staticmethod + def create_user(username, email, password, first_name, last_name, role, team): + django_user = DjangoUser.objects.create_user(username=username, + email=email, + password=password, + first_name=first_name, + last_name=last_name) + if(isinstance(django_user, DjangoUser)): + user = User.objects.create(user=django_user, + role=role, + team=team) + return user + else: + raise Exception("Error creating user") + + def delete(self, *args, **kwargs): + self.user.delete() def __str__(self) -> str: - return self.first_name + return self.user.username diff --git a/users/tests.py b/users/tests.py index a79ca8b..58cfd11 100644 --- a/users/tests.py +++ b/users/tests.py @@ -1,3 +1,80 @@ -# from django.test import TestCase +import pytest +from users.models import Role, User, Team +from django.contrib.auth.models import User as DjangoUser -# Create your tests here. + +@pytest.mark.django_db +class TestTeams: + + @pytest.fixture + def valid_teams(self): + team_1 = Team.objects.create( + name="Team1", description="Best team ever") + team_2 = Team.objects.create( + name="Team2", description="Best team ever") + team_3 = Team.objects.create( + name="Team3", description="Best team ever") + return team_1, team_2, team_3 + + def test_create_teams(self, valid_teams): + for team in valid_teams: + assert isinstance(team, Team) + assert Team.objects.filter(id=team.id).exists() + + def test_create_team_without_title(self): + with pytest.raises(Exception): + Team.objects.create('', "Best team ever") + + +@pytest.mark.django_db +class TestUsers: + + @pytest.fixture + def team_1(self): + return Team.objects.create(name="Team1", description="Best team ever") + + @pytest.fixture + def employee_1(self, team_1): + user1 = User.create_user( + username="user1", + email="user1@redhat.com", + password="password", + first_name="first_name", + last_name="last_name", + role=Role.EMPLOYEE, + team=team_1) + return user1 + + def test_create_user(self, employee_1): + assert isinstance(employee_1, User) + assert User.objects.filter(user=employee_1.user).exists() + + def test_delete_user(self, employee_1): + userId = employee_1.user.id + employee_1.delete() + assert not User.objects.filter(user=userId).exists() + assert not DjangoUser.objects.filter(pk=userId).exists() + + def test_create_user_without_email(self): + with pytest.raises(Exception): + User.create_user( + username="user1", + password="password", + first_name="first_name", + last_name="last_name", + role=Role.EMPLOYEE, + team=TestTeams.valid_team) + + def test_create_user_with_long_name(self): + longName = """ this is a very + long string if I had the + energy to type more and more ...""" + with pytest.raises(Exception): + User.create_user( + username="user1", + email="user1@redhat.com", + password="password", + first_name=longName, + last_name="last_name", + role=Role.EMPLOYEE, + team=TestTeams.valid_team)