Browse Source

Adding tests.

pull/359/head
Pēteris Caune 5 years ago
parent
commit
f7acaa57af
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 141 additions and 7 deletions
  1. +138
    -0
      hc/accounts/tests/test_transfer_project.py
  2. +0
    -6
      hc/accounts/views.py
  3. +3
    -1
      hc/test.py

+ 138
- 0
hc/accounts/tests/test_transfer_project.py View File

@ -0,0 +1,138 @@
from django.utils.timezone import now
from hc.api.models import Check
from hc.test import BaseTestCase
class ProjectTestCase(BaseTestCase):
def setUp(self):
super(ProjectTestCase, self).setUp()
Check.objects.create(project=self.project)
self.url = "/projects/%s/settings/" % self.project.code
def test_transfer_project_works(self):
self.client.login(username="[email protected]", password="password")
form = {"transfer_project": "1", "email": "[email protected]"}
r = self.client.post(self.url, form)
self.assertContains(r, "Transfer initiated!")
self.bobs_membership.refresh_from_db()
self.assertIsNotNone(self.bobs_membership.transfer_request_date)
def test_transfer_project_checks_ownership(self):
self.client.login(username="[email protected]", password="password")
form = {"transfer_project": "1", "email": "[email protected]"}
r = self.client.post(self.url, form)
self.assertEqual(r.status_code, 403)
def test_cancel_works(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"cancel_transfer": "1"})
self.assertContains(r, "Transfer cancelled!")
self.bobs_membership.refresh_from_db()
self.assertIsNone(self.bobs_membership.transfer_request_date)
def test_cancel_checks_ownership(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"cancel_transfer": "1"})
self.assertEqual(r.status_code, 403)
self.bobs_membership.refresh_from_db()
self.assertIsNotNone(self.bobs_membership.transfer_request_date)
def test_it_shows_transfer_request(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "would like to transfer")
self.assertNotContains(r, "upgrade your account first")
def test_it_shows_transfer_request_with_limit_notice(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.bobs_profile.check_limit = 0
self.bobs_profile.save()
self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url)
self.assertContains(r, "upgrade your account first")
def test_accept_works(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"accept_transfer": "1"})
self.assertContains(r, "You are now the owner of this project!")
self.project.refresh_from_db()
# Bob should now be the owner
self.assertEqual(self.project.owner, self.bob)
# Alice, the previous owner, should now be a member
self.assertTrue(self.project.team().filter(email="[email protected]").exists())
def test_accept_requires_a_transfer_request(self):
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"accept_transfer": "1"})
self.assertEqual(r.status_code, 403)
self.project.refresh_from_db()
# Alice should still be the owner
self.assertEqual(self.project.owner, self.alice)
def test_only_the_proposed_owner_can_accept(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"accept_transfer": "1"})
self.assertEqual(r.status_code, 403)
def test_it_checks_limits(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.bobs_profile.check_limit = 0
self.bobs_profile.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"accept_transfer": "1"})
self.assertEqual(r.status_code, 400)
def test_reject_works(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"reject_transfer": "1"})
self.assertEqual(r.status_code, 200)
self.project.refresh_from_db()
# Alice should still be the owner
self.assertEqual(self.project.owner, self.alice)
# The transfer_request_date should be cleared out
self.bobs_membership.refresh_from_db()
self.assertIsNone(self.bobs_membership.transfer_request_date)
def test_only_the_proposed_owner_can_reject(self):
self.bobs_membership.transfer_request_date = now()
self.bobs_membership.save()
self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, {"reject_transfer": "1"})
self.assertEqual(r.status_code, 403)

+ 0
- 6
hc/accounts/views.py View File

@ -372,9 +372,6 @@ def project(request, code):
ctx["transfer_status"] = "success"
elif "accept_transfer" in request.POST:
if not project.transfer_request:
return HttpResponseForbidden()
tr = project.transfer_request()
if not tr or tr.user != request.user:
return HttpResponseForbidden()
@ -396,9 +393,6 @@ def project(request, code):
messages.success(request, "You are now the owner of this project!")
elif "reject_transfer" in request.POST:
if not project.transfer_request:
return HttpResponseForbidden()
tr = project.transfer_request()
if not tr or tr.user != request.user:
return HttpResponseForbidden()


+ 3
- 1
hc/test.py View File

@ -34,7 +34,9 @@ class BaseTestCase(TestCase):
self.bobs_profile = Profile(user=self.bob)
self.bobs_profile.save()
Member.objects.create(user=self.bob, project=self.project)
self.bobs_membership = Member.objects.create(
user=self.bob, project=self.project
)
# Charlie should have no access to Alice's stuff
self.charlie = User(username="charlie", email="[email protected]")


Loading…
Cancel
Save