def test_team_access_works(self): self.team_check = Check(user=self.alice, name="Pair Programming") self.team_check.save() status = [] for email in [ "*****@*****.**", "*****@*****.**", "*****@*****.**" ]: self.client.login(username=email, password="******") url = "/checks/%s/log/" % self.team_check.code r = self.client.get(url) status.append(r.status_code) self.assertEqual(status, [200, 403, 403])
def setUp(self): super(LogTestCase, self).setUp() self.check = Check(user=self.alice) self.check.save() ping = Ping(owner=self.check) ping.save() # Older MySQL versions don't store microseconds. This makes sure # the ping is older than any notifications we may create later: ping.created = "2000-01-01T00:00:00+00:00" ping.save()
def test_it_allows_cross_team_access(self): self.bobs_profile.current_team = None self.bobs_profile.save() check = Check(user=self.alice) check.save() Ping.objects.create(owner=check, body="this is body") self.client.login(username="******", password="******") r = self.client.get("/checks/%s/last_ping/" % check.code) self.assertEqual(r.status_code, 200)
def test_it_sends_report(self): check = Check(name="Test Check", user=self.alice) check.save() 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)
def test_it_handles_many(self, mock): yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0, 100)] for name in names: check = Check(user=self.alice, name=name) check.alert_after = yesterday check.status = "up" check.save() result = Command().handle_many() self.assertTrue(result)
def test_it_switches(self): c = Check(user=self.alice, name="This belongs to Alice") c.save() self.client.login(username="******", password="******") url = "/accounts/switch_team/%s/" % self.alice.username r = self.client.get(url, follow=True) ### Assert the contents of r self.assertEqual(r.status_code, 200) self.assertContains(r,"<title>My Checks - healthchecks.io</title>", html=True)
def test_it_removes_old_never_logged_in_users(self): self.charlie.date_joined = self.year_ago self.charlie.save() # Charlie has one demo check charlies_project = Project.objects.create(owner=self.charlie) Check(project=charlies_project).save() Command(stdout=Mock()).handle() self.assertEqual(User.objects.filter(username="******").count(), 0) self.assertEqual(Check.objects.count(), 0)
def setUp(self): super(ListChecksTestCase, self).setUp() self.now = now().replace(microsecond=0) self.a1 = Check(user=self.alice, name="Alice 1") self.a1.timeout = td(seconds=3600) self.a1.grace = td(seconds=900) self.a1.last_ping = self.now self.a1.n_pings = 1 self.a1.status = "new" self.a1.tags = "a1-tag a1-additional-tag" self.a1.save() self.a2 = Check(user=self.alice, name="Alice 2") self.a2.timeout = td(seconds=86400) self.a2.grace = td(seconds=3600) self.a2.last_ping = self.now self.a2.status = "up" self.a2.tags = "a2-tag" self.a2.save()
def setUp(self): super().setUp() self.a1 = Check(project=self.project, name="Alice 1") self.a1.timeout = td(seconds=3600) self.a1.grace = td(seconds=900) self.a1.n_pings = 0 self.a1.status = "new" self.a1.tags = "foo bar" self.a1.save() self.url = "/api/v1/badges/"
def test_it_shows_all_other_checks_up_note(self, mock_bus): other = Check(project=self.project) other.name = "Foobar" other.status = "up" other.last_ping = now() - td(minutes=61) other.save() self.channel.notify(self.check) args, kwargs = mock_bus.SystemBus.return_value.call_blocking.call_args message, attachments, recipients = args[-1] self.assertIn("All the other checks are up.", message)
def add_check(request, code): project = _get_project_for_user(request, code) if project.num_checks_available() <= 0: return HttpResponseBadRequest() check = Check(project=project) check.save() check.assign_all_channels() url = reverse("hc-details", args=[check.code]) return redirect(url + "?new")
def test_it_handles_grace_period(self): check = Check(project=self.project, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = now() - td(days=1, minutes=30) check.alert_after = check.last_ping + td(days=1, hours=1) check.save() Command().handle_going_down() check.refresh_from_db() self.assertEqual(check.status, "up") self.assertEqual(Flip.objects.count(), 0)
def test_it_works(self): check = Check(user=self.alice, status="up") check.save() url = "/api/v1/checks/%s/pause" % check.code r = self.client.post(url, "", content_type="application/json", HTTP_X_API_KEY="abc") self.assertEqual(r.status_code, 200) check.refresh_from_db() self.assertEqual(check.status, "paused")
def setUp(self): super(BounceTestCase, self).setUp() self.check = Check(user=self.alice, status="up") self.check.save() self.channel = Channel(user=self.alice, kind="email") self.channel.value = "*****@*****.**" self.channel.save() self.n = Notification(owner=self.check, channel=self.channel) self.n.save()
def _setup_data(self, kind, value, status="down", email_verified=True): self.check = Check(project=self.project) self.check.status = status self.check.last_ping = now() - td(minutes=61) self.check.save() self.channel = Channel(project=self.project) self.channel.kind = kind self.channel.value = value self.channel.email_verified = email_verified self.channel.save() self.channel.checks.add(self.check)
def _welcome_check(request): check = None if "welcome_code" in request.session: code = request.session["welcome_code"] check = Check.objects.filter(code=code).first() if check is None: check = Check() check.save() request.session["welcome_code"] = str(check.code) return check
def test_it_works(self): check = Check() check.save() r = self.client.get("/ping/%s/" % check.code) assert r.status_code == 200 same_check = Check.objects.get(code=check.code) assert same_check.status == "up" pings = list(Ping.objects.all()) assert pings[0].scheme == "http"
def setUp(self): super(UpdateChannelTestCase, self).setUp() self.alice = User(username="******", email="*****@*****.**") self.alice.set_password("password") self.alice.save() self.check = Check(user=self.alice) self.check.save() self.channel = Channel(user=self.alice, kind="email") self.channel.email = "*****@*****.**" self.channel.save()
def test_it_shows_not_unique_note(self): self.project.show_slugs = True self.project.save() dupe = Check(project=self.project, name="Alice Was Here") dupe.slug = "alice-was-here" dupe.save() self.client.login(username="******", password="******") r = self.client.get(self.url) self.assertContains(r, "alice-was-here") self.assertContains(r, "(not unique)")
def create_check(request): created = False check = _lookup(request.project, request.json) if check is None: if request.project.num_checks_available() <= 0: return HttpResponseForbidden() check = Check(project=request.project) created = True _update(check, request.json) return JsonResponse(check.to_dict(), status=201 if created else 200)
def _setup_data(self, kind, value, status="down", email_verified=True): self.check = Check() self.check.status = status self.check.user = self.alice self.check.save() self.channel = Channel(user=self.alice) self.channel.kind = kind self.channel.value = value self.channel.email_verified = email_verified self.channel.save() self.channel.checks.add(self.check)
def test_it_skips_nag_if_none_down(self): check = Check(project=self.project, name="Test Check") check.last_ping = now() check.save() self.profile.nag_period = td(hours=1) self.profile.save() sent = self.profile.send_report(nag=True) self.assertFalse(sent) self.assertEqual(len(mail.outbox), 0)
def setUp(self): super().setUp() self.check = Check(project=self.project) self.check.name = "Foo" self.check.slug = "foo" self.check.subject = "success-keyword" self.check.subject_fail = "failure-keyword" self.check.methods = "POST" self.check.manual_resume = True self.check.save() self.copy_url = "/checks/%s/copy/" % self.check.code
def test_team_access_network(self): self.team_check = Check(user=self.alice, name="Pair Programming") self.team_check.save() status = [] for email in [ self.bob.email, "*****@*****.**", "*****@*****.**" ]: self.client.login(username=email, password="******") url = "/checks/{}/log/".format(self.team_check.code) r = self.client.get(url) status.append(r.status_code) self.assertEqual(status, [200, 403, 403])
def test_it_sends_report(self): check = Check(name="Test Check", user=self.alice) check.save() self.alice.profile.reports_allowed = 'monthly' self.alice.profile.save() self.alice.profile.send_report() ###Assert that the email was sent and check email content self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'Monthly Report')
def test_next_ping_with_cron_syntax(self): dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc) # Expect ping every round hour check = Check() check.kind = "cron" check.schedule = "0 * * * *" check.status = "up" check.last_ping = dt d = check.to_dict() self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")
def test_it_handles_120_char_ua(self): ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/44.0.2403.89 Safari/537.36") check = Check() check.save() r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua) assert r.status_code == 200 pings = list(Ping.objects.all()) assert pings[0].ua == ua
def checks(request): if request.method == "GET": q = Check.objects.filter(user=request.user) doc = {"checks": [check.to_dict() for check in q]} return JsonResponse(doc) elif request.method == "POST": name = str(request.json.get("name", "")) tags = str(request.json.get("tags", "")) timeout = DEFAULT_TIMEOUT if "timeout" in request.json: timeout = td(seconds=request.json["timeout"]) grace = DEFAULT_GRACE if "grace" in request.json: grace = td(seconds=request.json["grace"]) unique_fields = request.json.get("unique", []) if unique_fields: existing_checks = Check.objects.filter(user=request.user) if "name" in unique_fields: existing_checks = existing_checks.filter(name=name) if "tags" in unique_fields: existing_checks = existing_checks.filter(tags=tags) if "timeout" in unique_fields: existing_checks = existing_checks.filter(timeout=timeout) if "grace" in unique_fields: existing_checks = existing_checks.filter(grace=grace) if existing_checks.count() > 0: # There might be more than one matching check, return first first_match = existing_checks.first() return JsonResponse(first_match.to_dict(), status=200) check = Check(user=request.user, name=name, tags=tags, timeout=timeout, grace=grace) check.save() # This needs to be done after saving the check, because of # the M2M relation between checks and channels: if request.json.get("channels") == "*": check.assign_all_channels() return JsonResponse(check.to_dict(), status=201) # If request is neither GET nor POST, return "405 Method not allowed" return HttpResponse(status=405)
def create_check(request): created = False check = _lookup(request.user, request.json) if check is None: num_checks = Check.objects.filter(user=request.user).count() if num_checks >= request.user.profile.check_limit: return HttpResponseForbidden() check = Check(user=request.user) created = True _update(check, request.json) return JsonResponse(check.to_dict(), status=201 if created else 200)
def test_status_works_with_grace_period(self): check = Check() check.status = "up" check.last_ping = timezone.now() - timedelta(days=1, minutes=30) #self.assertFalse(check.in_grace_period()) #self.assertEqual(check.get_status(), "down") ### The above 2 asserts fail. Make them pass self.assertTrue(check.in_grace_period()) self.assertEqual(check.get_status(), "up")