Skip to content

Commit

Permalink
Fixes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Jul 28, 2015
1 parent 1639e70 commit b30e98a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,4 @@ Fail Examples:
RESPONSE:
Status Code: 404
Text: Ping Failed: Farmer not found.

11 changes: 11 additions & 0 deletions dataserv/Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Farmer(db.Model):
last_seen = db.Column(DateTime, default=datetime.utcnow)
last_audit = db.Column(DateTime, default=datetime.utcnow)

height = db.Column(db.Integer, default=0)

def __init__(self, btc_addr, last_seen=None, last_audit=None):
"""
A farmer is a un-trusted client that provides some disk space
Expand Down Expand Up @@ -108,3 +110,12 @@ def list_contracts(self):

con = Contract(self.btc_addr)
return con.list_contracts()

def set_height(self, height):
"""Set the farmers advertised height."""
self.validate()

self.height = height
db.session.commit()

return self.height
18 changes: 18 additions & 0 deletions dataserv/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ def list_contracts(btc_addr):
msg = "Farmer Not Found."
return make_response(msg, 404)


@app.route('/api/height/<btc_addr>/<int:height>', methods=["GET"])
def set_height(btc_addr, height):
# create Farmer object to represent user
user = Farmer(btc_addr)

# attempt to set height
try:
user.set_height(height)
return make_response("Height Accepted.", 200)
except ValueError:
msg = "Invalid BTC Address."
return make_response(msg, 400)
except LookupError:
msg = "Farmer Not Found."
return make_response(msg, 404)


if __name__ == '__main__': # pragma: no cover
# Create Database
db.create_all()
Expand Down
20 changes: 20 additions & 0 deletions tests/test_App.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,23 @@ def test_list_contract_no_bad_address(self):
addr = 'notvalidaddress'
rv = self.app.get('/api/contract/list/{0}'.format(addr))
self.assertEqual(rv.status_code, 400)

def test_farmer_set_height(self):
addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'
addr2 = 'notvalidaddress'

# not found
rv = self.app.get('/api/height/{0}/1'.format(addr1))
self.assertEqual(rv.status_code, 404)

# register farmer
self.app.get('/api/register/{0}'.format(addr1))

# correct
rv = self.app.get('/api/height/{0}/1'.format(addr1))
self.assertEqual(rv.status_code, 200)

# invalid btc address
rv = self.app.get('/api/height/{0}/1'.format(addr2))
self.assertEqual(rv.status_code, 400)

8 changes: 8 additions & 0 deletions tests/test_Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ def test_sha256(self):
ans = 'c059c8035bbd74aa81f4c787c39390b57b974ec9af25a7248c46a3ebfe0f9dc8'
self.assertEqual(sha256("storj"), ans)
self.assertNotEqual(sha256("not storj"), ans)

def test_height(self):
addr = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'
farmer = Farmer(addr)
farmer.register()

self.assertEqual(farmer.height, 0)
self.assertEqual(farmer.set_height(5), 5)

0 comments on commit b30e98a

Please sign in to comment.