Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndesi committed Mar 1, 2024
1 parent 723c2df commit 3c71680
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 62 deletions.
91 changes: 29 additions & 62 deletions test/Feature/Endpoint/Element/GetElementEndpoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';
import { SinonSandbox, SinonStubbedInstance, assert, createSandbox, match } from 'sinon';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { SinonSandbox, SinonStubbedInstance, assert, createSandbox } from 'sinon';
import { Container } from 'typedi';

import GetElementEndpoint from '~/Endpoint/Element/GetElementEndpoint';
Expand All @@ -8,6 +10,20 @@ import { WebSdkConfiguration } from '~/Service/WebSdkConfiguration';
import { Token, validateTokenFromString } from '~/Type/Definition/Token';
import { validateUuidFromString } from '~/Type/Definition/Uuid';

const server = setupServer(
http.get('http://mock-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5', () => {
return HttpResponse.json({
type: 'Data',
id: 'b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5',
data: {
created: '2023-10-06T20:27:56+00:00',
updated: '2023-10-06T20:27:56+00:00',
name: 'Test Data',
},
});
}),
);

describe('GetElementEndpoint tests', () => {
let sandbox: SinonSandbox;
let mockedLogger: SinonStubbedInstance<Logger>;
Expand All @@ -17,15 +33,22 @@ describe('GetElementEndpoint tests', () => {
beforeEach(() => {
sandbox = createSandbox();

server.listen();

mockedLogger = sandbox.createStubInstance(Logger);
Container.set(Logger, mockedLogger);

Container.get(WebSdkConfiguration).setApiHost('http://ember-nexus-dev-api');
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');
});

afterEach(() => {
sandbox.restore();
Container.reset();
server.resetHandlers();
});

afterEach(() => {
server.close();
});

it('should load an accessible node', async () => {
Expand All @@ -36,71 +59,15 @@ describe('GetElementEndpoint tests', () => {

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5 .',
'Executing HTTP GET request against url http://mock-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5 .',
);

expect(node).to.have.keys('id', 'type', 'data');
expect(node).to.not.have.keys('start', 'end');
expect(node.id).to.equal(nodeUuid);
expect(node.type).to.equal('Data');
expect(Object.keys(node.data)).to.have.lengthOf(4);
});

it('should load an accessible relation', async () => {
Container.get(WebSdkConfiguration).setToken(_token);

const relationUuid = validateUuidFromString('fd9f4b9b-9831-4d8a-be98-a438f28e5038');
const relation = await Container.get(GetElementEndpoint).getElement(relationUuid);

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/fd9f4b9b-9831-4d8a-be98-a438f28e5038 .',
);

expect(relation).to.have.keys('id', 'type', 'data', 'start', 'end');
expect(relation.id).to.equal(relationUuid);
expect(relation.type).to.equal('RELATION');
expect(Object.keys(relation.data)).to.have.lengthOf(3);
});

it('should throw error 401 if token is invalid', async () => {
Container.get(WebSdkConfiguration).setToken(validateTokenFromString('secret-token:IdoNotWorkLol'));
const nodeUuid = validateUuidFromString('b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5');

await expect(Container.get(GetElementEndpoint).getElement(nodeUuid)).to.eventually.be.rejected.and.deep.include({
category: 'server',
title: 'Unauthorized',
detail:
"Authorization for the request failed due to possible problems with the token (incorrect or expired), password (incorrect or changed), the user's unique identifier, or the user's status (e.g., missing, blocked, or deleted).",
status: 401,
});

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5 .',
);

assert.calledOnceWithExactly(mockedLogger.error, match.object);
});

it('should throw error 404 if element is not found', async () => {
Container.get(WebSdkConfiguration).setToken(_token);
const uuidWhichDoesNotExist = validateUuidFromString('00000000-0000-4000-a000-000000000000');

await expect(
Container.get(GetElementEndpoint).getElement(uuidWhichDoesNotExist),
).to.eventually.be.rejected.and.deep.include({
category: 'server',
title: 'Not found',
detail: 'Requested element was not found.',
status: 404,
});

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/00000000-0000-4000-a000-000000000000 .',
);

assert.calledOnceWithExactly(mockedLogger.error, match.object);
expect((node.data as any).created).to.be.instanceof(Date);
expect((node.data as any).updated).to.be.instanceof(Date);
expect(Object.keys(node.data)).to.have.lengthOf(3);
});
});
106 changes: 106 additions & 0 deletions test/OldFeatureEndpoint/Element/GetElementEndpoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { expect } from 'chai';
import { SinonSandbox, SinonStubbedInstance, assert, createSandbox, match } from 'sinon';
import { Container } from 'typedi';

