Test: WS Guard Tests Pass

This commit is contained in:
ADAMJR 2021-10-05 18:29:46 +01:00
parent 84a8ac831d
commit a788e4b3ae
4 changed files with 20 additions and 9 deletions

View File

@ -27,7 +27,7 @@ export default class Roles extends DBWrapper<string, RoleDocument> {
&& !theirRoleIds.includes(highestRole.id));
}
public async hasPermission(guild: Entity.Guild, member: Entity.GuildMember, permission: PermissionTypes.PermissionString) {
public async hasPermission(guild: Entity.Guild, member: Entity.GuildMember, permission: PermissionTypes.Permission) {
const guildRoles = await Role.find({ guildId: guild.id });
const totalPerms = guildRoles
.filter(r => member.roleIds.includes(r.id))
@ -36,7 +36,7 @@ export default class Roles extends DBWrapper<string, RoleDocument> {
const permNumber = (typeof permission === 'string')
? PermissionTypes.All[PermissionTypes.All[permission as string]]
: permission;
return hasPermission(totalPerms, permNumber as any);
return hasPermission(totalPerms, +permNumber);
}
public async create(guildId: string, options?: Partial<Entity.Role>) {

View File

@ -7,7 +7,7 @@ import Roles from '../../data/roles';
import Users from '../../data/users';
import Guilds from '../../data/guilds';
import GuildMembers from '../../data/guild-members';
import { PermissionTypes } from '../../types/permission-types';
import { PermissionTypes, getPermString } from '../../types/permission-types';
export class WSGuard {
constructor(
@ -38,13 +38,13 @@ export class WSGuard {
public async validateCan(client: Socket, guildId: string, permission: PermissionTypes.PermissionString) {
const can = await this.can(permission, guildId, this.userId(client));
if (!can)
throw new TypeError(`Missing Permissions: ${permission}`);
throw new TypeError(`Missing Permissions: ${getPermString(permission)}`);
}
public async validateCanInChannel(client: Socket, channelId: string, permission: PermissionTypes.PermissionString) {
const can = await this.canInChannel(permission, channelId, this.userId(client));
if (!can)
throw new TypeError(`Missing Permissions: ${permission}`);
throw new TypeError(`Missing Permissions: ${getPermString(permission)}`);
}
private async can(permission: PermissionTypes.PermissionString, guildId: string, userId: string) {
@ -52,7 +52,7 @@ export class WSGuard {
const member = await this.members.getInGuild(guildId, userId);
return (guild.ownerId === member.userId)
|| this.roles.hasPermission(guild, member, permission);
|| this.roles.hasPermission(guild, member, PermissionTypes.All[permission]);
}
public async canInChannel(permission: PermissionTypes.PermissionString, channelId: string, userId: string) {

View File

@ -60,18 +60,18 @@ describe.only('ws-guard', () => {
it('validateCan: missing SEND_MESSAGES, rejected', async () => {
await Mock.clearRolePerms(guild);
await expect(
guard.validateCan(client, guild.id, PermissionTypes.Text.SEND_MESSAGES)
guard.validateCan(client, guild.id, 'SEND_MESSAGES')
).to.be.rejectedWith('Missing Permissions: SEND_MESSAGES');
});
it('validateCan: has SEND_MESSAGES, fulfilled', async () => {
await expect(
guard.validateCan(client, guild.id, PermissionTypes.Text.SEND_MESSAGES)
guard.validateCan(client, guild.id, 'SEND_MESSAGES')
).to.be.fulfilled;
});
it('validateCan: is owner, fulfilled', async () => {
await Mock.makeGuildOwner(ws, client, guild);
await expect(
guard.validateCan(client, guild.id, PermissionTypes.Text.SEND_MESSAGES)
guard.validateCan(client, guild.id, 'SEND_MESSAGES')
).to.be.fulfilled;
});

View File

@ -44,4 +44,15 @@ export namespace PermissionTypes {
// | PermissionTypes.Text.ADD_REACTIONS
// | PermissionTypes.Voice.CONNECT
// | PermissionTypes.Voice.SPEAK;
}
export function getPermString(integer: number | string): string {
console.log('integer', integer);
return (typeof integer === 'string')
? Object
.entries(PermissionTypes.All)
.filter(([k, v]) => Number.isInteger(+v))
.find(([k, v]) => k === integer || v === integer)?.[0] ?? ''
: integer.toString();
}