|
| 1 | +from django.contrib.auth.models import User |
| 2 | +from django.test import TestCase, override_settings |
| 3 | +from django.urls import reverse |
| 4 | +from django_dynamic_fixture import get |
| 5 | + |
| 6 | +from readthedocs.organizations.models import Organization |
| 7 | +from readthedocs.projects.models import Domain, Project |
| 8 | + |
| 9 | + |
| 10 | +@override_settings(RTD_ALLOW_ORGANIZATIONS=False) |
| 11 | +class TestDomainViews(TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.user = get(User, username="user") |
| 14 | + self.project = get(Project, users=[self.user], slug="project") |
| 15 | + self.subproject = get(Project, users=[self.user], slug="subproject") |
| 16 | + self.project.add_subproject(self.subproject) |
| 17 | + self.client.force_login(self.user) |
| 18 | + |
| 19 | + def test_domain_creation(self): |
| 20 | + self.assertEqual(self.project.domains.count(), 0) |
| 21 | + |
| 22 | + resp = self.client.post( |
| 23 | + reverse("projects_domains_create", args=[self.project.slug]), |
| 24 | + data={"domain": "test.example.com"}, |
| 25 | + ) |
| 26 | + self.assertEqual(resp.status_code, 302) |
| 27 | + self.assertEqual(self.project.domains.count(), 1) |
| 28 | + |
| 29 | + domain = self.project.domains.first() |
| 30 | + self.assertEqual(domain.domain, "test.example.com") |
| 31 | + |
| 32 | + def test_domain_deletion(self): |
| 33 | + domain = get(Domain, project=self.project, domain="test.example.com") |
| 34 | + self.assertEqual(self.project.domains.count(), 1) |
| 35 | + |
| 36 | + resp = self.client.post( |
| 37 | + reverse("projects_domains_delete", args=[self.project.slug, domain.pk]), |
| 38 | + ) |
| 39 | + self.assertEqual(resp.status_code, 302) |
| 40 | + self.assertEqual(self.project.domains.count(), 0) |
| 41 | + |
| 42 | + def test_domain_edit(self): |
| 43 | + domain = get( |
| 44 | + Domain, project=self.project, domain="test.example.com", canonical=False |
| 45 | + ) |
| 46 | + |
| 47 | + self.assertEqual(domain.canonical, False) |
| 48 | + resp = self.client.post( |
| 49 | + reverse("projects_domains_edit", args=[self.project.slug, domain.pk]), |
| 50 | + data={"canonical": True}, |
| 51 | + ) |
| 52 | + self.assertEqual(resp.status_code, 302) |
| 53 | + self.assertEqual(self.project.domains.count(), 1) |
| 54 | + |
| 55 | + domain = self.project.domains.first() |
| 56 | + self.assertEqual(domain.domain, "test.example.com") |
| 57 | + self.assertEqual(domain.canonical, True) |
| 58 | + |
| 59 | + def test_adding_domain_on_subproject(self): |
| 60 | + self.assertEqual(self.subproject.domains.count(), 0) |
| 61 | + |
| 62 | + resp = self.client.post( |
| 63 | + reverse("projects_domains_create", args=[self.subproject.slug]), |
| 64 | + data={"domain": "test.example.com"}, |
| 65 | + ) |
| 66 | + self.assertEqual(resp.status_code, 401) |
| 67 | + self.assertEqual(self.subproject.domains.count(), 0) |
| 68 | + |
| 69 | + def test_delete_domain_on_subproject(self): |
| 70 | + domain = get(Domain, project=self.subproject, domain="test.example.com") |
| 71 | + self.assertEqual(self.subproject.domains.count(), 1) |
| 72 | + |
| 73 | + resp = self.client.post( |
| 74 | + reverse("projects_domains_delete", args=[self.subproject.slug, domain.pk]), |
| 75 | + ) |
| 76 | + self.assertEqual(resp.status_code, 302) |
| 77 | + self.assertEqual(self.subproject.domains.count(), 0) |
| 78 | + |
| 79 | + def test_edit_domain_on_subproject(self): |
| 80 | + domain = get( |
| 81 | + Domain, project=self.subproject, domain="test.example.com", canonical=False |
| 82 | + ) |
| 83 | + |
| 84 | + self.assertEqual(domain.canonical, False) |
| 85 | + resp = self.client.post( |
| 86 | + reverse("projects_domains_edit", args=[self.subproject.slug, domain.pk]), |
| 87 | + data={"canonical": True}, |
| 88 | + ) |
| 89 | + self.assertEqual(resp.status_code, 401) |
| 90 | + self.assertEqual(self.subproject.domains.count(), 1) |
| 91 | + |
| 92 | + domain = self.subproject.domains.first() |
| 93 | + self.assertEqual(domain.domain, "test.example.com") |
| 94 | + self.assertEqual(domain.canonical, False) |
| 95 | + |
| 96 | + |
| 97 | +@override_settings(RTD_ALLOW_ORGANIZATIONS=True) |
| 98 | +class TestDomainViewsWithOrganizations(TestDomainViews): |
| 99 | + def setUp(self): |
| 100 | + super().setUp() |
| 101 | + self.org = get( |
| 102 | + Organization, owners=[self.user], projects=[self.project, self.subproject] |
| 103 | + ) |
0 commit comments