diff --git a/.travis.yml b/.travis.yml index fdfe2f1b..d196eefb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,3 +14,4 @@ addons: script: - coverage run --source=hc manage.py test after_success: coveralls +cache: pip diff --git a/hc/accounts/tests/test_check_token.py b/hc/accounts/tests/test_check_token.py index e902754f..51ca036b 100644 --- a/hc/accounts/tests/test_check_token.py +++ b/hc/accounts/tests/test_check_token.py @@ -1,19 +1,12 @@ from django.contrib.auth.hashers import make_password -from django.contrib.auth.models import User -from django.test import TestCase - from hc.accounts.models import Profile +from hc.test import BaseTestCase -class CheckTokenTestCase(TestCase): +class CheckTokenTestCase(BaseTestCase): def setUp(self): super(CheckTokenTestCase, self).setUp() - - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.profile = Profile(user=self.alice) self.profile.token = make_password("secret-token") self.profile.save() diff --git a/hc/api/management/commands/ensuretriggers.py b/hc/api/management/commands/ensuretriggers.py index 695977fd..fc699aba 100644 --- a/hc/api/management/commands/ensuretriggers.py +++ b/hc/api/management/commands/ensuretriggers.py @@ -25,10 +25,14 @@ def _pg(cursor): def _mysql(cursor): cursor.execute(""" DROP TRIGGER IF EXISTS update_alert_after; + """) + cursor.execute(""" CREATE TRIGGER update_alert_after BEFORE UPDATE ON api_check - FOR EACH ROW SET NEW.alert_after = NEW.last_ping + INTERVAL (NEW.timeout + NEW.grace) MICROSECOND; + FOR EACH ROW SET + NEW.alert_after = + NEW.last_ping + INTERVAL (NEW.timeout + NEW.grace) MICROSECOND; """) @@ -42,7 +46,9 @@ def _sqlite(cursor): AFTER UPDATE OF last_ping, timeout, grace ON api_check FOR EACH ROW BEGIN UPDATE api_check - SET alert_after = datetime(strftime('%s', last_ping) + timeout/1000000 + grace/1000000, 'unixepoch') + SET alert_after = + datetime(strftime('%s', last_ping) + + timeout/1000000 + grace/1000000, 'unixepoch') WHERE id = OLD.id; END; """) @@ -52,14 +58,13 @@ class Command(BaseCommand): help = 'Ensures triggers exist in database' def handle(self, *args, **options): - cursor = connection.cursor() - - if connection.vendor == "postgresql": - _pg(cursor) - return "Created PostgreSQL trigger" - if connection.vendor == "mysql": - _mysql(cursor) - return "Created MySQL trigger" - if connection.vendor == "sqlite": - _sqlite(cursor) - return "Created SQLite trigger" + with connection.cursor() as cursor: + if connection.vendor == "postgresql": + _pg(cursor) + return "Created PostgreSQL trigger" + if connection.vendor == "mysql": + _mysql(cursor) + return "Created MySQL trigger" + if connection.vendor == "sqlite": + _sqlite(cursor) + return "Created SQLite trigger" diff --git a/hc/api/tests/test_email_webhook.py b/hc/api/tests/test_email_webhook.py index d5ada880..64e80b3c 100644 --- a/hc/api/tests/test_email_webhook.py +++ b/hc/api/tests/test_email_webhook.py @@ -1,6 +1,7 @@ import json from django.test import TestCase + from hc.api.models import Check, Ping diff --git a/hc/api/tests/test_ensuretriggers.py b/hc/api/tests/test_ensuretriggers.py new file mode 100644 index 00000000..129f2661 --- /dev/null +++ b/hc/api/tests/test_ensuretriggers.py @@ -0,0 +1,27 @@ +from datetime import timedelta + +from django.test import TestCase +from django.utils import timezone + +from hc.api.management.commands.ensuretriggers import Command +from hc.api.models import Check + + +class EnsureTriggersTestCase(TestCase): + + def test_ensure_triggers(self): + Command().handle() + + check = Check.objects.create() + assert check.alert_after is None + + check.last_ping = timezone.now() + check.save() + check.refresh_from_db() + assert check.alert_after is not None + alert_after = check.alert_after + + check.last_ping += timedelta(days=1) + check.save() + check.refresh_from_db() + assert check.alert_after > alert_after diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 83114bab..459d483e 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -1,17 +1,14 @@ -from django.contrib.auth.models import User from django.core import mail -from django.test import TestCase -from hc.api.models import Channel, Check, Notification from mock import patch from requests.exceptions import ReadTimeout +from hc.api.models import Channel, Check, Notification +from hc.test import BaseTestCase + -class NotifyTestCase(TestCase): +class NotifyTestCase(BaseTestCase): def _setup_data(self, channel_kind, channel_value, email_verified=True): - self.alice = User(username="alice") - self.alice.save() - self.check = Check() self.check.status = "down" self.check.save() diff --git a/hc/api/tests/test_ping.py b/hc/api/tests/test_ping.py index 8a885778..23194888 100644 --- a/hc/api/tests/test_ping.py +++ b/hc/api/tests/test_ping.py @@ -1,4 +1,5 @@ from django.test import Client, TestCase + from hc.api.models import Check, Ping diff --git a/hc/api/tests/test_sendalerts.py b/hc/api/tests/test_sendalerts.py index 52c27666..f21ca11e 100644 --- a/hc/api/tests/test_sendalerts.py +++ b/hc/api/tests/test_sendalerts.py @@ -1,24 +1,23 @@ -from datetime import datetime +from datetime import timedelta + +from django.utils import timezone +from mock import patch -from django.contrib.auth.models import User -from django.test import TestCase from hc.api.management.commands.sendalerts import Command from hc.api.models import Check -from mock import patch +from hc.test import BaseTestCase -class SendAlertsTestCase(TestCase): +class SendAlertsTestCase(BaseTestCase): @patch("hc.api.management.commands.sendalerts.Command.handle_one") def test_it_handles_few(self, mock): - alice = User(username="alice") - alice.save() - + yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0, 10)] for name in names: - check = Check(user=alice, name=name) - check.alert_after = datetime(2000, 1, 1) + check = Check(user=self.alice, name=name) + check.alert_after = yesterday check.status = "up" check.save() diff --git a/hc/api/tests/test_status.py b/hc/api/tests/test_status.py index d4290d25..21292da6 100644 --- a/hc/api/tests/test_status.py +++ b/hc/api/tests/test_status.py @@ -1,4 +1,5 @@ from django.test import TestCase + from hc.api.models import Check diff --git a/hc/front/tests/test_add_channel.py b/hc/front/tests/test_add_channel.py index dd381e4e..106a2290 100644 --- a/hc/front/tests/test_add_channel.py +++ b/hc/front/tests/test_add_channel.py @@ -1,19 +1,11 @@ -from django.conf import settings -from django.contrib.auth.models import User -from django.test import TestCase -from hc.api.models import Channel - +from django.test.utils import override_settings -class AddChannelTestCase(TestCase): +from hc.api.models import Channel +from hc.test import BaseTestCase - def setUp(self): - super(AddChannelTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - settings.PUSHOVER_API_TOKEN = "bogus_token" - settings.PUSHOVER_SUBSCRIPTION_URL = "bogus_url" +@override_settings(PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="url") +class AddChannelTestCase(BaseTestCase): def test_it_works(self): url = "/integrations/add/" diff --git a/hc/front/tests/test_add_check.py b/hc/front/tests/test_add_check.py index 343a3bfb..0bb813a9 100644 --- a/hc/front/tests/test_add_check.py +++ b/hc/front/tests/test_add_check.py @@ -1,15 +1,8 @@ -from django.contrib.auth.models import User -from django.test import TestCase from hc.api.models import Check +from hc.test import BaseTestCase -class AddCheckTestCase(TestCase): - - def setUp(self): - super(AddCheckTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() +class AddCheckTestCase(BaseTestCase): def test_it_works(self): url = "/checks/add/" diff --git a/hc/front/tests/test_basics.py b/hc/front/tests/test_basics.py index 86425a79..98d08683 100644 --- a/hc/front/tests/test_basics.py +++ b/hc/front/tests/test_basics.py @@ -1,4 +1,5 @@ from django.test import TestCase + from hc.api.models import Check diff --git a/hc/front/tests/test_channel_checks.py b/hc/front/tests/test_channel_checks.py index 05730d6e..1dd8109c 100644 --- a/hc/front/tests/test_channel_checks.py +++ b/hc/front/tests/test_channel_checks.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Channel +from hc.test import BaseTestCase -class ChannelChecksTestCase(TestCase): +class ChannelChecksTestCase(BaseTestCase): def setUp(self): super(ChannelChecksTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.channel = Channel(user=self.alice, kind="email") self.channel.value = "alice@example.org" self.channel.save() diff --git a/hc/front/tests/test_log.py b/hc/front/tests/test_log.py index 49952762..d4f420be 100644 --- a/hc/front/tests/test_log.py +++ b/hc/front/tests/test_log.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Check, Ping +from hc.test import BaseTestCase -class LogTestCase(TestCase): +class LogTestCase(BaseTestCase): def setUp(self): super(LogTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice) self.check.save() diff --git a/hc/front/tests/test_my_checks.py b/hc/front/tests/test_my_checks.py index 36a91f31..389aab42 100644 --- a/hc/front/tests/test_my_checks.py +++ b/hc/front/tests/test_my_checks.py @@ -1,16 +1,11 @@ -from django.contrib.auth.models import User -from django.test import TestCase from hc.api.models import Check +from hc.test import BaseTestCase -class MyChecksTestCase(TestCase): +class MyChecksTestCase(BaseTestCase): def setUp(self): super(MyChecksTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice, name="Alice Was Here") self.check.save() diff --git a/hc/front/tests/test_remove_channel.py b/hc/front/tests/test_remove_channel.py index 22253d26..9ea87c9d 100644 --- a/hc/front/tests/test_remove_channel.py +++ b/hc/front/tests/test_remove_channel.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Channel +from hc.test import BaseTestCase -class RemoveChannelTestCase(TestCase): +class RemoveChannelTestCase(BaseTestCase): def setUp(self): super(RemoveChannelTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.channel = Channel(user=self.alice, kind="email") self.channel.value = "alice@example.org" self.channel.save() diff --git a/hc/front/tests/test_remove_check.py b/hc/front/tests/test_remove_check.py index 41d165c4..10dc73aa 100644 --- a/hc/front/tests/test_remove_check.py +++ b/hc/front/tests/test_remove_check.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Check +from hc.test import BaseTestCase -class RemoveCheckTestCase(TestCase): +class RemoveCheckTestCase(BaseTestCase): def setUp(self): super(RemoveCheckTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice) self.check.save() diff --git a/hc/front/tests/test_update_channel.py b/hc/front/tests/test_update_channel.py index 3e35d90b..d5900385 100644 --- a/hc/front/tests/test_update_channel.py +++ b/hc/front/tests/test_update_channel.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Channel, Check +from hc.test import BaseTestCase -class UpdateChannelTestCase(TestCase): +class UpdateChannelTestCase(BaseTestCase): def setUp(self): super(UpdateChannelTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice) self.check.save() diff --git a/hc/front/tests/test_update_name.py b/hc/front/tests/test_update_name.py index bbf37865..9ef8327d 100644 --- a/hc/front/tests/test_update_name.py +++ b/hc/front/tests/test_update_name.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Check +from hc.test import BaseTestCase -class UpdateNameTestCase(TestCase): +class UpdateNameTestCase(BaseTestCase): def setUp(self): super(UpdateNameTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice) self.check.save() diff --git a/hc/front/tests/test_update_timeout.py b/hc/front/tests/test_update_timeout.py index 0b06cdd8..10c5acbd 100644 --- a/hc/front/tests/test_update_timeout.py +++ b/hc/front/tests/test_update_timeout.py @@ -1,16 +1,13 @@ from django.contrib.auth.models import User -from django.test import TestCase + from hc.api.models import Check +from hc.test import BaseTestCase -class UpdateTimeoutTestCase(TestCase): +class UpdateTimeoutTestCase(BaseTestCase): def setUp(self): super(UpdateTimeoutTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.check = Check(user=self.alice) self.check.save() diff --git a/hc/front/tests/test_verify_email.py b/hc/front/tests/test_verify_email.py index 0bfb6069..3d24d217 100644 --- a/hc/front/tests/test_verify_email.py +++ b/hc/front/tests/test_verify_email.py @@ -1,16 +1,11 @@ -from django.contrib.auth.models import User -from django.test import TestCase from hc.api.models import Channel +from hc.test import BaseTestCase -class VerifyEmailTestCase(TestCase): +class VerifyEmailTestCase(BaseTestCase): def setUp(self): super(VerifyEmailTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.channel = Channel(user=self.alice, kind="email") self.channel.value = "alice@example.org" self.channel.save() diff --git a/hc/payments/tests/test_billing.py b/hc/payments/tests/test_billing.py index ce2aba13..64770d4b 100644 --- a/hc/payments/tests/test_billing.py +++ b/hc/payments/tests/test_billing.py @@ -1,17 +1,13 @@ -from django.contrib.auth.models import User -from django.test import TestCase -from hc.payments.models import Subscription from mock import Mock, patch +from hc.payments.models import Subscription +from hc.test import BaseTestCase + -class BillingTestCase(TestCase): +class BillingTestCase(BaseTestCase): def setUp(self): super(BillingTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" diff --git a/hc/payments/tests/test_cancel_plan.py b/hc/payments/tests/test_cancel_plan.py index adac22f8..c8c86833 100644 --- a/hc/payments/tests/test_cancel_plan.py +++ b/hc/payments/tests/test_cancel_plan.py @@ -1,17 +1,13 @@ -from django.contrib.auth.models import User -from django.test import TestCase -from hc.payments.models import Subscription from mock import patch +from hc.payments.models import Subscription +from hc.test import BaseTestCase + -class CancelPlanTestCase(TestCase): +class CancelPlanTestCase(BaseTestCase): def setUp(self): super(CancelPlanTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.plan_id = "P5" diff --git a/hc/payments/tests/test_create_plan.py b/hc/payments/tests/test_create_plan.py index 1139fc1f..29f73765 100644 --- a/hc/payments/tests/test_create_plan.py +++ b/hc/payments/tests/test_create_plan.py @@ -1,17 +1,11 @@ -from django.contrib.auth.models import User -from django.test import TestCase -from hc.accounts.models import Profile -from hc.payments.models import Subscription from mock import patch +from hc.accounts.models import Profile +from hc.payments.models import Subscription +from hc.test import BaseTestCase -class CreatePlanTestCase(TestCase): - def setUp(self): - super(CreatePlanTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() +class CreatePlanTestCase(BaseTestCase): def _setup_mock(self, mock): """ Set up Braintree calls that the controller will use. """ diff --git a/hc/payments/tests/test_get_client_token.py b/hc/payments/tests/test_get_client_token.py index d79aad1d..34491e78 100644 --- a/hc/payments/tests/test_get_client_token.py +++ b/hc/payments/tests/test_get_client_token.py @@ -1,16 +1,10 @@ -from django.contrib.auth.models import User -from django.test import TestCase -from hc.payments.models import Subscription from mock import patch +from hc.payments.models import Subscription +from hc.test import BaseTestCase -class GetClientTokenTestCase(TestCase): - def setUp(self): - super(GetClientTokenTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() +class GetClientTokenTestCase(BaseTestCase): @patch("hc.payments.views.braintree") def test_it_works(self, mock_braintree): diff --git a/hc/payments/tests/test_invoice.py b/hc/payments/tests/test_invoice.py index c0737a49..5a94c5ab 100644 --- a/hc/payments/tests/test_invoice.py +++ b/hc/payments/tests/test_invoice.py @@ -1,17 +1,13 @@ -from django.contrib.auth.models import User -from django.test import TestCase -from hc.payments.models import Subscription from mock import Mock, patch +from hc.payments.models import Subscription +from hc.test import BaseTestCase + -class InvoiceTestCase(TestCase): +class InvoiceTestCase(BaseTestCase): def setUp(self): super(InvoiceTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() - self.sub = Subscription(user=self.alice) self.sub.subscription_id = "test-id" self.sub.customer_id = "test-customer-id" diff --git a/hc/payments/tests/test_pricing.py b/hc/payments/tests/test_pricing.py index c617740e..57ee9745 100644 --- a/hc/payments/tests/test_pricing.py +++ b/hc/payments/tests/test_pricing.py @@ -1,15 +1,8 @@ -from django.contrib.auth.models import User -from django.test import TestCase from hc.payments.models import Subscription +from hc.test import BaseTestCase -class PricingTestCase(TestCase): - - def setUp(self): - super(PricingTestCase, self).setUp() - self.alice = User(username="alice", email="alice@example.org") - self.alice.set_password("password") - self.alice.save() +class PricingTestCase(BaseTestCase): def test_anonymous(self): r = self.client.get("/pricing/") diff --git a/hc/test.py b/hc/test.py new file mode 100644 index 00000000..9fdb1495 --- /dev/null +++ b/hc/test.py @@ -0,0 +1,11 @@ +from django.contrib.auth.models import User +from django.test import TestCase + + +class BaseTestCase(TestCase): + + def setUp(self): + super(BaseTestCase, self).setUp() + self.alice = User(username="alice", email="alice@example.org") + self.alice.set_password("password") + self.alice.save()