forked from RedHatProductSecurity/osidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
145 lines (118 loc) · 3.99 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
import json
import pytest
from django.db.models.signals import (
m2m_changed,
post_delete,
post_save,
pre_delete,
pre_save,
)
from rest_framework.test import APIClient
def strip_private_bz_comments(body):
body = json.loads(body)
bugs = body.get("bugs", [])
if isinstance(bugs, dict):
for _, bug in bugs.items():
if "comments" in bug:
bug["comments"] = [
c for c in bug["comments"] if not c.get("is_private", False)
]
else:
for bug in bugs:
if "comments" in bug:
bug["comments"] = [
c for c in bug["comments"] if not c.get("is_private", False)
]
if bugs:
body["bugs"] = bugs
return json.dumps(body).encode("utf-8")
def clean_product_definitions_contacts(body):
body = json.loads(body)
contacts = body.get("contacts", [])
if contacts:
body["contacts"] = {
"foo": {"bz_username": "foo", "jboss_username": "foo"},
"bar": {"bz_username": "bar", "jboss_username": "bar"},
"baz": {"bz_username": "baz", "jboss_username": "baz"},
"ham": {"bz_username": "ham", "jboss_username": "ham"},
"bacon": {"bz_username": "bacon", "jboss_username": "bacon"},
"eggs": {"bz_username": "eggs", "jboss_username": "eggs"},
"cheese": {"bz_username": "cheese", "jboss_username": "cheese"},
"quux": {"bz_username": "quux", "jboss_username": "quux"},
}
return json.dumps(body).encode("utf-8")
def filter_response(response):
response["headers"].pop("Set-Cookie", None)
response["headers"].pop("x-ausername", None)
try:
response["body"]["string"] = strip_private_bz_comments(
response["body"]["string"]
)
response["body"]["string"] = clean_product_definitions_contacts(
response["body"]["string"]
)
except Exception:
...
return response
@pytest.fixture(scope="session")
def vcr_config():
return {
"filter_headers": [
"Authorization",
"Cookie",
],
"before_record_response": filter_response,
"filter_query_parameters": [
"Bugzilla_api_key",
],
"decode_compressed_response": True,
}
class TokenClient(APIClient):
def login(self, username, password):
r = self.post(
"/auth/token",
{"username": username, "password": password},
format="json",
)
self.credentials(HTTP_AUTHORIZATION=f"Bearer {r.data['access']}")
@pytest.fixture
def client():
return TokenClient()
@pytest.fixture
def ldap_test_username():
return "testuser"
@pytest.fixture
def ldap_test_password():
return "password"
@pytest.fixture
def auth_client(ldap_test_username, ldap_test_password):
client = TokenClient()
client.login(ldap_test_username, ldap_test_password)
return client
@pytest.fixture
def tokens(ldap_test_username, ldap_test_password):
client = APIClient()
r = client.post(
"/auth/token",
{"username": ldap_test_username, "password": ldap_test_password},
format="json",
)
return r.data
# https://www.cameronmaske.com/muting-django-signals-with-a-pytest-fixture/
@pytest.fixture(autouse=True) # Automatically use in tests.
def mute_signals(request):
# Skip applying, if marked with `enabled_signals`
if "enable_signals" in request.keywords:
return
signals = [pre_save, post_save, pre_delete, post_delete, m2m_changed]
restore = {}
for signal in signals:
# Temporally remove the signal's receivers (a.k.a attached functions)
restore[signal] = signal.receivers
signal.receivers = []
def restore_signals():
# When the test tears down, restore the signals.
for signal, receivers in restore.items():
signal.receivers = receivers
# Called after a test has finished.
request.addfinalizer(restore_signals)