-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-cleanup.js
150 lines (126 loc) · 3.67 KB
/
test-cleanup.js
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
require("dotenv/config");
const { StreamClient } = require("./dist/index.cjs.js");
const apiKey = process.env.STREAM_API_KEY;
const secret = process.env.STREAM_SECRET;
const createTestClient = () => {
return new StreamClient(apiKey, secret, { timeout: 10000 });
};
const client = createTestClient();
const cleanupBlockLists = async () => {
const blockLists = (await client.listBlockLists()).blocklists;
const customBlockLists = blockLists.filter((b) =>
b.name.startsWith("streamnodetest-F1"),
);
await Promise.all(
customBlockLists.map((b) => client.deleteBlockList({ name: b.name })),
);
};
const cleanupCalls = async () => {
const calls = (await client.video.queryCalls()).calls;
const testCalls = Object.keys(calls).filter((t) =>
t.startsWith("callnodetest"),
);
await Promise.all(
testCalls.map((t) =>
client.video.call(t.call.type, t.call.id).delete({ hard: true }),
),
);
};
const cleanupCallTypes = async () => {
const callTypes = (await client.video.listCallTypes()).call_types;
const customCallTypes = Object.keys(callTypes).filter(
(t) => t.startsWith("streamnodetest") || t.startsWith("calltype"),
);
await Promise.all(
customCallTypes.map((t) => client.video.deleteCallType({ name: t })),
);
};
const cleanupExternalStorage = async () => {
const storage = (await client.listExternalStorage()).external_storages;
const customStorage = Object.keys(storage).filter((k) =>
k.startsWith("streamnodetest"),
);
await Promise.all(
customStorage.map((s) => client.deleteExternalStorage({ name: s })),
);
};
const cleanUpChannelTypes = async () => {
const channelTypes = (await client.chat.listChannelTypes()).channel_types;
const customChannelTypes = Object.keys(channelTypes).filter((type) =>
type.startsWith("streamnodetest"),
);
await Promise.all(
customChannelTypes.map((channelType) =>
client.chat.deleteChannelType({ name: channelType }),
),
);
};
const cleanUpChannels = async () => {
const channels = (
await client.chat.queryChannels({
payload: {
filter_conditions: { name: { $autocomplete: "streamnodetest" } },
},
})
).channels;
await Promise.all(
channels.map((c) =>
client.chat
.channel(c.channel.type, c.channel.id)
.delete({ hardDelete: true }),
),
);
};
const cleanUpCommands = async () => {
const commands = (await client.chat.listCommands()).commands;
const customCommands = commands.filter((c) =>
c.name.startsWith("stream-node-test-command"),
);
await Promise.all(
customCommands.map((c) => client.chat.deleteCommand({ name: c.name })),
);
};
const cleanUpRoles = async () => {
const roles = (await client.listRoles()).roles;
const customRoles = roles.filter((r) => r.name.startsWith("streamnodetest"));
let grants = {};
customRoles.forEach((r) => {
grants[r.name] = [];
});
await client.updateApp({
grants,
});
await Promise.all(
customRoles.map((r) => client.deleteRole({ name: r.name })),
);
};
const cleanUpUsers = async () => {
const users = (
await client.queryUsers({
payload: {
filter_conditions: { name: { $autocomplete: "streamnodetest" } },
},
})
).users;
if (users.length > 0) {
await client.deleteUsers({
user_ids: users.map((u) => u.id),
user: "hard",
});
}
};
const cleanup = async () => {
await cleanupBlockLists();
await cleanupCallTypes();
await cleanUpChannelTypes();
await cleanUpChannels();
await cleanUpCommands();
await cleanUpRoles();
await cleanUpUsers();
await cleanupExternalStorage();
await cleanupCalls();
};
cleanup().then(() => {
console.log("cleanup done");
process.exit();
});