Skip to content

Commit

Permalink
fix: fix player registration (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Mar 8, 2021
1 parent b01dd75 commit f980e8e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
73 changes: 73 additions & 0 deletions src/auth/strategies/steam.strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Environment } from '@/environment/environment';
import { Player } from '@/players/models/player';
import { PlayersService } from '@/players/services/players.service';
import { Test, TestingModule } from '@nestjs/testing';
import { mongoose } from '@typegoose/typegoose';
import { SteamStrategy } from './steam.strategy';

const mockPlayer: Player = {
id: 'FAKE_PLAYER_ID',
name: 'FAKE_PLAYER',
steamId: 'FAKE_STEAM_ID',
hasAcceptedRules: true,
};

jest.mock('@/players/services/players.service', () => ({
PlayersService: jest.fn().mockImplementation(() => ({
findBySteamId: jest.fn().mockResolvedValue(mockPlayer),
createPlayer: jest.fn().mockResolvedValue({ ...mockPlayer, hasAcceptedRules: false }),
updatePlayer: jest.fn().mockResolvedValue(mockPlayer),
})),
}));

const environment = {
apiUrl: 'http://api.tf2pickup.pl',
steamApiKey: 'FAKE_STEAM_API_KEY',
};

describe('SteamStrategy', () => {
let strategy: SteamStrategy;
let playersService: jest.Mocked<PlayersService>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
SteamStrategy,
PlayersService,
{ provide: Environment, useValue: environment },
],
}).compile();

strategy = module.get<SteamStrategy>(SteamStrategy);
playersService = module.get(PlayersService);
});

it('should be defined', () => {
expect(strategy).toBeDefined();
});

describe('#validate()', () => {
describe('when the player does have an account', () => {
it('should return that player\'s profile', async () => {
const player = await strategy.validate('FAKE_STEAM_ID', { provider: 'steam', id: 'FAKE_STEAM_ID', displayName: 'FAKE_PLAYER', photos: [] });
expect(playersService.createPlayer).not.toHaveBeenCalled();
expect(player).toEqual(mockPlayer);
});
});

describe('when the player does not have an account', () => {
beforeEach(() => {
playersService.findBySteamId.mockImplementation(steamId => {
throw new mongoose.Error.DocumentNotFoundError({ steamId });
});
});

it('should create it', async () => {
const player = await strategy.validate('FAKE_STEAM_ID', { provider: 'steam', id: 'FAKE_STEAM_ID', displayName: 'FAKE_PLAYER', photos: [] });
expect(playersService.createPlayer).toHaveBeenCalled();
expect(player.steamId).toEqual('FAKE_STEAM_ID');
expect(player.hasAcceptedRules).toBe(false);
});
});
});
});
14 changes: 9 additions & 5 deletions src/auth/strategies/steam.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PlayersService } from '@/players/services/players.service';
import { Environment } from '@/environment/environment';
import { SteamProfile } from '@/players/steam-profile';
import { PassportStrategy } from '@nestjs/passport';
import { mongoose } from '@typegoose/typegoose';

@Injectable()
export class SteamStrategy extends PassportStrategy(steam.Strategy) {
Expand All @@ -22,18 +23,21 @@ export class SteamStrategy extends PassportStrategy(steam.Strategy) {
}

async validate(identifier: any, profile: SteamProfile) {
const player = await this.playerService.findBySteamId(profile.id);
if (player) {
try {
const player = await this.playerService.findBySteamId(profile.id);
return await this.playerService.updatePlayer(player.id, {
avatar: {
small: profile.photos[0]?.value,
medium: profile.photos[1]?.value,
large: profile.photos[2]?.value,
},
});

} else {
return await this.playerService.createPlayer(profile);
} catch (error) {
if (error instanceof mongoose.Error.DocumentNotFoundError) {
return await this.playerService.createPlayer(profile);
} else {
throw error;
}
}
}

Expand Down

0 comments on commit f980e8e

Please sign in to comment.