Skip to content

Commit

Permalink
Merge branch 'develop' into ft-front-tests-143401485
Browse files Browse the repository at this point in the history
  • Loading branch information
valeria-chemtai committed May 9, 2017
2 parents 4468262 + 18cf80b commit 62d9e60
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ hc/local_settings.py
static-collected

.DS_Store
.cache
1 change: 0 additions & 1 deletion hc/api/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def wrapper(request, *args, **kwds):
request.json = json.loads(request.body.decode("utf-8"))
except ValueError:
return make_error("could not parse request body")

if "HTTP_X_API_KEY" in request.META:
api_key = request.META["HTTP_X_API_KEY"]
else:
Expand Down
12 changes: 6 additions & 6 deletions hc/api/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class ApiAdminTestCase(BaseTestCase):
def setUp(self):
super(ApiAdminTestCase, self).setUp()
self.check = Check.objects.create(user=self.alice, tags="foo bar")

### Set Alice to be staff and superuser and save her :)
self.alice.is_staff = True
self.alice.is_superuser = True
self.alice.save()

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

Channel.objects.create(user=self.alice, kind="pushbullet",
value="test-token")

### Assert for the push bullet
chan = Channel.objects.create(user=self.alice, kind="pushbullet",
value="test-token")
self.assertEqual(chan.kind, "pushbullet")

def test_it_shows_channel_list_with_unverified_email(self):
self.client.login(username="alice@example.org", password="password")
Expand Down
11 changes: 6 additions & 5 deletions hc/api/tests/test_badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def setUp(self):
self.check = Check.objects.create(user=self.alice, tags="foo bar")

def test_it_rejects_bad_signature(self):
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
### Assert the expected response status code
resp = self.client.get("/badge/{}/12345678/foo.svg" .format(self.alice.username))

self.assertEqual(resp.status_code, 404)

def test_it_returns_svg(self):
sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
sig = sig[:8].decode("utf-8")
url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
url = "/badge/{}/{}/foo.svg".format(self.alice.username, sig)
resp = self.client.get(url)

r = self.client.get(url)
### Assert that the svg is returned
self.assertContains(resp, "svg", status_code=200)
10 changes: 8 additions & 2 deletions hc/api/tests/test_check_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ def test_it_strips_tags(self):
check.tags = " foo bar "
self.assertEquals(check.tags_list(), ["foo", "bar"])

### Repeat above test for when check is an empty string
check.tags = ""
self.assertEquals(check.tags_list(), [])

# Repeat above test for when check is an empty string

def test_in_grace_period_handles_new_check(self):
check = Check()
### Test that when a new check is created, it is not in the grace period

check.status = "new"
self.assertFalse(check.in_grace_period())
# Test that when a new check is created, it is not in the grace period

def test_status_works_with_grace_period(self):
check = Check()
Expand Down
68 changes: 42 additions & 26 deletions hc/api/tests/test_create_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@ def post(self, data, expected_error=None, expected_fragment=None):
self.assertEqual(r.json()["error"], expected_error)

if expected_fragment:

self.assertEqual(r.status_code, 400)
### Assert that the expected error is the response error
self.assertIn(expected_fragment, r.json()['error'])
# Assert that the expected error is the response error

return r

def test_it_works(self):
r = self.post({
resp = self.post({
"api_key": "abc",
"name": "Foo",
"tags": "bar,baz",
"timeout": 3600,
"grace": 60
})

self.assertEqual(r.status_code, 201)
self.assertEqual(resp.status_code, 201)

doc = r.json()
doc = resp.json()
assert "ping_url" in doc
self.assertEqual(doc["name"], "Foo")
self.assertEqual(doc["tags"], "bar,baz")

### Assert the expected last_ping and n_pings values
self.assertEqual(doc["last_ping"], None)
self.assertEqual(doc["n_pings"], 0)

self.assertTrue("schedule" not in doc)
self.assertTrue("tz" not in doc)
Expand All @@ -51,16 +53,29 @@ def test_it_works(self):

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")
resp = self.client.post(self.URL, payload,
content_type="application/json",
HTTP_X_API_KEY="abc")

self.assertEqual(resp.status_code, 201)

### Make the post request and get the response
r = {'status_code': 201} ### This is just a placeholder variable
def test_channel_assignment(self):

self.assertEqual(r['status_code'], 201)
check = Check()
check.status = "up"
check.user = self.alice
check.save()

### Test for the assignment of channels
channel = Channel(user=self.alice)
channel.save()
channel.checks.add(check)
count_before = channel.checks.count()
resp = self.post({
"api_key": "abc",
"channels": "*"
})
count_after = channel.checks.count()
self.assertEqual((count_after - count_before), 1)

def test_it_supports_unique(self):
existing = Check(user=self.alice, name="Foo")
Expand All @@ -72,30 +87,31 @@ def test_it_supports_unique(self):
"unique": ["name"]
})

