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

Fix tests

parent d2ffd8c9
Pipeline #19407 failed with stages
in 2 minutes and 43 seconds
......@@ -5,6 +5,7 @@ from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from marshmallow.exceptions import ValidationError
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_422_UNPROCESSABLE_ENTITY
......@@ -60,6 +61,8 @@ class GeneViewSet(BulkViewSet):
DEFAULT_LEVEL = 'phylum'
def get_permissions(self):
if self.action in ['gene_length', 'taxonomy_counts']:
return [AllowAny()]
return super(self.__class__, self).get_permissions()
def _count_windows(self, queryset, window_size=DEFAULT_WINDOW_SIZE, window_col=GENE_LENGTH_COL,
......
from django.contrib.auth.models import User
from requests.exceptions import HTTPError
from rest_framework.test import APITestCase
from rest_framework_jwt.settings import api_settings
from metagenedb.apps.catalog.factory import FunctionFactory
from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogFunctionAPIMock
class TestOperationsBulkViewSetNoCredentials(APITestCase):
def setUp(self):
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client)
def test_create_function(self):
data = {
"function_id": 'k_test1',
"source": "kegg",
"name": "Kegg Test 1"
}
with self.assertRaises(HTTPError):
self.function_api.post(data)
def test_update_existing_function(self):
function = FunctionFactory()
data = {
"function_id": function.function_id,
"source": function.source,
"name": "Kegg Test 1"
}
with self.assertRaises(HTTPError):
self.function_api.put(data, function.function_id)
class TestOperationsBulkViewSet(APITestCase):
"""
We are testing the different functions through the API directly through the mock redirecting
......@@ -14,7 +41,15 @@ class TestOperationsBulkViewSet(APITestCase):
"""
def setUp(self):
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client)
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)
jwt_token = jwt_encode_handler(payload)
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client, jwt_token=jwt_token)
def test_create_function(self):
data = {
......
......@@ -14,33 +14,43 @@ class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
REVERSE_PATH = ''
BAD_REQUESTS = range(400, 452)
def __init__(self, client):
def __init__(self, client, jwt_token=None):
self.client = client
self.reverse_path = ':'.join([self.BASE_REVERSE, self.REVERSE_PATH])
self.headers = {}
if jwt_token is not None:
self.headers.update({
'HTTP_AUTHORIZATION': f"JWT {jwt_token}",
})
def get_all(self, params=None):
response = self.client.get(reverse(f'{self.reverse_path}-list'), params)
response = self.client.get(reverse(f'{self.reverse_path}-list'), params, **self.headers)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
def get(self, entry_id, params=None):
response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}), params)
response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
params, **self.headers)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
def post(self, data):
response = self.client.post(reverse(f'{self.reverse_path}-list'), data, format='json')
response = self.client.post(reverse(f'{self.reverse_path}-list'), data, format='json', **self.headers)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
def put(self, data, entry_id=None):
if entry_id:
return self.client.put(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
data, format='json').json()
return self.client.put(reverse(f'{self.reverse_path}-list'), data, format='json').json()
response = self.client.put(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
data, format='json', **self.headers)
else:
response = self.client.put(reverse(f'{self.reverse_path}-list'), data, format='json', **self.headers)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
class MetageneDBCatalogGeneAPIMock(MetageneDBAPIMock):
......
......@@ -27,7 +27,7 @@ class TestParseGene(TestCase):
'cohort_assembled'
]
self.raw_line = "\t".join(raw_data)
self.import_igc_genes = ImportIGCGenes('test', 'test')
self.import_igc_genes = ImportIGCGenes('test', 'test_url', 'test_token')
def test_parse_gene_default_selected_keys(self):
"""
......@@ -71,7 +71,7 @@ class TestParseGene(TestCase):
class TestCleanGene(TestCase):
def setUp(self):
self.import_igc_genes = ImportIGCGenes('test', 'test')
self.import_igc_genes = ImportIGCGenes('test', 'test_url', 'test_token')
self.import_igc_genes._select_taxonomy = lambda x: x # Mock to return same dict
self.import_igc_genes._clean_functions = lambda x: x
self.gene_dict = {
......@@ -123,7 +123,7 @@ class TestSelectTaxonomy(TestCase):
self.genus_name = 'Genus1'
self.phylum_id = 'phylum_1'
self.phylum_name = 'Phylum1'
self.import_igc_genes = ImportIGCGenes('test', 'test')
self.import_igc_genes = ImportIGCGenes('test', 'test_url', 'test_token')
self.import_igc_genes.phylum_mapping = {
self.phylum_name: self.phylum_id
}
......@@ -227,7 +227,7 @@ class TestBuildTaxoMapping(APITestCase):
cls.phylum_items = TaxonomyFactory.create_batch(20, rank='phylum')
def setUp(self):
self.import_igc_genes = ImportIGCGenes('test', 'test')
self.import_igc_genes = ImportIGCGenes('test', 'test_url', 'test_token')
self.api_mock = MetageneDBCatalogTaxonomyAPIMock(self.client)
self.import_igc_genes.metagenedb_taxonomy_api = self.api_mock
......@@ -250,7 +250,7 @@ class TestBuildBuildFunctionCatalog(APITestCase):
cls.functions = FunctionFactory.create_batch(100)
def setUp(self):
self.import_igc_genes = ImportIGCGenes('test', 'test')
self.import_igc_genes = ImportIGCGenes('test', 'test_url', 'test_token')
self.api_mock = MetageneDBCatalogFunctionAPIMock(self.client)
self.import_igc_genes.metagenedb_function_api = self.api_mock
......
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