Skip to content

Commit

Permalink
added authentication headers test
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed Aug 19, 2015
1 parent 07c0fee commit 2b5a09a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_App.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import unittest
from btctxstore import BtcTxStore
from dataserv.run import app, db
from dataserv.app import secs_to_mins, online_farmers
from email.utils import formatdate
from datetime import datetime
from datetime import timedelta
from time import mktime


class AppTest(unittest.TestCase):
Expand Down Expand Up @@ -186,3 +191,33 @@ def test_farmer_order(self):
# get farmers
farmers = online_farmers()
self.assertEqual(farmers[0].btc_addr, addr1)


class AppAuthenticationHeadersTest(unittest.TestCase):

def setUp(self):
app.config["SKIP_AUTHENTICATION"] = False # monkey patch
self.app = app.test_client()
db.create_all()

def tearDown(self):
db.session.remove()
db.drop_all()

def test_success(self):
blockchain = BtcTxStore()
wif = blockchain.create_key()
address = blockchain.get_address(wif)

# create header date and authorization signature
header_date = formatdate(timeval=mktime(datetime.now().timetuple()),
localtime=True, usegmt=True)
message = app.config["ADDRESS"] + " " + header_date
header_authorization = blockchain.sign_unicode(wif, message)

headers = {"Date": header_date, "Authorization": header_authorization }
url = '/api/register/{0}'.format(address)
rv = self.app.get(url, headers=headers)
self.assertEqual(b"User registered.", rv.data)
self.assertEqual(rv.status_code, 200)

2 changes: 2 additions & 0 deletions tests/test_Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,5 @@ def callback():
farmer.authenticate(header_authorization, header_date)
self.assertRaises(ValueError, callback)

# TODO test incorrect address
# TODO test incorrect signature

0 comments on commit 2b5a09a

Please sign in to comment.