Completed
Branch get_test_coverage (768a8a)
by Andreas
33s
created

TestBtcdeAPIDocu.test_signature_get()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
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
        self.conn = btcde.Connection(self.XAPIKEY, self.XAPISECRET)
46
        
47
    def tearDown(self):
48
        del self.XAPIKEY
49
        del self.XAPISECRET
50
        del self.XAPINONCE
51
        del self.conn
52
     
53
    def test_signature_post(self, m):
54
        '''Test the signature on a post request.'''
55
        params = {'type': 'buy',
56
                  'trading_pair': 'btceur',
57
                  'max_amount': 10,
58
                  'price': 1337}
59
        response = {"order_id": "A1234BC",
60
                    "errors": [],
61
                    "credits": 8,}
62
        m.post(requests_mock.ANY, json = response, status_code=201)
63
        btcde.createOrder(self.conn, params.get('type'), params.get('trading_pair'), params.get('max_amount'), params.get('price'))
64
        history = m.request_history
65
        request_signature = history[0].headers.get('X-API-SIGNATURE')
66
        verified_signature = self.verifySignature(btcde.orderuri, 'POST', params)
67
        self.assertEqual(request_signature, verified_signature)
68
        
69
        
70
    def test_signature_get(self, m):
71
        '''Test the signature on a get request.'''
72
        pass
73