Skip to content

Commit

Permalink
Merge a6b0975 into 8279464
Browse files Browse the repository at this point in the history
  • Loading branch information
bmulobi committed Mar 24, 2017
2 parents 8279464 + a6b0975 commit 800cb3f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
19 changes: 17 additions & 2 deletions hc/accounts/tests/test_check_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from hc.test import BaseTestCase


class CheckTokenTestCase(BaseTestCase):
class CheckTokenTestCase(BaseTestCase):

def setUp(self):
super(CheckTokenTestCase, self).setUp()
Expand All @@ -15,14 +15,29 @@ def test_it_shows_form(self):

def test_it_redirects(self):
r = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertEqual(r.status_code, 302)
# self.assertIn(self.profile.user, self.client.session)
self.assertRedirects(r, "/checks/")

# After login, token should be blank
# After login, token should be blank
self.profile.refresh_from_db()
self.assertEqual(self.profile.token, "")

### Login and test it redirects already logged in
def test_redirects_already_logged_in(self):
self.client.login(username="ben@example.org", password="password")

form = {"email": "ben@example.org"}
result = self.client.post("/accounts/profile/", form)
assert result.status_code == 302
self.assertRedirects(result, '/accounts/login/?next=/accounts/profile/')



### Login with a bad token and check that it redirects
def test_redirects_login_with_bad_token(self):
r = self.client.post("/accounts/check_token/alice/bad-token/")
self.assertRedirects(r, "/accounts/login/")


### Any other tests?
12 changes: 11 additions & 1 deletion hc/accounts/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from hc.accounts.models import Member
from hc.api.models import Check


class LoginTestCase(TestCase):
class LoginTestCase(TestCase):

def test_it_sends_link(self):
check = Check()
Expand All @@ -21,12 +22,21 @@ def test_it_sends_link(self):

### Assert that a user was created

user_check = User.objects.filter(email="alice@example.org")
self.assertEqual(user_check[0].email, form["email"])

# And email sent
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Log in to healthchecks.io')

### Assert contents of the email body
self.assertIn('Hello,\n\nTo log into healthchecks.io, please open the link below:', mail.outbox[0].body)

### Assert that check is associated with the new user
user = User.objects.get(email="alice@example.org")

test_user = Check.objects.get(user=user)
self.assertEqual(test_user.user, user)

def test_it_pops_bad_link_from_session(self):
self.client.session["bad_link"] = True
Expand Down
29 changes: 25 additions & 4 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core import mail
from django.test import tag

from hc.test import BaseTestCase
from hc.accounts.models import Member
Expand All @@ -14,12 +15,17 @@ def test_it_sends_set_password_link(self):
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 302

# profile.token should be set now
# profile.token should be set now
self.alice.profile.refresh_from_db()
token = self.alice.profile.token

### Assert that the token is set
self.assertNotEqual(token, "")

### Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Set password on healthchecks.io')
self.assertIn("Hello,\n\nHere's a link to set a password", mail.outbox[0].body)

def test_it_sends_report(self):
check = Check(name="Test Check", user=self.alice)
Expand All @@ -28,11 +34,15 @@ def test_it_sends_report(self):
self.alice.profile.send_report()

###Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Monthly Report')
self.assertTrue("Hello,\n\nThis is a monthly report sent by health" in mail.outbox[0].body)

@tag('add_member')
def test_it_adds_team_member(self):
self.client.login(username="alice@example.org", password="password")

form = {"invite_team_member": "1", "email": "frank@example.org"}
form = {"invite_team_member": "1", "email": "ben@example.org"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 200

Expand All @@ -42,9 +52,16 @@ def test_it_adds_team_member(self):

### Assert the existence of the member emails

self.assertTrue("frank@example.org" in member_emails)
self.assertTrue("ben@example.org" in member_emails)

###Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject,
'You have been invited to join alice@example.org on healthchecks.io')

self.assertIn('Hello,\n\nalice@example.org invites you to their healthchecks.io account.'
'\n\nYou will be able to manage their existing monitoring checks and set up new\nones.'
, mail.outbox[0].body)

def test_add_team_member_checks_team_access_allowed_flag(self):
self.client.login(username="charlie@example.org", password="password")
Expand Down Expand Up @@ -80,7 +97,7 @@ def test_set_team_name_checks_team_access_allowed_flag(self):

form = {"set_team_name": "1", "team_name": "Charlies Team"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 403
assert r.status_code == 403

def test_it_switches_to_own_team(self):
self.client.login(username="bob@example.org", password="password")
Expand Down Expand Up @@ -108,3 +125,7 @@ def test_it_shows_badges(self):
self.assertNotContains(r, "bobs-tag.svg")

### Test it creates and revokes API key
def test_it_creates_and_revokes_api_key(self):
initial_key = self.profile.api_key
self.assertNotEqual(initial_key, self.profile.set_api_key(),
msg="Should create new key different from initial_key")
5 changes: 5 additions & 0 deletions hc/accounts/tests/test_switch_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ def test_it_switches(self):

### Assert the contents of r

self.assertEqual(200, r.status_code)
self.assertContains(r, 'bob')


def test_it_checks_team_membership(self):
self.client.login(username="charlie@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url)
### Assert the expected error code
assert r.status_code == 403

def test_it_switches_to_own_team(self):
self.client.login(username="alice@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
### Assert the expected error code
assert r.status_code == 200
2 changes: 2 additions & 0 deletions hc/accounts/tests/test_team_access_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def test_it_handles_missing_profile(self):
self.assertEqual(r.status_code, 200)

### Assert the new Profile objects count
test_profile = User.objects.filter(email='ned@example.org').count()
self.assertEqual(test_profile, 1)

0 comments on commit 800cb3f

Please sign in to comment.