|
1
|
|
|
from unittest import TestCase |
|
2
|
|
|
from mock import patch |
|
3
|
|
|
import time |
|
4
|
|
|
import hashlib |
|
5
|
|
|
import hmac |
|
6
|
|
|
import requests |
|
7
|
|
|
import requests_mock |
|
8
|
|
|
import btcde |
|
9
|
|
|
|
|
10
|
|
|
@requests_mock.Mocker() |
|
11
|
|
|
class TestBtcdeAPIDocu(TestCase): |
|
12
|
|
|
'''Tests are as in bitcoin.de API documentation. |
|
13
|
|
|
https://www.bitcoin.de/de/api/tapi/v2/docu''' |
|
14
|
|
|
|
|
15
|
|
|
def sortParams(self, url, params={}): |
|
16
|
|
|
'''To sort params for url string.''' |
|
17
|
|
|
self.encoded_string = '' |
|
18
|
|
|
if len(params) > 0: |
|
19
|
|
|
for key, value in sorted(params.items()): |
|
20
|
|
|
self.encoded_string += str(key) + '=' + str(value) + '&' |
|
21
|
|
|
self.encoded_string = self.encoded_string[:-1] |
|
22
|
|
|
self.url = url + '?' + self.encoded_string |
|
23
|
|
|
else: |
|
24
|
|
|
self.url = url |
|
25
|
|
|
return self.url, self.encoded_string |
|
26
|
|
|
|
|
27
|
|
|
def verifySignature(self, url, method, params): |
|
28
|
|
|
'''To verify API Signature.''' |
|
29
|
|
|
self.XAPINONCE += 1 |
|
30
|
|
|
self.url, self.encoded_string = self.sortParams(url, params) |
|
31
|
|
|
if method == 'POST': |
|
32
|
|
|
md5_encoded_query_string = hashlib.md5(self.encoded_string.encode()).hexdigest() |
|
33
|
|
|
else: |
|
34
|
|
|
md5_encoded_query_string = hashlib.md5(b'').hexdigest() |
|
35
|
|
|
hmac_data = method + '#' + \ |
|
36
|
|
|
self.url + '#' + self.XAPIKEY + \ |
|
37
|
|
|
'#' + str(self.XAPINONCE) + '#' + md5_encoded_query_string |
|
38
|
|
|
hmac_signed = hmac.new(bytearray(self.XAPISECRET.encode()), msg=hmac_data.encode(), digestmod=hashlib.sha256).hexdigest() |
|
39
|
|
|
return hmac_signed |
|
40
|
|
|
|
|
41
|
|
|
def setUp(self): |
|
42
|
|
|
self.XAPIKEY = 'f00b4r' |
|
43
|
|
|
self.XAPISECRET = 'b4rf00' |
|
44
|
|
|
self.XAPINONCE = int(time.time()) |
|
45
|
|
|
btcde.nonce = self.XAPINONCE |
|
46
|
|
|
self.conn = btcde.Connection(self.XAPIKEY, self.XAPISECRET) |
|
47
|
|
|
|
|
48
|
|
|
def tearDown(self): |
|
49
|
|
|
del self.XAPIKEY |
|
50
|
|
|
del self.XAPISECRET |
|
51
|
|
|
del self.XAPINONCE |
|
52
|
|
|
del self.conn |
|
53
|
|
|
|
|
54
|
|
|
def test_signature_post(self, m): |
|
55
|
|
|
'''Test the signature on a post request.''' |
|
56
|
|
|
params = {'type': 'buy', |
|
57
|
|
|
'trading_pair': 'btceur', |
|
58
|
|
|
'max_amount': 10, |
|
59
|
|
|
'price': 1337} |
|
60
|
|
|
response = {"order_id": "A1234BC", |
|
61
|
|
|
"errors": [], |
|
62
|
|
|
"credits": 8,} |
|
63
|
|
|
m.post(requests_mock.ANY, json = response, status_code=201) |
|
64
|
|
|
btcde.createOrder(self.conn, params.get('type'), params.get('trading_pair'), params.get('max_amount'), params.get('price')) |
|
65
|
|
|
history = m.request_history |
|
66
|
|
|
request_signature = history[0].headers.get('X-API-SIGNATURE') |
|
67
|
|
|
verified_signature = self.verifySignature(btcde.orderuri, 'POST', params) |
|
68
|
|
|
self.assertEqual(request_signature, verified_signature) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def test_signature_get(self, m): |
|
72
|
|
|
'''Test the signature on a get request.''' |
|
73
|
|
|
pass |
|
74
|
|
|
|