diff --git a/tests/conftest.py b/tests/conftest.py index feadd74..131b00b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -72,3 +72,29 @@ def transferred_derived_right(client, alice, bob, # Sleep for a bit to let the transaction become valid sleep(3) return created_derived_right_with_mock_source + + +@pytest.fixture +def retransferred_derived_right(client, bob, carly, transferred_derived_right): + import json + from time import sleep + + payload = { + 'rightId': transferred_derived_right['@id'], + 'rightsAssignment': { + 'action': 'loan', + }, + 'currentHolder': bob, + 'to': { + 'publicKey': carly['publicKey'], + 'privateKey': None, + } + } + + client.post(url_for('right_views.righttransferapi'), + data=json.dumps(payload), + headers={'Content-Type': 'application/json'}) + + # Sleep for a bit to let the transaction become valid + sleep(3) + return transferred_derived_right diff --git a/tests/test_api.py b/tests/test_api.py index df5b024..1dab11f 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -226,3 +226,16 @@ def test_retransferred_right(client, bob, carly, transferred_derived_right): headers={'Content-Type': 'application/json'}) assert resp.status_code == 200 assert resp.json == retransfer_expected + + +def test_right_history(client, alice, bob, carly, retransferred_derived_right): + right_id = retransferred_derived_right['@id'] + resp = client.get( + url_for('right_views.righthistoryapi', right_id=right_id)) + assert resp.status_code == 200 + assert resp.json[0]['user']['publicKey'] == alice['publicKey'] + assert resp.json[1]['user']['publicKey'] == bob['publicKey'] + assert resp.json[2]['user']['publicKey'] == carly['publicKey'] + + # First transaction should be the CREATE transaction + assert resp.json[0]['refId'] == right_id diff --git a/web/views/rights.py b/web/views/rights.py index e94ba88..c6a0a5d 100644 --- a/web/views/rights.py +++ b/web/views/rights.py @@ -42,6 +42,21 @@ def post(self): return res +class RightHistoryApi(Resource): + def get(self, right_id): + # Don't worry about whether the entity corresponding to `right_id` is a + # Right or a Copyright since we won't be loading it + right = entities.Right.from_persist_id(right_id, plugin=coalaip.plugin, + force_load=False) + return [{ + 'user': { + 'publicKey': event['user']['public_key'], + 'privateKey': event['user']['private_key'], + }, + 'refId': event['ref_id'], + } for event in right.history] + + class RightTransferApi(Resource): def post(self): parser = reqparse.RequestParser() @@ -86,5 +101,7 @@ def post(self): right_api.add_resource(RightApi, '/rights', strict_slashes=False) +right_api.add_resource(RightHistoryApi, '/rights/history/', + strict_slashes=False) right_api.add_resource(RightTransferApi, '/rights/transfer', strict_slashes=False)