diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 6a176f6e..f7ff436f 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -44,6 +44,9 @@ class DevicehubConfig(Config): """The minimum version of ereuse.org workbench that this devicehub accepts. we recommend not changing this value. """ + + TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots') + """This var is for save a snapshots in json format when fail something""" API_DOC_CONFIG_TITLE = 'Devicehub' API_DOC_CONFIG_VERSION = '0.2' API_DOC_CONFIG_COMPONENTS = { diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 01eab200..bd146b17 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -1,3 +1,5 @@ +""" This is the view for Snapshots """ + import os import json from time import time @@ -18,19 +20,17 @@ from ereuse_devicehub.resources.user.exceptions import InsufficientPermission SUPPORTED_WORKBENCH = StrictVersion('11.0') -TMP_SNAPSHOTS = 'tmp/snapshots' - -def save_json(req_json): +def save_json(req_json, tmp_snapshots): """ This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory The file need to be saved with one name format with the stamptime and uuid joins """ name_file = "{uuid}_{time}.json".format(uuid=req_json.get('uuid', ''), time=int(time())) - path_name = os.path.join(TMP_SNAPSHOTS, name_file) + path_name = os.path.join(tmp_snapshots, name_file) - if not os.path.isdir(TMP_SNAPSHOTS): - os.system('mkdir -p {}'.format(TMP_SNAPSHOTS)) + if not os.path.isdir(tmp_snapshots): + os.system('mkdir -p {}'.format(tmp_snapshots)) with open(path_name, 'w') as snapshot_file: snapshot_file.write(json.dumps(req_json)) @@ -42,7 +42,8 @@ class ActionView(View): def post(self): """Posts an action.""" json = request.get_json(validate=False) - path_snapshot = save_json(json) + tmp_snapshots = app.config['TMP_SNAPSHOTS'] + path_snapshot = save_json(json, tmp_snapshots) if not json or 'type' not in json: raise ValidationError('Resource needs a type.') # todo there should be a way to better get subclassess resource diff --git a/tests/conftest.py b/tests/conftest.py index be01b87f..bdae5797 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,6 +30,7 @@ class TestConfig(DevicehubConfig): SQLALCHEMY_DATABASE_URI = 'postgresql://dhub:ereuse@localhost/dh_test' TESTING = True SERVER_NAME = 'localhost' + TMP_SNAPSHOTS = '/tmp/snapshots' @pytest.fixture(scope='session') diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 60667da1..e5d88621 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -26,7 +26,7 @@ from ereuse_devicehub.resources.device.sync import MismatchBetweenProperties, \ from ereuse_devicehub.resources.enums import ComputerChassis, SnapshotSoftware from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.user.models import User -from ereuse_devicehub.resources.action.views import TMP_SNAPSHOTS, save_json +from ereuse_devicehub.resources.action.views import save_json from tests.conftest import file @@ -481,17 +481,18 @@ def test_pc_2(user: UserClient): @pytest.mark.mvp -def test_save_snapshot_in_file(): +def test_save_snapshot_in_file(app: Devicehub): """ This test check if works the function save_snapshot_in_file """ + tmp_snapshots = app.config['TMP_SNAPSHOTS'] snapshot_no_hid = file('basic.snapshot.nohid') - save_json(snapshot_no_hid) + save_json(snapshot_no_hid, tmp_snapshots) uuid = snapshot_no_hid['uuid'] - files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x] + files = [x for x in os.listdir(tmp_snapshots) if uuid in x] snapshot = {'software': '', 'version': '', 'uuid': ''} if files: - path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) + path_snapshot = os.path.join(tmp_snapshots, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read()) @@ -503,8 +504,9 @@ def test_save_snapshot_in_file(): @pytest.mark.mvp -def test_backup_snapshot_with_errors(user: UserClient): +def test_backup_snapshot_with_errors(app: Devicehub, user: UserClient): """ This test check if the file snapshot is create when some snapshot is wrong """ + tmp_snapshots = app.config['TMP_SNAPSHOTS'] snapshot_no_hid = file('basic.snapshot.badly_formed') uuid = snapshot_no_hid['uuid'] @@ -512,9 +514,9 @@ def test_backup_snapshot_with_errors(user: UserClient): with pytest.raises(KeyError): response = user.post(res=Snapshot, data=snapshot_no_hid) - files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x] + files = [x for x in os.listdir(tmp_snapshots) if uuid in x] if files: - path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) + path_snapshot = os.path.join(tmp_snapshots, files[0]) with open(path_snapshot) as file_snapshot: snapshot = json.loads(file_snapshot.read())