-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstellarAuth.test.js
More file actions
226 lines (186 loc) · 6.88 KB
/
stellarAuth.test.js
File metadata and controls
226 lines (186 loc) · 6.88 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
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
const request = require("supertest");
const app = require("./index");
const StellarSdk = require("@stellar/stellar-sdk");
describe("Stellar Authentication (SIWS)", () => {
let testKeypair;
let testPublicKey;
let authToken;
let challengeXDR;
beforeAll(() => {
// Generate test keypair for testing
testKeypair = StellarSdk.Keypair.random();
testPublicKey = testKeypair.publicKey();
});
describe("POST /auth/stellar/challenge", () => {
it("should generate a challenge for valid public key", async () => {
const response = await request(app)
.get("/auth/stellar/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
expect(response.body.success).toBe(true);
expect(response.body.challenge).toBeDefined();
expect(response.body.nonce).toBeDefined();
expect(response.body.expiresAt).toBeDefined();
challengeXDR = response.body.challenge;
});
it("should reject request without public key", async () => {
const response = await request(app)
.get("/auth/stellar/challenge")
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Stellar public key required");
});
it("should reject invalid public key format", async () => {
const response = await request(app)
.get("/auth/stellar/challenge")
.query({ publicKey: "invalid-key" })
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Invalid Stellar public key format");
});
});
describe("POST /auth/stellar/login", () => {
it("should reject login without required fields", async () => {
const response = await request(app)
.post("/auth/stellar/login")
.send({})
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe(
"Missing required fields: publicKey, challengeXDR",
);
});
it("should reject invalid challenge XDR", async () => {
const response = await request(app)
.post("/auth/stellar/login")
.send({
publicKey: testPublicKey,
challengeXDR: "invalid-xdr",
})
.expect(400);
expect(response.body.success).toBe(false);
});
// Note: Full integration test with signed challenge would require
// testnet account setup and funding, which is complex for unit tests
// In production, you'd set up funded test accounts for integration testing
});
describe("GET /auth/stellar/session", () => {
it("should reject request without authentication", async () => {
const response = await request(app)
.get("/auth/stellar/session")
.expect(401);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Access token required");
});
});
describe("POST /auth/stellar/logout", () => {
it("should reject logout without authentication", async () => {
const response = await request(app)
.post("/auth/stellar/logout")
.expect(401);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Access token required");
});
});
describe("GET /auth/stellar/challenge-status", () => {
it("should return challenge status for valid public key", async () => {
// First generate a challenge
await request(app)
.get("/auth/stellar/challenge")
.query({ publicKey: testPublicKey });
// Then check status
const response = await request(app)
.get("/auth/stellar/challenge-status")
.query({ publicKey: testPublicKey })
.expect(200);
expect(response.body.success).toBe(true);
expect(response.body.status).toBeDefined();
expect(response.body.status.exists).toBe(true);
});
it("should reject request without public key", async () => {
const response = await request(app)
.get("/auth/stellar/challenge-status")
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Public key required");
});
});
describe("POST /auth/stellar/validate-sessions", () => {
it("should validate all active sessions", async () => {
const response = await request(app)
.post("/auth/stellar/validate-sessions")
.expect(200);
expect(response.body.success).toBe(true);
expect(response.body.invalidatedSessions).toBeDefined();
expect(Array.isArray(response.body.invalidatedSessions)).toBe(true);
});
});
});
// Integration tests (require testnet setup)
describe("Stellar Authentication Integration Tests", () => {
let fundedKeypair;
let fundedPublicKey;
beforeAll(async () => {
// These tests require a funded testnet account
// Skip if test credentials are not available
if (
!process.env.STELLAR_TEST_PUBLIC_KEY ||
!process.env.STELLAR_TEST_SECRET
) {
console.log("Skipping integration tests - no test credentials provided");
return;
}
fundedPublicKey = process.env.STELLAR_TEST_PUBLIC_KEY;
fundedKeypair = StellarSdk.Keypair.fromSecret(
process.env.STELLAR_TEST_SECRET,
);
});
it("should complete full authentication flow", async () => {
if (!fundedKeypair) {
console.log("Skipping integration test");
return;
}
// 1. Generate challenge
const challengeResponse = await request(app)
.get("/auth/stellar/challenge")
.query({ publicKey: fundedPublicKey })
.expect(200);
expect(challengeResponse.body.success).toBe(true);
// 2. Parse and sign the challenge
const transaction = StellarSdk.TransactionBuilder.fromXDR(
challengeResponse.body.challenge,
"Test SDF Network ; September 2015",
);
transaction.sign(fundedKeypair);
const signedChallengeXDR = transaction.toXDR();
// 3. Verify and login
const loginResponse = await request(app)
.post("/auth/stellar/login")
.send({
publicKey: fundedPublicKey,
challengeXDR: signedChallengeXDR,
})
.expect(200);
expect(loginResponse.body.success).toBe(true);
expect(loginResponse.body.token).toBeDefined();
expect(loginResponse.body.user.publicKey).toBe(
fundedPublicKey.toLowerCase(),
);
const token = loginResponse.body.token;
// 4. Test authenticated endpoint
const sessionResponse = await request(app)
.get("/auth/stellar/session")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(sessionResponse.body.success).toBe(true);
expect(sessionResponse.body.session.publicKey).toBe(
fundedPublicKey.toLowerCase(),
);
// 5. Test logout
const logoutResponse = await request(app)
.post("/auth/stellar/logout")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(logoutResponse.body.success).toBe(true);
}, 30000); // Longer timeout for network operations
});