Browse Source

De-tested

pull/81/head
Njira Perci 8 years ago
parent
commit
59979c0127
14 changed files with 64 additions and 151 deletions
  1. +4
    -1
      .gitignore
  2. +3
    -12
      hc/accounts/tests/test_check_token.py
  3. +6
    -5
      hc/accounts/tests/test_login.py
  4. +8
    -37
      hc/accounts/tests/test_profile.py
  5. +4
    -3
      hc/accounts/tests/test_switch_team.py
  6. +1
    -1
      hc/accounts/tests/test_team_access_middleware.py
  7. +2
    -5
      hc/api/tests/test_admin.py
  8. +2
    -2
      hc/api/tests/test_badge.py
  9. +6
    -7
      hc/api/tests/test_check_model.py
  10. +17
    -31
      hc/api/tests/test_create_check.py
  11. +3
    -1
      hc/api/tests/test_ensuretriggers.py
  12. +5
    -26
      hc/api/tests/test_list_checks.py
  13. +2
    -18
      hc/api/tests/test_notify.py
  14. +1
    -2
      hc/test.py

+ 4
- 1
.gitignore View File

@ -3,4 +3,7 @@ __pycache__/
.coverage
hc.sqlite
hc/local_settings.py
static-collected
static-collected
.DS_Store
static/.DS_Store

+ 3
- 12
hc/accounts/tests/test_check_token.py View File

@ -21,17 +21,8 @@ class CheckTokenTestCase(BaseTestCase):
self.profile.refresh_from_db()
self.assertEqual(self.profile.token, "")
def test_it_redirects_already_logged_in(self):
# Login
self.client.login(username="[email protected]", password="password")
### Login and test it redirects already logged in
# Login again, when already authenticated
r = self.client.post("/accounts/check_token/alice/secret-token/")
self.assertRedirects(r, "/checks/")
### Login with a bad token and check that it redirects
def test_it_redirects_bad_login(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?

+ 6
- 5
hc/accounts/tests/test_login.py View File

@ -19,18 +19,19 @@ class LoginTestCase(TestCase):
r = self.client.post("/accounts/login/", form)
assert r.status_code == 302
# An user should have been created
self.assertEqual(User.objects.count(), 1)
### Assert that a user was created
# 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
# And check should be associated with the new user
check_again = Check.objects.get(code=check.code)
assert check_again.user
### Assert that check is associated with the new user
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?

+ 8
- 37
hc/accounts/tests/test_profile.py View File

@ -17,33 +17,9 @@ class ProfileTestCase(BaseTestCase):
# profile.token should be set now
self.alice.profile.refresh_from_db()
token = self.alice.profile.token
self.assertTrue(len(token) > 10)
### Assert that the token is set
# And an email should have been sent
self.assertEqual(len(mail.outbox), 1)
expected_subject = 'Set password on healthchecks.io'
self.assertEqual(mail.outbox[0].subject, expected_subject)
def test_it_creates_api_key(self):
self.client.login(username="[email protected]", password="password")
form = {"create_api_key": "1"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 200
self.alice.profile.refresh_from_db()
api_key = self.alice.profile.api_key
self.assertTrue(len(api_key) > 10)
def test_it_revokes_api_key(self):
self.client.login(username="[email protected]", password="password")
form = {"revoke_api_key": "1"}
r = self.client.post("/accounts/profile/", form)
assert r.status_code == 200
self.alice.profile.refresh_from_db()
self.assertEqual(self.alice.profile.api_key, "")
### Assert that the email was sent and check email content
def test_it_sends_report(self):
check = Check(name="Test Check", user=self.alice)
@ -51,12 +27,7 @@ class ProfileTestCase(BaseTestCase):
self.alice.profile.send_report()
# And an email should have been sent
self.assertEqual(len(mail.outbox), 1)
message = mail.outbox[0]
self.assertEqual(message.subject, 'Monthly Report')
self.assertIn("Test Check", message.body)
###Assert that the email was sent and check email content
def test_it_adds_team_member(self):
self.client.login(username="[email protected]", password="password")
@ -69,13 +40,11 @@ class ProfileTestCase(BaseTestCase):
for member in self.alice.profile.member_set.all():
member_emails.add(member.user.email)
self.assertEqual(len(member_emails), 2)
### Assert the existence of the member emails
self.assertTrue("[email protected]" in member_emails)
# And an email should have been sent
subj = ('You have been invited to join'
' [email protected] on healthchecks.io')
self.assertEqual(mail.outbox[0].subject, subj)
###Assert that the email was sent and check email content
def test_add_team_member_checks_team_access_allowed_flag(self):
self.client.login(username="[email protected]", password="password")
@ -137,3 +106,5 @@ class ProfileTestCase(BaseTestCase):
# Expect only Alice's tags
self.assertNotContains(r, "bobs-tag.svg")
### Test it creates and revokes API key

+ 4
- 3
hc/accounts/tests/test_switch_team.py View File

@ -13,18 +13,19 @@ class SwitchTeamTestCase(BaseTestCase):
url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
self.assertContains(r, "This belongs to Alice")
### Assert the contents of r
def test_it_checks_team_membership(self):
self.client.login(username="[email protected]", password="password")
url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url)
self.assertEqual(r.status_code, 403)
### Assert the expected error code
def test_it_switches_to_own_team(self):
self.client.login(username="[email protected]", password="password")
url = "/accounts/switch_team/%s/" % self.alice.username
r = self.client.get(url, follow=True)
self.assertEqual(r.status_code, 200)
### Assert the expected error code

+ 1
- 1
hc/accounts/tests/test_team_access_middleware.py View File

@ -14,4 +14,4 @@ class TeamAccessMiddlewareTestCase(TestCase):
r = self.client.get("/about/")
self.assertEqual(r.status_code, 200)
self.assertEqual(Profile.objects.count(), 1)
### Assert the new Profile objects count

+ 2
- 5
hc/api/tests/test_admin.py View File

@ -8,9 +8,7 @@ class ApiAdminTestCase(BaseTestCase):
super(ApiAdminTestCase, self).setUp()
self.check = Check.objects.create(user=self.alice, tags="foo bar")
self.alice.is_staff = True
self.alice.is_superuser = True
self.alice.save()
### Set Alice to be staff and superuser and save her :)
def test_it_shows_channel_list_with_pushbullet(self):
self.client.login(username="[email protected]", password="password")
@ -18,5 +16,4 @@ class ApiAdminTestCase(BaseTestCase):
ch = Channel(user=self.alice, kind="pushbullet", value="test-token")
ch.save()
r = self.client.get("/admin/api/channel/")
self.assertContains(r, "Pushbullet")
### Assert for the push bullet

+ 2
- 2
hc/api/tests/test_badge.py View File

@ -13,7 +13,7 @@ class BadgeTestCase(BaseTestCase):
def test_it_rejects_bad_signature(self):
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
assert r.status_code == 400
### Assert the expected response status code
def test_it_returns_svg(self):
sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
@ -21,4 +21,4 @@ class BadgeTestCase(BaseTestCase):
url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
r = self.client.get(url)
self.assertContains(r, "#4c1")
### Assert that the svg is returned

+ 6
- 7
hc/api/tests/test_check_model.py View File

@ -12,22 +12,19 @@ class CheckModelTestCase(TestCase):
check.tags = " foo bar "
self.assertEquals(check.tags_list(), ["foo", "bar"])
check.tags = " "
self.assertEquals(check.tags_list(), [])
def test_in_grace_period_handles_new_check(self):
check = Check()
self.assertFalse(check.in_grace_period())
### Repeat above test for when check is an empty string
def test_status_works_with_grace_period(self):
check = Check()
check.status = "up"
check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
self.assertTrue(check.in_grace_period())
self.assertEqual(check.get_status(), "up")
### The above 2 asserts fail. Make them pass
def test_paused_check_is_not_in_grace_period(self):
check = Check()
@ -37,3 +34,5 @@ class CheckModelTestCase(TestCase):
check.status = "paused"
self.assertFalse(check.in_grace_period())
### Test that when a new check is created, it is not in the grace period

+ 17
- 31
hc/api/tests/test_create_check.py View File

@ -16,7 +16,7 @@ class CreateCheckTestCase(BaseTestCase):
if expected_error:
self.assertEqual(r.status_code, 400)
self.assertEqual(r.json()["error"], expected_error)
### Assert that the expected error is the response error
return r
@ -35,8 +35,8 @@ class CreateCheckTestCase(BaseTestCase):
assert "ping_url" in doc
self.assertEqual(doc["name"], "Foo")
self.assertEqual(doc["tags"], "bar,baz")
self.assertEqual(doc["last_ping"], None)
self.assertEqual(doc["n_pings"], 0)
### Assert the expected last_ping and n_pings values
self.assertEqual(Check.objects.count(), 1)
check = Check.objects.get()
@ -47,45 +47,28 @@ class CreateCheckTestCase(BaseTestCase):
def test_it_accepts_api_key_in_header(self):
payload = json.dumps({"name": "Foo"})
r = self.client.post(self.URL, payload,
content_type="application/json",
HTTP_X_API_KEY="abc")
self.assertEqual(r.status_code, 201)
def test_it_assigns_channels(self):
channel = Channel(user=self.alice)
channel.save()
### Make the post request and get the response
r = {'status_code': 201} ### This is just a placeholder variable
r = self.post({"api_key": "abc", "channels": "*"})
self.assertEqual(r.status_code, 201)
check = Check.objects.get()
self.assertEqual(check.channel_set.get(), channel)
self.assertEqual(r['status_code'], 201)
def test_it_handles_missing_request_body(self):
r = self.client.post(self.URL, content_type="application/json")
self.assertEqual(r.status_code, 400)
self.assertEqual(r.json()["error"], "wrong api_key")
### Make the post request with a missing body and get the response
r = {'status_code': 400, 'error': "wrong api_key"} ### This is just a placeholder variable
self.assertEqual(r['status_code'], 400)
self.assertEqual(r["error"], "wrong api_key")
def test_it_handles_invalid_json(self):
r = self.client.post(self.URL, "this is not json",
content_type="application/json")
self.assertEqual(r.status_code, 400)
self.assertEqual(r.json()["error"], "could not parse request body")
### Make the post request with invalid json data type
r = {'status_code': 400, 'error': "could not parse request body"} ### This is just a placeholder variable
self.assertEqual(r['status_code'], 400)
self.assertEqual(r["error"], "could not parse request body")
def test_it_rejects_wrong_api_key(self):
self.post({"api_key": "wrong"},
expected_error="wrong api_key")
def test_it_rejects_small_timeout(self):
self.post({"api_key": "abc", "timeout": 0},
expected_error="timeout is too small")
def test_it_rejects_large_timeout(self):
self.post({"api_key": "abc", "timeout": 604801},
expected_error="timeout is too large")
def test_it_rejects_non_number_timeout(self):
self.post({"api_key": "abc", "timeout": "oops"},
expected_error="timeout is not a number")
@ -93,3 +76,6 @@ class CreateCheckTestCase(BaseTestCase):
def test_it_rejects_non_string_name(self):
self.post({"api_key": "abc", "name": False},
expected_error="name is not a string")
### Test for the assignment of channels
### Test for the 'timeout is too small' and 'timeout is too large' errors

+ 3
- 1
hc/api/tests/test_ensuretriggers.py View File

@ -19,9 +19,11 @@ class EnsureTriggersTestCase(TestCase):
check.save()
check.refresh_from_db()
assert check.alert_after is not None
### The above assert fails. Make it pass
alert_after = check.alert_after
check.last_ping += timedelta(days=1)
check.save()
check.refresh_from_db()
assert check.alert_after > alert_after
### Assert that alert_after is lesser than the check's alert_after

+ 5
- 26
hc/api/tests/test_list_checks.py View File

@ -33,30 +33,15 @@ class ListChecksTestCase(BaseTestCase):
def test_it_works(self):
r = self.get()
self.assertEqual(r.status_code, 200)
### Assert the response status code
doc = r.json()
self.assertTrue("checks" in doc)
checks = {check["name"]: check for check in doc["checks"]}
self.assertEqual(len(checks), 2)
self.assertEqual(checks["Alice 1"]["timeout"], 3600)
self.assertEqual(checks["Alice 1"]["grace"], 900)
self.assertEqual(checks["Alice 1"]["ping_url"], self.a1.url())
self.assertEqual(checks["Alice 1"]["last_ping"], self.now.isoformat())
self.assertEqual(checks["Alice 1"]["n_pings"], 1)
self.assertEqual(checks["Alice 1"]["status"], "new")
pause_url = "http://localhost:8000/api/v1/checks/%s/pause" % self.a1.code
self.assertEqual(checks["Alice 1"]["pause_url"], pause_url)
next_ping = self.now + td(seconds=3600)
self.assertEqual(checks["Alice 1"]["next_ping"], next_ping.isoformat())
self.assertEqual(checks["Alice 2"]["timeout"], 86400)
self.assertEqual(checks["Alice 2"]["grace"], 3600)
self.assertEqual(checks["Alice 2"]["ping_url"], self.a2.url())
self.assertEqual(checks["Alice 2"]["status"], "up")
### Assert the expected length of checks
### Assert the checks Alice 1 and Alice 2's timeout, grace, ping_url, status,
### last_ping, n_pings and pause_url
def test_it_shows_only_users_checks(self):
bobs_check = Check(user=self.bob, name="Bob 1")
@ -68,10 +53,4 @@ class ListChecksTestCase(BaseTestCase):
for check in data["checks"]:
self.assertNotEqual(check["name"], "Bob 1")
def test_it_accepts_api_key_from_request_body(self):
payload = json.dumps({"api_key": "abc"})
r = self.client.generic("GET", "/api/v1/checks/", payload,
content_type="application/json")
self.assertEqual(r.status_code, 200)
self.assertContains(r, "Alice")
### Test that it accepts an api_key in the request

+ 2
- 18
hc/api/tests/test_notify.py View File

@ -41,14 +41,6 @@ class NotifyTestCase(BaseTestCase):
n = Notification.objects.get()
self.assertEqual(n.error, "Connection timed out")
@patch("hc.api.transports.requests.request", side_effect=ConnectionError)
def test_webhooks_handle_connection_errors(self, mock_get):
self._setup_data("webhook", "http://example")
self.channel.notify(self.check)
n = Notification.objects.get()
self.assertEqual(n.error, "Connection failed")
@patch("hc.api.transports.requests.request")
def test_webhooks_ignore_up_events(self, mock_get):
self._setup_data("webhook", "http://example", status="up")
@ -57,16 +49,6 @@ class NotifyTestCase(BaseTestCase):
self.assertFalse(mock_get.called)
self.assertEqual(Notification.objects.count(), 0)
@patch("hc.api.transports.requests.request")
def test_webhooks_handle_500(self, mock_get):
self._setup_data("webhook", "http://example")
mock_get.return_value.status_code = 500
self.channel.notify(self.check)
n = Notification.objects.get()
self.assertEqual(n.error, "Received status code 500")
@patch("hc.api.transports.requests.request")
def test_webhooks_support_variables(self, mock_get):
template = "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME"
@ -239,3 +221,5 @@ class NotifyTestCase(BaseTestCase):
args, kwargs = mock_post.call_args
json = kwargs["json"]
self.assertEqual(json["message_type"], "CRITICAL")
### Test that the web hooks handle connection errors and error 500s

+ 1
- 2
hc/test.py View File

@ -35,5 +35,4 @@ class BaseTestCase(TestCase):
self.charlie.set_password("password")
self.charlie.save()
charlies_profile = Profile(user=self.charlie)
charlies_profile.save()
### Set Charles not to have access to Alice's stuff

Loading…
Cancel
Save