Skip to content

Commit

Permalink
Merge 644d3a3 into 5a9fc32
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoti committed Dec 14, 2016
2 parents 5a9fc32 + 644d3a3 commit f187d1b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
16 changes: 12 additions & 4 deletions hc/accounts/tests/test_check_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ def setUp(self):
self.profile.save()

def test_it_shows_form(self):
r = self.client.get("/accounts/check_token/alice/secret-token/")
self.assertContains(r, "You are about to log in")
response = self.client.get("/accounts/check_token/alice/secret-token/")
self.assertContains(response, "You are about to log in")

def test_it_redirects(self):
r = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertRedirects(r, "/checks/")
response = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertRedirects(response, "/checks/")

# 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_it_redirects_if_already_logged_in(self):
self.client.login(username="alice@example.org", password="password")
response = self.client.get("/accounts/check_token/alice/secret-token/")
assert response.status_code == 302
self.assertRedirects(response, "/checks/")

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

### Any other tests?
7 changes: 6 additions & 1 deletion hc/accounts/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ def test_it_sends_link(self):
assert r.status_code == 302

### Assert that a user was created
self.test_user = User.objects.get()
self.assertEqual(self.test_user.email, "alice@example.org")

# 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("To log into healthchecks.io, please open the link below",
mail.outbox[0].body)

### Assert that check is associated with the new user
self.test_check = Check.objects.get(user=self.test_user.id)
self.assertEqual(self.test_user.id, self.test_check.user_id)

def test_it_pops_bad_link_from_session(self):
self.client.session["bad_link"] = True
self.client.get("/accounts/login/")
assert "bad_link" not in self.client.session

### Any other tests?

68 changes: 50 additions & 18 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ def test_it_sends_set_password_link(self):
self.client.login(username="alice@example.org", password="password")

form = {"set_password": "1"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 302
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 302

# profile.token should be set now
self.alice.profile.refresh_from_db()
token = self.alice.profile.token
### Assert that the token is set
self.assertTrue(len(token)>10)

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

def test_it_sends_report(self):
check = Check(name="Test Check", user=self.alice)
Expand All @@ -28,37 +34,46 @@ 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("Monthly Report", mail.outbox[0].subject)
self.assertIn("This is a monthly report sent by healthchecks.io.",
mail.outbox[0].body)

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"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 200
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 200

member_emails = set()
for member in self.alice.profile.member_set.all():
member_emails.add(member.user.email)

### Assert the existence of the member emails
self.assertEqual(len(member_emails), 2) #Alice included as member

self.assertTrue("frank@example.org" in member_emails)
self.assertTrue(form["email"] in member_emails)

###Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
self.assertIn("You have been invited to join", mail.outbox[0].subject)
self.assertIn("invites you to their healthchecks.io account",
mail.outbox[0].body)

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

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

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

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

self.assertEqual(Member.objects.count(), 0)

Expand All @@ -69,8 +84,8 @@ def test_it_sets_team_name(self):
self.client.login(username="alice@example.org", password="password")

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

self.alice.profile.refresh_from_db()
self.assertEqual(self.alice.profile.team_name, "Alpha Team")
Expand All @@ -79,8 +94,8 @@ def test_set_team_name_checks_team_access_allowed_flag(self):
self.client.login(username="charlie@example.org", password="password")

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

def test_it_switches_to_own_team(self):
self.client.login(username="bob@example.org", password="password")
Expand All @@ -97,14 +112,31 @@ def test_it_shows_badges(self):
Check.objects.create(user=self.alice, tags="foo a-B_1 baz@")
Check.objects.create(user=self.bob, tags="bobs-tag")

r = self.client.get("/accounts/profile/")
self.assertContains(r, "foo.svg")
self.assertContains(r, "a-B_1.svg")
response = self.client.get("/accounts/profile/")
self.assertContains(response, "foo.svg")
self.assertContains(response, "a-B_1.svg")

# Expect badge URLs only for tags that match \w+
self.assertNotContains(r, "baz@.svg")
self.assertNotContains(response, "baz@.svg")

# Expect only Alice's tags
self.assertNotContains(r, "bobs-tag.svg")
self.assertNotContains(response, "bobs-tag.svg")

### Test it creates and revokes API key
def test_it_creates_and_revokes_api_key(self):
self.client.login(username="alice@example.org", password="password")
form = {"create_api_key":"1"}
response = self.client.post("/accounts/profile/", form)

assert response.status_code==200
self.assertIsNotNone(self.alice.profile.api_key)
self.assertContains(response, "The API key has been created!")


form_revoke = {"revoke_api_key":"1"}
response = self.client.post("/accounts/profile/", form_revoke)

assert response.status_code==200
self.alice.profile.refresh_from_db()
self.assertEqual(self.alice.profile.api_key, "")
self.assertContains(response, "The API key has been revoked!")
12 changes: 8 additions & 4 deletions hc/accounts/tests/test_switch_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ def test_it_switches(self):
self.client.login(username="bob@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
response = self.client.get(url, follow=True)

### Assert the contents of r
### Assert the contents of response
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "front/my_checks.html")


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)
response = self.client.get(url)
### Assert the expected error code
assert response.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)
response = self.client.get(url, follow=True)
### Assert the expected error code
assert response.status_code == 200
7 changes: 5 additions & 2 deletions hc/accounts/tests/test_team_access_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def test_it_handles_missing_profile(self):
user = User(username="ned", email="ned@example.org")
user.set_password("password")
user.save()
#returns all Profile Objects created
self.new_number = Profile.objects.filter(user_id__gte = 0)

self.client.login(username="ned@example.org", password="password")
r = self.client.get("/about/")
self.assertEqual(r.status_code, 200)
response = self.client.get("/about/")
self.assertEqual(response.status_code, 200)

### Assert the new Profile objects count
self.assertEqual(len(self.new_number), 1)

0 comments on commit f187d1b

Please sign in to comment.