forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stellar.py
More file actions
177 lines (149 loc) · 5.28 KB
/
Copy pathtest_stellar.py
File metadata and controls
177 lines (149 loc) · 5.28 KB
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
import json
import pytest
import echomirror
from conftest import route
@pytest.mark.asyncio
async def test_get_balance(mock_server, base_url):
# get_balance queries Horizon directly (no EchoMirror API round-trip), so
# we point horizon_url at the mock server and return a Horizon-shaped
# /accounts/{id} response.
route(
mock_server,
"GET",
"/accounts/GABC123",
200,
{
"balances": [
{"balance": "125.5000000", "asset_type": "native"},
{
"balance": "42.0000000",
"asset_type": "credit_alphanum4",
"asset_code": "ECHO",
"asset_issuer": "GISSUER",
},
]
},
)
client = echomirror.EchoMirrorClient(
"test-key",
network=echomirror.StellarNetwork.Testnet,
horizon_url=base_url,
)
stellar = echomirror.StellarClient(client)
balance = await stellar.get_balance("GABC123")
assert balance.xlm == "125.5000000"
assert balance.echo == "42.0000000"
assert balance.public_key == "GABC123"
@pytest.mark.asyncio
async def test_get_balance_account_not_found(mock_server, base_url):
route(mock_server, "GET", "/accounts/GMISSING", 404, {"message": "not found"})
client = echomirror.EchoMirrorClient("test-key", horizon_url=base_url)
stellar = echomirror.StellarClient(client)
with pytest.raises(echomirror.NotFoundError):
await stellar.get_balance("GMISSING")
@pytest.mark.asyncio
async def test_build_transfer_and_submit(mock_server, base_url):
route(
mock_server,
"POST",
"/stellar/build-transfer",
200,
{"xdr": "AAAAAgAAAA...", "fee": 100, "sequence": "123456789"},
)
route(
mock_server,
"POST",
"/stellar/submit",
200,
{
"id": "tx1",
"type": "send",
"asset": "ECHO",
"amount": "10.0000000",
"from": "GFROM",
"to": "GTO",
"memo": "gift",
"created_at": "2026-07-20T10:00:00Z",
"stellar_tx_hash": "hash123",
"ledger_sequence": 42,
},
)
client = echomirror.EchoMirrorClient("test-key", base_url=base_url)
stellar = echomirror.StellarClient(client)
unsigned = await stellar.build_transfer("GFROM", "GTO", 10.0, memo="gift")
assert unsigned.xdr == "AAAAAgAAAA..."
assert unsigned.fee == 100
build_req = mock_server.received[-1]
assert json.loads(build_req["body"]) == {
"from": "GFROM",
"to": "GTO",
"amount": 10.0,
"memo": "gift",
}
tx = await stellar.submit_transaction("signed-xdr-here")
assert tx.id == "tx1"
assert tx.tx_type == "send"
assert tx.from_address == "GFROM"
assert tx.to_address == "GTO"
assert tx.ledger_sequence == 42
@pytest.mark.asyncio
async def test_get_transaction_history(mock_server, base_url):
route(
mock_server,
"GET",
"/stellar/transactions",
200,
{
"transactions": [
{
"id": "tx1",
"type": "receive",
"asset": "XLM",
"amount": "5.0000000",
"from": "GA",
"to": "GB",
"memo": None,
"created_at": "2026-07-20T10:00:00Z",
"stellar_tx_hash": "hash1",
"ledger_sequence": None,
}
],
"cursor": "next-page-token",
},
)
client = echomirror.EchoMirrorClient("test-key", base_url=base_url)
stellar = echomirror.StellarClient(client)
page = await stellar.get_transaction_history("GB", limit=10)
assert len(page.transactions) == 1
assert page.transactions[0].tx_type == "receive"
assert page.cursor == "next-page-token"
@pytest.mark.asyncio
async def test_fund_testnet_account(mock_server, base_url):
# Friendbot is a plain `GET {friendbot_url}?addr=...` call, no EchoMirror
# API round-trip, so we point friendbot_url at the mock server directly.
route(mock_server, "GET", "/", 200, {"ok": True})
client = echomirror.EchoMirrorClient(
"test-key",
network=echomirror.StellarNetwork.Testnet,
friendbot_url=base_url,
)
stellar = echomirror.StellarClient(client)
await stellar.fund_testnet_account("GABC123")
req = mock_server.received[-1]
assert req["method"] == "GET"
assert "addr=GABC123" in req["path"]
@pytest.mark.asyncio
async def test_fund_testnet_account_rejects_mainnet(mock_server, base_url):
client = echomirror.EchoMirrorClient(
"test-key", network=echomirror.StellarNetwork.Mainnet
)
stellar = echomirror.StellarClient(client)
with pytest.raises(echomirror.ConfigError):
await stellar.fund_testnet_account("GABC123")
@pytest.mark.asyncio
async def test_get_balance_network_error(mock_server, base_url):
route(mock_server, "GET", "/accounts/GABC123", 500, {"message": "horizon unavailable"})
client = echomirror.EchoMirrorClient("test-key", horizon_url=base_url)
stellar = echomirror.StellarClient(client)
with pytest.raises(echomirror.EchoMirrorException):
await stellar.get_balance("GABC123")