Skip to content

Commit

Permalink
renamed variable r to response and added minor edits to code after PR…
Browse files Browse the repository at this point in the history
… review ref[#135592991]

added uniform assertions for the redirect status code and PEP8 formatting

added uniform assertions for the redirect status code and PEP8 formatting

modified logic of testing for user creation and code for pep8 compliance

minor edits to code to make assertions uniform and pep8 compliance

minor edits to make assertions uniform and for pep8 compliance

minor edit to code for pep8 compliance and reworked logic for obtaining count of profile objects
  • Loading branch information
ianoti committed Dec 20, 2016
1 parent 9613121 commit 86f11d3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 56 deletions.
10 changes: 4 additions & 6 deletions hc/accounts/tests/test_check_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ def test_it_shows_form(self):
self.assertContains(response, "You are about to log in")

def test_it_redirects(self):
response = self.client.post("/accounts/check_token/alice/secret-token/")
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.assertEqual(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.assertEqual(response.status_code, 302)
self.assertRedirects(response, "/accounts/login/")

### Any other tests?
28 changes: 16 additions & 12 deletions hc/accounts/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ def test_it_sends_link(self):

form = {"email": "alice@example.org"}

r = self.client.post("/accounts/login/", form)
assert r.status_code == 302
# assert the user doesn't exist before post operation
self.assertEqual(len(User.objects.filter(email=form["email"])), 0)
self.old_user_count = User.objects.count()

### Assert that a user was created
self.test_user = User.objects.get()
self.assertEqual(self.test_user.email, "alice@example.org")
response = self.client.post("/accounts/login/", form)
self.assertEqual(response.status_code, 302)

# Assert that a user was created
self.new_user_count = User.objects.count()
self.test_user_list = User.objects.filter(email=form["email"])
self.assertEqual(self.test_user_list[0].email, "alice@example.org")
self.assertEqual(self.new_user_count - self.old_user_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)
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)
# Assert that check is associated with the new user
self.test_check = Check.objects.get(user=self.test_user_list[0].id)
self.assertEqual(self.test_user_list[0].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?
45 changes: 22 additions & 23 deletions hc/accounts/tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,68 @@ def test_it_sends_set_password_link(self):

form = {"set_password": "1"}
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 302
self.assertEqual(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 token is set
self.assertTrue(len(token) > 10)

### 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("Set password on healthchecks.io",
mail.outbox[0].subject)
mail.outbox[0].subject)
self.assertIn("Here's a link to set a password for your account on",
mail.outbox[0].body)
mail.outbox[0].body)

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)
self.assertEqual("Monthly Report", mail.outbox[0].subject)
self.assertIn("This is a monthly report sent by healthchecks.io.",
mail.outbox[0].body)
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"}
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 200
self.assertEqual(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
# Assert the existence of the member emails
self.assertEqual(len(member_emails), 2) # Alice included as member

self.assertTrue(form["email"] 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(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)
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"}
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 403
self.assertEqual(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"}
response = self.client.post("/accounts/profile/", form)
assert response.status_code == 200
self.assertEqual(response.status_code, 200)

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

Expand All @@ -85,7 +85,7 @@ def test_it_sets_team_name(self):

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

self.alice.profile.refresh_from_db()
self.assertEqual(self.alice.profile.team_name, "Alpha Team")
Expand All @@ -95,7 +95,7 @@ def test_set_team_name_checks_team_access_allowed_flag(self):

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

def test_it_switches_to_own_team(self):
self.client.login(username="bob@example.org", password="password")
Expand All @@ -122,21 +122,20 @@ def test_it_shows_badges(self):
# Expect only Alice's tags
self.assertNotContains(response, "bobs-tag.svg")

### Test it creates and revokes API key
# 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"}
form = {"create_api_key": "1"}
response = self.client.post("/accounts/profile/", form)

assert response.status_code==200
self.assertEqual(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"}
form_revoke = {"revoke_api_key": "1"}
response = self.client.post("/accounts/profile/", form_revoke)

assert response.status_code==200
self.assertEqual(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!")
17 changes: 8 additions & 9 deletions hc/accounts/tests/test_switch_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
class SwitchTeamTestCase(BaseTestCase):

def test_it_switches(self):
c = Check(user=self.alice, name="This belongs to Alice")
c.save()
alice_check = Check(user=self.alice, name="This belongs to Alice")
alice_check.save()

self.client.login(username="bob@example.org", password="password")

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

### Assert the contents of response
# 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):
def test_it_checks_access_denied_if_not_member(self):
self.client.login(username="charlie@example.org", password="password")

url = "/accounts/switch_team/%s/" % self.alice.username
response = self.client.get(url)
### Assert the expected error code
assert response.status_code == 403
# Assert the expected error code
self.assertEqual(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
response = self.client.get(url, follow=True)
### Assert the expected error code
assert response.status_code == 200
# Assert the expected error code
self.assertEqual(response.status_code, 200)
10 changes: 4 additions & 6 deletions hc/accounts/tests/test_team_access_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ 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)
response = self.client.get("/about/")
self.assertEqual(response.status_code, 200)

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

0 comments on commit 86f11d3

Please sign in to comment.