change allocate and deallocate

This commit is contained in:
Cayo Puigdefabregas 2020-11-26 18:44:08 +01:00
parent ab7bf75bd7
commit b072bc3b57
4 changed files with 90 additions and 42 deletions

View file

@ -31,7 +31,7 @@ def upgrade():
op.drop_table('allocate', schema=f'{get_inv()}') op.drop_table('allocate', schema=f'{get_inv()}')
op.create_table('allocate', op.create_table('allocate',
sa.Column('code', citext.CIText(), nullable=True, comment=' This is a internal code for mainteing the secrets of the personal datas of the new holder '), sa.Column('code', citext.CIText(), nullable=True, comment=' This is a internal code for mainteing the secrets of the personal datas of the new holder '),
sa.Column('end_users', sa.Numeric(precision=4), nullable=False), sa.Column('end_users', sa.Numeric(precision=4), nullable=True),
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
@ -41,6 +41,7 @@ def upgrade():
# Deallocate action # Deallocate action
op.drop_table('deallocate', schema=f'{get_inv()}') op.drop_table('deallocate', schema=f'{get_inv()}')
op.create_table('deallocate', op.create_table('deallocate',
sa.Column('code', citext.CIText(), nullable=True, comment=' This is a internal code for mainteing the secrets of the personal datas of the new holder '),
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),

View file

@ -315,7 +315,7 @@ class Allocate(JoinedTableMixin, ActionWithMultipleDevices):
""" """
code = Column(CIText(), default='', nullable=True) code = Column(CIText(), default='', nullable=True)
code.comment = """ This is a internal code for mainteing the secrets of the personal datas of the new holder """ code.comment = """ This is a internal code for mainteing the secrets of the personal datas of the new holder """
end_users = Column(Numeric(precision=4), check_range('end_users', 0), nullable=False) end_users = Column(Numeric(precision=4), check_range('end_users', 0), nullable=True)
class Deallocate(JoinedTableMixin, ActionWithMultipleDevices): class Deallocate(JoinedTableMixin, ActionWithMultipleDevices):

View file

@ -1,3 +1,5 @@
from datetime import datetime, timedelta
from dateutil.tz import tzutc
from flask import current_app as app from flask import current_app as app
from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema from marshmallow import Schema as MarshmallowSchema, ValidationError, fields as f, validates_schema
from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \ from marshmallow.fields import Boolean, DateTime, Decimal, Float, Integer, Nested, String, \
@ -65,58 +67,57 @@ class Remove(ActionWithOneDevice):
class Allocate(ActionWithMultipleDevices): class Allocate(ActionWithMultipleDevices):
__doc__ = m.Allocate.__doc__ __doc__ = m.Allocate.__doc__
agent = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Trade.to_comment) start_time = DateTime(data_key='start_time', required=True,
description = SanitizedStr(default='', description=m.Action.description.comment) description=m.Action.start_time.comment)
start_time = DateTime(data_key='start_time', description=m.Action.start_time.comment)
end_time = DateTime(data_key='end_time', required=False, end_time = DateTime(data_key='end_time', required=False,
description=m.Action.end_time.comment) description=m.Action.end_time.comment)
code = SanitizedStr(data_key='Transaction', validate=Length(min=1, max=STR_BIG_SIZE), code = SanitizedStr(data_key='transaction', validate=Length(min=1, max=STR_BIG_SIZE),
required=False, required=False,
description='The code of the agent to assigned.') description='The code of the agent to assigned.')
end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")], end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")])
required=True)
@validates_schema @validates_schema
def validate_allocate(self, data: dict): def validate_allocate(self, data: dict):
txt = "You need to allocate for a day before today"
delay = timedelta(days=1)
today = datetime.now().replace(tzinfo=tzutc()) + delay
start_time = data['start_time'].replace(tzinfo=tzutc())
if start_time > today:
raise ValidationError(txt)
txt = "You need deallocate before allocate this device again" txt = "You need deallocate before allocate this device again"
for device in data['devices']: for device in data['devices']:
if device.allocated == False: if device.allocated:
device.allocated = True raise ValidationError(txt)
continue
actions = [a for a in device.actions]
actions.sort(key=lambda x: x.created)
actions.reverse()
for allocate in actions:
if isinstance(allocate, m.Allocate):
same_allocate = [
allocate.code == data['code'],
allocate.start_time == data['start_time'],
allocate.end_users == data['end_users']
]
if not all(same_allocate):
raise ValidationError(txt)
if isinstance(allocate, m.Deallocate):
break
device.allocated = True device.allocated = True
class Deallocate(ActionWithMultipleDevices): class Deallocate(ActionWithMultipleDevices):
__doc__ = m.Deallocate.__doc__ __doc__ = m.Deallocate.__doc__
start_time = DateTime(data_key='start_time', description=m.Action.start_time.comment) start_time = DateTime(data_key='start_time', required=True,
description=m.Action.start_time.comment)
code = SanitizedStr(data_key='transaction', validate=Length(min=1, max=STR_BIG_SIZE),
required=False,
description='The code of the agent to assigned.')
@validates_schema @validates_schema
def validate_deallocate(self, data: dict): def validate_deallocate(self, data: dict):
txt = "Sorry some of this devices are actually deallocate" txt = "You need to deallocate for a day before today"
import pdb; pdb.set_trace()
delay = timedelta(days=1)
today = datetime.now().replace(tzinfo=tzutc()) + delay
start_time = data['start_time'].replace(tzinfo=tzutc())
if start_time > today:
raise ValidationError(txt)
txt = "Sorry some of this devices are actually deallocate"
for device in data['devices']: for device in data['devices']:
if hasattr(device, 'allocated') and device.allocated: if not device.allocated:
device.allocated = False
else:
raise ValidationError(txt) raise ValidationError(txt)
device.allocated = False
class EraseBasic(ActionWithOneDevice): class EraseBasic(ActionWithOneDevice):
__doc__ = m.EraseBasic.__doc__ __doc__ = m.EraseBasic.__doc__