import GetElementEndpoint from '~/Endpoint/Element/GetElementEndpoint';
import { Logger } from '~/Service/Logger';
import { WebSdkConfiguration } from '~/Service/WebSdkConfiguration';
import { Token, validateTokenFromString } from '~/Type/Definition/Token';
import { validateUuidFromString } from '~/Type/Definition/Uuid';

describe('GetElementEndpoint tests', () => {
let sandbox: SinonSandbox;
let mockedLogger: SinonStubbedInstance<Logger>;

const _token: Token = validateTokenFromString('secret-token:Au6srY6s3cW5THS6LeCl9Z');

beforeEach(() => {
sandbox = createSandbox();

mockedLogger = sandbox.createStubInstance(Logger);
Container.set(Logger, mockedLogger);

Container.get(WebSdkConfiguration).setApiHost('http://ember-nexus-dev-api');
});

afterEach(() => {
sandbox.restore();
Container.reset();
});

it('should load an accessible node', async () => {
Container.get(WebSdkConfiguration).setToken(_token);

const nodeUuid = validateUuidFromString('b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5');
const node = await Container.get(GetElementEndpoint).getElement(nodeUuid);

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5 .',
);

expect(node).to.have.keys('id', 'type', 'data');
expect(node).to.not.have.keys('start', 'end');
expect(node.id).to.equal(nodeUuid);
expect(node.type).to.equal('Data');
expect(Object.keys(node.data)).to.have.lengthOf(4);
});

it('should load an accessible relation', async () => {
Container.get(WebSdkConfiguration).setToken(_token);

const relationUuid = validateUuidFromString('fd9f4b9b-9831-4d8a-be98-a438f28e5038');
const relation = await Container.get(GetElementEndpoint).getElement(relationUuid);

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/fd9f4b9b-9831-4d8a-be98-a438f28e5038 .',
);

expect(relation).to.have.keys('id', 'type', 'data', 'start', 'end');
expect(relation.id).to.equal(relationUuid);
expect(relation.type).to.equal('RELATION');
expect(Object.keys(relation.data)).to.have.lengthOf(3);
});

it('should throw error 401 if token is invalid', async () => {
Container.get(WebSdkConfiguration).setToken(validateTokenFromString('secret-token:IdoNotWorkLol'));
const nodeUuid = validateUuidFromString('b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5');

await expect(Container.get(GetElementEndpoint).getElement(nodeUuid)).to.eventually.be.rejected.and.deep.include({
category: 'server',
title: 'Unauthorized',
detail:
"Authorization for the request failed due to possible problems with the token (incorrect or expired), password (incorrect or changed), the user's unique identifier, or the user's status (e.g., missing, blocked, or deleted).",
status: 401,
});

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/b1e85bf9-6a79-4e50-ae5a-ed49beac8cb5 .',
);

assert.calledOnceWithExactly(mockedLogger.error, match.object);
});

it('should throw error 404 if element is not found', async () => {
Container.get(WebSdkConfiguration).setToken(_token);
const uuidWhichDoesNotExist = validateUuidFromString('00000000-0000-4000-a000-000000000000');

await expect(
Container.get(GetElementEndpoint).getElement(uuidWhichDoesNotExist),
).to.eventually.be.rejected.and.deep.include({
category: 'server',
title: 'Not found',
detail: 'Requested element was not found.',
status: 404,
});

assert.calledOnceWithExactly(
mockedLogger.debug,
'Executing HTTP GET request against url http://ember-nexus-dev-api/00000000-0000-4000-a000-000000000000 .',
);

assert.calledOnceWithExactly(mockedLogger.error, match.object);
});
});

0 comments on commit 3c71680

Please sign in to comment.