-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api-client.js
More file actions
executable file
Β·158 lines (136 loc) Β· 5.12 KB
/
test-api-client.js
File metadata and controls
executable file
Β·158 lines (136 loc) Β· 5.12 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
#!/usr/bin/env node
/**
* Simple API test client for M1 - Auth + Couples
* This demonstrates the complete authentication and couple management flow
*/
const API_BASE = 'http://localhost:3001';
async function makeRequest(endpoint, options = {}) {
const url = `${API_BASE}${endpoint}`;
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers,
},
...options,
});
const data = response.ok ? await response.json() : await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${data}`);
}
return data;
}
async function testM1API() {
console.log('π§ͺ Testing M1 API - Auth + Couples Flow\n');
try {
// Step 1: Request verification code for Alice
console.log('1οΈβ£ Requesting verification code for Alice...');
await makeRequest('/auth/request-code', {
method: 'POST',
body: JSON.stringify({ email: 'alice@example.com' }),
});
console.log('β
Verification code requested (check server logs for the code)');
// Step 2: Verify code and get token for Alice
console.log('\n2οΈβ£ Verifying code for Alice...');
const aliceAuth = await makeRequest('/auth/verify-code', {
method: 'POST',
body: JSON.stringify({
email: 'alice@example.com',
code: '123456' // Use the code from server logs
}),
});
console.log('β
Alice authenticated, token received');
// Step 3: Create couple as Alice
console.log('\n3οΈβ£ Creating couple as Alice...');
const couple = await makeRequest('/couples', {
method: 'POST',
headers: {
'Authorization': `Bearer ${aliceAuth.accessToken}`,
},
});
console.log('β
Couple created:', couple.coupleId);
// Step 4: Get Alice's user info
console.log('\n4οΈβ£ Getting Alice\'s user info...');
const aliceInfo = await makeRequest('/auth/me', {
headers: {
'Authorization': `Bearer ${aliceAuth.accessToken}`,
},
});
console.log('β
Alice info:', aliceInfo);
// Step 5: Create invite as Alice
console.log('\n5οΈβ£ Creating invite as Alice...');
const invite = await makeRequest('/invites', {
method: 'POST',
headers: {
'Authorization': `Bearer ${aliceAuth.accessToken}`,
},
});
console.log('β
Invite created:', invite.code, invite.link);
// Step 6: Get invite info
console.log('\n6οΈβ£ Getting invite info...');
const inviteInfo = await makeRequest(`/invites/${invite.code}`);
console.log('β
Invite info:', inviteInfo);
// Step 7: Request verification code for Bob
console.log('\n7οΈβ£ Requesting verification code for Bob...');
await makeRequest('/auth/request-code', {
method: 'POST',
body: JSON.stringify({ email: 'bob@example.com' }),
});
console.log('β
Verification code requested for Bob');
// Step 8: Verify code and get token for Bob
console.log('\n8οΈβ£ Verifying code for Bob...');
const bobAuth = await makeRequest('/auth/verify-code', {
method: 'POST',
body: JSON.stringify({
email: 'bob@example.com',
code: '123456' // Use the code from server logs
}),
});
console.log('β
Bob authenticated, token received');
// Step 9: Bob accepts the invite
console.log('\n9οΈβ£ Bob accepting the invite...');
await makeRequest(`/invites/${invite.code}/accept`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${bobAuth.accessToken}`,
},
});
console.log('β
Bob accepted the invite');
// Step 10: Get Bob's updated info
console.log('\nπ Getting Bob\'s updated info...');
const bobInfo = await makeRequest('/auth/me', {
headers: {
'Authorization': `Bearer ${bobAuth.accessToken}`,
},
});
console.log('β
Bob info:', bobInfo);
// Step 11: Get couple info as Alice
console.log('\n1οΈβ£1οΈβ£ Getting couple info as Alice...');
const coupleInfo = await makeRequest('/couples/me', {
headers: {
'Authorization': `Bearer ${aliceAuth.accessToken}`,
},
});
console.log('β
Couple info:', coupleInfo);
console.log('\nπ Complete M1 API flow tested successfully!');
console.log('\nπ Flow Summary:');
console.log(' β
Email-based authentication with verification codes');
console.log(' β
User creation and JWT token generation');
console.log(' β
Couple creation and management');
console.log(' β
Invite system with codes and links');
console.log(' β
Invite acceptance and couple joining');
console.log(' β
Encrypted display names and secure data handling');
} catch (error) {
console.error('β API test failed:', error.message);
console.log('\nπ‘ Make sure the API server is running:');
console.log(' cd services/api && npm run dev');
process.exit(1);
}
}
// Check if fetch is available (Node.js 18+)
if (typeof fetch === 'undefined') {
console.error('β This script requires Node.js 18+ with fetch support');
console.log(' Or install node-fetch: npm install node-fetch');
process.exit(1);
}
// Run the test
testM1API();