Skip to content

Commit

Permalink
chore: add a bug repro for permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Jan 3, 2025
1 parent 3fc6e43 commit 5d0d56f
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions packages/lib-modules/src/__tests__/bugRepros.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IntegrationTest, expect, IModuleTestsSetupData, modulesTestSetup, EventsAwaiter } from '@takaro/test';
import { GameEvents, HookEvents } from '../dto/index.js';
import { EventChatMessageChannelEnum } from '@takaro/apiclient';
import { randomUUID } from 'crypto';

const group = 'Bug repros';

Expand Down Expand Up @@ -323,6 +324,105 @@ const tests = [
expect((await eventsAfter)[0].data.meta.msg).to.be.eq('goodbye world');
},
}),
new IntegrationTest<IModuleTestsSetupData>({
group,
snapshot: false,
name: 'Bug repro: permissions from old module versions cleanly transfer when upgrading',
setup: modulesTestSetup,
/**
* Scenario:
* Module with PermA, v0.0.1 is installed
* User then tags v0.0.2 and installs this
* Players with permA can still use the module function that requires that permission
*/
test: async function () {
const permission = 'USE_TEST_PERMISSION';
const module = (
await this.client.module.moduleControllerCreate({
name: randomUUID(),
latestVersion: {
permissions: [{ permission, canHaveCount: false, friendlyName: 'test', description: 'blabla test' }],
},
})
).data.data;

await this.client.command.commandControllerCreate({
name: 'test',
versionId: module.latestVersion.id,
trigger: 'test',
function: `import { data, takaro, checkPermission } from '@takaro/helpers';
async function main() {
const { player, pog } = data;
if (!checkPermission(pog, '${permission}')) {
await takaro.gameserver.gameServerControllerSendMessage('${this.setupData.gameserver.id}', {
message: 'no permission',
});
} else {
await takaro.gameserver.gameServerControllerSendMessage('${this.setupData.gameserver.id}', {
message: 'yes permission',
});
}
}
await main();`,
});

// Create version 0.0.1
const v1 = (
await this.client.module.moduleVersionControllerTagVersion({
moduleId: module.id,
tag: '0.0.1',
})
).data.data;

// Install this version
await this.client.module.moduleInstallationsControllerInstallModule({
gameServerId: this.setupData.gameserver.id,
versionId: v1.id,
});

// Create a role with the permission
const permissionCode = await this.client.permissionCodesToInputs([permission]);
const role = (await this.client.role.roleControllerCreate({ name: 'testing role', permissions: permissionCode }))
.data.data;

// Assign the role to a player
await this.client.player.playerControllerAssignRole(this.setupData.players[0].id, role.id);

// Firing the command should return 'yes permission'
const events = (await new EventsAwaiter().connect(this.client)).waitForEvents(GameEvents.CHAT_MESSAGE, 1);
await this.client.command.commandControllerTrigger(this.setupData.gameserver.id, {
msg: '/test',
playerId: this.setupData.players[0].id,
});

expect((await events)[0].data.meta.msg).to.be.eq('yes permission');

// Tag a new version
const v2 = (
await this.client.module.moduleVersionControllerTagVersion({
moduleId: module.id,
tag: '0.0.2',
})
).data.data;

// Install this version
await this.client.module.moduleInstallationsControllerInstallModule({
gameServerId: this.setupData.gameserver.id,
versionId: v2.id,
});

// Firing the command should return 'yes permission'
const eventsAfter = (await new EventsAwaiter().connect(this.client)).waitForEvents(GameEvents.CHAT_MESSAGE, 1);
await this.client.command.commandControllerTrigger(this.setupData.gameserver.id, {
msg: '/test',
playerId: this.setupData.players[0].id,
});

expect((await eventsAfter)[0].data.meta.msg).to.be.eq('yes permission');
},
}),
];

describe(group, function () {
Expand Down

0 comments on commit 5d0d56f

Please sign in to comment.