-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_root.py
210 lines (174 loc) · 5.95 KB
/
test_root.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from unittest import mock
import pytest
from bs4 import BeautifulSoup
from flask import current_app
from noggin import __version__
from noggin.app import ipa_admin, talisman
@pytest.fixture
def nonfas_group(ipa_testing_config, app):
ipa_admin.group_add(a_cn="nonfas-group")
yield
ipa_admin.group_del("nonfas-group")
@pytest.fixture
def nonfas_user(ipa_testing_config, app):
ipa_admin.user_add(
"nonfas-user",
o_givenname="NonFAS",
o_sn="User",
o_cn="NonFAS User",
o_mail="nonfas-user@unit.tests",
o_userpassword="nonfas-user_password",
)
yield
ipa_admin.user_del("nonfas-user")
@pytest.fixture
def client_with_https(client):
current_app.debug = False
talisman.force_https = True
yield client
talisman.force_https = False
def test_root(client):
"""Test the root page"""
result = client.get('/')
assert result.status_code == 200
page = BeautifulSoup(result.data, 'html.parser')
assert page.title
assert page.title.string == 'noggin'
@pytest.mark.vcr()
def test_root_authenticated(client, logged_in_dummy_user):
"""Test the root page when the user is authenticated"""
result = client.get('/')
assert result.status_code == 302
assert result.location == "/user/dummy/"
def test_root_registration_closed(client, mocker):
"""Test the root page when registration is closed"""
mocker.patch.dict(current_app.config, {"REGISTRATION_OPEN": False})
result = client.get('/')
assert result.status_code == 200
page = BeautifulSoup(result.data, 'html.parser')
register_tab = page.select_one("#register")
alert = register_tab.select_one("div.alert-info")
assert alert is not None
assert alert.string.strip() == "Registration is closed at the moment."
def test_page_not_found(client):
"""Test the 404 error page"""
result = client.get('/dudemcpants/')
assert result.status_code == 404
@pytest.mark.vcr()
def test_search_json(client, logged_in_dummy_user, dummy_group):
"""Test the /search/json endpoint"""
result = client.get('/search/json?username=dummy&group=dummy-group')
assert result.status_code == 200
assert result.json == [
{'cn': 'Dummy User', 'uid': 'dummy', 'url': '/user/dummy/'},
{
'cn': 'dummy-group',
'description': 'The dummy-group group',
'url': '/group/dummy-group/',
},
]
@pytest.mark.vcr()
def test_search_json_empty(client, logged_in_dummy_user):
"""Test the /search/json endpoint with an empty search"""
result = client.get('/search/json')
assert result.status_code == 200
assert result.json == []
@pytest.mark.vcr()
def test_search_json_user_nonfas(client, logged_in_dummy_user, nonfas_user):
"""The /search/json endpoint should only return FAS users"""
result = client.get('/search/json?user=nonfas')
assert result.status_code == 200
assert result.json == []
@pytest.mark.vcr()
def test_search_json_group_nonfas(client, logged_in_dummy_user, nonfas_group):
"""The /search/json endpoint should only return FAS groups"""
result = client.get('/search/json?group=nonfas')
assert result.status_code == 200
assert result.json == []
@pytest.mark.vcr()
def test_healthz_liveness(client):
"""Test the /healthz/live check endpoint"""
result = client.get('/healthz/live')
assert result.status_code == 200
assert result.json == {"status": 200, "title": "OK"}
assert result.data == b'{"status": 200, "title": "OK"}'
@pytest.mark.vcr()
def test_healthz_readiness_ok(client):
"""Test the /healthz/ready check endpoint"""
result = client.get('/healthz/ready')
assert result.status_code == 200
assert result.json == {"status": 200, "title": "OK"}
assert result.data == b'{"status": 200, "title": "OK"}'
@pytest.mark.vcr()
def test_healthz_readiness_not_ok(client):
"""Test the /healthz/ready check endpoint when not ready (IPA disabled)"""
with mock.patch("noggin.app.ipa_admin.ping") as ipaping:
ipaping.side_effect = Exception()
result = client.get('/healthz/ready')
assert result.status_code == 503
assert result.json == {
"status": 503,
"title": "Can't connect to the FreeIPA Server",
}
assert (
result.data
== b'{"status": 503, "title": "Can\'t connect to the FreeIPA Server"}'
)
@pytest.mark.vcr()
def test_healthz_no_https(client_with_https):
"""Test that the healthz endpoints don't require HTTPS"""
# Make sure we force HTTPS on regular endpoints
result = client_with_https.get('/')
assert result.status_code == 302
# The heathlz endpoints should not be redirected
result = client_with_https.get('/healthz/live')
assert result.status_code == 200
result = client_with_https.get('/healthz/ready')
assert result.status_code == 200
def test_version(client):
"""Test the version in the footer"""
result = client.get('/')
assert result.status_code == 200
page = BeautifulSoup(result.data, 'html.parser')
powered_by = page.select_one("footer div div small")
assert (
powered_by.prettify()
== """
<small>
Powered by
<a href="https://github.com/fedora-infra/noggin">
noggin
</a>
v{}
</small>
""".strip().format(
__version__
)
)
def test_version_openshift(mocker, client):
"""Test the version in the footer in openshift"""
mocker.patch.dict(
"noggin.controller.os.environ",
{
"OPENSHIFT_BUILD_REFERENCE": "tests",
"OPENSHIFT_BUILD_COMMIT": "abcdef0123456789",
},
)
result = client.get('/')
assert result.status_code == 200
page = BeautifulSoup(result.data, 'html.parser')
powered_by = page.select_one("footer div div small")
assert (
powered_by.prettify()
== """
<small>
Powered by
<a href="https://github.com/fedora-infra/noggin">
noggin
</a>
v{} (tests:abcdef0)
</small>
""".strip().format(
__version__
)
)