-
Notifications
You must be signed in to change notification settings - Fork 12
/
conftest.py
188 lines (152 loc) · 4.95 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pathlib
import dramatiq
import factory
import pytest
from django.contrib.auth import get_user_model
from django.contrib.auth import models as auth_models
from guardian.shortcuts import assign_perm
from pytest_factoryboy import register
from apps.accounts import factories as ac_factories
from apps.accounts import models as ac_models
from apps.accounts.permissions import manage_datacenter, manage_provider
from apps.greencheck import factories as gc_factories
from apps.greencheck.models import GreencheckASN, GreencheckIp
# https://factoryboy.readthedocs.io/en/stable/recipes.html#using-reproducible-randomness
factory.random.reseed_random("venture not into the land of flaky tests")
User = get_user_model()
register(gc_factories.UserFactory)
register(gc_factories.SiteCheckFactory)
register(gc_factories.GreencheckFactory)
register(gc_factories.ServiceFactory)
register(gc_factories.HostingProviderFactory)
register(gc_factories.GreenIpFactory)
register(gc_factories.GreenASNFactory)
register(gc_factories.GreenDomainFactory)
register(gc_factories.SiteCheckFactory)
register(ac_factories.SupportingEvidenceFactory)
register(ac_factories.ProviderRequestFactory)
@pytest.fixture
def provider_groups():
return auth_models.Group.objects.filter(name__in=["datacenter", "hostingprovider"])
@pytest.fixture
def sample_hoster_user(provider_groups):
"""A user created when they register"""
user = gc_factories.UserFactory.build(username="joebloggs", email="[email protected]")
user.set_password("topSekrit")
user.save()
user.groups.add(*provider_groups)
[grp.save() for grp in provider_groups]
user.save()
return user
@pytest.fixture
def greenweb_staff_user():
"""
Create a user with the permissions and group ownership
of internal green web staff, who are paid to maintain
the database
"""
user = gc_factories.UserFactory.build(
username="greenweb_staff", email="[email protected]"
)
user.set_password("topSekrit")
# give them an id so we can set up many to many relationships with groups
user.save()
groups = auth_models.Group.objects.filter(
name__in=["admin", "datacenter", "hostingprovider"]
)
user.groups.add(*groups)
[grp.save() for grp in groups]
user.save()
return user
@pytest.fixture
def sample_sitecheck():
return gc_factories.SiteCheckFactory.build()
@pytest.fixture
def hosting_provider():
return ac_models.Hostingprovider(
archived=False,
country="US",
customer=False,
icon="",
iconurl="",
model="groeneenergie",
name="Amazon US West",
partner="",
showonwebsite=True,
website="http://aws.amazon.com",
)
@pytest.fixture
def user_with_provider(sample_hoster_user):
"""
Return a user that's been persisted to the database,
and has a hosting provider associated with it
"""
hp = gc_factories.HostingProviderFactory.create(created_by=sample_hoster_user)
assign_perm(manage_provider.codename, sample_hoster_user, hp)
return sample_hoster_user
@pytest.fixture
def hosting_provider_with_sample_user(hosting_provider, sample_hoster_user):
"""
Return a hosting provider that's been persisted to the database,
and has a user associated with it
"""
hosting_provider.created_by = sample_hoster_user
hosting_provider.save()
assign_perm(manage_provider.codename, sample_hoster_user, hosting_provider)
return hosting_provider
@pytest.fixture
def datacenter(sample_hoster_user):
dc = ac_models.Datacenter(
country="NL",
dc12v=False,
greengrid=True,
mja3=True,
model="groeneenergie",
name="KPN DC2",
pue=1.3,
residualheat=False,
showonwebsite=True,
temperature=22,
temperature_type="C",
created_by=sample_hoster_user,
virtual=False,
website="http://www.xs4all.nl/zakelijk/colocation/datacenters/dc2.php",
)
dc.save()
assign_perm(manage_datacenter.codename, sample_hoster_user, dc)
return dc
@pytest.fixture
def green_ip(hosting_provider):
hosting_provider.save()
return GreencheckIp.objects.create(
active=True,
ip_start="172.217.168.238",
ip_end="172.217.168.239",
hostingprovider=hosting_provider,
)
@pytest.fixture
def green_asn(hosting_provider):
hosting_provider.save()
return GreencheckASN(
active=True,
asn=12345,
hostingprovider=hosting_provider,
)
@pytest.fixture
def csv_file():
this_file = pathlib.Path(__file__)
return this_file.parent.joinpath(
"apps", "greencheck", "fixtures", "test_dataset_conftest.csv"
)
# test broker, so we don't need to rely on rabbit for tests
@pytest.fixture
def broker():
broker = dramatiq.get_broker()
broker.flush_all()
return broker
@pytest.fixture
def worker(broker):
worker = dramatiq.Worker(broker, worker_timeout=100)
worker.start()
yield worker
worker.stop()