fix: close competition cutover integrity gaps
This commit is contained in:
1 parent
7ea2002975
commit
683d602d63
57 files changed
+4805
-1582
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import 'reflect-metadata';
|
||||
import { GUARDS_METADATA } from '@nestjs/common/constants';
|
||||
import mongoose from 'mongoose';
|
||||
import { METADATA_KEY_IS_PUBLIC_ACCESS_ALLOWED } from '../auth/allow-public-access.decorator';
|
||||
import { ROLES_KEY } from '../auth/roles.decorator';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
import type { UserDocument } from '../global/database/schema';
|
||||
import { MatchController } from './match.controller';
|
||||
|
||||
describe('MatchController participant access', () => {
|
||||
function createController() {
|
||||
const joinedMatch = { _id: new mongoose.Types.ObjectId() };
|
||||
const response = { _id: joinedMatch._id.toString(), joined: true };
|
||||
const matchService = {
|
||||
join: jest.fn().mockResolvedValue(joinedMatch),
|
||||
serialize: jest.fn().mockReturnValue(response),
|
||||
};
|
||||
|
||||
return {
|
||||
controller: new MatchController(matchService as never),
|
||||
joinedMatch,
|
||||
matchService,
|
||||
response,
|
||||
};
|
||||
}
|
||||
|
||||
function createUser(communityId: mongoose.Types.ObjectId): UserDocument {
|
||||
return {
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
communityId,
|
||||
role: 'member',
|
||||
} as UserDocument;
|
||||
}
|
||||
|
||||
it('requires an authenticated community member for the self-join route', () => {
|
||||
// Metadata must be read from the unbound prototype method itself.
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
const handler = MatchController.prototype.joinMatch;
|
||||
const roles = Reflect.getMetadata(ROLES_KEY, handler) as
|
||||
| string[]
|
||||
| undefined;
|
||||
const guards = Reflect.getMetadata(GUARDS_METADATA, handler) as
|
||||
| unknown[]
|
||||
| undefined;
|
||||
|
||||
expect(
|
||||
Reflect.getMetadata(METADATA_KEY_IS_PUBLIC_ACCESS_ALLOWED, handler),
|
||||
).not.toBe(true);
|
||||
expect(roles).toHaveLength(3);
|
||||
expect(roles).toEqual(expect.arrayContaining(['owner', 'admin', 'member']));
|
||||
expect(guards).toEqual(expect.arrayContaining([RolesGuard]));
|
||||
});
|
||||
|
||||
it('joins only the authenticated user', async () => {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const currentUser = createUser(communityId);
|
||||
const preferredPositions = [{ position: 'MID' as const, priority: 1 }];
|
||||
const { controller, joinedMatch, matchService, response } =
|
||||
createController();
|
||||
|
||||
await expect(
|
||||
controller.joinMatch(currentUser, communityId, matchId, {
|
||||
preferredPositions,
|
||||
}),
|
||||
).resolves.toBe(response);
|
||||
expect(matchService.join).toHaveBeenCalledWith({
|
||||
matchId,
|
||||
communityId,
|
||||
userId: currentUser._id,
|
||||
preferredPositions,
|
||||
});
|
||||
expect(matchService.serialize).toHaveBeenCalledWith(joinedMatch, {
|
||||
joined: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects a dev-auth user from a different URL community', async () => {
|
||||
const currentUser = createUser(new mongoose.Types.ObjectId());
|
||||
const requestedCommunityId = new mongoose.Types.ObjectId();
|
||||
const { controller, matchService } = createController();
|
||||
|
||||
await expect(
|
||||
controller.joinMatch(
|
||||
currentUser,
|
||||
requestedCommunityId,
|
||||
new mongoose.Types.ObjectId(),
|
||||
{ preferredPositions: [{ position: 'TOP', priority: 1 }] },
|
||||
),
|
||||
).rejects.toThrow('Different community.');
|
||||
expect(matchService.join).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user