Skip to content

Commit

Permalink
Implement history querying for Rights
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai committed Feb 10, 2017
1 parent d457193 commit 8694da9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions web/views/rights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -86,5 +101,7 @@ def post(self):


right_api.add_resource(RightApi, '/rights', strict_slashes=False)
right_api.add_resource(RightHistoryApi, '/rights/history/<string:right_id>',
strict_slashes=False)
right_api.add_resource(RightTransferApi, '/rights/transfer',
strict_slashes=False)

0 comments on commit 8694da9

Please sign in to comment.