-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from TaloDev/develop
Release 0.34.0
- Loading branch information
Showing
12 changed files
with
209 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Migration } from '@mikro-orm/migrations' | ||
|
||
export class AddAPIKeyUpdatedAtColumn extends Migration { | ||
|
||
async up(): Promise<void> { | ||
this.addSql('alter table `apikey` add `updated_at` datetime null;') | ||
} | ||
|
||
async down(): Promise<void> { | ||
this.addSql('alter table `apikey` drop column `updated_at`;') | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { EntityManager } from '@mikro-orm/mysql' | ||
import request from 'supertest' | ||
import { UserType } from '../../../src/entities/user' | ||
import GameActivity, { GameActivityType } from '../../../src/entities/game-activity' | ||
import createUserAndToken from '../../utils/createUserAndToken' | ||
import userPermissionProvider from '../../utils/userPermissionProvider' | ||
import createOrganisationAndGame from '../../utils/createOrganisationAndGame' | ||
import APIKey from '../../../src/entities/api-key' | ||
|
||
describe('API key service - put', () => { | ||
it.each(userPermissionProvider([ | ||
UserType.ADMIN | ||
]))('should return a %i for a %s user', async (statusCode, _, type) => { | ||
const [organisation, game] = await createOrganisationAndGame() | ||
const [token, user] = await createUserAndToken({ type, emailConfirmed: true }, organisation) | ||
|
||
const key = new APIKey(game, user) | ||
await (<EntityManager>global.em).persistAndFlush(key) | ||
|
||
const res = await request(global.app) | ||
.put(`/games/${game.id}/api-keys/${key.id}`) | ||
.send({ scopes: ['read:players', 'write:events'] }) | ||
.auth(token, { type: 'bearer' }) | ||
.expect(statusCode) | ||
|
||
if (statusCode === 200) { | ||
expect(res.body.apiKey.gameId).toBe(game.id) | ||
expect(res.body.apiKey.scopes).toStrictEqual(['read:players', 'write:events']) | ||
} | ||
|
||
const activity = await (<EntityManager>global.em).getRepository(GameActivity).findOne({ | ||
type: GameActivityType.API_KEY_UPDATED, | ||
game, | ||
extra: { | ||
keyId: key.id, | ||
display: { | ||
'Scopes': 'read:players, write:events' | ||
} | ||
} | ||
}) | ||
|
||
if (statusCode === 200) { | ||
expect(activity).not.toBeNull() | ||
} else { | ||
expect(activity).toBeNull() | ||
} | ||
}) | ||
|
||
it('should not update an api key if the user\'s email is not confirmed', async () => { | ||
const [organisation, game] = await createOrganisationAndGame() | ||
const [token, user] = await createUserAndToken({ type: UserType.ADMIN }, organisation) | ||
|
||
const key = new APIKey(game, user) | ||
await (<EntityManager>global.em).persistAndFlush(key) | ||
|
||
const res = await request(global.app) | ||
.put(`/games/${game.id}/api-keys/${key.id}`) | ||
.send({ scopes: ['read:players', 'write:events'] }) | ||
.auth(token, { type: 'bearer' }) | ||
.expect(403) | ||
|
||
expect(res.body).toStrictEqual({ message: 'You need to confirm your email address to update API keys' }) | ||
}) | ||
|
||
it('should not update an api key for a non-existent game', async () => { | ||
const [, game] = await createOrganisationAndGame() | ||
const [token, user] = await createUserAndToken({ emailConfirmed: true, type: UserType.ADMIN }) | ||
|
||
const key = new APIKey(game, user) | ||
await (<EntityManager>global.em).persistAndFlush(key) | ||
|
||
const res = await request(global.app) | ||
.put(`/games/99999/api-keys/${key.id}`) | ||
.send({ scopes: [] }) | ||
.auth(token, { type: 'bearer' }) | ||
.expect(404) | ||
|
||
expect(res.body).toStrictEqual({ message: 'Game not found' }) | ||
}) | ||
|
||
it('should not create an api key for a game the user has no access to', async () => { | ||
const [, otherGame] = await createOrganisationAndGame() | ||
const [token, user] = await createUserAndToken({ emailConfirmed: true, type: UserType.ADMIN }) | ||
|
||
const key = new APIKey(otherGame, user) | ||
await (<EntityManager>global.em).persistAndFlush(key) | ||
|
||
const res = await request(global.app) | ||
.put(`/games/${otherGame.id}/api-keys/${key.id}`) | ||
.send({ scopes: [] }) | ||
.auth(token, { type: 'bearer' }) | ||
.expect(403) | ||
|
||
expect(res.body).toStrictEqual({ message: 'Forbidden' }) | ||
}) | ||
|
||
it('should not update an api key that does not exist', async () => { | ||
const [organisation, game] = await createOrganisationAndGame() | ||
const [token] = await createUserAndToken({ emailConfirmed: true, type: UserType.ADMIN }, organisation) | ||
|
||
const res = await request(global.app) | ||
.put(`/games/${game.id}/api-keys/99999`) | ||
.send({ scopes: ['read:players', 'write:events'] }) | ||
.auth(token, { type: 'bearer' }) | ||
.expect(404) | ||
|
||
expect(res.body).toStrictEqual({ message: 'API key not found' }) | ||
}) | ||
}) |