diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fa7d5a..9533907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Endpoint/User/PostRegisterEndpoint.ts b/src/Endpoint/User/PostRegisterEndpoint.ts index 538f8c4..c58afa5 100644 --- a/src/Endpoint/User/PostRegisterEndpoint.ts +++ b/src/Endpoint/User/PostRegisterEndpoint.ts @@ -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; diff --git a/test/Unit/Endpoint/Element/DeleteElementEndpoint/DeleteElementEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/DeleteElementEndpoint/DeleteElementEndpointUnauthorized401Response.test.ts index 0202417..012156c 100644 --- a/test/Unit/Endpoint/Element/DeleteElementEndpoint/DeleteElementEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/DeleteElementEndpoint/DeleteElementEndpointUnauthorized401Response.test.ts @@ -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); diff --git a/test/Unit/Endpoint/Element/GetElementChildrenEndpoint/GetElementChildrenEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/GetElementChildrenEndpoint/GetElementChildrenEndpointUnauthorized401Response.test.ts index ec58ae6..ac8bc18 100644 --- a/test/Unit/Endpoint/Element/GetElementChildrenEndpoint/GetElementChildrenEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/GetElementChildrenEndpoint/GetElementChildrenEndpointUnauthorized401Response.test.ts @@ -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); diff --git a/test/Unit/Endpoint/Element/GetElementEndpoint/GetElementEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/GetElementEndpoint/GetElementEndpointUnauthorized401Response.test.ts index 3d3d091..a1ef3e5 100644 --- a/test/Unit/Endpoint/Element/GetElementEndpoint/GetElementEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/GetElementEndpoint/GetElementEndpointUnauthorized401Response.test.ts @@ -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'); diff --git a/test/Unit/Endpoint/Element/GetElementParentsEndpoint/GetElementParentsEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/GetElementParentsEndpoint/GetElementParentsEndpointUnauthorized401Response.test.ts index 9cbbc41..36f50a5 100644 --- a/test/Unit/Endpoint/Element/GetElementParentsEndpoint/GetElementParentsEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/GetElementParentsEndpoint/GetElementParentsEndpointUnauthorized401Response.test.ts @@ -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'); diff --git a/test/Unit/Endpoint/Element/GetElementRelatedEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/GetElementRelatedEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts index 5bbd9fe..ac092b4 100644 --- a/test/Unit/Endpoint/Element/GetElementRelatedEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/GetElementRelatedEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts @@ -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'); diff --git a/test/Unit/Endpoint/Element/GetIndexEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/GetIndexEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts index 6e1f5a7..786909a 100644 --- a/test/Unit/Endpoint/Element/GetIndexEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/GetIndexEndpoint/GetElementRelatedEndpointUnauthorized401Response.test.ts @@ -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); diff --git a/test/Unit/Endpoint/Element/PatchElementEndpoint/PatchElementEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/PatchElementEndpoint/PatchElementEndpointUnauthorized401Response.test.ts index ff59157..4d34639 100644 --- a/test/Unit/Endpoint/Element/PatchElementEndpoint/PatchElementEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/PatchElementEndpoint/PatchElementEndpointUnauthorized401Response.test.ts @@ -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 = { diff --git a/test/Unit/Endpoint/Element/PostElementEndpoint/PostElementEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/Element/PostElementEndpoint/PostElementEndpointUnauthorized401Response.test.ts index 987075f..4fd7c0a 100644 --- a/test/Unit/Endpoint/Element/PostElementEndpoint/PostElementEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/Element/PostElementEndpoint/PostElementEndpointUnauthorized401Response.test.ts @@ -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 = { diff --git a/test/Unit/Endpoint/User/DeleteTokenEndpoint/DeleteTokenEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/User/DeleteTokenEndpoint/DeleteTokenEndpointUnauthorized401Response.test.ts index 365a991..6db8292 100644 --- a/test/Unit/Endpoint/User/DeleteTokenEndpoint/DeleteTokenEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/User/DeleteTokenEndpoint/DeleteTokenEndpointUnauthorized401Response.test.ts @@ -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, diff --git a/test/Unit/Endpoint/User/GetMeEndpoint/GetMeEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/User/GetMeEndpoint/GetMeEndpointUnauthorized401Response.test.ts index d96242d..297e5c6 100644 --- a/test/Unit/Endpoint/User/GetMeEndpoint/GetMeEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/User/GetMeEndpoint/GetMeEndpointUnauthorized401Response.test.ts @@ -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); diff --git a/test/Unit/Endpoint/User/GetTokenEndpoint/GetTokenEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/User/GetTokenEndpoint/GetTokenEndpointUnauthorized401Response.test.ts index c45773b..2cdaadd 100644 --- a/test/Unit/Endpoint/User/GetTokenEndpoint/GetTokenEndpointUnauthorized401Response.test.ts +++ b/test/Unit/Endpoint/User/GetTokenEndpoint/GetTokenEndpointUnauthorized401Response.test.ts @@ -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); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpoint204.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpoint204.test.ts new file mode 100644 index 0000000..41a8b34 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpoint204.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointBadResponseError.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointBadResponseError.test.ts new file mode 100644 index 0000000..775f6b8 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointBadResponseError.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNetworkError.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNetworkError.test.ts new file mode 100644 index 0000000..e3f0529 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNetworkError.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNoContentTypeResponseError.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNoContentTypeResponseError.test.ts new file mode 100644 index 0000000..7cea366 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointNoContentTypeResponseError.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointTooManyRequests429Response.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointTooManyRequests429Response.test.ts new file mode 100644 index 0000000..3d77677 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointTooManyRequests429Response.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointUnauthorized401Response.test.ts new file mode 100644 index 0000000..3538517 --- /dev/null +++ b/test/Unit/Endpoint/User/PostChangePasswordEndpoint/PostChangePasswordEndpointUnauthorized401Response.test.ts @@ -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('test@localhost.dev'); + 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(); +}); diff --git a/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpoint201.test.ts b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpoint201.test.ts new file mode 100644 index 0000000..0bd7acf --- /dev/null +++ b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpoint201.test.ts @@ -0,0 +1,40 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostRegisterEndpoint } 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/register', () => { + return new HttpResponse(null, { + status: 201, + headers: { + Location: '/a5f95955-1d24-43db-8832-f365e6a96dfa', + }, + }); + }), +); + +const testLogger: TestLogger = new TestLogger(); +Container.set(Logger, testLogger); +Container.get(WebSdkConfiguration).setApiHost('http://mock-api'); + +test('PostRegisterEndpoint should handle node response', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + const userUuid = await Container.get(PostRegisterEndpoint).postRegister(uniqueUserIdentifier, password); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/register .')).to.be + .true; + + expect(userUuid as string).to.equal('a5f95955-1d24-43db-8832-f365e6a96dfa'); + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointBadResponseError.test.ts b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointBadResponseError.test.ts new file mode 100644 index 0000000..e0ee889 --- /dev/null +++ b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointBadResponseError.test.ts @@ -0,0 +1,44 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostRegisterEndpoint } 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/register', () => { + 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('PostRegisterEndpoint should handle bad response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostRegisterEndpoint).postRegister(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(ParseError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/register .')).to.be + .true; + + expect(testLogger.assertErrorHappened("Unable to parse response as content type is not 'application/problem+json'.")) + .to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNetworkError.test.ts b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNetworkError.test.ts new file mode 100644 index 0000000..88c98a4 --- /dev/null +++ b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNetworkError.test.ts @@ -0,0 +1,38 @@ +import { expect } from 'chai'; +import { http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostRegisterEndpoint } 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/register', () => { + return Response.error(); + }), +); + +const testLogger: TestLogger = new TestLogger(); +Container.set(Logger, testLogger); +Container.get(WebSdkConfiguration).setApiHost('http://mock-api'); + +test('PostRegisterEndpoint should handle network error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostRegisterEndpoint).postRegister(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(NetworkError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/register .')).to.be + .true; + + expect(testLogger.assertErrorHappened('Experienced generic network error during creating resource.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNoContentTypeResponseError.test.ts b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNoContentTypeResponseError.test.ts new file mode 100644 index 0000000..259768f --- /dev/null +++ b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointNoContentTypeResponseError.test.ts @@ -0,0 +1,42 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostRegisterEndpoint } 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/register', () => { + 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('PostRegisterEndpoint should handle no content type response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostRegisterEndpoint).postRegister(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(ParseError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/register .')).to.be + .true; + + expect(testLogger.assertErrorHappened('Response does not contain content type header.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointTooManyRequests429Response.test.ts b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointTooManyRequests429Response.test.ts new file mode 100644 index 0000000..e7d3c74 --- /dev/null +++ b/test/Unit/Endpoint/User/PostRegisterEndpoint/PostRegisterEndpointTooManyRequests429Response.test.ts @@ -0,0 +1,51 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostRegisterEndpoint } 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/register', () => { + 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('PostRegisterEndpoint should handle bad response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostRegisterEndpoint).postRegister(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(Response429TooManyRequestsError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/register .')).to.be + .true; + + expect(testLogger.assertErrorHappened('Server returned 429 too many requests.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpoint200.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpoint200.test.ts new file mode 100644 index 0000000..a88b192 --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpoint200.test.ts @@ -0,0 +1,44 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + return HttpResponse.json( + { + type: '_TokenResponse', + token: 'secret-token:ERgAAnWl0CY8bQs0m11nZ3', + }, + { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8', + }, + }, + ); + }), +); + +const testLogger: TestLogger = new TestLogger(); +Container.set(Logger, testLogger); +Container.get(WebSdkConfiguration).setApiHost('http://mock-api'); + +test('PostTokenEndpoint should handle node response', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + const token = await Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect(token as string).to.equal('secret-token:ERgAAnWl0CY8bQs0m11nZ3'); + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointBadResponseError.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointBadResponseError.test.ts new file mode 100644 index 0000000..2fe2605 --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointBadResponseError.test.ts @@ -0,0 +1,46 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + 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('PostTokenEndpoint should handle bad response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(ParseError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect( + testLogger.assertErrorHappened( + "Unable to parse response as content type is neither 'application/json' nor 'application/problem+json'.", + ), + ).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNetworkError.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNetworkError.test.ts new file mode 100644 index 0000000..7557d6b --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNetworkError.test.ts @@ -0,0 +1,37 @@ +import { expect } from 'chai'; +import { http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + return Response.error(); + }), +); + +const testLogger: TestLogger = new TestLogger(); +Container.set(Logger, testLogger); +Container.get(WebSdkConfiguration).setApiHost('http://mock-api'); + +test('PostTokenEndpoint should handle network error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(NetworkError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect(testLogger.assertErrorHappened('Experienced generic network error during creating resource.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNoContentTypeResponseError.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNoContentTypeResponseError.test.ts new file mode 100644 index 0000000..7be9693 --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointNoContentTypeResponseError.test.ts @@ -0,0 +1,41 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + 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('PostTokenEndpoint should handle no content type response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(ParseError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect(testLogger.assertErrorHappened('Response does not contain content type header.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointTooManyRequests429Response.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointTooManyRequests429Response.test.ts new file mode 100644 index 0000000..3762c8a --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointTooManyRequests429Response.test.ts @@ -0,0 +1,50 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + 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('PostTokenEndpoint should handle bad response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(Response429TooManyRequestsError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect(testLogger.assertErrorHappened('Server returned 429 too many requests.')).to.be.true; + + mockServer.close(); +}); diff --git a/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointUnauthorized401Response.test.ts b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointUnauthorized401Response.test.ts new file mode 100644 index 0000000..7d9f15e --- /dev/null +++ b/test/Unit/Endpoint/User/PostTokenEndpoint/PostTokenEndpointUnauthorized401Response.test.ts @@ -0,0 +1,51 @@ +import { expect } from 'chai'; +import { HttpResponse, http } from 'msw'; +import { setupServer } from 'msw/node'; +import { Container } from 'typedi'; + +import { PostTokenEndpoint } 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/token', () => { + 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('PostTokenEndpoint should handle unauthorized response error', async () => { + mockServer.listen(); + + const uniqueUserIdentifier = createUniqueUserIdentifierFromString('test@localhost.dev'); + const password = '1234'; + + await expect( + Container.get(PostTokenEndpoint).postToken(uniqueUserIdentifier, password), + ).to.eventually.be.rejectedWith(Response401UnauthorizedError); + + expect(testLogger.assertDebugHappened('Executing HTTP POST request against url http://mock-api/token .')).to.be.true; + + expect(testLogger.assertErrorHappened('Server returned 401 unauthorized.')).to.be.true; + + mockServer.close(); +});