Skip to content

Commit

Permalink
Merge pull request #97 from ember-nexus/github-issue/93
Browse files Browse the repository at this point in the history
GitHub issue/93
  • Loading branch information
Syndesi authored Oct 6, 2024
2 parents a424dc3 + 1bc0407 commit 50597dc
Show file tree
Hide file tree
Showing 30 changed files with 773 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Remaining unit tests for user endpoints.
### Fixed
- Fix bug in register endpoint, where successful responses could not be parsed, closes #93.

## 0.0.63 - 2024-10-06
### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/User/PostRegisterEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PostRegisterEndpoint {
return Promise.reject(new NetworkError(`Experienced generic network error during creating resource.`, error));
})
.then(async (response: Response) => {
if (response.ok && response.status == 204) {
if (response.ok && response.status == 201) {
if (response.headers.has('Location')) {
const location = response.headers.get('Location') as string;
const rawUuid = location.split('/').at(-1) as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('DeleteElementEndpoint should handle bad response error', async () => {
test('DeleteElementEndpoint should handle unauthorized response error', async () => {
const sandbox = createSandbox();
mockServer.listen();
const fetchHelper = Container.get(FetchHelper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetElementChildrenEndpoint should handle bad response error', async () => {
test('GetElementChildrenEndpoint should handle unauthorized response error', async () => {
const sandbox = createSandbox();
mockServer.listen();
const fetchHelper = Container.get(FetchHelper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetElementEndpoint should handle bad response error', async () => {
test('GetElementEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
const uuid = validateUuidFromString('5324396a-636a-4263-ac38-62fef3132ead');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetElementParentsEndpoint should handle bad response error', async () => {
test('GetElementParentsEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
const uuid = validateUuidFromString('6b2e22d9-33a9-4b42-8d2a-61c8945f357f');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetElementRelatedEndpoint should handle bad response error', async () => {
test('GetElementRelatedEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
const uuid = validateUuidFromString('ab76441a-6540-4fe1-849b-9b7167b891da');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetIndexEndpoint should handle bad response error', async () => {
test('GetIndexEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
await expect(Container.get(GetIndexEndpoint).getIndex()).to.eventually.be.rejectedWith(Response401UnauthorizedError);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PatchElementEndpoint should handle bad response error', async () => {
test('PatchElementEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
const uuid = validateUuidFromString('2fe3ba3b-c44c-45b8-a427-4724c46f9951');
const data: Data = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostElementEndpoint should handle bad response error', async () => {
test('PostElementEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
const parentUuid = validateUuidFromString('0cadf906-f607-4952-afbc-05d85e613bd7');
const element: NodeWithOptionalId = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('DeleteTokenEndpoint should handle bad response error', async () => {
test('DeleteTokenEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
await expect(Container.get(DeleteTokenEndpoint).deleteToken()).to.eventually.be.rejectedWith(
Response401UnauthorizedError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetMeEndpoint should handle bad response error', async () => {
test('GetMeEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
await expect(Container.get(GetMeEndpoint).getMe()).to.eventually.be.rejectedWith(Response401UnauthorizedError);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('GetTokenEndpoint should handle bad response error', async () => {
test('GetTokenEndpoint should handle unauthorized response error', async () => {
mockServer.listen();
await expect(Container.get(GetTokenEndpoint).getToken()).to.eventually.be.rejectedWith(Response401UnauthorizedError);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
return new HttpResponse(null, {
status: 204,
headers: {},
});
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle node response', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await Container.get(PostChangePasswordEndpoint).postChangePassword(
uniqueUserIdentifier,
currentPassword,
newPassword,
);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

mockServer.close();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { ParseError } from '../../../../../src/Error';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
return HttpResponse.text('Some content which can not be interpreted as JSON.', {
status: 200,
headers: {
'Content-Type': 'text/plain; charset=utf-8',
},
});
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle bad response error', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await expect(
Container.get(PostChangePasswordEndpoint).postChangePassword(uniqueUserIdentifier, currentPassword, newPassword),
).to.eventually.be.rejectedWith(ParseError);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

expect(testLogger.assertErrorHappened("Unable to parse response as content type is not 'application/problem+json'."))
.to.be.true;

mockServer.close();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'chai';
import { http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { NetworkError } from '../../../../../src/Error';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
return Response.error();
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle network error', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await expect(
Container.get(PostChangePasswordEndpoint).postChangePassword(uniqueUserIdentifier, currentPassword, newPassword),
).to.eventually.be.rejectedWith(NetworkError);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

expect(testLogger.assertErrorHappened('Experienced generic network error during creating resource.')).to.be.true;

mockServer.close();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { ParseError } from '../../../../../src/Error';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
const response = HttpResponse.text('Some content which can not be interpreted as JSON.', {
status: 200,
});
response.headers.delete('Content-Type');
return response;
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle no content type response error', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await expect(
Container.get(PostChangePasswordEndpoint).postChangePassword(uniqueUserIdentifier, currentPassword, newPassword),
).to.eventually.be.rejectedWith(ParseError);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

expect(testLogger.assertErrorHappened('Response does not contain content type header.')).to.be.true;

mockServer.close();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { Response429TooManyRequestsError } from '../../../../../src/Error';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
return HttpResponse.json(
{
type: 'http://ember-nexus-api/error/429/too-many-requests',
title: 'Unauthorized',
status: 429,
detail: 'wip',
},
{
status: 429,
headers: {
'Content-Type': 'application/problem+json; charset=utf-8',
},
},
);
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle bad response error', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await expect(
Container.get(PostChangePasswordEndpoint).postChangePassword(uniqueUserIdentifier, currentPassword, newPassword),
).to.eventually.be.rejectedWith(Response429TooManyRequestsError);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

expect(testLogger.assertErrorHappened('Server returned 429 too many requests.')).to.be.true;

mockServer.close();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from 'chai';
import { HttpResponse, http } from 'msw';
import { setupServer } from 'msw/node';
import { Container } from 'typedi';

import { PostChangePasswordEndpoint } from '../../../../../src/Endpoint/User';
import { Response401UnauthorizedError } from '../../../../../src/Error';
import { Logger, WebSdkConfiguration } from '../../../../../src/Service';
import { createUniqueUserIdentifierFromString } from '../../../../../src/Type/Definition';
import { TestLogger } from '../../../TestLogger';

const mockServer = setupServer(
http.post('http://mock-api/change-password', () => {
return HttpResponse.json(
{
type: 'http://ember-nexus-api/error/401/unauthorized',
title: 'Unauthorized',
status: 401,
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,
headers: {
'Content-Type': 'application/problem+json; charset=utf-8',
},
},
);
}),
);

const testLogger: TestLogger = new TestLogger();
Container.set(Logger, testLogger);
Container.get(WebSdkConfiguration).setApiHost('http://mock-api');

test('PostChangePasswordEndpoint should handle unauthorized response error', async () => {
mockServer.listen();

const uniqueUserIdentifier = createUniqueUserIdentifierFromString('[email protected]');
const currentPassword = '1234';
const newPassword = '4321';

await expect(
Container.get(PostChangePasswordEndpoint).postChangePassword(uniqueUserIdentifier, currentPassword, newPassword),
).to.eventually.be.rejectedWith(Response401UnauthorizedError);

expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/change-password .')).to
.be.true;

expect(testLogger.assertErrorHappened('Server returned 401 unauthorized.')).to.be.true;

mockServer.close();
});
Loading

0 comments on commit 50597dc

Please sign in to comment.