1
|
|
|
from splinter import Browser |
2
|
|
|
import time |
3
|
|
|
import os |
4
|
|
|
import six |
5
|
|
|
import json |
6
|
|
|
import re |
7
|
|
|
from bitpay.client import Client |
8
|
|
|
from bitpay import key_utils |
9
|
|
|
|
10
|
|
|
ROOT_ADDRESS = os.environ['RCROOTADDRESS'] |
11
|
|
|
USER_NAME = os.environ['RCTESTUSER'] |
12
|
|
|
PASSWORD = os.environ['RCTESTPASSWORD'] |
13
|
|
|
PEM = '-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICg7E4NN53YkaWuAwpoqjfAofjzKI7Jq1f532dX+0O6QoAcGBSuBBAAK\noUQDQgAEjZcNa6Kdz6GQwXcUD9iJ+t1tJZCx7hpqBuJV2/IrQBfue8jh8H7Q/4vX\nfAArmNMaGotTpjdnymWlMfszzXJhlw==\n-----END EC PRIVATE KEY-----\n' |
14
|
|
|
client = Client() |
15
|
|
|
invoice = None |
16
|
|
|
exception = None |
17
|
|
|
|
18
|
|
|
@given(u'the user pairs with BitPay with a valid pairing code') |
19
|
|
|
def step_impl(context): |
20
|
|
|
claim_code = get_claim_code_from_server() |
21
|
|
|
global client |
22
|
|
|
client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) |
23
|
|
|
try: |
24
|
|
|
client.pair_pos_client(claim_code) |
25
|
|
|
except Exception as error: |
26
|
|
|
if error.args[0] == "500: Unable to create token because of too many requests.": |
27
|
|
|
time.sleep(60) |
28
|
|
|
client.pair_pos_client(claim_code) |
29
|
|
|
assert client.tokens['pos'] |
30
|
|
|
|
31
|
|
|
@given(u'the user waits {wait:d} seconds') |
32
|
|
|
def step_impl(context, wait): |
33
|
|
|
time.sleep(wait) |
34
|
|
|
|
35
|
|
|
@then(u'the user is paired with BitPay') |
36
|
|
|
def step_impl(context): |
37
|
|
|
assert client.verify_tokens() |
38
|
|
|
|
39
|
|
|
@given(u'the user fails to pair with a semantically {valid} code {code}') |
40
|
|
|
def step_impl(context, code, valid): |
41
|
|
|
try: |
42
|
|
|
client.pair_pos_client(code) |
43
|
|
|
except Exception as error: |
44
|
|
|
global exception |
45
|
|
|
exception = error |
46
|
|
|
if exception.args[0] == "500: Unable to create token because of too many requests.": |
47
|
|
|
time.sleep(60) |
48
|
|
|
try: |
49
|
|
|
client.pair_pos_client(code) |
50
|
|
|
except Exception as error: |
51
|
|
|
global exception |
52
|
|
|
exception = error |
53
|
|
|
|
54
|
|
|
@when(u'the user fails to pair with BitPay because of an incorrect port') |
55
|
|
|
def step_impl(context): |
56
|
|
|
badAddress = ROOT_ADDRESS.split(":") |
57
|
|
|
badAddress = badAddress[0] + ":" + badAddress[1] + ":999" |
58
|
|
|
newclient = Client(api_uri=badAddress, insecure=True) |
59
|
|
|
try: |
60
|
|
|
newclient.pair_pos_client("1a2C3d4") |
61
|
|
|
raise "That should totally not have worked" |
62
|
|
|
except Exception as error: |
63
|
|
|
global exception |
64
|
|
|
exception = error |
65
|
|
|
|
66
|
|
|
@given(u'the user requests a client-side pairing') |
67
|
|
|
def step_impl(context): |
68
|
|
|
global pairing_code |
69
|
|
|
client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=PEM) |
70
|
|
|
pairing_code = client.create_token("merchant") |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
@then(u'they will receive a claim code') |
74
|
|
|
def step_impl(context): |
75
|
|
|
assert re.match("^\w{7,7}$", pairing_code) != None |
76
|
|
|
|
77
|
|
|
@then(u'they will receive a {error} matching {message}') |
78
|
|
|
def step_impl(context, error, message): |
79
|
|
|
assert exception.__class__.__name__ == error and exception.args[0] == message, "expected %s to match %s" % (exception.args[0], message) |
80
|
|
|
|
81
|
|
|
@given(u'the user is authenticated with BitPay') |
82
|
|
|
def step_impl(context): |
83
|
|
|
global client |
84
|
|
|
client = client_from_stored_values() |
85
|
|
|
assert client.verify_tokens() |
86
|
|
|
|
87
|
|
|
@when(u'the user creates an invoice for {amount:f} {currency} with float input') |
88
|
|
|
def step_impl(context, amount, currency): |
89
|
|
|
create_invoice(amount, currency) |
90
|
|
|
|
91
|
|
|
@when(u'the user creates an invoice for {amount:d} {currency} with integer input') |
92
|
|
|
def step_impl(context, amount, currency): |
93
|
|
|
create_invoice(amount, currency) |
94
|
|
|
|
95
|
|
|
@when(u'the user creates an invoice for {amount} {currency} with string input') |
96
|
|
|
def step_impl(context, amount, currency): |
97
|
|
|
if amount == '""': |
98
|
|
|
amount = "" |
99
|
|
|
if currency == '""': |
100
|
|
|
currency == "" |
101
|
|
|
create_invoice(amount, currency) |
102
|
|
|
|
103
|
|
|
@then(u'they should recieve an invoice in response for {amount:g} {currency}') |
104
|
|
|
def step_impl(context, amount, currency): |
105
|
|
|
global invoice |
106
|
|
|
assert invoice['price'] == amount and invoice['currency'] == currency |
107
|
|
|
|
108
|
|
|
def create_invoice(amount, currency): |
109
|
|
|
global client |
110
|
|
|
global invoice |
111
|
|
|
try: |
112
|
|
|
token = client.tokens['pos'] |
113
|
|
|
invoice = client.create_invoice({"price": amount, "currency": currency, "token": token }) |
114
|
|
|
except Exception as error: |
115
|
|
|
global exception |
116
|
|
|
print(error.__class__.__name__) |
117
|
|
|
print(error.args[0]) |
118
|
|
|
exception = error |
119
|
|
|
|
120
|
|
|
@given(u'that a user knows an invoice id') |
121
|
|
|
def step_impl(context): |
122
|
|
|
global client |
123
|
|
|
global invoice |
124
|
|
|
client = client_from_stored_values() |
125
|
|
|
invoice = client.create_invoice({"price": 10, "currency": "USD", "token": client.tokens['pos'] }) |
126
|
|
|
|
127
|
|
|
@then(u'they can retrieve that invoice') |
128
|
|
|
def step_impl(context): |
129
|
|
|
global client |
130
|
|
|
global invoice |
131
|
|
|
amount = invoice['price'] |
132
|
|
|
invoice_id = invoice['id'] |
133
|
|
|
retrieved_invoice = client.get_invoice(invoice_id) |
134
|
|
|
assert amount == retrieved_invoice['price'] |
135
|
|
|
|
136
|
|
|
def client_from_stored_values(): |
137
|
|
|
for f in ["local.pem", "tokens.json"]: |
138
|
|
|
try: |
139
|
|
|
open("temp/" + f) |
140
|
|
|
exists = True |
141
|
|
|
except: |
142
|
|
|
exists = False |
143
|
|
|
break |
144
|
|
|
if exists: |
145
|
|
|
f = open("temp/local.pem", 'r') |
146
|
|
|
pem = f.read() |
147
|
|
|
f = open("temp/tokens.json", 'r') |
148
|
|
|
token = f.read() |
149
|
|
|
token = json.loads(token) |
150
|
|
|
client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem, tokens=token) |
151
|
|
|
else: |
152
|
|
|
claim_code = get_claim_code_from_server() |
153
|
|
|
pem = key_utils.generate_pem() |
154
|
|
|
client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem) |
155
|
|
|
token = json.dumps(client.pair_pos_client(claim_code)) |
156
|
|
|
if not os.path.exists("temp"): |
157
|
|
|
os.makedirs("temp") |
158
|
|
|
f = open("temp/local.pem", 'w') |
159
|
|
|
f.write(pem) |
160
|
|
|
f = open("temp/tokens.json", 'w') |
161
|
|
|
f.write(token) |
162
|
|
|
return client |
163
|
|
|
|
164
|
|
|
def get_claim_code_from_server(): |
165
|
|
|
browser = Browser('phantomjs', service_args=['--ignore-ssl-errors=true']) |
166
|
|
|
browser.visit(ROOT_ADDRESS + "/merchant-login") |
167
|
|
|
browser.fill_form({"email": USER_NAME, "password": PASSWORD}) |
168
|
|
|
browser.find_by_id("loginButton")[0].click() |
169
|
|
|
time.sleep(5) |
170
|
|
|
browser.visit(ROOT_ADDRESS + "/api-tokens") |
171
|
|
|
browser.find_by_css(".token-access-new-button").find_by_css(".btn").find_by_css(".icon-plus")[0].click() |
172
|
|
|
browser.find_by_id("token-new-form").find_by_css(".btn")[0].click() |
173
|
|
|
return browser.find_by_css(".token-claimcode")[0].html |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
|