-
Notifications
You must be signed in to change notification settings - Fork 223
/
conftest.py
64 lines (47 loc) · 2.12 KB
/
conftest.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import pytest
from django.core.cache import cache
from django.contrib.auth.models import Group
from socorro import settings as socorro_settings
from socorro.libmarkus import set_up_metrics
# Load the socorro/tests/conftest.py file so webapp tests can use those pytest fixtures
pytest_plugins = ["socorro.tests.conftest"]
def pytest_sessionstart(session):
# Make sure Markus is set up with the RegisteredMetricsFilter
set_up_metrics(
statsd_host=socorro_settings.STATSD_HOST,
statsd_port=socorro_settings.STATSD_PORT,
hostname=socorro_settings.HOSTNAME,
debug=socorro_settings.LOCAL_DEV_ENV,
)
def pytest_runtest_setup(item):
# Clear the cache before every test; this reduces inter-test rate-limiting
cache.clear()
class UserHelper:
def __init__(self, django_user_model):
self._django_user_model = django_user_model
def create_user(self, username="example", password="pwd"):
user = self._django_user_model.objects.create_user(
username=username, password=password
)
return user
def create_protected_user(self, username="example", password="pwd"):
user = self.create_user(username=username, password=password)
group = Group.objects.get(name="Hackers")
user.groups.add(group)
assert user.has_perm("crashstats.view_pii")
assert user.has_perm("crashstats.view_rawdump")
return user
def create_protected_plus_user(self, username="example", password="pwd"):
user = self.create_user(username=username, password=password)
group = Group.objects.get(name="Hackers Plus")
user.groups.add(group)
assert user.has_perm("crashstats.view_pii")
assert user.has_perm("crashstats.view_rawdump")
assert user.has_perm("crashstats.run_custom_queries")
return user
@pytest.fixture
def user_helper(django_user_model):
return UserHelper(django_user_model)