Skip to content

Commit

Permalink
Merge 00022db into 1e842f9
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonGeek committed Dec 20, 2016
2 parents 1e842f9 + 00022db commit f5a91aa
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ hc/local_settings.py
static-collected

.DS_Store
static/.DS_Store
static/.DS_Store
.idea/
11 changes: 11 additions & 0 deletions .idea/healthchecks_stark.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: gunicorn hc.wsgi --log-file -
web: gunicorn hc.wsgi --log-file -

5 changes: 5 additions & 0 deletions hc/api/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ApiAdminTestCase(BaseTestCase):
def setUp(self):
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 :)

Expand All @@ -15,5 +18,7 @@ def test_it_shows_channel_list_with_pushbullet(self):

ch = Channel(user=self.alice, kind="pushbullet", value="test-token")
ch.save()
self.assertEqual(ch.value, "test-token")

self.assertEqual(ch.value, 'test-token')
### Assert for the push bullet
3 changes: 3 additions & 0 deletions hc/api/tests/test_badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def setUp(self):

def test_it_rejects_bad_signature(self):
r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
self.assertEqual(r.status_code, 400)
### Assert the expected response status code

def test_it_returns_svg(self):
Expand All @@ -21,4 +22,6 @@ def test_it_returns_svg(self):
url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)

r = self.client.get(url)
self.assertContains(r, '#4c1', status_code=200)
### Assert that the svg is returned

7 changes: 7 additions & 0 deletions hc/api/tests/test_check_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def test_it_strips_tags(self):

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

def test_status_works_with_grace_period(self):
Expand All @@ -35,4 +37,9 @@ def test_paused_check_is_not_in_grace_period(self):
check.status = "paused"
self.assertFalse(check.in_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

36 changes: 32 additions & 4 deletions hc/api/tests/test_create_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ def setUp(self):
def post(self, data, expected_error=None):
r = self.client.post(self.URL, json.dumps(data),
content_type="application/json")

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
Expand All @@ -35,6 +38,8 @@ def test_it_works(self):
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

Expand All @@ -46,12 +51,17 @@ def test_it_works(self):
self.assertEqual(check.grace.total_seconds(), 60)

def test_it_accepts_api_key_in_header(self):
payload = json.dumps({"name": "Foo"})
payload = json.dumps({"name": "Foo", "api key": "abc"})
r = self.client.post(self.URL, payload, content_type="application/json")
self.assertEqual(r.status_code, 400)



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

self.assertEqual(r['status_code'], 201)




def test_it_handles_missing_request_body(self):
### Make the post request with a missing body and get the response
Expand All @@ -77,5 +87,23 @@ def test_it_rejects_non_string_name(self):
self.post({"api_key": "abc", "name": False},
expected_error="name is not a string")

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

j = r.json()
self.assertEqual(j["error"], "timeout is too small")

def test_timeout_is_too_large(self):
r = self.post({"api_key": "abc", "timeout": 3456567},
expected_error="timeout is too large")

j = r.json()
self.assertEqual(j["error"], "timeout is too large")

def test_for_the_assignment_of_channels(self):
check = Check(user=self.alice, status="up", name="Alice 1")
self.assertTrue(check, None)

### Test for the assignment of channels
### Test for the 'timeout is too small' and 'timeout is too large' errors
4 changes: 3 additions & 1 deletion hc/api/tests/test_ensuretriggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def test_ensure_triggers(self):
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 that alert_after is lesser than the check's alert_after
assert alert_after < check.alert_after
### Assert that alert_after is lesser than the check's alert_after
37 changes: 34 additions & 3 deletions hc/api/tests/test_list_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,32 @@ def get(self):

def test_it_works(self):
r = self.get()
### Assert the response status code

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

checks = {check["name"]: check for check in doc["checks"]}
self.assertEqual(r.status_code, 200)
self.assertEqual(len(checks), 2)
self.assertIn('Alice 1', checks.keys())
self.assertEqual(checks[self.a1.name]['timeout'], 3600)
self.assertEqual(checks[self.a1.name]['grace'], 900)
self.assertNotEqual(checks[self.a1.name]['pause_url'], 0)
self.assertNotEqual(len(checks[self.a1.name]['ping_url']), 0)
self.assertEqual(checks[self.a1.name]['status'], "new")
self.assertEqual(checks[self.a1.name]['last_ping'], self.now.isoformat())
self.assertEqual(checks[self.a1.name]['n_pings'], 1)

self.assertIn('Alice 2', checks.keys())
self.assertEqual(checks[self.a2.name]['timeout'], 86400)
self.assertEqual(checks[self.a2.name]['grace'], 3600)
self.assertNotEqual(checks[self.a2.name]['pause_url'], 0)
self.assertNotEqual(len(checks[self.a2.name]['ping_url']), 0)
self.assertEqual(checks[self.a2.name]['status'], "up")
self.assertEqual(checks[self.a2.name]['last_ping'], self.now.isoformat())
self.assertEqual(checks[self.a2.name]['n_pings'], 0)
### Assert the response status code


### 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
Expand All @@ -52,5 +72,16 @@ def test_it_shows_only_users_checks(self):
self.assertEqual(len(data["checks"]), 2)
for check in data["checks"]:
self.assertNotEqual(check["name"], "Bob 1")
self.assertEqual(r.status_code, 200)

def test_it_accepts_api_key_in_request(self):
r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="bbb")
self.assertEqual(r.status_code, 400)

