1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
Copyright (c) 2015-2016 WePay. |
6
|
|
|
|
7
|
|
|
Based on a stripped-down version of the AWS Signature v4 implementation. |
8
|
|
|
|
9
|
|
|
http://opensource.org/licenses/Apache2.0 |
10
|
|
|
""" |
11
|
|
|
|
12
|
|
|
from __future__ import print_function |
13
|
|
|
import unittest |
14
|
|
|
import nose2 |
15
|
|
|
from wepay.signer import Signer |
16
|
|
|
|
17
|
|
|
# Test data |
18
|
|
|
DEFAULT_CLIENT_ID = 12173158495 |
19
|
|
|
DEFAULT_CLIENT_SECRET = '1594122c5c36f438f8ba' |
20
|
|
|
DEFAULT_SIGNATURE = ('c2de34c15cd76f797cf80781747da3874639a827a4cb79dcd862cc17b35cf2e2c721ea7d49ab' |
21
|
|
|
'9f60590d637ae0f51fd4ed8ddb551b922e0cd7e35a13b86de360') |
22
|
|
|
DEFAULT_PAGE = 'https://wepay.com/account/12345' |
23
|
|
|
DEFAULT_REDIRECT_URI = 'https://partnersite.com/home' |
24
|
|
|
DEFAULT_QS = 'client_id={}&page={}&redirect_uri={}&stoken={}&token={}' |
25
|
|
|
DEFAULT_TOKEN = '10c936ca-5e7c-508b-9e60-b211c20be9bc' |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class Test(unittest.TestCase): |
29
|
|
|
"""Unit tests for the wepay.signer.Signer class.""" |
30
|
|
|
|
31
|
|
|
def setUp(self): |
32
|
|
|
"""Instantiate the class.""" |
33
|
|
|
self.signer = Signer(DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET) |
34
|
|
|
|
35
|
|
|
def test_get_self_key(self): |
36
|
|
|
"""Make sure we can read the self_key property.""" |
37
|
|
|
self.assertEqual('WePay', self.signer.self_key) |
38
|
|
|
|
39
|
|
|
def test_get_client_key(self): |
40
|
|
|
"""Make sure we can read the client_key property.""" |
41
|
|
|
self.assertEqual("{}".format(DEFAULT_CLIENT_ID), self.signer.client_id) |
42
|
|
|
|
43
|
|
|
def test_get_client_secret(self): |
44
|
|
|
"""Make sure we can read the client_secret property.""" |
45
|
|
|
self.assertEqual("{}".format(DEFAULT_CLIENT_SECRET), self.signer.client_secret) |
46
|
|
|
|
47
|
|
|
def test_get_hash_algo(self): |
48
|
|
|
"""Make sure we can read the hash_algo property.""" |
49
|
|
|
self.assertEqual('sha512', self.signer.hash_algo().name) |
50
|
|
|
|
51
|
|
|
def test_sign(self): |
52
|
|
|
"""Generate a new signature and ensure it matches what is expected with the same inputs.""" |
53
|
|
|
signature = self.signer.sign({ |
54
|
|
|
'page': DEFAULT_PAGE, |
55
|
|
|
'redirect_uri': DEFAULT_REDIRECT_URI, |
56
|
|
|
'token': DEFAULT_TOKEN, |
57
|
|
|
}) |
58
|
|
|
|
59
|
|
|
self.assertEqual(DEFAULT_SIGNATURE, signature) |
60
|
|
|
|
61
|
|
|
def test_query_string_params(self): |
62
|
|
|
"""Generate a new querystring with signature and ensure it matches what is expected with the same inputs.""" |
63
|
|
|
querystring = self.signer.generate_query_string_params({ |
64
|
|
|
'page': DEFAULT_PAGE, |
65
|
|
|
'redirect_uri': DEFAULT_REDIRECT_URI, |
66
|
|
|
'token': DEFAULT_TOKEN, |
67
|
|
|
}) |
68
|
|
|
|
69
|
|
|
self.assertEqual( |
70
|
|
|
DEFAULT_QS.format(DEFAULT_CLIENT_ID, DEFAULT_PAGE, DEFAULT_REDIRECT_URI, DEFAULT_SIGNATURE, DEFAULT_TOKEN), |
71
|
|
|
querystring |
72
|
|
|
) |
73
|
|
|
|
74
|
|
|
def test_query_string_params_client_secret(self): |
75
|
|
|
"""Generate a new querystring with signature and ensure it matches what is expected with the same inputs.""" |
76
|
|
|
querystring = self.signer.generate_query_string_params({ |
77
|
|
|
'page': DEFAULT_PAGE, |
78
|
|
|
'redirect_uri': DEFAULT_REDIRECT_URI, |
79
|
|
|
'token': DEFAULT_TOKEN, |
80
|
|
|
'client_id': DEFAULT_CLIENT_ID, |
81
|
|
|
'client_secret': DEFAULT_CLIENT_SECRET, |
82
|
|
|
}) |
83
|
|
|
|
84
|
|
|
self.assertEqual( |
85
|
|
|
DEFAULT_QS.format(DEFAULT_CLIENT_ID, DEFAULT_PAGE, DEFAULT_REDIRECT_URI, DEFAULT_SIGNATURE, DEFAULT_TOKEN), |
86
|
|
|
querystring |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
if __name__ == '__main__': |
90
|
|
|
nose2.main() |
91
|
|
|
|