Skip to content
Snippets Groups Projects
Commit cdbf7bfa0738 authored by Arnaud Vergnet's avatar Arnaud Vergnet :sun_with_face:
Browse files

test: separate tests in files

parent 98fd04dcce96
No related branches found
No related tags found
1 merge request!62tests: improve coverage
......@@ -13,131 +13,7 @@
#
# 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/>.
"""cubicweb-api automatic tests
uncomment code below if you want to activate automatic test for your cube:
.. sourcecode:: python
from cubicweb.devtools.testlib import AutomaticWebTest
class AutomaticWebTest(AutomaticWebTest):
'''provides `to_test_etypes` and/or `list_startup_views` implementation
to limit test scope
'''
def to_test_etypes(self):
'''only test views for entities of the returned types'''
return set(('My', 'Cube', 'Entity', 'Types'))
def list_startup_views(self):
'''only test startup views of the returned identifiers'''
return ('some', 'startup', 'views')
"""
import json
from cubicweb.schema_exporters import JSONSchemaExporter
from test.util import BASE_URL, ApiBaseTC
class ApiTC(ApiBaseTC):
@classmethod
def init_config(cls, config):
super().init_config(config)
config.global_set_option("base-url", BASE_URL)
def test_get_schema(self):
schema = self.webapp.get(
self.get_api_path("schema"),
headers=self.custom_headers,
).json
exporter = JSONSchemaExporter()
exported_schema = exporter.export_as_dict(self.repo.schema)
assert exported_schema == schema
def test_successful_login_returns_204(self):
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,
)
def test_wrong_login_returns_401(self):
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,
)
def test_missing_custom_headers_returns_400(self):
expected_response = {
"data": [
{
"exception": "MissingRequiredParameter",
"field": "X-Client-Name",
"message": "Missing required parameter: X-Client-Name",
},
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
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
assert response == expected_response
response = self.webapp.post(
self.get_api_path("rql"),
params=json.dumps([{"query": "test", "params": {}}]),
content_type="application/json",
status=400,
).json
assert response == expected_response
response = self.webapp.post(
self.get_api_path("rql"),
params={"queries": ""},
content_type="multipart/form-data",
status=400,
).json
assert response == expected_response
def test_logged_user_can_insert_data(self):
self.login_request()
group_eid = self.webapp.post(
self.get_api_path("rql"),
params=json.dumps(
[
{
"query": "INSERT CWGroup G: G name 'test-group'",
}
]
),
content_type="application/json",
headers=self.custom_headers,
status=200,
).json[0][0][0]
with self.admin_access.repo_cnx() as cnx:
assert cnx.entity_from_eid(group_eid).name == "test-group"
def test_current_user_returns_user_as_json(self):
self.login_request()
response = self.webapp.get(
self.get_api_path("current-user"), headers=self.custom_headers, status=200
).json
assert response["login"] == self.admlogin
assert response["dcTitle"] == self.admlogin
assert isinstance(response["eid"], int)
from test.util import ApiBaseTC
class ApiMountedOnBaseUrlTC(ApiBaseTC):
......@@ -170,22 +46,6 @@
)
class ApiLoginDisabledTC(ApiBaseTC):
settings = {
"cubicweb.includes": ["cubicweb.pyramid.auth"],
}
def test_login_is_disabled(self):
"""we 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,
)
if __name__ == "__main__":
from unittest import main
......
# copyright 2022-2023 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/>.
from test.util import ApiBaseTC
class ApiCurrentUserTC(ApiBaseTC):
def test_current_user_returns_user_as_json(self):
self.login_request()
response = self.webapp.get(
self.get_api_path("current-user"), headers=self.custom_headers, status=200
).json
assert response["login"] == self.admlogin
assert response["dcTitle"] == self.admlogin
assert isinstance(response["eid"], int)
# copyright 2022-2023 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
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_login_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",
}
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"),
params=json.dumps({"login": self.admlogin, "password": self.admpassword}),
content_type="application/json",
status=400,
).json
assert response == {
"data": [
{
"exception": "MissingRequiredParameter",
"field": "X-Client-Name",
"message": "Missing required parameter: X-Client-Name",
},
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
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,
)
......@@ -179,6 +179,26 @@
"title": "OpenApiValidationError",
}
def test_missing_custom_headers_returns_400(self):
response = self.webapp.post(
self.get_api_path("rql"),
params=self.get_body([{"query": "test", "params": {}}]),
content_type=self.content_type,
status=400,
).json
assert response == {
"data": [
{
"exception": "MissingRequiredParameter",
"field": "X-Client-Name",
"message": "Missing required parameter: X-Client-Name",
},
],
"message": "Your request could not be validated against the openapi "
"specification.",
"title": "OpenApiValidationError",
}
class ApiRqlMultipartTC(ApiRqlTC):
@property
......
# copyright 2022-2023 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/>.
from test.util import ApiBaseTC
from cubicweb.schema_exporters import JSONSchemaExporter
class ApiSchemaTC(ApiBaseTC):
def test_get_schema(self):
schema = self.webapp.get(
self.get_api_path("schema"),
headers=self.custom_headers,
).json
exporter = JSONSchemaExporter()
exported_schema = exporter.export_as_dict(self.repo.schema)
assert exported_schema == schema
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment