|
1 |
| -# -*- coding: utf-8 -*- |
2 |
| -import django_dynamic_fixture |
3 | 1 | import pytest
|
4 |
| - |
5 | 2 | from django.contrib.auth.models import User
|
| 3 | +from django_dynamic_fixture import get |
6 | 4 |
|
| 5 | +from readthedocs.builds.models import Version |
| 6 | +from readthedocs.organizations.models import Organization, Team, TeamInvite, TeamMember |
7 | 7 | from readthedocs.projects.models import Project
|
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.django_db
|
11 | 11 | class TestProjectOrganizationSignal:
|
12 | 12 |
|
13 |
| - def test_project_get_deleted_upon_user_delete(self): |
14 |
| - """If the user has Project where he is the only |
15 |
| - user, upon deleting his account, the Project |
16 |
| - should also get deleted.""" |
17 |
| - |
18 |
| - project = django_dynamic_fixture.get(Project) |
19 |
| - user1 = django_dynamic_fixture.get(User) |
20 |
| - project.users.add(user1) |
21 |
| - |
22 |
| - project.refresh_from_db() |
23 |
| - assert project.users.all().count() == 1 |
24 |
| - |
25 |
| - # Delete the user |
26 |
| - user1.delete() |
27 |
| - # The object should not exist |
28 |
| - project = Project.objects.all().filter(id=project.id) |
29 |
| - assert not project.exists() |
30 |
| - |
31 |
| - def test_multiple_users_project_not_delete(self): |
32 |
| - """Check Project which have multiple users do not |
33 |
| - get deleted when any of the user delete his account.""" |
34 |
| - |
35 |
| - project = django_dynamic_fixture.get(Project) |
36 |
| - user1 = django_dynamic_fixture.get(User) |
37 |
| - user2 = django_dynamic_fixture.get(User) |
38 |
| - project.users.add(user1, user2) |
39 |
| - |
40 |
| - project.refresh_from_db() |
41 |
| - assert project.users.all().count() > 1 |
42 |
| - # Delete 1 user of the project |
43 |
| - user1.delete() |
44 |
| - |
45 |
| - # The project should still exist and it should have 1 user |
46 |
| - project.refresh_from_db() |
47 |
| - assert project.id |
48 |
| - obj_users = project.users.all() |
49 |
| - assert len(obj_users) == 1 |
50 |
| - assert user2 in obj_users |
| 13 | + def test_delete_user_deletes_projects(self): |
| 14 | + """When deleting a user, delete projects where it's the only owner.""" |
| 15 | + user = get(User) |
| 16 | + another_user = get(User) |
| 17 | + project_one = get(Project, slug="one", users=[user]) |
| 18 | + project_two = get(Project, slug="two", users=[user]) |
| 19 | + project_three = get(Project, slug="three", users=[another_user]) |
| 20 | + project_four = get(Project, slug="four", users=[user, another_user]) |
| 21 | + |
| 22 | + project_five = get( |
| 23 | + Project, |
| 24 | + slug="five", |
| 25 | + users=[], |
| 26 | + ) |
| 27 | + assert Project.objects.all().count() == 5 |
| 28 | + assert Version.objects.all().count() == 5 |
| 29 | + |
| 30 | + user.delete() |
| 31 | + assert {project_three, project_four, project_five} == set(Project.objects.all()) |
| 32 | + assert Version.objects.all().count() == 3 |
| 33 | + |
| 34 | + def test_delete_user_deletes_organizations(self): |
| 35 | + """When deleting a user, delete organizations where it's the only owner.""" |
| 36 | + user = get(User) |
| 37 | + member = get(User) |
| 38 | + another_user = get(User) |
| 39 | + project_one = get(Project, slug="one") |
| 40 | + project_two = get(Project, slug="two") |
| 41 | + project_three = get(Project, slug="three") |
| 42 | + org_one = get(Organization, slug="one", owners=[user], projects=[project_one]) |
| 43 | + org_two = get( |
| 44 | + Organization, |
| 45 | + slug="two", |
| 46 | + owners=[user, another_user], |
| 47 | + projects=[project_two], |
| 48 | + ) |
| 49 | + org_three = get( |
| 50 | + Organization, slug="three", owners=[another_user], projects=[project_three] |
| 51 | + ) |
| 52 | + |
| 53 | + team_one = get( |
| 54 | + Team, organization=org_one, members=[member, user], projects=[project_one] |
| 55 | + ) |
| 56 | + team_two = get( |
| 57 | + Team, |
| 58 | + organization=org_three, |
| 59 | + members=[another_user], |
| 60 | + projects=[project_three], |
| 61 | + ) |
| 62 | + |
| 63 | + get(TeamInvite, team=team_one, organization=org_one) |
| 64 | + |
| 65 | + |
| 66 | + assert Organization.objects.all().count() == 3 |
| 67 | + assert Project.objects.all().count() == 3 |
| 68 | + assert Version.objects.all().count() == 3 |
| 69 | + assert Team.objects.all().count() == 2 |
| 70 | + assert TeamInvite.objects.all().count() == 1 |
| 71 | + assert TeamMember.objects.all().count() == 3 |
| 72 | + assert User.objects.all().count() == 3 |
| 73 | + |
| 74 | + user.delete() |
| 75 | + |
| 76 | + assert {org_two, org_three} == set(Organization.objects.all()) |
| 77 | + assert {project_two, project_three} == set(Project.objects.all()) |
| 78 | + assert Version.objects.all().count() == 2 |
| 79 | + assert {team_two} == set(Team.objects.all()) |
| 80 | + assert TeamInvite.objects.all().count() == 0 |
| 81 | + assert TeamMember.objects.all().count() == 1 |
| 82 | + assert User.objects.all().count() == 2 |
0 commit comments