Newer
Older
# copyright 2022-2024 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact https://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
from test.util import ApiBaseTC, check_missing_custom_header_response
class ApiLoginTC(ApiBaseTC):
def test_successful_login_returns_204(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="application/json",
headers=self.custom_headers,
status=204,
)
assert response.body == b""
def test_wrong_password_returns_401(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": "INVALID PASSWORD"}),
content_type="application/json",
headers=self.custom_headers,
status=401,
).json
assert response == {
"data": None,
"message": "Invalid credentials",
"title": "AuthenticationError",
}
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
def test_wrong_login_returns_401(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps(
{"login": "a_login_that_does_not_exist", "password": "PASSWORD"}
),
content_type="application/json",
headers=self.custom_headers,
status=401,
).json
assert response == {
"data": None,
"message": "Invalid credentials",
"title": "AuthenticationError",
}
def test_invalid_login_type(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": 5, "password": "PASSWORD"}),
content_type="application/json",
headers=self.custom_headers,
status=400,
).json
assert response == {
"data": [
{
"exception": "ValidationError",
"field": "login",
"message": "5 is not of type 'string'",
}
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
def test_invalid_password_type(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": "login", "password": 5}),
content_type="application/json",
headers=self.custom_headers,
status=400,
).json
assert response == {
"data": [
{
"exception": "ValidationError",
"field": "password",
"message": "5 is not of type 'string'",
}
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
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
def test_wrong_content_type_returns_400(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="text/plain",
headers=self.custom_headers,
status=400,
).json
assert response == {
"data": [
{
"exception": "MediaTypeNotFound",
"message": "Content for the following mimetype not found: "
"text/plain. Valid mimetypes: ['application/json']",
}
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
def test_wrong_params_returns_400(self):
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"test": "testing"}),
content_type="application/json",
headers=self.custom_headers,
status=400,
).json
assert response == {
"data": [
{
"exception": "ValidationError",
"field": "login/password",
"message": "'login' is a required property",
},
{
"exception": "ValidationError",
"field": "login/password",
"message": "'password' is a required property",
},
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
def test_missing_login_data_returns_400(self):
response = self.webapp.post(
self.get_api_path("login"),
content_type="application/json",
headers=self.custom_headers,
status=400,
).json
assert response == {
"data": [
{
"exception": "MissingRequiredRequestBody",
"message": "Missing required request body",
}
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
def test_missing_custom_headers_returns_400(self):
response = self.webapp.post(
self.get_api_path("login"),
status=400,
).json
check_missing_custom_header_response(response)
response = self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="application/json",
status=400,
).json
check_missing_custom_header_response(response)
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class ApiLoginDisabledDefaultTC(ApiBaseTC):
settings = {
"cubicweb.includes": ["cubicweb.pyramid.auth"],
}
def test_login_is_disabled(self):
"""check that it is disabled by default"""
self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="application/json",
headers=self.custom_headers,
status=404,
)
class ApiLoginDisabledManualTC(ApiBaseTC):
settings = {
"cubicweb.includes": ["cubicweb.pyramid.auth"],
"cubicweb_api.enable_login_route": "no",
}
def test_login_is_disabled(self):
"""check that it is disabled when setting to 'no'"""
self.webapp.post(
self.get_api_path("login"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="application/json",
headers=self.custom_headers,
status=404,
)