first iteration #155 decode snapshot json

This commit is contained in:
nad 2021-06-29 17:15:45 +02:00
parent f5ff089929
commit e3a0ea7e5b
3 changed files with 19 additions and 8 deletions

View File

@ -4,23 +4,25 @@ from datetime import timedelta
from distutils.version import StrictVersion from distutils.version import StrictVersion
from uuid import UUID from uuid import UUID
import jwt
from flask import current_app as app, request, g from flask import current_app as app, request, g
from teal.db import ResourceNotFound
from teal.marshmallow import ValidationError from teal.marshmallow import ValidationError
from teal.resource import View from teal.resource import View
from teal.db import ResourceNotFound
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, Snapshot, VisualTest, from ereuse_devicehub.resources.action.models import (Action, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate, InitTransfer, Live, Allocate, Deallocate,
Trade, Confirm, ConfirmRevoke, Revoke) Trade, Confirm, ConfirmRevoke, Revoke)
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.action.views import trade as trade_view from ereuse_devicehub.resources.action.views import trade as trade_view
from ereuse_devicehub.resources.action.views.snapshot import SnapshotView, save_json, move_json from ereuse_devicehub.resources.action.views.snapshot import SnapshotView, save_json, move_json
from ereuse_devicehub.resources.device.models import Device, Computer, DataStorage
from ereuse_devicehub.resources.enums import Severity
SUPPORTED_WORKBENCH = StrictVersion('11.0') SUPPORTED_WORKBENCH = StrictVersion('11.0')
class AllocateMix(): class AllocateMix():
model = None model = None
@ -170,8 +172,12 @@ class ActionView(View):
def post(self): def post(self):
"""Posts an action.""" """Posts an action."""
json = request.get_json(validate=False) json = request.get_json(validate=False)
if not json or 'type' not in json: if not json:
raise ValidationError('Resource needs a type.') raise ValidationError('Post request needs a json.')
elif 'type' not in json:
# JN TODO Use the user's key instead an empty string
key = ''
json = jwt.decode(json, key, algorithms="HS256")
# todo there should be a way to better get subclassess resource # todo there should be a way to better get subclassess resource
# defs # defs
resource_def = app.resources[json['type']] resource_def = app.resources[json['type']]
@ -220,4 +226,3 @@ class ActionView(View):
def transfer_ownership(self): def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device""" """Perform a InitTransfer action to change author_id of device"""
pass pass

View File

@ -337,3 +337,8 @@ def test_david(user: UserClient):
def test_eresueprice_computer_type(user: UserClient): def test_eresueprice_computer_type(user: UserClient):
s = file_workbench('computer-type.snapshot') s = file_workbench('computer-type.snapshot')
snapshot, _ = user.post(res=em.Snapshot, data=s) snapshot, _ = user.post(res=em.Snapshot, data=s)
def test_workbench_encoded_snapshot(user: UserClient):
s = file_workbench('encoded.snapshot')
snapshot, _ = user.post(res=em.Snapshot, data=s)

View File

@ -0,0 +1 @@
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoiU25hcHNob3QiLCJ1dWlkIjoiMDA0MWQ4ZTgtNTY5Yy00NDUyLWIyOTktMDc0YjVmY2M2NDIyIiwic29mdHdhcmUiOiJXb3JrYmVuY2giLCJ2ZXJzaW9uIjoiMTIuMGIwIiwiY2xvc2VkIjp0cnVlLCJlbmRUaW1lIjoiMjAyMS0wNi0yOVQxMzo0NzoxNi42MTI5NjkrMDA6MDAiLCJlbGFwc2VkIjozMSwiZGV2aWNlIjp7ImFjdGlvbnMiOlt7ImVsYXBzZWQiOjEwLCJ0eXBlIjoiQmVuY2htYXJrUmFtU3lzYmVuY2giLCJyYXRlIjoxMC4wMDAxfV0sInR5cGUiOiJDb21wdXRlciIsIm1hbnVmYWN0dXJlciI6IkludGVsIENsaWVudCBTeXN0ZW1zIiwibW9kZWwiOiJOVUM2Q0FZSCIsInNlcmlhbE51bWJlciI6Ikc2QVk5MjIwMEw3OCIsImNoYXNzaXMiOiJWaXJ0dWFsIiwic2t1IjpudWxsLCJ2ZXJzaW9uIjoiSjI2ODQzLTQwOSJ9LCJjb21wb25lbnRzIjpbeyJhY3Rpb25zIjpbeyJlbGFwc2VkIjoxM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