Commit 799382cb authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

refacto base for tests as admin

parent 23929d48
Pipeline #19418 failed with stages
in 2 minutes and 15 seconds
from django.contrib.auth.models import User
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from rest_framework_jwt.settings import api_settings
from metagenedb.apps.catalog.factory import FunctionFactory from metagenedb.apps.catalog.factory import FunctionFactory
from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogFunctionAPIMock from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogFunctionAPIMock
from metagenedb.common.utils.tests.apitestbase import AdminUserBasedTest
class TestOperationsBulkViewSetNoCredentials(APITestCase): class TestOperationsBulkViewSetNoCredentials(APITestCase):
...@@ -32,7 +31,7 @@ class TestOperationsBulkViewSetNoCredentials(APITestCase): ...@@ -32,7 +31,7 @@ class TestOperationsBulkViewSetNoCredentials(APITestCase):
self.function_api.put(data, function.function_id) self.function_api.put(data, function.function_id)
class TestOperationsBulkViewSet(APITestCase): class TestOperationsBulkViewSet(AdminUserBasedTest):
""" """
We are testing the different functions through the API directly through the mock redirecting We are testing the different functions through the API directly through the mock redirecting
requests to the test database. requests to the test database.
...@@ -41,15 +40,8 @@ class TestOperationsBulkViewSet(APITestCase): ...@@ -41,15 +40,8 @@ class TestOperationsBulkViewSet(APITestCase):
""" """
def setUp(self): def setUp(self):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER super().setUp()
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER self.function_api = MetageneDBCatalogFunctionAPIMock(self.client, jwt_token=self.jwt_token)
user = User.objects.create_user(username='user_admin', email='user@admin.com', password='pass')
user.is_active = True
user.is_staff = True
user.save()
payload = jwt_payload_handler(user)
jwt_token = jwt_encode_handler(payload)
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client, jwt_token=jwt_token)
def test_create_function(self): def test_create_function(self):
data = { data = {
......
...@@ -20,24 +20,6 @@ class TestGenes(TestCase): ...@@ -20,24 +20,6 @@ class TestGenes(TestCase):
resp = self.client.get(url) resp = self.client.get(url)
self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.status_code, status.HTTP_200_OK)
def test_get_genes_auth(self):
"""
Authenticated users should be able to access genes
"""
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
user = User.objects.create_user(username='user', email='user@foo.com', password='pass')
user.is_active = True
user.save()
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
url = reverse('api:catalog:v1:genes-list')
resp = self.client.get(url, format='json', HTTP_AUTHORIZATION=f"JWT {token}")
self.assertEqual(resp.status_code, status.HTTP_200_OK)
class TestCountWindowsAPI(APITestCase): class TestCountWindowsAPI(APITestCase):
......
from django.contrib.auth.models import User
from rest_framework.test import APITestCase
from rest_framework_jwt.settings import api_settings
class AdminUserBasedTest(APITestCase):
"""
We are testing the different functions through the API directly through the mock redirecting
requests to the test database.
The extent is a bit more than a unittest since it is not just involving concerned methods.
"""
def setUp(self):
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
user = User.objects.create_user(username='user_admin', email='user@admin.com', password='pass')
user.is_active = True
user.is_staff = True
user.save()
payload = jwt_payload_handler(user)
self.jwt_token = jwt_encode_handler(payload)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment