forked from healthchecks/healthchecks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
76 lines (56 loc) · 2.57 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations
from typing import TYPE_CHECKING
from django.contrib.auth.models import User
from django.core.signing import TimestampSigner
from django.test import Client, TestCase
from hc.accounts.models import Member, Profile, Project
if TYPE_CHECKING:
# _MonkeyPatchedWSGIResponse is defined in django-stubs,
# we import with a "TestHttpResponse" alias.
# We list it in __all__ so subclasses can import and use it.
from django.test.client import _MonkeyPatchedWSGIResponse as TestHttpResponse
else:
from django.http import HttpResponse as TestHttpResponse
__all__ = ["BaseTestCase", "TestHttpResponse"]
class BaseTestCase(TestCase):
def setUp(self) -> None:
super().setUp()
self.csrf_client = Client(enforce_csrf_checks=True)
# Alice is a normal user for tests. Alice has team access enabled.
self.alice = User(username="alice", email="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.project = Project(owner=self.alice, api_key="X" * 32)
self.project.name = "Alices Project"
self.project.badge_key = self.alice.username
self.project.ping_key = "p" * 22
self.project.save()
self.profile = Profile(user=self.alice)
self.profile.sms_limit = 50
self.profile.save()
# Bob is on Alice's team and should have access to her stuff
self.bob = User(username="bob", email="bob@example.org")
self.bob.set_password("password")
self.bob.save()
self.bobs_project = Project(owner=self.bob)
self.bobs_project.badge_key = self.bob.username
self.bobs_project.save()
self.bobs_profile = Profile(user=self.bob)
self.bobs_profile.save()
self.bobs_membership = Member.objects.create(
user=self.bob, project=self.project, role=Member.Role.REGULAR
)
# Charlie should have no access to Alice's stuff
self.charlie = User(username="charlie", email="charlie@example.org")
self.charlie.set_password("password")
self.charlie.save()
self.charlies_project = Project(owner=self.charlie)
self.charlies_project.badge_key = self.charlie.username
self.charlies_project.save()
self.charlies_profile = Profile(user=self.charlie)
self.charlies_profile.save()
self.channels_url = "/projects/%s/integrations/" % self.project.code
def set_sudo_flag(self) -> None:
session = self.client.session
session["sudo"] = TimestampSigner().sign("active")
session.save()