123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import { ConnectionService } from '../global/database/connection.service';
|
|
import { RiotAccountService } from '../riot/riot-account.service';
|
|
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
|
import { DakaoUserService } from './dakao-user.service';
|
|
|
|
const communityId = new mongoose.Types.ObjectId();
|
|
const params = {
|
|
communityId,
|
|
kakaoTalkId: 'new-id',
|
|
previousNickname: '예전 닉',
|
|
};
|
|
|
|
function setup() {
|
|
const user = {
|
|
_id: new mongoose.Types.ObjectId(),
|
|
communityId,
|
|
kakaoTalkId: 'old-id',
|
|
kakaoTalkNickname: '예전 닉',
|
|
riotAccounts: [{ puuid: 'puuid', gameName: '소환사', tagLine: 'KR1' }],
|
|
role: 'member',
|
|
};
|
|
const find = jest.fn().mockReturnValue({
|
|
limit: jest
|
|
.fn()
|
|
.mockReturnValue({ exec: jest.fn().mockResolvedValue([user]) }),
|
|
});
|
|
const findOne = jest
|
|
.fn()
|
|
.mockReturnValue({ exec: jest.fn().mockResolvedValue(null) });
|
|
const update = jest
|
|
.fn()
|
|
.mockImplementation(async () => ({ ...user, kakaoTalkId: 'new-id' }));
|
|
const findOneAndUpdate = jest.fn().mockReturnValue({ exec: update });
|
|
const service = new DakaoUserService(
|
|
{
|
|
getModel: jest
|
|
.fn()
|
|
.mockResolvedValue({ find, findOne, findOneAndUpdate }),
|
|
} as unknown as ConnectionService,
|
|
{} as RiotAccountService,
|
|
{} as RiotAccountEnrichmentService,
|
|
);
|
|
return { service, user, find, findOne, findOneAndUpdate, update };
|
|
}
|
|
|
|
describe('DakaoUserService.transferKakaoTalkId', () => {
|
|
it('selects registered users by exact nickname in the current community and changes only the ID', async () => {
|
|
const { service, user, find, findOneAndUpdate } = setup();
|
|
expect(await service.transferKakaoTalkId(params)).toBe('transferred');
|
|
expect(find).toHaveBeenCalledWith({
|
|
communityId,
|
|
kakaoTalkNickname: '예전 닉',
|
|
'riotAccounts.0': { $exists: true },
|
|
});
|
|
expect(findOneAndUpdate).toHaveBeenCalledWith(
|
|
{
|
|
_id: user._id,
|
|
communityId,
|
|
kakaoTalkId: 'old-id',
|
|
kakaoTalkNickname: '예전 닉',
|
|
'riotAccounts.0': { $exists: true },
|
|
},
|
|
{ $set: { kakaoTalkId: 'new-id' } },
|
|
{ returnDocument: 'after' },
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
['not-found', 0],
|
|
['ambiguous', 2],
|
|
] as const)('returns %s without mutation', async (status, count) => {
|
|
const { service, user, find, findOneAndUpdate } = setup();
|
|
find.mockReturnValue({
|
|
limit: () => ({
|
|
exec: async () => Array.from({ length: count }, () => user),
|
|
}),
|
|
});
|
|
expect(await service.transferKakaoTalkId(params)).toBe(status);
|
|
expect(findOneAndUpdate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not mutate an already linked user', async () => {
|
|
const { service, user, findOneAndUpdate } = setup();
|
|
user.kakaoTalkId = 'new-id';
|
|
expect(await service.transferKakaoTalkId(params)).toBe('already-linked');
|
|
expect(findOneAndUpdate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('refuses an ID occupied by another user', async () => {
|
|
const { service, findOne, findOneAndUpdate } = setup();
|
|
findOne.mockReturnValue({
|
|
exec: async () => ({ _id: new mongoose.Types.ObjectId() }),
|
|
});
|
|
expect(await service.transferKakaoTalkId(params)).toBe('id-conflict');
|
|
expect(findOne).toHaveBeenCalledWith({
|
|
communityId,
|
|
kakaoTalkId: 'new-id',
|
|
});
|
|
expect(findOneAndUpdate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('reports a concurrent change instead of overwriting it', async () => {
|
|
const { service, update } = setup();
|
|
update.mockResolvedValue(null);
|
|
expect(await service.transferKakaoTalkId(params)).toBe('changed');
|
|
});
|
|
|
|
it('handles a concurrent ID collision', async () => {
|
|
const { service, update } = setup();
|
|
update.mockRejectedValue({ code: 11000 });
|
|
expect(await service.transferKakaoTalkId(params)).toBe('id-conflict');
|
|
});
|
|
|
|
it('propagates unexpected database failures for retry', async () => {
|
|
const { service, update } = setup();
|
|
update.mockRejectedValue(new Error('database unavailable'));
|
|
await expect(service.transferKakaoTalkId(params)).rejects.toThrow(
|
|
'database unavailable',
|
|
);
|
|
});
|
|
});
|