Browse Source

Adding tests for POST /channels/

pull/7/head
Pēteris Caune 9 years ago
parent
commit
8355d7c13b
3 changed files with 75 additions and 11 deletions
  1. +2
    -3
      hc/api/models.py
  2. +66
    -0
      hc/front/tests/test_update_channel.py
  3. +7
    -8
      hc/front/views.py

+ 2
- 3
hc/api/models.py View File

@ -65,9 +65,8 @@ class Check(models.Model):
return "down"
def assign_all_channels(self):
for channel in Channel.objects.filter(user=self.user):
channel.checks.add(self)
channel.save()
channels = Channel.objects.filter(user=self.user)
self.channel_set.add(*channels)
class Ping(models.Model):


+ 66
- 0
hc/front/tests/test_update_channel.py View File

@ -0,0 +1,66 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel, Check
class UpdateChannelTestCase(TestCase):
def setUp(self):
self.alice = User(username="alice")
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 = "[email protected]"
self.channel.save()
def test_it_works(self):
payload = {
"channel": self.channel.code,
"check-%s" % self.check.code: True
}
self.client.login(username="alice", password="password")
r = self.client.post("/channels/", data=payload)
assert r.status_code == 302
channel = Channel.objects.get(code=self.channel.code)
checks = channel.checks.all()
assert len(checks) == 1
assert checks[0].code == self.check.code
def test_it_checks_channel_user(self):
mallory = User(username="mallory")
mallory.set_password("password")
mallory.save()
payload = {"channel": self.channel.code}
self.client.login(username="mallory", password="password")
r = self.client.post("/channels/", data=payload)
# self.channel does not belong to mallory, this should fail--
assert r.status_code == 403
def test_it_checks_check_user(self):
mallory = User(username="mallory")
mallory.set_password("password")
mallory.save()
mc = Channel(user=mallory, kind="email")
mc.email = "[email protected]"
mc.save()
payload = {
"channel": mc.code,
"check-%s" % self.check.code: True
}
self.client.login(username="mallory", password="password")
r = self.client.post("/channels/", data=payload)
# mc belongs to mallorym but self.check does not--
assert r.status_code == 403

+ 7
- 8
hc/front/views.py View File

@ -179,21 +179,21 @@ def channels(request):
if request.method == "POST":
code = request.POST["channel"]
channel = Channel.objects.get(code=code)
assert channel.user == request.user
if channel.user != request.user:
return HttpResponseForbidden()
channel.checks = []
print (request.POST)
new_checks = []
for key in request.POST:
if key.startswith("check-"):
code = key[6:]
check = Check.objects.get(code=code)
assert check.user == request.user
channel.checks.add(check)
if check.user != request.user:
return HttpResponseForbidden()
new_checks.append(check)
channel.save()
channel.checks = new_checks
return redirect("hc-channels")
channels = Channel.objects.filter(user=request.user).order_by("created")
num_checks = Check.objects.filter(user=request.user).count()
@ -216,7 +216,6 @@ def add_channel(request):
checks = Check.objects.filter(user=request.user)
channel.checks.add(*checks)
channel.save()
return redirect("hc-channels")


Loading…
Cancel
Save