|
|
@ -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 |