Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix accounts tests #1

Merged
merged 17 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
d76b231
added tests verifying user creation, email body content and check ass…
ianoti Dec 12, 2016
ad7464b
rewrote email body test for higher specificity ref[#135592991]
ianoti Dec 12, 2016
fcb696b
fixed bug of incorrect assertion ref[#135592991]
ianoti Dec 12, 2016
07d809b
added tests for token set, and email subject and content for set_pass…
ianoti Dec 12, 2016
36977ae
added tests for token creation, revoking, addition of team members re…
ianoti Dec 12, 2016
4d7fb5c
added test for redirecting if logged in and wrong token login ref[#13…
ianoti Dec 12, 2016
6fce4c8
added tests for error codes and content of page when switching teams …
ianoti Dec 12, 2016
314f09e
added test asserting number of created profile objects ref[#135592991]
ianoti Dec 13, 2016
5a91463
added assertion for redirect url and rewrote client.post to client.ge…
ianoti Dec 13, 2016
9930679
added status code assertion to test_it_switches ref[#135592991]
ianoti Dec 14, 2016
eb4e75d
reworked token test assertion in test_profile ref[#135592991]
ianoti Dec 14, 2016
595668c
renamed variable r to response ref[#135592991]
ianoti Dec 14, 2016
312bf7b
rewrote test to check for creation of user by verifying user details …
ianoti Dec 14, 2016
a89963c
removed hardcoding of email check in memeber_emails ref[#135592991]
ianoti Dec 14, 2016
4e67c3f
renamed variable r to response ref[#135592991]
ianoti Dec 14, 2016
9613121
renamed variable r to response ref[#135592991]
ianoti Dec 14, 2016
8fcd36c
added minor edits to code after PR review ref[#135592991]
ianoti Dec 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some consistency with the tests. If you are asserting the status code maybe assert for all

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assert is the main python assert and not the one for the base test class

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a safe query when we have more than 1 user

self.assertEqual(self.test_user.email, "alice@example.org")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a better logic to test is assert that there is no user with a particular email, then create a user with that email, then assert that a user with that email now exixts and that the count of User objects has incremented by one.


# 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/")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try putting in a bad link that gets popped out

assert "bad_link" not in self.client.session

### Any other tests?

32 changes: 32 additions & 0 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ def test_it_sends_set_password_link(self):
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,6 +34,10 @@ 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")
Expand All @@ -41,10 +51,15 @@ 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) #Alice included as member

self.assertTrue("frank@example.org" in member_emails)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOn't hard code the email address


###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")
Expand Down Expand Up @@ -108,3 +123,20 @@ def test_it_shows_badges(self):
self.assertNotContains(r, "bobs-tag.svg")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert that after adding a team member, an access allowed flag is present

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the code, the user Charlie doesn't have team access enabled hence why the expected error code is a 403 because access is being denied. the test for successful adding of a team member is handled in test_it_adds_team_member and the expected status code is 200 since Alice has team access enabled.


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

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


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what plans do you have for those two spaces?

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

assert r.status_code==200
self.alice.profile.refresh_from_db()
self.assertEqual(self.alice.profile.api_key, "")
self.assertContains(r, "The API key has been revoked!")
4 changes: 4 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,8 @@ def test_it_switches(self):
r = self.client.get(url, follow=True)

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


def test_it_checks_team_membership(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a better name for the test to have it less ambiguous

Expand All @@ -22,10 +24,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
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
3 changes: 3 additions & 0 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 with id => 0
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)

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