Files
lolympus-tournament-support/apps/api/src/modules/dakao/dakao-user.service.spec.ts
T

248 lines
7.4 KiB
TypeScript

import mongoose from 'mongoose';
import { ConnectionService } from '../global/database/connection.service';
import { RiotAccount, UserDocument } from '../global/database/schema';
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
import { RiotAccountService } from '../riot/riot-account.service';
import { DakaoUserService } from './dakao-user.service';
const objectId = (hex: string) => new mongoose.Types.ObjectId(hex);
function makeQuery<T>(value: T) {
return {
exec: jest.fn().mockResolvedValue(value),
};
}
function makeService(params: {
user: Partial<UserDocument> | null;
community?: Record<string, unknown> | null;
riotAccountService?: Partial<RiotAccountService>;
enrichmentService?: Partial<RiotAccountEnrichmentService>;
}) {
const findOneAndUpdate = jest
.fn()
.mockReturnValue(makeQuery(params.user as UserDocument));
const findOne = jest.fn().mockReturnValue(makeQuery(params.user));
const findById = jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
lean: jest.fn().mockReturnValue(makeQuery(params.community ?? {})),
}),
});
const connectionService = {
getModel: jest.fn().mockImplementation(async (name: string) => {
if (name === 'user') return { findOneAndUpdate, findOne };
if (name === 'community') return { findById };
throw new Error(`Unexpected model: ${name}`);
}),
} as unknown as ConnectionService;
const riotAccountService = {
getByRiotId: jest.fn(),
...params.riotAccountService,
} as unknown as RiotAccountService;
const enrichmentService = {
enrichRiotAccount: jest.fn(),
...params.enrichmentService,
} as unknown as RiotAccountEnrichmentService;
return {
service: new DakaoUserService(
connectionService,
riotAccountService,
enrichmentService,
),
connectionService,
findOneAndUpdate,
findOne,
findById,
riotAccountService,
enrichmentService,
};
}
describe('DakaoUserService', () => {
it('registers an additional Riot account when the user already has one', async () => {
const save = jest.fn().mockResolvedValue(undefined);
const existingAccount = {
puuid: 'puuid-1',
gameName: '기존소환사',
tagLine: 'KR1',
isPrimary: true,
} as RiotAccount;
const {
service,
connectionService,
riotAccountService,
enrichmentService,
} = makeService({
user: {
_id: objectId('222222222222222222222222'),
riotAccounts: [existingAccount],
save,
} as Partial<UserDocument>,
riotAccountService: {
getByRiotId: jest.fn().mockResolvedValue({
puuid: 'puuid-2',
gameName: '새소환사',
tagLine: 'KR2',
}),
},
enrichmentService: {
enrichRiotAccount: jest.fn().mockResolvedValue({ account: null }),
},
});
const result = await service.registerRiotAccount({
communityId: objectId('111111111111111111111111'),
kakaoTalkId: 'kakao-user-1',
kakaoTalkNickname: '프리리',
gameName: '새소환사',
tagLine: 'KR2',
});
expect(result.riotAccount).toMatchObject({
puuid: 'puuid-2',
gameName: '새소환사',
tagLine: 'KR2',
isPrimary: false,
});
expect((result.user.riotAccounts as RiotAccount[])[0]).toMatchObject({
puuid: 'puuid-1',
isPrimary: true,
});
expect(result.user.riotAccounts).toHaveLength(2);
expect(connectionService.getModel).toHaveBeenCalledWith('user');
expect(connectionService.getModel).toHaveBeenCalledWith('community');
expect(riotAccountService.getByRiotId).toHaveBeenCalledWith(
'새소환사',
'KR2',
{
region: 'asia',
},
);
expect(enrichmentService.enrichRiotAccount).toHaveBeenCalledWith(
expect.objectContaining({
userId: expect.any(mongoose.Types.ObjectId),
puuid: 'puuid-2',
options: { force: true, platform: 'kr', region: 'asia' },
}),
);
expect(save).toHaveBeenCalled();
});
it('reuses an already attached Riot account by puuid', async () => {
const save = jest.fn().mockResolvedValue(undefined);
const existingAccount = {
puuid: 'puuid-1',
gameName: '기존소환사',
tagLine: 'KR1',
isPrimary: true,
} as RiotAccount;
const { service, enrichmentService } = makeService({
user: {
_id: objectId('222222222222222222222222'),
riotAccounts: [existingAccount],
save,
} as Partial<UserDocument>,
riotAccountService: {
getByRiotId: jest.fn().mockResolvedValue({
puuid: 'puuid-1',
gameName: '바뀐소환사',
tagLine: 'KR1',
}),
},
});
const result = await service.registerRiotAccount({
communityId: objectId('111111111111111111111111'),
kakaoTalkId: 'kakao-user-1',
kakaoTalkNickname: '프리리',
gameName: '바뀐소환사',
tagLine: 'KR1',
});
expect(result.user.riotAccounts).toHaveLength(1);
expect(result.riotAccount).toMatchObject({
puuid: 'puuid-1',
gameName: '바뀐소환사',
tagLine: 'KR1',
isPrimary: true,
});
expect(enrichmentService.enrichRiotAccount).not.toHaveBeenCalled();
expect(save).toHaveBeenCalled();
});
it('changes the primary Riot account for the Dakao user', async () => {
const save = jest.fn().mockResolvedValue(undefined);
const oldPrimary = {
puuid: 'puuid-1',
gameName: '기존소환사',
tagLine: 'KR1',
isPrimary: true,
} as RiotAccount;
const nextPrimary = {
puuid: 'puuid-2',
gameName: '프리리',
tagLine: 'KR1',
isPrimary: false,
} as RiotAccount;
const { service, findOne } = makeService({
user: {
_id: objectId('222222222222222222222222'),
kakaoTalkNickname: '이전닉',
riotAccounts: [oldPrimary, nextPrimary],
save,
} as Partial<UserDocument>,
});
const result = await service.changePrimaryRiotAccount({
communityId: objectId('111111111111111111111111'),
kakaoTalkId: 'kakao-user-1',
kakaoTalkNickname: '프리리',
gameName: '프리리',
tagLine: 'KR1',
});
expect(findOne).toHaveBeenCalledWith({
communityId: objectId('111111111111111111111111'),
kakaoTalkId: 'kakao-user-1',
});
expect(oldPrimary.isPrimary).toBe(false);
expect(nextPrimary.isPrimary).toBe(true);
expect(result.riotAccount).toBe(nextPrimary);
expect(result.user.kakaoTalkNickname).toBe('프리리');
expect(save).toHaveBeenCalled();
});
it('rejects primary account changes for unregistered Riot accounts', async () => {
const save = jest.fn().mockResolvedValue(undefined);
const { service } = makeService({
user: {
_id: objectId('222222222222222222222222'),
riotAccounts: [
{
puuid: 'puuid-1',
gameName: '기존소환사',
tagLine: 'KR1',
isPrimary: true,
},
],
save,
} as Partial<UserDocument>,
});
await expect(
service.changePrimaryRiotAccount({
communityId: objectId('111111111111111111111111'),
kakaoTalkId: 'kakao-user-1',
kakaoTalkNickname: '프리리',
gameName: '프리리',
tagLine: 'KR1',
}),
).rejects.toMatchObject({
code: 'riot_account_not_found',
detail: '프리리#KR1',
});
expect(save).not.toHaveBeenCalled();
});
});