-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
160 lines (131 loc) · 8.61 KB
/
test.ts
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
import { Flair } from "./bindings/Flair";
import { GetFlairsJson } from "./bindings/GetFlairsJson";
import { AddFlairJson } from "./bindings/AddFlairJson";
import { DeleteFlairJson } from "./bindings/DeleteFlairJson";
import { GetUserFlairJson } from "./bindings/GetUserFlairJson";
import { AddUserFlairJson } from "./bindings/AddUserFlairJson";
import { DeleteUserFlairJson } from "./bindings/DeleteUserFlairJson";
const PORT = 6969;
let success = 0;
let failure = 0;
let tot = 0;
(async () => {
const community_actor_id = 'https://localhost/c/play';
const user_actor_id_1 = 'https://localhost/u/Nerd02'; //This user is a mod
const user_actor_id_2 = 'https://localhost/u/Coda'; //This user isn't a mod
const user_actor_id_federated = 'https://lemmy.basedcount.com/u/Nerd02'; //This user isn't a mod
const local_instance = 'example.com'; //The instance running on this server
const federated_instance = 'lemmy.basedcount.com'; //The instance running on some other server
const jwt1 = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6ImxvY2FsaG9zdCIsImlhdCI6MTY5NDk2NjE5OH0.ttmvkJSBnLI84ZUTusYKJCyRiU6iDXCQx2f45n2HmOE'; //JWT of user 1
const jwt2 = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQsImlzcyI6ImxvY2FsaG9zdCIsImlhdCI6MTY5NDg4Mzc4NH0.armD8BmUPDd6Xw18c9mCAQXJxcPIdpTdR6qfT6sZjN0'; //JWT of user 2
// @ts-ignore
const jwt_federated = process.env.FEDERATED_JWT; //JWT of federated user - WARNING: this actually controls someone's account
console.log('Welcome to the "flair" testing script. The script assumes the database to be empty before execution.\nIf the first test fails you might have to start the dev server with "cargo run -- serve".\nIf the 2nd or 3rd tests fail you might have to delete your "flairs.db" file.\n');
console.log('\n===BASE===');
test('server is online', await isServerOnline());
test('no saved flairs on startup', (await listCommunitiesWithFlairs()).length === 0);
test('user doesn\'t have a flair on startup', await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_1 }) === null);
console.log('\n===COMMUNITY===');
test('add user flair', await addFlair({ community_actor_id, display_name: 'TEMP', mod_only: false, name: 'auth', path: '', instance_domain: local_instance }, jwt1));
test('flair got added', (await getFlairs({ community_actor_id, mod_only: false })).length === 1);
test('update existing user flair', await addFlair({ community_actor_id, display_name: 'AuthCenter', mod_only: false, name: 'auth', path: '', instance_domain: local_instance }, jwt1));
test('existing flair got updated', (await getFlairs({ community_actor_id, mod_only: false }))[0].display_name === 'AuthCenter');
test('add mod only user flair', await addFlair({ community_actor_id, display_name: 'Based', mod_only: true, name: 'based', path: '', instance_domain: local_instance }, jwt1));
test('mod flair got added', (await getFlairs({ community_actor_id, mod_only: true })).length === 2);
test('community has flairs enabled', (await listCommunitiesWithFlairs()).length > 0);
console.log('\n===USER===');
test('assign flair to user', await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, flair_name: 'auth', instance_domain: local_instance }, jwt1));
test('flair got assigned', (await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_1 }))?.name === 'auth' ?? false);
test('remove flair from user', await deleteUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, instance_domain: local_instance }, jwt1));
test('user is now unflaired', await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_1 }) === null);
test('reassign flair to user', await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, flair_name: 'auth', instance_domain: local_instance }, jwt1));
test('change flair', await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, flair_name: 'based', instance_domain: local_instance }, jwt1));
test('flair got changed', (await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_1 }))?.name === 'based' ?? false);
test('delete flair while it\'s assigned to user', await deleteFlair({ community_actor_id, name: 'based', instance_domain: local_instance }, jwt1));
test('flair got removed', (await getFlairs({ community_actor_id, mod_only: true })).length === 1);
test('user is now unflaired', await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_1 }) === null);
test('mod can change other people\'s flairs', await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_2, flair_name: 'auth', instance_domain: local_instance }, jwt1));
test('mod can remove other people\'s flairs', await deleteUserFlair({ community_actor_id, user_actor_id: user_actor_id_2, instance_domain: local_instance }, jwt1));
console.log('\n===SECURITY===');
test('non mod can\'t add new flairs', !await addFlair({ community_actor_id, display_name: 'TEMP', mod_only: false, name: 'temp', path: '', instance_domain: local_instance }, jwt2));
test('non mod can\'t delete existing flairs', !await deleteFlair({ community_actor_id, name: 'auth', instance_domain: local_instance }, jwt2));
test('non mod can\'t change other people\'s flairs', !await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, flair_name: 'auth', instance_domain: local_instance }, jwt2));
test('non mod can\'t remove other people\'s flairs', !await deleteUserFlair({ community_actor_id, user_actor_id: user_actor_id_1, instance_domain: local_instance }, jwt2));
console.log('\n===FEDERATION===');
test('assign flair to user from a federated instance', await assignUserFlair({ community_actor_id, user_actor_id: user_actor_id_federated, flair_name: 'auth', instance_domain: federated_instance }, jwt_federated));
test('flair got assigned', (await getUserFlair({ community_actor_id, user_actor_id: user_actor_id_federated }))?.name === 'auth' ?? false);
await deleteFlair({ community_actor_id, name: 'auth', instance_domain: local_instance }, jwt1); //Cleanup
console.log(`\nTests over:\n\t✅ - Passed ${success}/${tot} \n\t❌ - Failed ${failure}/${tot}`);
})();
function test(prompt: string, ok: boolean) {
console.log(`Test: ${prompt}`);
tot++;
if (ok) {
console.log('┕━━━ ✅ - Passed');
success++;
} else {
console.log('┕━━━ ❌ - Failed')
failure++;
}
}
/* API FUNCTIONS */
async function isServerOnline() {
try {
const res = await fetch(`http://localhost:${PORT}`, { method: 'HEAD' });
return res.ok;
} catch (e) {
return false;
}
}
async function getFlairs(params: GetFlairsJson,) {
const res = await GET('/v1/community', params);
return await res.json() as Flair[];
}
async function addFlair(params: AddFlairJson, jwt: string) {
const res = await PUT('/v1/community', params, jwt);
return res.ok;
}
async function deleteFlair(params: DeleteFlairJson, jwt: string) {
const res = await DELETE('/v1/community', params, jwt);
return res.ok;
}
async function getUserFlair(params: GetUserFlairJson) {
const res = await GET('/v1/user', params);
return await res.json() as Flair | null;
}
async function assignUserFlair(params: AddUserFlairJson, jwt: string) {
const res = await PUT('/v1/user', params, jwt);
return res.ok;
}
async function deleteUserFlair(params: DeleteUserFlairJson, jwt: string) {
const res = await DELETE('/v1/user', params, jwt);
return res.ok;
}
async function listCommunitiesWithFlairs() {
const res = await GET('/v1/setup', {});
return await res.json() as Array<String>;
}
/* HTTP METHOD WRAPPERS */
async function GET(endpoint: string, params: object) {
const query = Object.entries(params).map(o => `${o[0]}=${o[1]}`);
const url = `http://localhost:${PORT}/api${endpoint}?${query.join('&')}`;
return fetch(url, {
headers: { "Content-Type": "application/json" }
});
}
async function PUT(endpoint: string, params: object, jwt: string) {
const url = `http://localhost:${PORT}/api${endpoint}`;
return fetch(url, {
headers: { "Content-Type": "application/json", "authorization": `Bearer ${jwt}` },
method: 'PUT',
body: JSON.stringify(params)
});
}
async function DELETE(endpoint: string, params: object, jwt: string) {
const url = `http://localhost:${PORT}/api${endpoint}`;
return fetch(url, {
headers: { "Content-Type": "application/json", "authorization": `Bearer ${jwt}` },
method: 'DELETE',
body: JSON.stringify(params)
});
}