View file

@ -2,7 +2,7 @@ import ipaddress
import copy import copy
import pytest import pytest
from datetime import timedelta from datetime import datetime, timedelta
from decimal import Decimal from decimal import Decimal
from typing import Tuple, Type from typing import Tuple, Type
@ -278,21 +278,22 @@ def test_allocate(user: UserClient):
""" Tests allocate """ """ Tests allocate """
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot) snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id'] device_id = snapshot['device']['id']
post_request = {"Transaction": "ccc", "name": "John", "end_users": 1, post_request = {"transaction": "ccc",
"devices": [device_id], "description": "aaa", "name": "John",
"severity": "Info",
"end_users": 1,
"devices": [device_id],
"description": "aaa",
"start_time": "2020-11-01T02:00:00+00:00", "start_time": "2020-11-01T02:00:00+00:00",
"end_time": "2020-12-01T02:00:00+00:00" "end_time": "2020-12-01T02:00:00+00:00"
} }
allocate, _ = user.post(res=models.Allocate, data=post_request) allocate, _ = user.post(res=models.Allocate, data=post_request)
allocate_get, _ = user.get(res=models.Action, item=allocate['id'])
for f in ('name', 'Transaction', 'created', 'start_time', 'end_users'):
assert allocate_get[f] == allocate[f]
# Normal allocate # Normal allocate
device, _ = user.get(res=Device, item=device_id) device, _ = user.get(res=Device, item=device_id)
assert device['allocated'] == True assert device['allocated'] == True
action = [a for a in device['actions'] if a['type'] == 'Allocate'][0] action = [a for a in device['actions'] if a['type'] == 'Allocate'][0]
assert action['Transaction'] == allocate['Transaction'] assert action['transaction'] == allocate['transaction']
assert action['created'] == allocate['created'] assert action['created'] == allocate['created']
assert action['start_time'] == allocate['start_time'] assert action['start_time'] == allocate['start_time']
assert action['end_users'] == allocate['end_users'] assert action['end_users'] == allocate['end_users']
@ -303,7 +304,7 @@ def test_allocate(user: UserClient):
post_bad_request2 = copy.copy(post_request) post_bad_request2 = copy.copy(post_request)
post_bad_request2['start_time'] = "2020-11-01T02:00:00+00:01" post_bad_request2['start_time'] = "2020-11-01T02:00:00+00:01"
post_bad_request3 = copy.copy(post_request) post_bad_request3 = copy.copy(post_request)
post_bad_request3['Transaction'] = "aaa" post_bad_request3['transaction'] = "aaa"
res1, _ = user.post(res=models.Allocate, data=post_bad_request1, status=422) res1, _ = user.post(res=models.Allocate, data=post_bad_request1, status=422)
res2, _ = user.post(res=models.Allocate, data=post_bad_request2, status=422) res2, _ = user.post(res=models.Allocate, data=post_bad_request2, status=422)
res3, _ = user.post(res=models.Allocate, data=post_bad_request3, status=422) res3, _ = user.post(res=models.Allocate, data=post_bad_request3, status=422)
@ -312,6 +313,28 @@ def test_allocate(user: UserClient):
assert r['type'] == 'ValidationError' assert r['type'] == 'ValidationError'
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_allocate_bad_dates(user: UserClient):
""" Tests allocate """
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id']
delta = timedelta(days=30)
future = datetime.now() + delta
post_request = {"transaction": "ccc",
"name": "John",
"severity": "Info",
"end_users": 1,
"devices": [device_id],
"description": "aaa",
"start_time": future,
}
res, _ = user.post(res=models.Allocate, data=post_request, status=422)
assert res['code'] == 422
assert res['type'] == 'ValidationError'
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_deallocate(user: UserClient): def test_deallocate(user: UserClient):
@ -319,12 +342,13 @@ def test_deallocate(user: UserClient):
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot) snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id'] device_id = snapshot['device']['id']
post_deallocate = {"start_time": "2020-11-01T02:00:00+00:00", post_deallocate = {"start_time": "2020-11-01T02:00:00+00:00",
"devices": [device_id] "transaction": "ccc",
"devices": [device_id]
} }
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422) res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=422)
assert res['code'] == 422 assert res['code'] == 422
assert res['type'] == 'ValidationError' assert res['type'] == 'ValidationError'
post_allocate = {"Transaction": "ccc", "name": "John", "end_users": 1, post_allocate = {"transaction": "ccc", "name": "John", "end_users": 1,
"devices": [device_id], "description": "aaa", "devices": [device_id], "description": "aaa",
"start_time": "2020-11-01T02:00:00+00:00", "start_time": "2020-11-01T02:00:00+00:00",
"end_time": "2020-12-01T02:00:00+00:00" "end_time": "2020-12-01T02:00:00+00:00"
@ -342,6 +366,28 @@ def test_deallocate(user: UserClient):
assert res['type'] == 'ValidationError' assert res['type'] == 'ValidationError'
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_deallocate_bad_dates(user: UserClient):
""" Tests deallocate with bad date of start_time """
snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot)
device_id = snapshot['device']['id']
delta = timedelta(days=30)
future = datetime.now() + delta
post_deallocate = {"start_time": future,
"devices": [device_id]
}
post_allocate = {"devices": [device_id], "description": "aaa",
"start_time": "2020-11-01T02:00:00+00:00"
}
import pdb; pdb.set_trace()
user.post(res=models.Allocate, data=post_allocate)
res, _ = user.post(res=models.Deallocate, data=post_deallocate, status=400)
assert res['code'] == 422
assert res['type'] == 'ValidationError'
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.parametrize('action_model_state', @pytest.mark.parametrize('action_model_state',
(pytest.param(ams, id=ams[0].__name__) (pytest.param(ams, id=ams[0].__name__)