Completed
Push — master ( e0dbd4...85d261 )
by
unknown
32s
created

step_impl()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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