Skip to content

Commit

Permalink
Merge pull request #8 from andela/ch-fix-accounts-tests-#156687691
Browse files Browse the repository at this point in the history
#156687691 Chore fix accounts tests
  • Loading branch information
jimmykamau committed May 2, 2018
2 parents a6385c4 + 526bce8 commit e5e0df2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
17 changes: 15 additions & 2 deletions hc/accounts/tests/test_check_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ def test_it_redirects(self):
self.assertEqual(self.profile.token, "")

### Login and test it redirects already logged in
def test_it_redirects_already_logged_in(self):
# Login
self.client.login(username="alice@example.org", password="password")
# Login again, check it redirects
url = "/accounts/check_token/alice/secret-token/"
r = self.client.post(url)
self.assertRedirects(r, "/checks/")

### Login with a bad token and check that it redirects

### Any other tests?
def test_it_redirects_after_login_with_a_bad_token(self):
# Login with a bad token
url = "/accounts/check_token/alice/invalid-token/"
r = self.client.post(url, follow=True)
self.assertRedirects(r, "/accounts/login/")
self.assertContains(r, "incorrect or expired")

### Any other tests?
13 changes: 8 additions & 5 deletions hc/accounts/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ def test_it_sends_link(self):
r = self.client.post("/accounts/login/", form)
assert r.status_code == 302

### Assert that a user was created
# Assert that a user was created
self.assertEqual(User.objects.count(), 1)

# 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
# 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
# Assert that check is associated with the new user
get_check = Check.objects.get(code=check.code)
self.assertEqual(get_check.user, User.objects.get(email='alice@example.org'))

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?

# Any other tests?
29 changes: 25 additions & 4 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@ def test_it_sends_set_password_link(self):
# profile.token should be set now
self.alice.profile.refresh_from_db()
token = self.alice.profile.token
### Assert that the token is set
# Assert that the token is set
self.assertTrue(token)

### Assert that the email was sent and check email content
# 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")

def test_it_sends_report(self):
check = Check(name="Test Check", user=self.alice)
check.save()

self.alice.profile.send_report()

###Assert that the email was sent and check email content
### Assert that the email was sent and check email content
self.assertEqual(len(mail.outbox), 1)
message = mail.outbox[0]

self.assertEqual(message.subject, 'Monthly Report')
self.assertIn("This is a monthly report sent by healthchecks.io", message.body)

def test_it_adds_team_member(self):
self.client.login(username="alice@example.org", password="password")
Expand All @@ -41,10 +50,12 @@ def test_it_adds_team_member(self):
member_emails.add(member.user.email)

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

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

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

def test_add_team_member_checks_team_access_allowed_flag(self):
self.client.login(username="charlie@example.org", password="password")
Expand Down Expand Up @@ -108,3 +119,13 @@ def test_it_shows_badges(self):
self.assertNotContains(r, "bobs-tag.svg")

### Test it creates and revokes API key
def test_it_revokes_api_key(self):
self.client.login(username="alice@example.org", password="password")

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

self.profile.refresh_from_db()
self.assertEqual(self.profile.api_key, "")

3 changes: 3 additions & 0 deletions hc/accounts/tests/test_switch_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_it_switches(self):
r = self.client.get(url, follow=True)

### Assert the contents of r
self.assertContains(r, "This belongs to Alice")


def test_it_checks_team_membership(self):
Expand All @@ -22,10 +23,12 @@ def test_it_checks_team_membership(self):
url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url)
### Assert the expected error code
self.assertEqual(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
self.assertEqual(r.status_code, 200)
1 change: 1 addition & 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,4 @@ def test_it_handles_missing_profile(self):
self.assertEqual(r.status_code, 200)

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

0 comments on commit e5e0df2

Please sign in to comment.