2018-03-17 17:20:31 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
2018-06-17 01:06:58 +00:00
|
|
|
# Copyright © 2018 Maël Kervella
|
2018-03-17 17:20:31 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Defines the test suite for the API
|
2018-04-14 11:49:39 +00:00
|
|
|
"""
|
2018-03-17 17:20:31 +00:00
|
|
|
|
2018-05-23 15:26:41 +00:00
|
|
|
import json
|
|
|
|
from rest_framework.test import APITestCase
|
|
|
|
from requests import codes
|
|
|
|
|
|
|
|
#import cotisations.models as cotisations
|
|
|
|
#import machines.models as machines
|
|
|
|
#import topologie.models as topologie
|
|
|
|
import users.models as users
|
|
|
|
|
|
|
|
|
|
|
|
class APIEndpointsTestCase(APITestCase):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Test case to test that all endpoints are reachable with respects to
|
|
|
|
authentication and permission checks.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
no_auth_endpoints: A list of endpoints that should be reachable
|
|
|
|
without authentication.
|
|
|
|
auth_no_perm_endpoints: A list of endpoints that should be reachable
|
|
|
|
when being authenticated but without permissions.
|
|
|
|
auth_perm_endpoints: A list of endpoints that should be reachable
|
|
|
|
when being authenticated and having the correct permissions.
|
|
|
|
stduser: A standard user with no permission used for the tests and
|
|
|
|
initialized at the beggining of this test case.
|
|
|
|
superuser: A superuser (with all permissions) used for the tests and
|
|
|
|
initialized at the beggining of this test case.
|
|
|
|
"""
|
2018-05-23 15:26:41 +00:00
|
|
|
no_auth_endpoints = [
|
|
|
|
'/api/'
|
|
|
|
]
|
|
|
|
auth_no_perm_endpoints = []
|
|
|
|
auth_perm_endpoints = [
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/cotisations/article/',
|
|
|
|
# '/api/cotisations/article/<pk>/',
|
|
|
|
'/api/cotisations/banque/',
|
|
|
|
# '/api/cotisations/banque/<pk>/',
|
|
|
|
'/api/cotisations/cotisation/',
|
|
|
|
# '/api/cotisations/cotisation/<pk>/',
|
|
|
|
'/api/cotisations/facture/',
|
|
|
|
# '/api/cotisations/facture/<pk>/',
|
|
|
|
'/api/cotisations/paiement/',
|
|
|
|
# '/api/cotisations/paiement/<pk>/',
|
|
|
|
'/api/cotisations/vente/',
|
|
|
|
# '/api/cotisations/vente/<pk>/',
|
|
|
|
'/api/machines/domain/',
|
|
|
|
# '/api/machines/domain/<pk>/',
|
|
|
|
'/api/machines/extension/',
|
|
|
|
# '/api/machines/extension/<pk>/',
|
|
|
|
'/api/machines/interface/',
|
|
|
|
# '/api/machines/interface/<pk>/',
|
|
|
|
'/api/machines/iplist/',
|
|
|
|
# '/api/machines/iplist/<pk>/',
|
|
|
|
'/api/machines/iptype/',
|
|
|
|
# '/api/machines/iptype/<pk>/',
|
|
|
|
'/api/machines/ipv6list/',
|
|
|
|
# '/api/machines/ipv6list/<pk>/',
|
|
|
|
'/api/machines/machine/',
|
|
|
|
# '/api/machines/machine/<pk>/',
|
|
|
|
'/api/machines/machinetype/',
|
|
|
|
# '/api/machines/machinetype/<pk>/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/machines/mx/',
|
|
|
|
# '/api/machines/mx/<pk>/',
|
|
|
|
'/api/machines/nas/',
|
|
|
|
# '/api/machines/nas/<pk>/',
|
|
|
|
'/api/machines/ns/',
|
|
|
|
# '/api/machines/ns/<pk>/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/machines/ouvertureportlist/',
|
|
|
|
# '/api/machines/ouvertureportlist/<pk>/',
|
|
|
|
'/api/machines/ouvertureport/',
|
|
|
|
# '/api/machines/ouvertureport/<pk>/',
|
|
|
|
'/api/machines/servicelink/',
|
|
|
|
# '/api/machines/servicelink/<pk>/',
|
|
|
|
'/api/machines/service/',
|
|
|
|
# '/api/machines/service/<pk>/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/machines/soa/',
|
|
|
|
# '/api/machines/soa/<pk>/',
|
|
|
|
'/api/machines/srv/',
|
|
|
|
# '/api/machines/srv/<pk>/',
|
|
|
|
'/api/machines/txt/',
|
|
|
|
# '/api/machines/txt/<pk>/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/machines/vlan/',
|
|
|
|
# '/api/machines/vlan/<pk>/',
|
2018-06-17 02:00:15 +00:00
|
|
|
'/api/preferences/optionaluser/',
|
|
|
|
'/api/preferences/optionalmachine/',
|
|
|
|
'/api/preferences/optionaltopologie/',
|
|
|
|
'/api/preferences/generaloption/',
|
|
|
|
'/api/preferences/service/',
|
|
|
|
# '/api/preferences/service/<pk>/',
|
|
|
|
'/api/preferences/assooption/',
|
|
|
|
'/api/preferences/homeoption/',
|
|
|
|
'/api/preferences/mailmessageoption/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/topologie/acesspoint/',
|
|
|
|
# '/api/topologie/acesspoint/<pk>/',
|
|
|
|
'/api/topologie/building/',
|
|
|
|
# '/api/topologie/building/<pk>/',
|
|
|
|
'/api/topologie/constructorswitch/',
|
|
|
|
# '/api/topologie/constructorswitch/<pk>/',
|
|
|
|
'/api/topologie/modelswitch/',
|
|
|
|
# '/api/topologie/modelswitch/<pk>/',
|
|
|
|
'/api/topologie/room/',
|
|
|
|
# '/api/topologie/room/<pk>/',
|
|
|
|
'/api/topologie/stack/',
|
|
|
|
# '/api/topologie/stack/<pk>/',
|
|
|
|
'/api/topologie/switch/',
|
|
|
|
# '/api/topologie/switch/<pk>/',
|
|
|
|
'/api/topologie/switchbay/',
|
|
|
|
# '/api/topologie/switchbay/<pk>/',
|
|
|
|
'/api/topologie/switchport/',
|
|
|
|
# '/api/topologie/switchport/<pk>/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/users/adherent/',
|
|
|
|
# '/api/users/adherent/<pk>/',
|
|
|
|
'/api/users/ban/',
|
|
|
|
# '/api/users/ban/<pk>/',
|
|
|
|
'/api/users/club/',
|
|
|
|
# '/api/users/club/<pk>/',
|
|
|
|
'/api/users/listright/',
|
|
|
|
# '/api/users/listright/<pk>/',
|
|
|
|
'/api/users/school/',
|
|
|
|
# '/api/users/school/<pk>/',
|
|
|
|
'/api/users/serviceuser/',
|
|
|
|
# '/api/users/serviceuser/<pk>/',
|
|
|
|
'/api/users/shell/',
|
|
|
|
# '/api/users/shell/<pk>/',
|
|
|
|
'/api/users/user/',
|
|
|
|
# '/api/users/user/<pk>/',
|
|
|
|
'/api/users/whitelist/',
|
|
|
|
# '/api/users/whitelist/<pk>/',
|
2018-06-17 02:00:15 +00:00
|
|
|
'/api/dns/zones/',
|
|
|
|
'/api/dhcp/hostmacip/',
|
|
|
|
'/api/mailing/standard',
|
|
|
|
'/api/mailing/club',
|
|
|
|
'/api/services/regen/',
|
2018-05-23 15:26:41 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
stduser = None
|
|
|
|
superuser = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
# A user with no rights
|
|
|
|
cls.stduser = users.User.objects.create_user(
|
|
|
|
"apistduser",
|
|
|
|
"apistduser",
|
|
|
|
"apistduser@example.net",
|
|
|
|
"apistduser"
|
|
|
|
)
|
|
|
|
# A user with all the rights
|
|
|
|
cls.superuser = users.User.objects.create_superuser(
|
|
|
|
"apisuperuser",
|
|
|
|
"apisuperuser",
|
|
|
|
"apisuperuser@example.net",
|
|
|
|
"apisuperuser"
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO :
|
|
|
|
# Create 1 object of every model so there is an exisiting object
|
|
|
|
# when quering for pk=1
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.stduser.delete()
|
|
|
|
cls.superuser.delete()
|
|
|
|
super().tearDownClass()
|
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
def check_responses_code(self, urls, expected_code, formats=None,
|
2018-05-23 15:26:41 +00:00
|
|
|
assert_more=None):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Utility function to test if a list of urls answer an expected code.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
urls: The list of urls to test
|
|
|
|
expected_code: The HTTP return code expected
|
|
|
|
formats: The list of formats to use for the request. Default is to
|
|
|
|
only test `None` format.
|
|
|
|
assert_more: An optional function to assert more specific data in
|
|
|
|
the same test. The response object, the url and the format
|
|
|
|
used are passed as arguments.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: The response got did not have the expected status
|
|
|
|
code.
|
|
|
|
Any exception raised in the evalutation of `assert_more`.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
2018-06-17 01:06:58 +00:00
|
|
|
if formats is None:
|
|
|
|
formats = [None]
|
2018-05-23 15:26:41 +00:00
|
|
|
for url in urls:
|
|
|
|
for format in formats:
|
|
|
|
with self.subTest(url=url, format=format):
|
|
|
|
response = self.client.get(url, format=format)
|
|
|
|
assert response.status_code == expected_code
|
2018-06-17 01:06:58 +00:00
|
|
|
if assert_more is not None:
|
2018-05-23 15:26:41 +00:00
|
|
|
assert_more(response, url, format)
|
|
|
|
|
|
|
|
def test_no_auth_endpoints_with_no_auth(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does not require to be
|
|
|
|
authenticated, returns a Ok (200) response when not authenticated.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 200 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
urls = [endpoint.replace('<pk>', '1')
|
|
|
|
for endpoint in self.no_auth_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.ok)
|
|
|
|
|
|
|
|
def test_auth_endpoints_with_no_auth(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does require to be authenticated,
|
2018-05-23 15:26:41 +00:00
|
|
|
returns a Unauthorized (401) response when not authenticated.
|
2018-06-17 01:06:58 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 401 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
urls = [endpoint.replace('<pk>', '1') for endpoint in \
|
|
|
|
self.auth_no_perm_endpoints + self.auth_perm_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.unauthorized)
|
|
|
|
|
|
|
|
def test_no_auth_endpoints_with_auth(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does not require to be
|
|
|
|
authenticated, returns a Ok (200) response when authenticated.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 200 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.stduser)
|
|
|
|
urls = [endpoint.replace('<pk>', '1')
|
|
|
|
for endpoint in self.no_auth_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.ok)
|
|
|
|
|
|
|
|
def test_auth_no_perm_endpoints_with_auth_and_no_perm(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does require to be authenticated and
|
|
|
|
no special permissions, returns a Ok (200) response when authenticated
|
|
|
|
but without permissions.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 200 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.stduser)
|
|
|
|
urls = [endpoint.replace('<pk>', '1')
|
|
|
|
for endpoint in self.auth_no_perm_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.ok)
|
|
|
|
|
|
|
|
def test_auth_perm_endpoints_with_auth_and_no_perm(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does require to be authenticated and
|
2018-05-23 15:26:41 +00:00
|
|
|
special permissions, returns a Forbidden (403) response when
|
|
|
|
authenticated but without permissions.
|
2018-06-17 01:06:58 +00:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 403 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.stduser)
|
|
|
|
urls = [endpoint.replace('<pk>', '1')
|
|
|
|
for endpoint in self.auth_perm_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.forbidden)
|
|
|
|
|
|
|
|
def test_auth_endpoints_with_auth_and_perm(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that does require to be authenticated,
|
|
|
|
returns a Ok (200) response when authenticated with all permissions.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 200 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.superuser)
|
|
|
|
urls = [endpoint.replace('<pk>', '1') for endpoint \
|
|
|
|
in self.auth_no_perm_endpoints + self.auth_perm_endpoints]
|
|
|
|
self.check_responses_code(urls, codes.ok)
|
|
|
|
|
|
|
|
def test_endpoints_not_found(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint that uses a primary key parameter,
|
2018-05-23 15:26:41 +00:00
|
|
|
returns a Not Found (404) response when queried with non-existing
|
2018-06-17 01:06:58 +00:00
|
|
|
primary key.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 404 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.superuser)
|
|
|
|
# Select only the URLs with '<pk>' and replace it with '42'
|
|
|
|
urls = [endpoint.replace('<pk>', '42') for endpoint in \
|
|
|
|
self.no_auth_endpoints + self.auth_no_perm_endpoints + \
|
|
|
|
self.auth_perm_endpoints if '<pk>' in endpoint]
|
|
|
|
self.check_responses_code(urls, codes.not_found)
|
|
|
|
|
|
|
|
def test_formats(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint returns a Ok (200) response when using
|
|
|
|
different formats. Also checks that 'json' format returns a valid
|
|
|
|
JSON object.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have a 200 status code.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(user=self.superuser)
|
|
|
|
|
|
|
|
urls = [endpoint.replace('<pk>', '1') for endpoint in \
|
|
|
|
self.no_auth_endpoints + self.auth_no_perm_endpoints + \
|
|
|
|
self.auth_perm_endpoints]
|
|
|
|
|
|
|
|
def assert_more(response, url, format):
|
|
|
|
"""Assert the response is valid json when format is json"""
|
|
|
|
if format is 'json':
|
|
|
|
json.loads(response.content.decode())
|
|
|
|
|
|
|
|
self.check_responses_code(urls, codes.ok,
|
|
|
|
formats=[None, 'json', 'api'],
|
|
|
|
assert_more=assert_more)
|
|
|
|
|
|
|
|
class APIPaginationTestCase(APITestCase):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Test case to check that the pagination is used on all endpoints that
|
|
|
|
should use it.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
endpoints: A list of endpoints that should use the pagination.
|
|
|
|
superuser: A superuser used in the tests to access the endpoints.
|
|
|
|
"""
|
|
|
|
|
2018-05-23 15:26:41 +00:00
|
|
|
endpoints = [
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/cotisations/article/',
|
|
|
|
'/api/cotisations/banque/',
|
|
|
|
'/api/cotisations/cotisation/',
|
|
|
|
'/api/cotisations/facture/',
|
|
|
|
'/api/cotisations/paiement/',
|
|
|
|
'/api/cotisations/vente/',
|
|
|
|
'/api/machines/domain/',
|
|
|
|
'/api/machines/extension/',
|
|
|
|
'/api/machines/interface/',
|
|
|
|
'/api/machines/iplist/',
|
|
|
|
'/api/machines/iptype/',
|
|
|
|
'/api/machines/ipv6list/',
|
|
|
|
'/api/machines/machine/',
|
|
|
|
'/api/machines/machinetype/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/machines/mx/',
|
|
|
|
'/api/machines/nas/',
|
|
|
|
'/api/machines/ns/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/machines/ouvertureportlist/',
|
|
|
|
'/api/machines/ouvertureport/',
|
|
|
|
'/api/machines/servicelink/',
|
|
|
|
'/api/machines/service/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/machines/soa/',
|
|
|
|
'/api/machines/srv/',
|
|
|
|
'/api/machines/txt/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/machines/vlan/',
|
2018-06-17 02:00:15 +00:00
|
|
|
'/api/preferences/service/',
|
2018-05-23 15:26:41 +00:00
|
|
|
'/api/topologie/acesspoint/',
|
|
|
|
'/api/topologie/building/',
|
|
|
|
'/api/topologie/constructorswitch/',
|
|
|
|
'/api/topologie/modelswitch/',
|
|
|
|
'/api/topologie/room/',
|
|
|
|
'/api/topologie/stack/',
|
|
|
|
'/api/topologie/switch/',
|
|
|
|
'/api/topologie/switchbay/',
|
|
|
|
'/api/topologie/switchport/',
|
2018-06-20 11:44:43 +00:00
|
|
|
'/api/users/adherent/',
|
|
|
|
'/api/users/ban/',
|
|
|
|
'/api/users/club/',
|
|
|
|
'/api/users/listright/',
|
|
|
|
'/api/users/school/',
|
|
|
|
'/api/users/serviceuser/',
|
|
|
|
'/api/users/shell/',
|
|
|
|
'/api/users/user/',
|
|
|
|
'/api/users/whitelist/',
|
2018-06-17 02:00:15 +00:00
|
|
|
'/api/dns/zones/',
|
|
|
|
'/api/dhcp/hostmacip/',
|
|
|
|
'/api/mailing/standard',
|
|
|
|
'/api/mailing/club',
|
|
|
|
'/api/services/regen/',
|
2018-05-23 15:26:41 +00:00
|
|
|
]
|
|
|
|
superuser = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
# A user with all the rights
|
|
|
|
cls.superuser = users.User.objects.create_superuser(
|
|
|
|
"apisuperuser",
|
|
|
|
"apisuperuser",
|
|
|
|
"apisuperuser@example.net",
|
|
|
|
"apisuperuser"
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.superuser.delete()
|
|
|
|
super().tearDownClass()
|
|
|
|
|
|
|
|
def test_pagination(self):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Tests that every endpoint is using the pagination correctly.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: An endpoint did not have one the following keyword
|
|
|
|
in the JSOn response: 'count', 'next', 'previous', 'results'
|
|
|
|
or more that 100 results were returned.
|
2018-05-23 15:26:41 +00:00
|
|
|
"""
|
|
|
|
self.client.force_authenticate(self.superuser)
|
|
|
|
for url in self.endpoints:
|
|
|
|
with self.subTest(url=url):
|
|
|
|
response = self.client.get(url, format='json')
|
|
|
|
res_json = json.loads(response.content.decode())
|
|
|
|
assert 'count' in res_json.keys()
|
|
|
|
assert 'next' in res_json.keys()
|
|
|
|
assert 'previous' in res_json.keys()
|
|
|
|
assert 'results' in res_json.keys()
|
|
|
|
assert not len('results') > 100
|
2018-03-17 17:20:31 +00:00
|
|
|
|