From e79e579e79be8f59bf906a120e71a776d3405a63 Mon Sep 17 00:00:00 2001 From: Dopeamin Date: Tue, 10 Sep 2024 17:39:24 +0200 Subject: [PATCH] resolves comments --- src/services/identifierService.ts | 69 ++----------------- src/services/userService.ts | 18 +---- tests/integration/services/identifier.test.ts | 45 +++--------- tests/integration/services/user.test.ts | 14 +--- tests/utils.ts | 5 +- 5 files changed, 22 insertions(+), 129 deletions(-) diff --git a/src/services/identifierService.ts b/src/services/identifierService.ts index 58faa00..986e5f6 100644 --- a/src/services/identifierService.ts +++ b/src/services/identifierService.ts @@ -17,24 +17,9 @@ export interface IdentifierInterface { create(userId: string, req: IdentifierCreateReq): Promise; delete(userId: string, identifierId: string): Promise; list(sort?: string, filter?: string[], page?: number, pageSize?: number): Promise; - listAllWithPaging(page?: number, pageSize?: number): Promise; listByValueAndType(value: string, type: IdentifierType): Promise; - listByValueAndTypeWithPaging( - value: string, - type: IdentifierType, - sort?: string, - page?: number, - pageSize?: number, - ): Promise; - existsByValueAndType(value: string, type: IdentifierType): Promise; - listAllByUserIdWithPaging(userId: string, page?: number, pageSize?: number): Promise; - listAllByUserIdAndTypeWithPaging( - userId: string, - type: IdentifierType, - page?: number, - pageSize?: number, - ): Promise; - listAllEmailsByUserId(userId: string): Promise; + listByUserId(userId: string, page?: number, pageSize?: number): Promise; + listByUserIdAndType(userId: string, type: IdentifierType, page?: number, pageSize?: number): Promise; updateIdentifier( userId: string, identifierId: string, @@ -119,15 +104,7 @@ class Identifier implements IdentifierInterface { } } - async listAllWithPaging(page?: number, pageSize?: number): Promise { - return this.list(undefined, undefined, page, pageSize); - } - - async listByValueAndType(value: string, type: IdentifierType): Promise { - return this.list(undefined, [`identifierValue:eq:${value}`, `identifierType:eq:${type}`], undefined, undefined); - } - - async listByValueAndTypeWithPaging( + async listByValueAndType( value: string, type: IdentifierType, sort?: string, @@ -137,13 +114,7 @@ class Identifier implements IdentifierInterface { return this.list(sort, [`identifierValue:eq:${value}`, `identifierType:eq:${type}`], page, pageSize); } - async existsByValueAndType(value: string, type: IdentifierType): Promise { - const list = await this.listByValueAndType(value, type); - - return list.identifiers.length > 0; - } - - async listAllByUserIdWithPaging(userId: string, page?: number, pageSize?: number): Promise { + async listByUserId(userId: string, page?: number, pageSize?: number): Promise { let id = userId; // filter queries are using userID without prefix @@ -154,12 +125,7 @@ class Identifier implements IdentifierInterface { return this.list(undefined, [`userID:eq:${id}`], page, pageSize); } - listAllByUserIdAndTypeWithPaging( - userId: string, - type: IdentifierType, - page?: number, - pageSize?: number, - ): Promise { + listByUserIdAndType(userId: string, type: IdentifierType, page?: number, pageSize?: number): Promise { let id = userId; // filter queries are using userID without prefix @@ -170,31 +136,6 @@ class Identifier implements IdentifierInterface { return this.list(undefined, [`userID:eq:${id}`, `identifierType:eq:${type}`], page, pageSize); } - async listAllEmailsByUserId(userId: string): Promise { - const list: IdentifierRsp[] = []; - - const firstRes = await this.listAllByUserIdAndTypeWithPaging(userId, IdentifierType.Email); - - list.push(...firstRes.identifiers); - - const { paging } = firstRes; - - const listPromise: Promise[] = []; - while (paging.page < paging.totalPages) { - const currentPage = paging.page; - paging.page = currentPage + 1; - - const temp = this.listAllByUserIdAndTypeWithPaging(userId, IdentifierType.Email, paging.page); - - listPromise.push(temp); - } - - const listRes = await Promise.all(listPromise); - list.push(...listRes.flatMap((res) => res.identifiers)); - - return list; - } - async updateIdentifier( userId: string, identifierId: string, diff --git a/src/services/userService.ts b/src/services/userService.ts index e27b191..186e59b 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -5,7 +5,6 @@ import { UserCreateReq, UsersApi, User, UserStatus, GenericRsp } from '../genera export interface UserInterface { create(req: UserCreateReq): Promise; - create(fullName: string, status: UserStatus, explicitWebauthnID: string): Promise; createActiveByName(fullName: string): Promise; delete(id: string): Promise; get(id: string): Promise; @@ -19,23 +18,8 @@ class UserService implements UserInterface { this.client = new UsersApi(undefined, '', axios); } - async create(req: UserCreateReq): Promise; - async create(fullName: string, status: UserStatus, explicitWebauthnID: string): Promise; - async create(arg1: UserCreateReq | string, arg2?: UserStatus, arg3?: string): Promise { - if (typeof arg1 === 'string' && arg2 && arg3) { - Assert.notEmptyString(arg1, 'User.create() "fullName" param must not be null'); - - const request: UserCreateReq = { - fullName: arg1, - status: arg2, - explicitWebauthnID: arg3, - }; - - return this.create(request); - } - + async create(req: UserCreateReq): Promise { try { - const req = arg1 as UserCreateReq; Assert.notNull(req, 'User.create() "req" param must not be null'); Assert.notEmptyString(req.fullName ? req.fullName : '', 'User.create() "fullName" param must not be empty'); Assert.notNull(req.status, 'User.create() "status" param must not be null'); diff --git a/tests/integration/services/identifier.test.ts b/tests/integration/services/identifier.test.ts index 4074e31..6ef8db5 100644 --- a/tests/integration/services/identifier.test.ts +++ b/tests/integration/services/identifier.test.ts @@ -4,7 +4,6 @@ import { IdentifierCreateReq, IdentifierStatus, IdentifierType, - IdentifierList, Identifier as IdentifierRsp, } from '../../../src/generated'; import Utils from '../../utils'; @@ -19,7 +18,7 @@ describe('Identifier Service Tests', () => { sdk = Utils.SDK(); // Create a test user and email identifier - TEST_USER_ID = await Utils.createUserId(); + TEST_USER_ID = (await Utils.createUser()).userID; TEST_USER_EMAIL = Utils.createRandomTestEmail(); // Create an email identifier for the user @@ -30,26 +29,10 @@ describe('Identifier Service Tests', () => { }); }); - test('should check if existing email identifier is present', async () => { - // Check if email identifier is present - let ret: IdentifierList = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Email); - expect(ret.identifiers.length).not.toEqual(0); - - // Check if email is listed under phone identifiers (should not be found) - ret = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Phone); - expect(ret.identifiers.length).toEqual(0); - - // Check existence using existsByValueAndType - const emailExists = await sdk.identifiers().existsByValueAndType(TEST_USER_EMAIL, IdentifierType.Email); - const phoneExists = await sdk.identifiers().existsByValueAndType(TEST_USER_EMAIL, IdentifierType.Phone); - expect(emailExists).toBe(true); - expect(phoneExists).toBe(false); - }); - test('should throw error on empty identifier creation', async () => { expect.assertions(3); - const userId = await Utils.createUserId(); + const userId = (await Utils.createUser()).userID; const req: IdentifierCreateReq = { identifierType: IdentifierType.Email, identifierValue: '', // Empty email value @@ -66,7 +49,7 @@ describe('Identifier Service Tests', () => { }); test('should successfully create an identifier', async () => { - const userId = await Utils.createUserId(); + const userId = (await Utils.createUser()).userID; const email = Utils.createRandomTestEmail(); const req: IdentifierCreateReq = { identifierType: IdentifierType.Email, @@ -81,7 +64,7 @@ describe('Identifier Service Tests', () => { }); test('should list all identifiers by userId', async () => { - const ret = await sdk.identifiers().listAllByUserIdWithPaging(TEST_USER_ID); + const ret = await sdk.identifiers().listByUserId(TEST_USER_ID); const identifierExists = ret.identifiers.some( (identifier) => identifier.identifierID === TEST_USER_EMAIL_IDENTIFIER.identifierID, ); @@ -96,19 +79,11 @@ describe('Identifier Service Tests', () => { }); test('should list identifiers by value and type with paging', async () => { - const ret = await sdk - .identifiers() - .listByValueAndTypeWithPaging(TEST_USER_EMAIL, IdentifierType.Email, undefined, 1, 10); + const ret = await sdk.identifiers().listByValueAndType(TEST_USER_EMAIL, IdentifierType.Email, undefined, 1, 10); expect(ret.identifiers.length).toBeGreaterThan(0); expect(ret.identifiers[0].value).toEqual(TEST_USER_EMAIL); }); - test('should list identifiers with paging', async () => { - const ret = await sdk.identifiers().listAllWithPaging(1, 10); - expect(ret.identifiers).toBeDefined(); - expect(ret.identifiers.length).toBeGreaterThan(0); - }); - test('should list all identifiers', async () => { const ret = await sdk.identifiers().list(undefined, undefined, 1, 100); expect(ret).not.toBeNull(); @@ -141,7 +116,7 @@ describe('Identifier Service Tests', () => { }); test('should list all emails by userId', async () => { - const testSize = 11; + const testSize = 3; // Create multiple email identifiers for the same user const promises: Promise[] = []; @@ -158,8 +133,10 @@ describe('Identifier Service Tests', () => { await Promise.all(promises); - const allEmails = await sdk.identifiers().listAllEmailsByUserId(TEST_USER_ID); - expect(allEmails.length).toEqual(testSize + 1); // One email was already created before + const allEmails = await sdk + .identifiers() + .listByUserIdAndType(TEST_USER_ID, IdentifierType.Email, undefined, undefined); + expect(allEmails.identifiers.length).toEqual(testSize + 1); // One email was already created before }); test('should successfully delete an identifier', async () => { @@ -215,7 +192,7 @@ describe('Identifier Service Tests', () => { test('should fail to create identifier with invalid type', async () => { expect.assertions(2); - const userId = await Utils.createUserId(); + const userId = (await Utils.createUser()).userID; const req: IdentifierCreateReq = { identifierType: 'invalid_type' as IdentifierType, // Invalid identifier type identifierValue: Utils.createRandomTestEmail(), diff --git a/tests/integration/services/user.test.ts b/tests/integration/services/user.test.ts index 12820cb..9dd9392 100644 --- a/tests/integration/services/user.test.ts +++ b/tests/integration/services/user.test.ts @@ -32,16 +32,6 @@ describe('User Validation Tests', () => { expect(sendResponse.fullName).toEqual(req.fullName); }); - test('should handle successful create using fullName and userStatus', async () => { - expect.assertions(2); - - const fullName = Utils.createRandomTestName(); - const sendResponse = await sdk.users().create(fullName, UserStatus.Active, Utils.generateString(10)); - - expect(sendResponse.fullName).toEqual(fullName); - expect(sendResponse.status).toEqual(UserStatus.Active); - }); - test('should handle not found delete', async () => { expect.assertions(3); try { @@ -56,7 +46,7 @@ describe('User Validation Tests', () => { test('should handle successful delete', async () => { expect.assertions(2); - const userId = await Utils.createUserId(); + const userId = (await Utils.createUser()).userID; await sdk.users().delete(userId); @@ -82,7 +72,7 @@ describe('User Validation Tests', () => { test('should handle successful get', async () => { expect.assertions(1); - const userId = await Utils.createUserId(); + const userId = (await Utils.createUser()).userID; const getResponse = await sdk.users().get(userId); expect(getResponse.userID).toEqual(userId); }); diff --git a/tests/utils.ts b/tests/utils.ts index d069719..3374aa3 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -2,6 +2,7 @@ import AxiosMockAdapter from 'axios-mock-adapter'; import axios, { AxiosInstance } from 'axios'; import { SDK, Config } from '../src'; import { BaseError, httpStatusCodes } from '../src/errors'; +import { User } from '../src/generated'; class Utils { public static SDK(): SDK { @@ -88,10 +89,10 @@ class Utils { return `+491509${this.generateNumber(7)}`; } - public static async createUserId(): Promise { + public static async createUser(): Promise { const rsp = await this.SDK().users().createActiveByName(this.createRandomTestName()); - return rsp.userID; + return rsp; } public static testConstants = {