Skip to content

Commit

Permalink
Merge branch 'BC-5522-impl-of-deletion-api' of https://github.com/hpi…
Browse files Browse the repository at this point in the history
…-schul-cloud/schulcloud-server into BC-5522-impl-of-deletion-api
  • Loading branch information
WojciechGrancow committed Nov 15, 2023
2 parents c866efe + 01340fb commit bf68c68
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ spec:
- containerPort: 3030
name: api
protocol: TCP
- containerPort: 4030
name: api-admin
protocol: TCP
- containerPort: 9090
name: api-metrics
protocol: TCP
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { UnauthorizedException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { ConfigService } from '@nestjs/config';
import { createMock } from '@golevelup/ts-jest';
import { XApiKeyStrategy } from './x-api-key.strategy';
import { IXApiKeyConfig } from '../config/x-api-key.config';

describe('XApiKeyStrategy', () => {
let module: TestingModule;
let strategy: XApiKeyStrategy;
let configService: ConfigService<IXApiKeyConfig, true>;
Configuration.set('ADMIN_API__ALLOWED_API_KEYS', '1ab2c3d4e5f61ab2c3d4e5f6');

beforeAll(async () => {
module = await Test.createTestingModule({
imports: [],
providers: [
XApiKeyStrategy,
{
provide: ConfigService,
useValue: createMock<ConfigService<IXApiKeyConfig, true>>({ get: () => ['1ab2c3d4e5f61ab2c3d4e5f6'] }),
},
],
}).compile();

strategy = module.get(XApiKeyStrategy);
configService = module.get(ConfigService<IXApiKeyConfig, true>);
});

afterAll(async () => {
await module.close();
});

beforeEach(() => {
jest.resetAllMocks();
});

describe('validate', () => {
const setup = () => {
const CORRECT_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6';
const INVALID_API_KEY = '1ab2c3d4e5f61ab2c3d4e5f6778173';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const done = jest.fn((error: Error | null, data: boolean | null) => {});

return { CORRECT_API_KEY, INVALID_API_KEY, done };
};
describe('when a valid api key is provided', () => {
it('should do nothing', () => {
const { CORRECT_API_KEY, done } = setup();
strategy.validate(CORRECT_API_KEY, done);
expect(done).toBeCalledWith(null, true);
});
});

describe('when a invalid api key is provided', () => {
it('should throw error', () => {
const { INVALID_API_KEY, done } = setup();
strategy.validate(INVALID_API_KEY, done);
expect(done).toBeCalledWith(new UnauthorizedException(), null);
});
});
});

describe('constructor', () => {
it('should create strategy', () => {
const ApiKeyStrategy = new XApiKeyStrategy(configService);
expect(ApiKeyStrategy).toBeDefined();
expect(ApiKeyStrategy).toBeInstanceOf(XApiKeyStrategy);
});
});
});
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"EXTERNAL_TOOL_MAX_LOGO_SIZE_IN_BYTES": 300000
},
"ADMIN_API": {
"ENABLED": true
"ENABLED": true,
"ALLOWED_API_KEYS": "1ab2c3d4e5f61ab2c3d4e5f6"
}
}

0 comments on commit bf68c68

Please sign in to comment.