ilp-node 0.6.0

Interledger node (sender, connector, receiver bundle)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
mod redis_helpers;
mod test_helpers;

use approx::relative_eq;
use futures::Future;
use ilp_node::InterledgerNode;
use redis_helpers::*;
use reqwest::{self, r#async::Body};
use serde_json::{json, Number, Value};
use test_helpers::*;
use tokio::runtime::Builder as RuntimeBuilder;
use warp::http;

// Integration tests of node settings APIs

const NODE_ILP_ADDRESS: &str = "example.node";
const USERNAME: &str = "test_account";
const ASSET_CODE: &str = "XRP";
const ASSET_SCALE: usize = 9;
const XRP_RATE: f64 = 1.0;
const ETH_RATE: f64 = 1.2;
const SETTLEMENT_ENGINE_ASSET_CODE: &str = "XRP";
const SETTLEMENT_ENGINE_URL: &str = "http://localhost:3000/";

#[test]
fn node_settings_test() {
    install_tracing_subscriber();

    let mut runtime = RuntimeBuilder::new().build().unwrap();

    let redis_test_context = TestContext::new();
    let node_http_port = get_open_port(Some(7770));
    let node_settlement_port = get_open_port(Some(3000));
    let mut connection_info = redis_test_context.get_client_connection_info();
    connection_info.db = 1;

    let node: InterledgerNode = serde_json::from_value(json!({
        "ilp_address": NODE_ILP_ADDRESS,
        "admin_auth_token": "admin",
        "redis_connection": connection_info_to_string(connection_info),
        "http_bind_address": format!("127.0.0.1:{}", node_http_port),
        "settlement_api_bind_address": format!("127.0.0.1:{}", node_settlement_port),
        "secret_seed": random_secret(),
        "route_broadcast_interval": 200,
        "exchange_rate_poll_interval": 60000,
        "exchange_rate_poll_failure_tolerance": 5,
    }))
    .unwrap();
    let node_to_serve = node.clone();
    let node_context = move |_| Ok(node);

    let get_root = move |node: InterledgerNode| {
        // GET / (stat)
        let client = reqwest::r#async::Client::new();
        client
            .get(&format!(
                "http://localhost:{}/",
                node.http_bind_address.port()
            ))
            .send()
            .map_err(|err| panic!(err))
            .and_then(|mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(account) = json {
                    assert_eq!(
                        account.get("status").expect("status was expected"),
                        &Value::String("Ready".to_owned())
                    );
                    assert_eq!(
                        account
                            .get("ilp_address")
                            .expect("ilp_address was expected"),
                        &Value::String(
                            node.ilp_address
                                .clone()
                                .expect("ILP address was expected")
                                .to_string()
                        )
                    );
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok(node)
            })
    };

    let put_rates = move |node: InterledgerNode| {
        // PUT /rates
        let client = reqwest::r#async::Client::new();
        client
            .put(&format!(
                "http://localhost:{}/rates",
                node.http_bind_address.port()
            ))
            .header(
                "Authorization",
                &format!("Bearer {}", node.admin_auth_token),
            )
            .json(&json!({
                "XRP": XRP_RATE,
                "ETH": ETH_RATE,
            }))
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(rates) = json {
                    if let Value::Number(num) = rates.get("XRP").expect("XRP was expected") {
                        assert!(relative_eq!(
                            XRP_RATE,
                            num.as_f64().expect("rate is expected to be f64"),
                            epsilon = std::f64::EPSILON
                        ));
                    } else {
                        panic!("Invalid response JSON! {}", &content);
                    }
                    if let Value::Number(num) = rates.get("ETH").expect("ETH was expected") {
                        assert!(relative_eq!(
                            ETH_RATE,
                            num.as_f64().expect("rate is expected to be f64"),
                            epsilon = std::f64::EPSILON
                        ));
                    } else {
                        panic!("Invalid response JSON! {}", &content);
                    }
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok(node)
            })
    };

    let get_rates = move |node: InterledgerNode| {
        // GET /rates
        let client = reqwest::r#async::Client::new();
        client
            .get(&format!(
                "http://localhost:{}/rates",
                node.http_bind_address.port()
            ))
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(rates) = json {
                    if let Value::Number(num) = rates.get("XRP").expect("XRP was expected") {
                        assert!(relative_eq!(
                            XRP_RATE,
                            num.as_f64().expect("rate is expected to be f64"),
                            epsilon = std::f64::EPSILON
                        ));
                    } else {
                        panic!("Invalid response JSON! {}", &content);
                    }
                    if let Value::Number(num) = rates.get("ETH").expect("ETH was expected") {
                        assert!(relative_eq!(
                            ETH_RATE,
                            num.as_f64().expect("rate is expected to be f64"),
                            epsilon = std::f64::EPSILON
                        ));
                    } else {
                        panic!("Invalid response JSON! {}", &content);
                    }
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok(node)
            })
    };

    let get_routes = move |node: InterledgerNode| {
        // GET /routes
        let client = reqwest::r#async::Client::new();
        client
            .get(&format!(
                "http://localhost:{}/routes",
                node.http_bind_address.port()
            ))
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(_rates) = json {
                    // nothing so far
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok(node)
            })
    };

    // This is not a part of the tests. This is tested in `accounts.rs`.
    let post_accounts = move |node: InterledgerNode| {
        // POST /accounts
        let client = reqwest::r#async::Client::new();
        client
            .post(&format!(
                "http://localhost:{}/accounts",
                node.http_bind_address.port()
            ))
            .header(
                "Authorization",
                &format!("Bearer {}", node.admin_auth_token),
            )
            .json(&json!({
                "username": USERNAME,
                "asset_code": ASSET_CODE,
                "asset_scale": ASSET_SCALE
            }))
            .send()
            .map_err(|err| panic!(err))
            .and_then(|mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(account) = json {
                    account
                        .get("id")
                        .map(|value| match value {
                            Value::String(string) => string,
                            _ => panic!("Invalid response JSON! {}", &content),
                        })
                        .expect("id was expected");
                    let ilp_address = account
                        .get("ilp_address")
                        .map(|value| match value {
                            Value::String(string) => string,
                            _ => panic!("Invalid response JSON! {}", &content),
                        })
                        .expect("ilp_address was expected");
                    Ok((node, ilp_address.to_owned()))
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
            })
    };

    let put_routes_static = move |(node, ilp_address): (InterledgerNode, String)| {
        // PUT /routes/static
        let client = reqwest::r#async::Client::new();
        client
            .put(&format!(
                "http://localhost:{}/routes/static",
                node.http_bind_address.port()
            ))
            .header(
                "Authorization",
                &format!("Bearer {}", node.admin_auth_token),
            )
            .json(&json!({
                "example.a": USERNAME,
                "example.b": USERNAME,
                "example.c": USERNAME,
            }))
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(account) = json {
                    assert_eq!(
                        account.get("example.a").expect("example.a was expected"),
                        USERNAME,
                    );
                    assert_eq!(
                        account.get("example.b").expect("example.b was expected"),
                        USERNAME,
                    );
                    assert_eq!(
                        account.get("example.c").expect("example.c was expected"),
                        USERNAME,
                    );
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok((node, ilp_address))
            })
    };

    let put_routes_static_prefix = move |(node, ilp_address): (InterledgerNode, String)| {
        // PUT /routes/static/:prefix
        let client = reqwest::r#async::Client::new();
        client
            .put(&format!(
                "http://localhost:{}/routes/static/{}",
                node.http_bind_address.port(),
                ilp_address
            ))
            .header(
                "Authorization",
                &format!("Bearer {}", node.admin_auth_token),
            )
            .header("Content-Type", "application/json")
            .body(USERNAME)
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                assert_eq!(content, USERNAME);
                Ok((node, ilp_address))
            })
    };

    // Should cause Unauthorized
    let put_routes_static_prefix_unauthorized =
        move |(node, ilp_address): (InterledgerNode, String)| {
            // PUT /routes/static/:prefix
            let client = reqwest::r#async::Client::new();
            client
                .put(&format!(
                    "http://localhost:{}/routes/static/{}",
                    node.http_bind_address.port(),
                    ilp_address
                ))
                .header("Authorization", &format!("Bearer {}", "wrong_token"))
                .body(Body::from(USERNAME))
                .send()
                .map_err(|err| panic!(err))
                .and_then(move |mut res| {
                    let content = res.text().wait().expect("Error getting response!");
                    let json: Value = serde_json::from_str(&content)
                        .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                    if let Value::Object(account) = json {
                        assert_eq!(
                            account.get("status").expect("status was expected"),
                            &Value::Number(Number::from(http::StatusCode::UNAUTHORIZED.as_u16()))
                        );
                    } else {
                        panic!("Invalid response JSON! {}", &content);
                    }
                    Ok(node)
                })
        };

    // The API can't find the settlement engine actually but it is OK because
    // this is just testing if the API correctly accepts the requests or not.
    let put_settlement_engines = move |node: InterledgerNode| {
        // PUT /settlement/engines
        let client = reqwest::r#async::Client::new();
        client
            .put(&format!(
                "http://localhost:{}/settlement/engines",
                node.http_bind_address.port()
            ))
            .header(
                "Authorization",
                &format!("Bearer {}", node.admin_auth_token),
            )
            .json(&json!({
                SETTLEMENT_ENGINE_ASSET_CODE: SETTLEMENT_ENGINE_URL,
            }))
            .send()
            .map_err(|err| panic!(err))
            .and_then(move |mut res| {
                let content = res.text().wait().expect("Error getting response!");
                assert!(res.error_for_status_ref().is_ok(), "{}", &content);
                let json: Value = serde_json::from_str(&content)
                    .unwrap_or_else(|_| panic!("Could not parse JSON! JSON: {}", &content));
                if let Value::Object(account) = json {
                    assert_eq!(
                        account
                            .get(SETTLEMENT_ENGINE_ASSET_CODE)
                            .unwrap_or_else(|| panic!(
                                "{} was expected",
                                SETTLEMENT_ENGINE_ASSET_CODE
                            )),
                        &Value::String(SETTLEMENT_ENGINE_URL.to_owned())
                    );
                } else {
                    panic!("Invalid response JSON! {}", &content);
                }
                Ok(node)
            })
    };

    runtime
        .block_on(
            node_to_serve
                .serve()
                .and_then(node_context)
                .and_then(get_root)
                .and_then(put_rates)
                .and_then(get_rates)
                .and_then(get_routes)
                .and_then(post_accounts)
                .and_then(put_routes_static)
                .and_then(put_routes_static_prefix)
                .and_then(put_routes_static_prefix_unauthorized)
                .and_then(put_settlement_engines),
        )
        .expect("Could not spin up node and tests.");
}