r = self.client.get("/api/v1/checks/", HTTP_X_API_KEY="abc")
self.assertEqual(r.status_code, 200)

r = self.client.get("/api/v1/checks/")
self.assertEqual(r.status_code, 400)

### Test that it accepts an api_key in the request
### Test that it accepts an api_key in the request
18 changes: 17 additions & 1 deletion hc/api/tests/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,20 @@ def test_victorops(self, mock_post):
json = kwargs["json"]
self.assertEqual(json["message_type"], "CRITICAL")

### Test that the web hooks handle connection errors and error 500s
@patch("hc.api.transports.requests.request", side_effect=ConnectionError)
def test_that_the_web_hooks_handle_connection_errors_and_error_500s(self, mock_get):
self._setup_data("webhook", "http://example")
self.channel.notify(self.check)

n = Notification.objects.get()
self.assertEqual(n.error, "Connection failed")

### Test that the web hooks handle connection errors and error 500s

@patch("hc.api.transports.requests.request", side_effect=Timeout)
def test_webhooks_handle_connection_timeout(self, mock_get):
self._setup_data("webhook", "http://example")
self.channel.notify(self.check)

n = Notification.objects.get()
self.assertEqual(n.error, "Connection timed out")
6 changes: 6 additions & 0 deletions hc/api/tests/test_pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def test_it_works(self):
r = self.client.post(url, "", content_type="application/json",
HTTP_X_API_KEY="abc")

self.assertEqual(r.status_code, 200)
self.assertEqual(check.status, 'up')
### Assert the expected status code and check's status

def test_it_validates_ownership(self):
Expand All @@ -24,4 +26,8 @@ def test_it_validates_ownership(self):

self.assertEqual(r.status_code, 400)

r = self.client.get(url, "", content_type="application/json",
HTTP_X_API_KEY="abc")

self.assertEqual(r.status_code, 405)
### Test that it only allows post requests
36 changes: 32 additions & 4 deletions hc/api/tests/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def test_it_reads_forwarded_ip(self):
r = self.client.get("/ping/%s/" % self.check.code,
HTTP_X_FORWARDED_FOR=ip)
ping = Ping.objects.latest("id")
### Assert the expected response status code and ping's remote address
assert ping.remote_addr == ip
assert r.status_code == 200


ip = "1.1.1.1, 2.2.2.2"
r = self.client.get("/ping/%s/" % self.check.code,
Expand All @@ -62,12 +64,38 @@ def test_it_reads_forwarded_protocol(self):
r = self.client.get("/ping/%s/" % self.check.code,
HTTP_X_FORWARDED_PROTO="https")
ping = Ping.objects.latest("id")
self.assertEqual(r.status_code, 200)
self.assertEqual(ping.scheme, 'https')
### Assert the expected response status code and ping's scheme

def test_it_never_caches(self):
r = self.client.get("/ping/%s/" % self.check.code)
assert "no-cache" in r.get("Cache-Control")

### Test that when a ping is made a check with a paused status changes status
### Test that a post to a ping works
### Test that the csrf_client head works
def test_check_status_after_ping(self):
self.check_status = "paused"
self.check.save()
r = self.client.get("/ping/%s/" % self.check.code)
check = Check.objects.latest("id")
assert check.status == "up"
### Test that when a ping is made a check with a paused status changes status

ping = Ping.objects.latest("id")
assert ping.scheme == "http"

def test_post_ping_works(self):
r = self.client.post("/ping/%s/" % self.check.code)
assert r.status_code == 200
### Test that a post to a ping works


def test_csrf_client_head(self):
csrf_client = Client(enforce_csrf_checks=True)
r = csrf_client.post("/ping/%s/" % self.check.code)
assert r.status_code == 200

### Test that the csrf_client head works




3 changes: 2 additions & 1 deletion hc/api/tests/test_sendalerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_it_handles_grace_period(self):
check.save()

# Expect no exceptions--
Command().handle_one(check)
Command().handle_many()
self.assertFalse(Command().handle_many())

### Assert when Command's handle many that when handle_many should return True
23 changes: 11 additions & 12 deletions hc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import os
import warnings
import dj_database_url


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

HOST = "localhost"
Expand Down Expand Up @@ -98,19 +96,21 @@
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'hc',
'USER': 'postgres',
'PASSWORD': '',
'TEST': {'CHARSET': 'UTF8'}
}
}

if os.environ.get("DB") == "mysql":
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'USER': 'root',
'NAME': 'hc',
'TEST': {'CHARSET': 'UTF8'}
}
}
#if os.environ.get("DB") == "mysql":
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'USER': 'root',
# 'NAME': 'hc',
# 'TEST': {'CHARSET': 'UTF8'}
# }
# }


LANGUAGE_CODE = 'en-us'

Expand Down Expand Up @@ -161,4 +161,3 @@
from .local_settings import *
else:
warnings.warn("local_settings.py not found, using defaults")

Loading

0 comments on commit f5a91aa

Please sign in to comment.