Repository navigation
Expand file tree
/
Copy pathfederation.test.js
More file actions
136 lines (108 loc) · 4.32 KB
/
Copy pathfederation.test.js
File metadata and controls
136 lines (108 loc) · 4.32 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
/**
* __tests__/federation.test.js
* Integration tests for federation endpoints per SEP-0002.
*/
"use strict";
const request = require("supertest");
const app = require("../src/server");
const usernameService = require("../src/services/usernameService");
describe("Federation API", () => {
const testUsername = "testuser";
const testPublicKey = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
beforeAll(async () => {
// Register a test user
try {
usernameService.registerUsername(testUsername, testPublicKey);
} catch (err) {
// User might already exist
}
});
afterAll(async () => {
// Clean up
try {
usernameService.removeUsername(testUsername);
} catch (err) {
// User might not exist
}
});
describe("GET /.well-known/stellar.toml", () => {
it("should return valid TOML with FEDERATION_SERVER", async () => {
const response = await request(app)
.get("/.well-known/stellar.toml")
.expect(200)
.expect("Content-Type", /application\/toml/);
expect(response.text).toContain("FEDERATION_SERVER=");
expect(response.text).toContain("/federation");
expect(response.text).not.toContain("[FEDERATION_SERVER]");
});
});
describe("GET /federation", () => {
describe("type=name", () => {
it("should resolve a local stellar address", async () => {
const stellarAddress = `${testUsername}*stellarmicropay.io`;
const response = await request(app)
.get("/federation")
.query({ q: stellarAddress, type: "name" })
.expect(200);
expect(response.body).toHaveProperty("stellar_address", stellarAddress);
expect(response.body).toHaveProperty("account_id", testPublicKey);
});
it("should resolve configured legacy local domains", async () => {
const stellarAddress = `${testUsername}*stellarmicropay.com`;
const response = await request(app)
.get("/federation")
.query({ q: stellarAddress, type: "name" })
.expect(200);
expect(response.body).toHaveProperty("stellar_address", stellarAddress);
expect(response.body).toHaveProperty("account_id", testPublicKey);
});
it("should return 404 for non-existent username", async () => {
const stellarAddress = "nonexistent*stellarmicropay.io";
const response = await request(app)
.get("/federation")
.query({ q: stellarAddress, type: "name" })
.expect(404);
expect(response.body).toHaveProperty("error", "Username not found");
});
it("should return 400 for invalid stellar address format", async () => {
const invalidAddress = "invalidaddress";
const response = await request(app)
.get("/federation")
.query({ q: invalidAddress, type: "name" })
.expect(400);
expect(response.body).toHaveProperty("error");
});
});
describe("type=id", () => {
it("should resolve a local account ID", async () => {
const response = await request(app)
.get("/federation")
.query({ q: testPublicKey, type: "id" })
.expect(200);
expect(response.body).toHaveProperty("stellar_address", `${testUsername}*stellarmicropay.io`);
expect(response.body).toHaveProperty("account_id", testPublicKey);
});
it("should return 404 for non-existent account ID", async () => {
const unknownPublicKey = "GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
const response = await request(app)
.get("/federation")
.query({ q: unknownPublicKey, type: "id" })
.expect(404);
expect(response.body).toHaveProperty("error", "Account ID not found");
});
});
it("should return 400 for missing parameters", async () => {
const response = await request(app)
.get("/federation")
.expect(400);
expect(response.body).toHaveProperty("error", "Missing required parameters: q and type");
});
it("should return 400 for invalid type", async () => {
const response = await request(app)
.get("/federation")
.query({ q: "test", type: "invalid" })
.expect(400);
expect(response.body).toHaveProperty("error", "Invalid type parameter. Must be 'name' or 'id'");
});
});
});