/[sudobot]/branches/6.x/tests/services/PermissionManager.test.ts
ViewVC logotype

Annotation of /branches/6.x/tests/services/PermissionManager.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6748 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import "../setup";
2    
3     import assert from "assert";
4     import {
5     APIGuild,
6     APIRole,
7     Guild,
8     GuildDefaultMessageNotifications,
9     GuildMember,
10     PermissionsBitField,
11     PermissionsString
12     } from "discord.js";
13     import { RawGuildMemberData } from "discord.js/typings/rawDataTypes";
14     import { after, before, beforeEach, describe, it, test } from "node:test";
15     import Client from "../../src/core/Client";
16     import { GUILD_ID, registerFileHandler, unregisterFileHandler } from "../clientsetup";
17     import { randomSnowflake } from "../utils";
18    
19     const client = new Client(
20     {
21     intents: []
22     },
23     {
24     services: ["@services/ConfigManager", "@services/PermissionManager"]
25     }
26     );
27    
28     describe("Permission Manager", () => {
29     let owner_id: string, user_id: string, mod_role: string, admin_role: string;
30     let roles: APIRole[];
31     let guild: Guild;
32     let normalMember: GuildMember, moderatorMember: GuildMember, adminMember: GuildMember, adminOnlyMember: GuildMember;
33    
34     before(() => {
35     registerFileHandler();
36     return client.boot();
37     });
38    
39     after(() => {
40     unregisterFileHandler();
41     });
42    
43     test("Initialization", () => {
44     assert.strictEqual(Object.keys(client.permissionManager.levels).length, 100);
45     assert.deepEqual(client.permissionManager.guildPermissionLevels, {});
46     });
47    
48     beforeEach(() => {
49     owner_id = randomSnowflake();
50     user_id = randomSnowflake();
51     mod_role = randomSnowflake();
52     admin_role = randomSnowflake();
53     roles = (
54     [
55     {
56     id: mod_role,
57     name: "Moderator",
58     permissions: new PermissionsBitField(["BanMembers", "KickMembers"]).bitfield.toString()
59     },
60     {
61     id: admin_role,
62     name: "Admin",
63     permissions: new PermissionsBitField("Administrator").bitfield.toString()
64     },
65     {
66     id: GUILD_ID,
67     name: "@everyone",
68     permissions: new PermissionsBitField("SendMessages").bitfield.toString()
69     }
70     ] as APIRole[]
71     ).map((role, index) => ({
72     ...role,
73     color: 0xf14a69,
74     hoist: false,
75     managed: true,
76     mentionable: true,
77     position: index
78     }));
79    
80     guild = new (Guild as any)(client, {
81     id: GUILD_ID,
82     afk_channel_id: null,
83     application_id: null,
84     default_message_notifications: GuildDefaultMessageNotifications.OnlyMentions,
85     description: null,
86     owner_id,
87     discovery_splash: null,
88     name: "Test Guild",
89     roles,
90     banner: null
91     } as Partial<APIGuild>);
92    
93     normalMember = new (GuildMember as any)(
94     client,
95     {
96     user: {
97     discriminator: "2084",
98     global_name: "ABC",
99     id: user_id,
100     username: "root"
101     },
102     roles: [GUILD_ID],
103     guild_id: GUILD_ID,
104     joined_at: new Date().toISOString()
105     } as RawGuildMemberData,
106     guild
107     );
108    
109     moderatorMember = new (GuildMember as any)(
110     client,
111     {
112     user: {
113     discriminator: "2084",
114     global_name: "ABC",
115     id: user_id,
116     username: "root"
117     },
118     roles: [mod_role, GUILD_ID],
119     guild_id: GUILD_ID,
120     joined_at: new Date().toISOString()
121     } as RawGuildMemberData,
122     guild
123     );
124    
125     adminMember = new (GuildMember as any)(
126     client,
127     {
128     user: {
129     discriminator: "2084",
130     global_name: "ABC",
131     id: user_id,
132     username: "root"
133     },
134     roles: [admin_role, mod_role, GUILD_ID],
135     guild_id: GUILD_ID,
136     joined_at: new Date().toISOString()
137     } as RawGuildMemberData,
138     guild
139     );
140    
141     adminOnlyMember = new (GuildMember as any)(
142     client,
143     {
144     user: {
145     discriminator: "2084",
146     global_name: "ABC",
147     id: user_id,
148     username: "root"
149     },
150     roles: [admin_role, GUILD_ID],
151     guild_id: GUILD_ID,
152     joined_at: new Date().toISOString()
153     } as RawGuildMemberData,
154     guild
155     );
156     });
157    
158     it("Can determine member permissions", () => {
159     const normalResult = [...client.permissionManager.getMemberPermissions(normalMember, true).values()];
160     const moderatorResult = [...client.permissionManager.getMemberPermissions(moderatorMember, true).values()];
161     const adminResult = [...client.permissionManager.getMemberPermissions(adminMember, true).values()];
162     const adminOnlyResult = [...client.permissionManager.getMemberPermissions(adminOnlyMember, true).values()];
163    
164     normalResult.sort();
165     moderatorResult.sort();
166     adminResult.sort();
167     adminOnlyResult.sort();
168    
169     assert.deepEqual(normalResult, ["SendMessages"] as PermissionsString[]);
170     assert.deepEqual(moderatorResult, ["BanMembers", "KickMembers", "SendMessages"] as PermissionsString[]);
171     assert.deepEqual(adminResult, ["Administrator", "BanMembers", "KickMembers", "SendMessages"] as PermissionsString[]);
172     assert.deepEqual(adminOnlyResult, ["Administrator", "SendMessages"] as PermissionsString[]);
173     });
174    
175     it("Can determine if a member is able to bypass AutoMod", () => {
176     assert.strictEqual(client.permissionManager.isImmuneToAutoMod(normalMember), false);
177     assert.strictEqual(client.permissionManager.isImmuneToAutoMod(moderatorMember), false);
178     assert.strictEqual(
179     client.permissionManager.isImmuneToAutoMod(moderatorMember, [PermissionsBitField.Flags.BanMembers]),
180     true
181     );
182     assert.strictEqual(client.permissionManager.isImmuneToAutoMod(adminMember), true);
183     assert.strictEqual(client.permissionManager.isImmuneToAutoMod(adminMember, [PermissionsBitField.Flags.BanMembers]), true);
184     assert.strictEqual(client.permissionManager.isImmuneToAutoMod(adminOnlyMember), true);
185     assert.strictEqual(
186     client.permissionManager.isImmuneToAutoMod(adminOnlyMember, [PermissionsBitField.Flags.BanMembers]),
187     true
188     );
189     });
190     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26