forked from Ventura94/NOWPayments-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nowpay.py
195 lines (152 loc) · 5.06 KB
/
test_nowpay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Testing Module"""
from os import getenv
import pytest
from dotenv import load_dotenv
from requests.exceptions import HTTPError
from src.nowpay import NOWPayments
load_dotenv()
@pytest.fixture
def now_pay() -> NOWPayments:
"""
NOWPayments class fixture.
:params str api_key: Note, this is not a valid api key, just one that satisfies the regex
:return: NOWPayments class.
"""
return NOWPayments(getenv("API_KEY"))
@pytest.fixture
def now_pay_sandbox() -> NOWPayments:
"""
NOWPayments class fixture.
:params str api_key: Note, this is not a valid api key, just one that satisfies the regex
:return: NOWPayments class.
"""
return NOWPayments(getenv("SANDBOX_API_KEY"), debug_mode=True, sandbox=True)
def test_api_url(now_pay) -> None:
"""
API url param test
:param now_pay: NOWPayments class fixture
:return:
"""
assert now_pay.api_url == "https://api.nowpayments.io/v1/{}"
def test_sandbox_url(
now_pay_sandbox: NOWPayments,
) -> None:
"""
API Sandbox url param test
:param now_pay: NOWPayments class fixture
:return:
"""
assert now_pay_sandbox.api_url == "https://api-sandbox.nowpayments.io/v1/{}"
def test_wrong_api_format() -> None:
"""
API Sandbox key param test
:param now_pay: NOWPayments class fixture
:return:
"""
with pytest.raises(ValueError):
NOWPayments("XXXXX", debug_mode=True, sandbox=True)
def test_get_requests(
now_pay_sandbox: NOWPayments, # pylint: disable=redefined-outer-name
) -> None:
"""
Get request test
"""
response = now_pay_sandbox.get("STATUS")
assert response == "https://api-sandbox.nowpayments.io/v1/status"
response = now_pay_sandbox.create_payment(100, "usd", "btc")
assert response == "https://api-sandbox.nowpayments.io/v1/payment"
def test_api_status(
now_pay: NOWPayments, # pylint: disable=redefined-outer-name
) -> None:
"""
Get api status test
"""
assert now_pay.status() == {"message": "OK"}
def test_currencies(now_pay: NOWPayments) -> None:
"""
Get available currencies test.
"""
assert now_pay.currencies().get("currencies", "Not found") != "Not found"
def test_merchant_coins(
now_pay: NOWPayments, # pylint: disable=redefined-outer-name
) -> None:
"""
Get available merchant currencies test
"""
assert (
now_pay.merchant_coins().get("selectedCurrencies", "Not found") != "Not found"
)
def test_estimate(now_pay: NOWPayments) -> None:
"""
Get estimate price test.
"""
amount = 100
currency_from = "usd"
currency_to = "btc"
result = now_pay.estimate(amount, currency_from, currency_to)
assert float(result.get("estimated_amount", "Not found")) >= 0.0001
assert float(result.get("estimated_amount", "Not found")) <= 0.01
def test_estimate_error(now_pay: NOWPayments) -> None:
"""
Get estimate price test with error.
"""
amount = 100
currency_from = "nowpay"
currency_to = "btc"
with pytest.raises(HTTPError):
now_pay.estimate(amount, currency_from, currency_to)
def test_create_payment_unexpected_keyword_argument_error(
now_pay: NOWPayments, # pylint: disable=redefined-outer-name
) -> None:
"""
Create payment test with unexpected keyword argument error
"""
with pytest.raises(TypeError):
now_pay.create_payment(
price_amount=100,
price_currency="usd",
pay_currency="btc",
unexpected="argument",
)
def test_create_payment(now_pay: NOWPayments) -> None:
"""
Create payment test
"""
result = now_pay.create_payment(
price_amount=100, price_currency="usd", pay_currency="btc"
)
assert result.get("payment_id", "Not found") != "Not found"
def test_create_payment_with_argument(now_pay: NOWPayments) -> None:
"""
Create payment test with argument
"""
result = now_pay.create_payment(
price_amount=100,
price_currency="usd",
pay_currency="btc",
order_description="My order",
)
assert result.get("order_description", "Not found") == "My order"
def test_create_payment_with_error(now_pay: NOWPayments) -> None:
"""
Create payment test with error
"""
with pytest.raises(
HTTPError,
match="Error 500: This currency is currently unavailable. Try it in 2 hours",
):
now_pay.create_payment(
price_amount=100, price_currency="usd", pay_currency="cup"
)
def test_payment_status(now_pay: NOWPayments) -> None:
"""Create payment, then check status is waiting"""
payment_id = now_pay.create_payment(
price_amount=100, price_currency="usd", pay_currency="btc"
)["payment_id"]
payment = now_pay.payment_status(int(payment_id))
assert payment["payment_status"] == "waiting", "payment_status"
assert payment["price_amount"] == 100, "price_amount"
def test_min_amount(now_pay: NOWPayments) -> None:
"""Tests if the minimum amount endpoint works"""
response = now_pay.min_amount("btc", "eth")
assert response["min_amount"] <= 0.1