-
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.
BC-7902 - change logic of authorization (#15)
- Loading branch information
1 parent
3bd6e5a
commit 6a7e1fc
Showing
8 changed files
with
320 additions
and
53 deletions.
There are no files selected for viewing
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,21 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
extensionsToTreatAsEsm: ['.ts'], | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
rootDir: 'src', | ||
testRegex: '.*\\.spec\\.ts$', | ||
collectCoverageFrom: ['**/*.(t|j)s'], | ||
coverageDirectory: '../coverage', | ||
testEnvironment: 'node', | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { HttpRequest } from 'uws'; | ||
import { Logger } from '../logging/logger.js'; | ||
import { AuthorizationService } from './authorization.service.js'; | ||
|
||
describe(AuthorizationService.name, () => { | ||
let module: TestingModule; | ||
let service: AuthorizationService; | ||
let configService: DeepMocked<ConfigService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
AuthorizationService, | ||
{ | ||
provide: ConfigService, | ||
useValue: createMock<ConfigService>(), | ||
}, | ||
{ | ||
provide: Logger, | ||
useValue: createMock<Logger>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<AuthorizationService>(AuthorizationService); | ||
configService = module.get(ConfigService); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const setupRequest = (roomId = 'roomId', cookies = 'other=ABC;jwt=eyJhbGciOiJIU') => { | ||
const req: DeepMocked<HttpRequest> = createMock<HttpRequest>(); | ||
jest.spyOn(req, 'getParameter').mockReturnValue(roomId); | ||
jest.spyOn(req, 'getHeader').mockReturnValue(cookies); | ||
configService.getOrThrow.mockReturnValue('API_HOST'); | ||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
return { req, fetchSpy }; | ||
}; | ||
|
||
describe('hasPermission', () => { | ||
describe('when the user request has permission', () => { | ||
const setup = () => { | ||
const { req, fetchSpy } = setupRequest(); | ||
|
||
fetchSpy.mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve({ isAuthorized: true, userId: '123' }), | ||
} as any); | ||
|
||
const expectedResult = { error: null, hasWriteAccess: true, room: 'roomId', userid: '123' }; | ||
|
||
return { req, expectedResult }; | ||
}; | ||
|
||
it('should return an expectedResult response payload', async () => { | ||
const { req, expectedResult } = setup(); | ||
|
||
const response = await service.hasPermission(req); | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('when the user has no permission', () => { | ||
const setup = () => { | ||
const { req, fetchSpy } = setupRequest(); | ||
|
||
fetchSpy.mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve({ isAuthorized: false, userId: '123' }), | ||
} as any); | ||
|
||
const expectedResult = { | ||
error: { | ||
code: 4401, | ||
reason: 'Unauthorized', | ||
}, | ||
hasWriteAccess: false, | ||
room: null, | ||
userid: null, | ||
}; | ||
|
||
return { req, expectedResult }; | ||
}; | ||
|
||
it('should return an expectedResult response payload', async () => { | ||
const { req, expectedResult } = setup(); | ||
|
||
const response = await service.hasPermission(req); | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('when the roomId is not in request params', () => { | ||
const setup = () => { | ||
const { req } = setupRequest(''); | ||
|
||
const expectedResult = { | ||
error: { | ||
code: 4500, | ||
reason: 'RoomId not found', | ||
}, | ||
hasWriteAccess: false, | ||
room: null, | ||
userid: null, | ||
}; | ||
|
||
return { req, expectedResult }; | ||
}; | ||
|
||
it('should return an expectedResult response payload', async () => { | ||
const { req, expectedResult } = setup(); | ||
|
||
const response = await service.hasPermission(req); | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('when the jwtToken is not in request cookies', () => { | ||
const setup = () => { | ||
const { req } = setupRequest('roomId', 'other=ABC'); | ||
const expectedResult = { | ||
error: { | ||
code: 4500, | ||
reason: 'JWT token not found', | ||
}, | ||
hasWriteAccess: false, | ||
room: null, | ||
userid: null, | ||
}; | ||
|
||
return { req, expectedResult }; | ||
}; | ||
|
||
it('should return an expectedResult response payload', async () => { | ||
const { req, expectedResult } = setup(); | ||
|
||
const response = await service.hasPermission(req); | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('when the roomId not found on server', () => { | ||
const setup = () => { | ||
const { req, fetchSpy } = setupRequest(); | ||
|
||
fetchSpy.mockResolvedValue({ | ||
ok: false, | ||
status: 404, | ||
statusText: 'Not Found', | ||
json: () => Promise.resolve({}), | ||
} as any); | ||
|
||
const expectedResult = { | ||
error: { | ||
code: 4404, | ||
reason: 'Not Found', | ||
}, | ||
hasWriteAccess: false, | ||
room: null, | ||
userid: null, | ||
}; | ||
|
||
return { req, expectedResult }; | ||
}; | ||
|
||
it('should return an expectedResult response payload', async () => { | ||
const { req, expectedResult } = setup(); | ||
|
||
const response = await service.hasPermission(req); | ||
|
||
expect(response).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.