finish tests

This commit is contained in:
Cayo Puigdefabregas 2021-05-21 13:16:06 +02:00
parent 28da6a953d
commit 74dd300cad
2 changed files with 161 additions and 127 deletions

View File

@ -54,6 +54,7 @@ def test_api_docs(client: Client):
'/metrics/', '/metrics/',
'/tags/', '/tags/',
'/tags/{tag_id}/device/{device_id}', '/tags/{tag_id}/device/{device_id}',
'/trade-documents/',
'/users/', '/users/',
'/users/login/' '/users/login/'
# '/devices/{dev1_id}/merge/{dev2_id}', # '/devices/{dev1_id}/merge/{dev2_id}',
@ -119,4 +120,4 @@ def test_api_docs(client: Client):
'scheme': 'basic', 'scheme': 'basic',
'name': 'Authorization' 'name': 'Authorization'
} }
assert len(docs['definitions']) == 120 assert len(docs['definitions']) == 122

View File

@ -281,7 +281,8 @@ def test_endpoint_confirm(user: UserClient, user2: UserClient):
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [device_id] 'devices': [device_id],
'documents': []
} }
user2.post(res=models.Action, data=request_confirm) user2.post(res=models.Action, data=request_confirm)
@ -319,13 +320,15 @@ def test_confirm_revoke(user: UserClient, user2: UserClient):
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [device_id] 'devices': [device_id],
'documents': []
} }
request_revoke = { request_revoke = {
'type': 'Revoke', 'type': 'Revoke',
'action': trade.id, 'action': trade.id,
'devices': [device_id], 'devices': [device_id],
'documents': []
} }
@ -402,9 +405,9 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [snap1['device']['id'], snap2['device']['id'], snap3['device']['id']] 'devices': [snap1['device']['id'], snap2['device']['id'], snap3['device']['id']],
'documents': []
} }
# import pdb; pdb.set_trace()
user2.post(res=models.Action, data=request_confirm) user2.post(res=models.Action, data=request_confirm)
assert trade.devices[0].actions[-1].t == 'Confirm' assert trade.devices[0].actions[-1].t == 'Confirm'
@ -417,7 +420,8 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
'action': trade.id, 'action': trade.id,
'devices': [ 'devices': [
snap10['device']['id'] snap10['device']['id']
] ],
'documents': []
} }
user2.post(res=models.Action, data=request_confirm, status=422) user2.post(res=models.Action, data=request_confirm, status=422)
@ -484,7 +488,8 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
'action': device_10.actions[-1].id, 'action': device_10.actions[-1].id,
'devices': [ 'devices': [
snap10['device']['id'] snap10['device']['id']
] ],
'documents': []
} }
user2.post(res=models.Action, data=request_confirm_revoke) user2.post(res=models.Action, data=request_confirm_revoke)
@ -496,7 +501,8 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
'action': device_10.actions[-1].id, 'action': device_10.actions[-1].id,
'devices': [ 'devices': [
snap9['device']['id'] snap9['device']['id']
] ],
'documents': []
} }
# check validation error # check validation error
@ -521,9 +527,9 @@ def test_usecase_confirmation(user: UserClient, user2: UserClient):
'action': trade.id, 'action': trade.id,
'devices': [ 'devices': [
snap10['device']['id'] snap10['device']['id']
] ],
'documents': []
} }
# import pdb; pdb.set_trace()
user2.post(res=models.Action, data=request_reconfirm) user2.post(res=models.Action, data=request_reconfirm)
assert device_10.actions[-1].t == 'Confirm' assert device_10.actions[-1].t == 'Confirm'
assert device_10.actions[-1].user == trade.user_from assert device_10.actions[-1].user == trade.user_from
@ -610,9 +616,9 @@ def test_confirmRevoke(user: UserClient, user2: UserClient):
# is create one revoke action # is create one revoke action
device_10 = trade.devices[-1] device_10 = trade.devices[-1]
lot, _ = user.delete({}, lot, _ = user.delete({},
res=Lot, res=Lot,
item='{}/devices'.format(lot['id']), item='{}/devices'.format(lot['id']),
query=devices[-1:], status=200) query=devices[-1:], status=200)
assert len(trade.lot.devices) == len(trade.devices) == 9 assert len(trade.lot.devices) == len(trade.devices) == 9
assert not device_10 in trade.devices assert not device_10 in trade.devices
assert device_10.actions[-1].t == 'Revoke' assert device_10.actions[-1].t == 'Revoke'
@ -642,7 +648,8 @@ def test_confirmRevoke(user: UserClient, user2: UserClient):
'action': device_10.actions[-2].id, 'action': device_10.actions[-2].id,
'devices': [ 'devices': [
snap10['device']['id'] snap10['device']['id']
] ],
'documents': []
} }
# check validation error # check validation error
@ -654,7 +661,8 @@ def test_confirmRevoke(user: UserClient, user2: UserClient):
'action': trade.id, 'action': trade.id,
'devices': [ 'devices': [
snap10['device']['id'] snap10['device']['id']
] ],
'documents': []
} }
user2.post(res=models.Action, data=request_reconfirm) user2.post(res=models.Action, data=request_reconfirm)
assert device_10.actions[-1].t == 'Confirm' assert device_10.actions[-1].t == 'Confirm'
@ -668,7 +676,10 @@ def test_confirmRevoke(user: UserClient, user2: UserClient):
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_simple_add_document(user: UserClient): def test_simple_add_document(user: UserClient):
"""Example of one document inserted into one lot""" """Example of one document inserted into one lot"""
doc = TradeDocument(**{'file_name': 'test', 'owner_id': user.user['id']}) lot, _ = user.post({'name': 'MyLot'}, res=Lot)
doc = TradeDocument(**{'file_name': 'test',
'owner_id': user.user['id'],
'lot_id': lot['id']})
db.session.add(doc) db.session.add(doc)
db.session.flush() db.session.flush()
@ -690,7 +701,6 @@ def test_add_document_to_lot(user: UserClient, user2: UserClient, client: Client
path_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'] , user.email) path_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'] , user.email)
path = os.path.join(path_dir_base, lot['id']) path = os.path.join(path_dir_base, lot['id'])
assert len(os.listdir(path)) == 1 assert len(os.listdir(path)) == 1
# import pdb; pdb.set_trace()
user.get(res=TradeDocument, item=doc['id']) user.get(res=TradeDocument, item=doc['id'])
user.delete(res=TradeDocument, item=doc['id']) user.delete(res=TradeDocument, item=doc['id'])
@ -714,8 +724,6 @@ def test_usecase1_document(user: UserClient, user2: UserClient):
"""Example of one usecase about confirmation""" """Example of one usecase about confirmation"""
# the pRp (manatest_usecase_confirmationger) creates a temporary lot # the pRp (manatest_usecase_confirmationger) creates a temporary lot
lot, _ = user.post({'name': 'MyLot'}, res=Lot) lot, _ = user.post({'name': 'MyLot'}, res=Lot)
path_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'] , user.email)
path = os.path.join(path_dir_base, lot['id'])
data = {'lot': lot['id'], 'file_name': 'test.csv'} data = {'lot': lot['id'], 'file_name': 'test.csv'}
base64_bytes = base64.b64encode(b'This is a test') base64_bytes = base64.b64encode(b'This is a test')
base64_string = base64_bytes.decode('utf-8') base64_string = base64_bytes.decode('utf-8')
@ -747,12 +755,11 @@ def test_usecase1_document(user: UserClient, user2: UserClient):
assert trade.documents[1].actions[-1].user == trade.user_to assert trade.documents[1].actions[-1].user == trade.user_to
assert trade.documents[0].path_name != trade.documents[1].path_name assert trade.documents[0].path_name != trade.documents[1].path_name
# ========
# import pdb; pdb.set_trace()
# the SCRAP confirms 1 of the 2 documents in its outgoing lot # the SCRAP confirms 1 of the 2 documents in its outgoing lot
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [],
'documents': [trade.documents[0].id] 'documents': [trade.documents[0].id]
} }
@ -762,126 +769,152 @@ def test_usecase1_document(user: UserClient, user2: UserClient):
n_actions = len(trade.documents[0].actions) n_actions = len(trade.documents[0].actions)
# check validation error # check validation error
# request_confirm = {
# 'type': 'Confirm',
# 'action': trade.id,
# 'devices': [
# snap10['device']['id']
# ]
# }
# user2.post(res=models.Action, data=request_confirm, status=422)
# ========
shutil.rmtree(path)
"""
# The manager add 3 device more into the lot
lot, _ = user.post({},
res=Lot,
item='{}/devices'.format(lot['id']),
query=devices[7:])
assert trade.devices[-1].actions[-2].t == 'Trade'
assert trade.devices[-1].actions[-1].t == 'Confirm'
assert trade.devices[-1].actions[-1].user == trade.user_to
assert len(trade.devices[0].actions) == n_actions
# the SCRAP confirms the rest of devices
request_confirm = { request_confirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [ 'devices': [],
snap1['device']['id'], 'documents': [trade.documents[0].id],
snap2['device']['id'], }
snap3['device']['id'],
snap4['device']['id'], user2.post(res=models.Action, data=request_confirm, status=422)
snap5['device']['id'],
snap6['device']['id'], # Both users can up de documents
snap7['device']['id'], user2.post(res=TradeDocument, data=data)
snap8['device']['id'], assert len(trade.documents) == 3
snap9['device']['id'],
snap10['device']['id'] # user1 confirm document2
] request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [],
'documents': [trade.documents[2].id],
}
assert trade.documents[2].actions[-1].user == trade.user_from
assert trade.documents[2].actions[-1].t == 'Confirm'
user.post(res=models.Action, data=request_confirm)
assert trade.documents[2].actions[-1].user == trade.user_to
assert trade.documents[2].actions[-1].t == 'Confirm'
# clean docs of test
path_dir_base = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'], user.email)
path = os.path.join(path_dir_base, lot['id'])
path_dir_base_from = os.path.join(app.config['PATH_DOCUMENTS_STORAGE'], user2.email)
path_from = os.path.join(path_dir_base_from, lot['id'])
shutil.rmtree(path)
shutil.rmtree(path_from)
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_document_confirm_revoke(user: UserClient, user2: UserClient):
"""We want check one simple revoke"""
lot, _ = user.post({'name': 'MyLot'}, res=Lot)
data = {'lot': lot['id'], 'file_name': 'test.csv'}
base64_bytes = base64.b64encode(b'This is a test')
base64_string = base64_bytes.decode('utf-8')
data['file'] = base64_string
user.post(res=TradeDocument, data=data)
# the manager shares the temporary lot with the SCRAP as an incoming lot
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFrom': user2.email,
'userTo': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirm': True,
}
user.post(res=models.Action, data=request_post)
trade = models.Trade.query.one()
# the SCRAP confirms 1 in its outgoing lot
request_confirm = {
'type': 'Confirm',
'action': trade.id,
'devices': [],
'documents': [trade.documents[0].id]
} }
user2.post(res=models.Action, data=request_confirm) user2.post(res=models.Action, data=request_confirm)
assert trade.devices[-1].actions[-3].t == 'Trade'
assert trade.devices[-1].actions[-1].t == 'Confirm'
assert trade.devices[-1].actions[-1].user == trade.user_from
assert len(trade.devices[0].actions) == n_actions
# The manager remove one device of the lot and automaticaly # the SCRAP revoke the document
# is create one revoke action request_revoke = {
device_10 = trade.devices[-1] 'type': 'Revoke',
lot, _ = user.delete({}, 'action': trade.id,
res=Lot, 'devices': [],
item='{}/devices'.format(lot['id']), 'documents': [trade.documents[0].id]
query=devices[-1:], status=200)
assert len(trade.lot.devices) == len(trade.devices) == 9
assert not device_10 in trade.devices
assert device_10.actions[-1].t == 'Revoke'
lot, _ = user.delete({},
res=Lot,
item='{}/devices'.format(lot['id']),
query=devices[-1:], status=200)
assert device_10.actions[-1].t == 'Revoke'
assert device_10.actions[-2].t == 'Confirm'
# the SCRAP confirms the revoke action
request_confirm_revoke = {
'type': 'ConfirmRevoke',
'action': device_10.actions[-1].id,
'devices': [
snap10['device']['id']
]
} }
user2.post(res=models.Action, data=request_confirm_revoke) user2.post(res=models.Action, data=request_revoke)
assert device_10.actions[-1].t == 'ConfirmRevoke' assert trade.documents[0].actions[-1].t == 'Revoke'
assert device_10.actions[-2].t == 'Revoke' assert trade.documents[0].actions[-2].t == 'Confirm'
user2.post(res=models.Action, data=request_revoke, status=422)
request_confirm_revoke = {
'type': 'ConfirmRevoke', @pytest.mark.mvp
'action': device_10.actions[-1].id, @pytest.mark.usefixtures(conftest.app_context.__name__)
'devices': [ def test_document_confirm_revoke_confirmRevoke(user: UserClient, user2: UserClient):
snap9['device']['id'] """We want check one simple revoke"""
] lot, _ = user.post({'name': 'MyLot'}, res=Lot)
data = {'lot': lot['id'], 'file_name': 'test.csv'}
base64_bytes = base64.b64encode(b'This is a test')
base64_string = base64_bytes.decode('utf-8')
data['file'] = base64_string
user.post(res=TradeDocument, data=data)
# the manager shares the temporary lot with the SCRAP as an incoming lot
# for the CRAP to confirm it
request_post = {
'type': 'Trade',
'devices': [],
'userFrom': user2.email,
'userTo': user.email,
'price': 10,
'date': "2020-12-01T02:00:00+00:00",
'lot': lot['id'],
'confirm': True,
} }
# check validation error user.post(res=models.Action, data=request_post)
user2.post(res=models.Action, data=request_confirm_revoke, status=422) trade = models.Trade.query.one()
# the SCRAP confirms 1 in its outgoing lot
# The manager add again device_10 request_confirm = {
assert len(trade.devices) == 9
lot, _ = user.post({},
res=Lot,
item='{}/devices'.format(lot['id']),
query=devices[-1:])
assert device_10.actions[-1].t == 'Confirm'
assert device_10 in trade.devices
assert len(trade.devices) == 10
# the SCRAP confirms the action trade for device_10
request_reconfirm = {
'type': 'Confirm', 'type': 'Confirm',
'action': trade.id, 'action': trade.id,
'devices': [ 'devices': [],
snap10['device']['id'] 'documents': [trade.documents[0].id]
]
} }
user2.post(res=models.Action, data=request_reconfirm)
assert device_10.actions[-1].t == 'Confirm'
assert device_10.actions[-1].user == trade.user_from
assert device_10.actions[-2].t == 'Confirm'
assert device_10.actions[-2].user == trade.user_to
assert device_10.actions[-3].t == 'ConfirmRevoke'
assert len(device_10.actions) == 13
""" user2.post(res=models.Action, data=request_confirm)
# the SCRAP revoke the document
request_revoke = {
'type': 'Revoke',
'action': trade.id,
'devices': [],
'documents': [trade.documents[0].id]
}
user2.post(res=models.Action, data=request_revoke)
# Manager confirmRevoke
request_revoke = {
'type': 'ConfirmRevoke',
'action': trade.id,
'devices': [],
'documents': [trade.documents[0].id]
}
user.post(res=models.Action, data=request_revoke)
assert trade.documents[0].actions[-1].t == 'ConfirmRevoke'
assert trade.documents[0].actions[-2].t == 'Revoke'
user.post(res=models.Action, data=request_revoke, status=422)