# Expect 200 instead of 201
self.assertEqual(r.status_code, 200)

# And there should be only one check in the database:
self.assertEqual(Check.objects.count(), 1)

def test_it_handles_missing_request_body(self):
### 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")

resp = self.post({})
self.assertEqual(resp.status_code, 400)

def test_it_handles_invalid_json(self):
### 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")
form = {"email": "alice@example.org"}
resp = self.post(form)
self.assertEqual(resp.status_code, 400)

def test_it_rejects_wrong_api_key(self):
r = self.post({"api_key": "wrong"})
self.assertEqual(r.status_code, 403)

### Test for the 'timeout is too small' errors
### Test for the 'timeout is too large' errors

def test_timeout_is_too_small(self):
self.post({"api_key": "abc", "timeout": 1},
expected_fragment="timeout is too small")

def test_timeout_is_too_large(self):
resp = self.post({"api_key": "abc", "timeout": 36000000},
expected_fragment="timeout is too large")

def test_it_rejects_non_number_timeout(self):
self.post({"api_key": "abc", "timeout": "oops"},
Expand Down
43 changes: 34 additions & 9 deletions hc/api/tests/test_list_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,41 @@ def get(self):

def test_it_works(self):
resp = self.get()
### Assert the response status code
# ## Assert the response status code
self.assertEqual(resp.status_code, 200)

doc = resp.json()
self.assertTrue("checks" in doc)

# self.assertTrue("checks" in doc)
checks = {check["name"]: check for check in doc["checks"]}

### 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
# ## Assert the expected length of checks

self.assertEqual(len(doc["checks"]), 2)
# ## Assert the checks Alice 1 and Alice 2's timeout, grace, ping_url,
# status,
# Alice 1
self.assertEqual(checks['Alice 1']['grace'], 900)
self.assertEqual(checks['Alice 1']['timeout'], 3600)
self.assertEqual(checks['Alice 1']['ping_url'],
self.a1.url())
self.assertEqual(checks['Alice 1']['status'], 'new')
# Alice 2
self.assertEqual(checks['Alice 2']['grace'], 3600)
self.assertEqual(checks['Alice 2']['timeout'], 86400)
self.assertEqual(checks['Alice 2']['ping_url'],
self.a2.url())
self.assertEqual(checks['Alice 2']['status'], 'up')
# ## last_ping, n_pings and pause_url
# Alice 1
self.assertEqual(checks['Alice 1']['last_ping'],
self.a1.last_ping.isoformat())
self.assertEqual(checks['Alice 1']['n_pings'], self.a1.n_pings)
self.assertNotEqual(checks['Alice 1']['pause_url'],
self.a1.url())
# Alice 2
self.assertEqual(checks['Alice 2']['last_ping'],
self.a2.last_ping.isoformat())
self.assertEqual(checks['Alice 2']['n_pings'], 0)
self.assertNotEqual(checks['Alice 2']['pause_url'],
self.a2.url())

def test_it_shows_only_users_checks(self):
bobs_check = Check(user=self.bob, name="Bob 1")
Expand All @@ -58,4 +82,5 @@ def test_it_shows_only_users_checks(self):

def test_it_accepts_api_key_from_request_body(self):
payload = json.dumps({"api_key": "abc"})
### Test that it accepts an api_key in the request
# Test that it accepts an api_key in the request
self.assertEqual(payload, '{"api_key": "abc"}')
18 changes: 16 additions & 2 deletions hc/api/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,22 @@ def test_webhooks_handle_timeouts(self, mock_get):
n = Notification.objects.get()
self.assertEqual(n.error, "Connection timed out")

### Test that the web hooks handle connection errors
### Test that the web hooks handle error 500s
@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()
# Test that the web hooks handle connection errors
self.assertEqual(n.error, "Connection failed")

@patch("hc.api.transports.requests.request")
def test_webhooks_handle_error_500(self, mock_get):

self._setup_data("webhook", "http://example")
self.channel.notify(self.check)
# Test that the web hooks handle error 500s
mock_get.return_value.status_code = 500

@patch("hc.api.transports.requests.request")
def test_webhooks_ignore_up_events(self, mock_get):
Expand Down
1 change: 0 additions & 1 deletion hc/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def _update(check, spec):
check.assign_all_channels()
elif spec["channels"] == "":
check.channel_set.clear()

return check


Expand Down

0 comments on commit 62d9e60

Please sign in to comment.