@ -0,0 +1,10 @@ | |||
class TeamAccessMiddleware(object): | |||
def process_request(self, request): | |||
if not request.user.is_authenticated(): | |||
return | |||
profile = request.user.profile | |||
if profile.current_team: | |||
request.team = profile.current_team | |||
else: | |||
request.team = profile |
@ -0,0 +1,21 @@ | |||
# -*- coding: utf-8 -*- | |||
# Generated by Django 1.9 on 2016-05-09 10:34 | |||
from __future__ import unicode_literals | |||
from django.db import migrations, models | |||
import django.db.models.deletion | |||
class Migration(migrations.Migration): | |||
dependencies = [ | |||
('accounts', '0005_auto_20160509_0801'), | |||
] | |||
operations = [ | |||
migrations.AddField( | |||
model_name='profile', | |||
name='current_team', | |||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.Profile'), | |||
), | |||
] |
@ -2,11 +2,11 @@ from django.contrib.auth.models import User | |||
from django.core import mail | |||
from hc.test import BaseTestCase | |||
from hc.accounts.models import Profile, Member | |||
from hc.accounts.models import Member | |||
from hc.api.models import Check | |||
class LoginTestCase(BaseTestCase): | |||
class ProfileTestCase(BaseTestCase): | |||
def test_it_sends_set_password_link(self): | |||
self.client.login(username="[email protected]", password="password") | |||
@ -16,8 +16,9 @@ class LoginTestCase(BaseTestCase): | |||
assert r.status_code == 302 | |||
# profile.token should be set now | |||
profile = Profile.objects.for_user(self.alice) | |||
self.assertTrue(len(profile.token) > 10) | |||
self.alice.profile.refresh_from_db() | |||
token = self.alice.profile.token | |||
self.assertTrue(len(token) > 10) | |||
# And an email should have been sent | |||
self.assertEqual(len(mail.outbox), 1) | |||
@ -31,8 +32,9 @@ class LoginTestCase(BaseTestCase): | |||
r = self.client.post("/accounts/profile/", form) | |||
assert r.status_code == 200 | |||
profile = Profile.objects.for_user(self.alice) | |||
self.assertTrue(len(profile.api_key) > 10) | |||
self.alice.profile.refresh_from_db() | |||
api_key = self.alice.profile.api_key | |||
self.assertTrue(len(api_key) > 10) | |||
def test_it_revokes_api_key(self): | |||
self.client.login(username="[email protected]", password="password") | |||
@ -41,15 +43,14 @@ class LoginTestCase(BaseTestCase): | |||
r = self.client.post("/accounts/profile/", form) | |||
assert r.status_code == 200 | |||
profile = Profile.objects.for_user(self.alice) | |||
self.assertEqual(profile.api_key, "") | |||
self.alice.profile.refresh_from_db() | |||
self.assertEqual(self.alice.profile.api_key, "") | |||
def test_it_sends_report(self): | |||
check = Check(name="Test Check", user=self.alice) | |||
check.save() | |||
profile = Profile.objects.for_user(self.alice) | |||
profile.send_report() | |||
self.alice.profile.send_report() | |||
# And an email should have been sent | |||
self.assertEqual(len(mail.outbox), 1) | |||
@ -65,8 +66,7 @@ class LoginTestCase(BaseTestCase): | |||
r = self.client.post("/accounts/profile/", form) | |||
assert r.status_code == 200 | |||
profile = Profile.objects.for_user(self.alice) | |||
member = profile.member_set.get() | |||
member = self.alice.profile.member_set.get() | |||
self.assertEqual(member.user.email, "[email protected]") | |||
@ -81,7 +81,7 @@ class LoginTestCase(BaseTestCase): | |||
bob = User(username="bob", email="[email protected]") | |||
bob.save() | |||
m = Member(team=Profile.objects.for_user(self.alice), user=bob) | |||
m = Member(team=self.alice.profile, user=bob) | |||
m.save() | |||
form = {"remove_team_member": "1", "email": "[email protected]"} | |||
@ -89,3 +89,13 @@ class LoginTestCase(BaseTestCase): | |||
assert r.status_code == 200 | |||
self.assertEqual(Member.objects.count(), 0) | |||
def test_it_sets_team_name(self): | |||
self.client.login(username="[email protected]", password="password") | |||
form = {"set_team_name": "1", "team_name": "Alpha Team"} | |||
r = self.client.post("/accounts/profile/", form) | |||
assert r.status_code == 200 | |||
self.alice.profile.refresh_from_db() | |||
self.assertEqual(self.alice.profile.team_name, "Alpha Team") |
@ -0,0 +1,37 @@ | |||
from django.contrib.auth.models import User | |||
from hc.test import BaseTestCase | |||
from hc.accounts.models import Member, Profile | |||
class SwitchTeamTestCase(BaseTestCase): | |||
def setUp(self): | |||
super(SwitchTeamTestCase, self).setUp() | |||
self.bob = User(username="bob", email="[email protected]") | |||
self.bob.set_password("password") | |||
self.bob.save() | |||
bobs_profile = Profile(user=self.bob) | |||
bobs_profile.save() | |||
m = Member(team=bobs_profile, user=self.alice) | |||
m.save() | |||
def test_it_switches(self): | |||
self.client.login(username="[email protected]", password="password") | |||
url = "/accounts/switch_team/%s/" % self.bob.username | |||
r = self.client.get(url, follow=True) | |||
self.assertContains(r, "[email protected]") | |||
def test_it_checks_team_membership(self): | |||
self.client.login(username="[email protected]", password="password") | |||
url = "/accounts/switch_team/%s/" % self.bob.username | |||
r = self.client.get(url) | |||
self.assertEqual(r.status_code, 403) |
@ -3,14 +3,13 @@ from datetime import timedelta as td | |||
from hc.api.models import Check, User | |||
from hc.test import BaseTestCase | |||
from hc.accounts.models import Profile | |||
class ListChecksTestCase(BaseTestCase): | |||
def setUp(self): | |||
super(ListChecksTestCase, self).setUp() | |||
self.profile = Profile(user=self.alice, api_key="abc") | |||
self.profile.save() | |||
self.checks = [ | |||
Check(user=self.alice, name="Alice 1", timeout=td(seconds=3600), grace=td(seconds=900)), | |||
Check(user=self.alice, name="Alice 2", timeout=td(seconds=86400), grace=td(seconds=3600)), | |||
@ -40,8 +39,9 @@ class ListChecksTestCase(BaseTestCase): | |||
bob = User(username="bob", email="[email protected]") | |||
bob.save() | |||
bob_check = Check(user=bob, name="Bob 1") | |||
bob_check.save() | |||
r = self.get("/api/v1/checks/", { "api_key": "abc" }) | |||
r = self.get("/api/v1/checks/", {"api_key": "abc"}) | |||
self.assertEqual(len(r.json()["checks"]), 2) | |||
checks = { check["name"]: check for check in r.json()["checks"] } | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Channel | |||
from hc.test import BaseTestCase | |||
@ -20,14 +18,10 @@ class ChannelChecksTestCase(BaseTestCase): | |||
self.assertContains(r, "Assign Checks to Channel", status_code=200) | |||
def test_it_checks_owner(self): | |||
mallory = User(username="mallory", email="[email protected]") | |||
mallory.set_password("password") | |||
mallory.save() | |||
# channel does not belong to mallory so this should come back | |||
# with 403 Forbidden: | |||
url = "/integrations/%s/checks/" % self.channel.code | |||
self.client.login(username="mallory@example.org", password="password") | |||
self.client.login(username="[email protected]", password="password") | |||
r = self.client.get(url) | |||
assert r.status_code == 403 | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Check, Ping | |||
from hc.test import BaseTestCase | |||
@ -37,10 +35,6 @@ class LogTestCase(BaseTestCase): | |||
assert r.status_code == 404 | |||
def test_it_checks_ownership(self): | |||
charlie = User(username="charlie", email="[email protected]") | |||
charlie.set_password("password") | |||
charlie.save() | |||
url = "/checks/%s/log/" % self.check.code | |||
self.client.login(username="[email protected]", password="password") | |||
r = self.client.get(url) | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Channel | |||
from hc.test import BaseTestCase | |||
@ -31,11 +29,7 @@ class RemoveChannelTestCase(BaseTestCase): | |||
def test_it_checks_owner(self): | |||
url = "/integrations/%s/remove/" % self.channel.code | |||
mallory = User(username="mallory", email="[email protected]") | |||
mallory.set_password("password") | |||
mallory.save() | |||
self.client.login(username="[email protected]", password="password") | |||
self.client.login(username="[email protected]", password="password") | |||
r = self.client.post(url) | |||
assert r.status_code == 403 | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Check | |||
from hc.test import BaseTestCase | |||
@ -30,11 +28,7 @@ class RemoveCheckTestCase(BaseTestCase): | |||
def test_it_checks_owner(self): | |||
url = "/checks/%s/remove/" % self.check.code | |||
mallory = User(username="mallory", email="[email protected]") | |||
mallory.set_password("password") | |||
mallory.save() | |||
self.client.login(username="[email protected]", password="password") | |||
self.client.login(username="[email protected]", password="password") | |||
r = self.client.post(url) | |||
assert r.status_code == 403 | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Channel, Check | |||
from hc.test import BaseTestCase | |||
@ -31,35 +29,27 @@ class UpdateChannelTestCase(BaseTestCase): | |||
assert checks[0].code == self.check.code | |||
def test_it_checks_channel_user(self): | |||
mallory = User(username="mallory", email="[email protected]") | |||
mallory.set_password("password") | |||
mallory.save() | |||
payload = {"channel": self.channel.code} | |||
self.client.login(username="mallory@example.org", password="password") | |||
self.client.login(username="[email protected]", password="password") | |||
r = self.client.post("/integrations/", data=payload) | |||
# self.channel does not belong to mallory, this should fail-- | |||
# self.channel does not belong to charlie, this should fail-- | |||
assert r.status_code == 403 | |||
def test_it_checks_check_user(self): | |||
mallory = User(username="mallory", email="[email protected]") | |||
mallory.set_password("password") | |||
mallory.save() | |||
mc = Channel(user=mallory, kind="email") | |||
mc.email = "[email protected]" | |||
mc.save() | |||
charlies_channel = Channel(user=self.charlie, kind="email") | |||
charlies_channel.email = "[email protected]" | |||
charlies_channel.save() | |||
payload = { | |||
"channel": mc.code, | |||
"channel": charlies_channel.code, | |||
"check-%s" % self.check.code: True | |||
} | |||
self.client.login(username="mallory@example.org", password="password") | |||
self.client.login(username="charlie@example.org", password="password") | |||
r = self.client.post("/integrations/", data=payload) | |||
# mc belongs to mallorym but self.check does not-- | |||
# mc belongs to charlie but self.check does not-- | |||
assert r.status_code == 403 | |||
def test_it_handles_missing_channel(self): | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Check | |||
from hc.test import BaseTestCase | |||
@ -23,11 +21,6 @@ class UpdateNameTestCase(BaseTestCase): | |||
assert check.name == "Alice Was Here" | |||
def test_it_checks_ownership(self): | |||
charlie = User(username="charlie", email="[email protected]") | |||
charlie.set_password("password") | |||
charlie.save() | |||
url = "/checks/%s/name/" % self.check.code | |||
payload = {"name": "Charlie Sent This"} | |||
@ -1,5 +1,3 @@ | |||
from django.contrib.auth.models import User | |||
from hc.api.models import Check | |||
from hc.test import BaseTestCase | |||
@ -41,10 +39,6 @@ class UpdateTimeoutTestCase(BaseTestCase): | |||
assert r.status_code == 404 | |||
def test_it_checks_ownership(self): | |||
charlie = User(username="charlie", email="[email protected]") | |||
charlie.set_password("password") | |||
charlie.save() | |||
url = "/checks/%s/timeout/" % self.check.code | |||
payload = {"timeout": 3600, "grace": 60} | |||
@ -1,11 +1,26 @@ | |||
from django.contrib.auth.models import User | |||
from django.test import TestCase | |||
from hc.accounts.models import Profile | |||
class BaseTestCase(TestCase): | |||
def setUp(self): | |||
super(BaseTestCase, self).setUp() | |||
# Normal user for tests | |||
self.alice = User(username="alice", email="[email protected]") | |||
self.alice.set_password("password") | |||
self.alice.save() | |||
self.profile = Profile(user=self.alice, api_key="abc") | |||
self.profile.save() | |||
# "malicious user for tests | |||
self.charlie = User(username="charlie", email="[email protected]") | |||
self.charlie.set_password("password") | |||
self.charlie.save() | |||
charlies_profile = Profile(user=self.charlie) | |||
charlies_profile.save() |