Skip to content

Commit 40e5d9e

Browse files
authored
Merge pull request #3926 from hove-io/replace_get_authorized_instances
Replace _get_authorized_instances
2 parents 4b9f3e9 + 0082b17 commit 40e5d9e

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

source/jormungandr/jormungandr/instance_manager.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
import logging
4040
from jormungandr.protobuf_to_dict import protobuf_to_dict
4141
from jormungandr.exceptions import ApiNotFound, RegionNotFound, DeadSocketException, InvalidArguments
42-
from jormungandr import authentication, cache, app
42+
from jormungandr.authentication import abort_request, can_read_user
43+
from jormungandr import authentication, cache, memory_cache, app
4344
from jormungandr.instance import Instance
4445
import gevent
4546
import os
@@ -99,7 +100,7 @@ def __init__(
99100
self.start_ping = start_ping
100101
self.instances = {}
101102
self.context = zmq.Context()
102-
self.is_ready = False
103+
self.is_ready = False # type: bool
103104

104105
def __repr__(self):
105106
return '<InstanceManager>'
@@ -297,7 +298,7 @@ def get_instances(self, name=None, lon=None, lat=None, object_id=None, api='ALL'
297298
else:
298299
# Requests without any coverage
299300
# fetch all the authorized instances (free + private) using cached function has_access()
300-
authorized_instances = self._get_authorized_instances(user, api)
301+
authorized_instances = self.get_all_available_instances(user)
301302
if not authorized_instances:
302303
# user doesn't have access to any of the instances
303304
context = 'User has no access to any instance'
@@ -341,3 +342,26 @@ def regions(self, region=None, lon=None, lat=None, request_id=None):
341342
resp_dict['region_id'] = key_region
342343
response['regions'].append(resp_dict)
343344
return response
345+
346+
@memory_cache.memoize(app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 30))
347+
@cache.memoize(app.config[str('CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 300))
348+
def get_all_available_instances(self, user):
349+
result = []
350+
if app.config.get('PUBLIC', False) or app.config.get('DISABLE_DATABASE', False):
351+
return list(self.instances.values())
352+
353+
if not user:
354+
logging.getLogger(__name__).warning('get all available instances no user')
355+
# for not-public navitia a user is mandatory
356+
# To manage database error of the following type we should fetch one more time from database
357+
# Can connect to database but at least one table/attribute is not accessible due to transaction problem
358+
if can_read_user():
359+
abort_request(user=user)
360+
else:
361+
return result
362+
363+
bdd_instances = user.get_all_available_instances()
364+
for bdd_instance in bdd_instances:
365+
if bdd_instance.name in self.instances:
366+
result.append(self.instances[bdd_instance.name])
367+
return result

source/jormungandr/jormungandr/tests/instance_manager_tests.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ def manager():
5252
return instance_manager
5353

5454

55-
def get_instances_test(manager):
55+
def get_instances_test(manager, mocker):
56+
mock = mocker.patch.object(
57+
manager,
58+
'get_all_available_instances',
59+
return_value=[manager.instances['paris'], manager.instances['pdl']],
60+
)
5661
with app.test_request_context('/'):
5762
instances = manager.get_instances()
5863
assert len(instances) == 2
@@ -67,6 +72,11 @@ def get_instances_by_coord_test(manager, mocker):
6772
mock = mocker.patch.object(
6873
manager, '_all_keys_of_coord_in_instances', return_value=[manager.instances['paris']]
6974
)
75+
mock = mocker.patch.object(
76+
manager,
77+
'get_all_available_instances',
78+
return_value=[manager.instances['paris'], manager.instances['pdl']],
79+
)
7080
with app.test_request_context('/'):
7181
instances = manager.get_instances(lon=4, lat=3)
7282
assert len(instances) == 1
@@ -76,6 +86,11 @@ def get_instances_by_coord_test(manager, mocker):
7686

7787
def get_instances_by_object_id_test(manager, mocker):
7888
mock = mocker.patch.object(manager, '_all_keys_of_id_in_instances', return_value=[manager.instances['pdl']])
89+
mock = mocker.patch.object(
90+
manager,
91+
'get_all_available_instances',
92+
return_value=[manager.instances['paris'], manager.instances['pdl']],
93+
)
7994
with app.test_request_context('/'):
8095
instances = manager.get_instances(object_id='sa:pdl')
8196
assert len(instances) == 1

source/jormungandr/tests/coverage_tests.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,24 @@
2929
from __future__ import absolute_import, print_function, unicode_literals, division
3030
from .tests_mechanism import AbstractTestFixture, dataset
3131
from .check_utils import *
32+
from jormungandr import app
3233
import logging
3334

3435

36+
class CoverageTestFixture(AbstractTestFixture):
37+
def setUp(self):
38+
self.old_public_val = app.config['PUBLIC']
39+
app.config['PUBLIC'] = True
40+
self.old_db_val = app.config['DISABLE_DATABASE']
41+
app.config['DISABLE_DATABASE'] = True
42+
43+
def tearDown(self):
44+
app.config['PUBLIC'] = self.old_public_val
45+
app.config['DISABLE_DATABASE'] = self.old_db_val
46+
47+
3548
@dataset({"main_routing_test": {}, "null_status_test": {}})
36-
class TestNullStatus(AbstractTestFixture):
49+
class TestNullStatus(CoverageTestFixture):
3750
"""
3851
Test with an empty coverage
3952
"""

0 commit comments

Comments
 (0)