@tomei/sso 0.65.1-test.1 → 0.66.1-test.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,12 +27,12 @@ describe('GroupReportingUser', () => {
27
27
  jest.clearAllMocks();
28
28
  });
29
29
 
30
-
31
30
  describe('init', () => {
32
31
  const mockDbTransaction = {};
33
32
 
34
33
  it('should initialize GroupReportingUser without GroupReportingUserId', async () => {
35
- const initGroupReportingUser = await GroupReportingUser.init(mockDbTransaction);
34
+ const initGroupReportingUser =
35
+ await GroupReportingUser.init(mockDbTransaction);
36
36
  expect(initGroupReportingUser).toBeDefined();
37
37
  expect(initGroupReportingUser).toBeInstanceOf(GroupReportingUser);
38
38
  });
@@ -47,7 +47,7 @@ describe('GroupReportingUser', () => {
47
47
 
48
48
  const initGroupReportingUser = await GroupReportingUser.init(
49
49
  mockDbTransaction,
50
- '1'
50
+ '1',
51
51
  );
52
52
  expect(initGroupReportingUser).toBeDefined();
53
53
  expect(initGroupReportingUser).toBeInstanceOf(GroupReportingUser);
@@ -57,19 +57,23 @@ describe('GroupReportingUser', () => {
57
57
  });
58
58
 
59
59
  it('should throw ClassError when GroupReportingUser is not found', async () => {
60
- jest.spyOn(GroupReportingUserRepository.prototype, 'findByPk').mockResolvedValue(null);
60
+ jest
61
+ .spyOn(GroupReportingUserRepository.prototype, 'findByPk')
62
+ .mockResolvedValue(null);
61
63
 
62
64
  await expect(
63
- GroupReportingUser.init(mockDbTransaction, '1')
65
+ GroupReportingUser.init(mockDbTransaction, '1'),
64
66
  ).rejects.toThrow(ClassError);
65
67
  });
66
68
 
67
69
  it('should throw ClassError when failed to initialize GroupReportingUser', async () => {
68
- jest.spyOn(GroupReportingUserRepository.prototype, 'findByPk').mockRejectedValue(new Error());
70
+ jest
71
+ .spyOn(GroupReportingUserRepository.prototype, 'findByPk')
72
+ .mockRejectedValue(new Error());
69
73
 
70
74
  await expect(
71
- GroupReportingUser.init(mockDbTransaction, '1')
75
+ GroupReportingUser.init(mockDbTransaction, '1'),
72
76
  ).rejects.toThrow(Error);
73
77
  });
74
78
  });
75
- });
79
+ });
@@ -9,6 +9,7 @@ import { YN } from '../../../../src/enum/yn.enum';
9
9
  import SystemPrivilegeModel from '../../../../src/models/system-privilege.entity';
10
10
  import { GroupSystemAccessRepository } from '../../../../src/components/group-system-access/group-system-access.repository';
11
11
  import { GroupRepository } from '../../../../src/components/group/group.repository';
12
+ import axios from 'axios';
12
13
 
13
14
  const mockUserData = {
14
15
  UserId: 1,
@@ -58,11 +59,19 @@ describe('LoginUser', () => {
58
59
  let sessionService: any;
59
60
  let userId: number;
60
61
  let dbTransaction: any;
62
+ const mockLoginApiEndpoint = {
63
+ baseUrl: 'http://sso-api',
64
+ getProfile: '/get-profile',
65
+ apiKey: 'test-api-key',
66
+ };
61
67
 
62
68
  beforeEach(() => {
63
69
  sessionService = {};
64
70
  userId = 1;
65
71
  dbTransaction = null;
72
+ jest
73
+ .spyOn(ComponentConfig, 'getComponentConfigValue')
74
+ .mockReturnValue(mockLoginApiEndpoint as any);
66
75
  });
67
76
 
68
77
  it('should initialize LoginUser with valid userId', async () => {
@@ -93,9 +102,9 @@ describe('LoginUser', () => {
93
102
  },
94
103
  };
95
104
 
96
- const findOneMock = jest
97
- .spyOn(UserRepository.prototype, 'findOne')
98
- .mockResolvedValueOnce(user as any);
105
+ const axiosPostMock = jest
106
+ .spyOn(axios, 'post')
107
+ .mockResolvedValueOnce({ data: { loginUser: user } } as any);
99
108
 
100
109
  const result = await LoginUser.init(
101
110
  sessionService,
@@ -103,63 +112,29 @@ describe('LoginUser', () => {
103
112
  dbTransaction,
104
113
  );
105
114
 
106
- expect(findOneMock).toHaveBeenCalledTimes(1);
107
- expect(findOneMock).toHaveBeenCalledWith({
108
- where: {
109
- UserId: userId,
110
- },
111
- include: [
112
- {
113
- model: expect.anything(),
114
- },
115
- ],
116
- transaction: dbTransaction,
117
- });
115
+ expect(axiosPostMock).toHaveBeenCalledTimes(1);
116
+ expect(axiosPostMock).toHaveBeenCalledWith(
117
+ `${mockLoginApiEndpoint.baseUrl}${mockLoginApiEndpoint.getProfile}`,
118
+ { UserId: userId },
119
+ { headers: { 'Content-Type': 'application/json', 'x-api-key': mockLoginApiEndpoint.apiKey } },
120
+ );
118
121
  expect(result).toBeInstanceOf(LoginUser);
119
122
  expect(result.UserId).toBe(user.UserId);
120
123
  expect(result.FullName).toBe(user.FullName);
121
124
  expect(result.Email).toBe(user.Email);
122
- expect(result.Password).toBe(user.Password);
123
125
  expect(result.Status).toBe(user.Status);
124
- expect(result.DefaultPasswordChangedYN).toBe(
125
- user.DefaultPasswordChangedYN,
126
- );
127
- expect(result.FirstLoginAt).toBe(user.FirstLoginAt);
128
- expect(result.LastLoginAt).toBe(user.LastLoginAt);
129
- expect(result.MFAEnabled).toBe(user.MFAEnabled);
130
- expect(result.MFAConfig).toBe(user.MFAConfig);
131
- expect(result.RecoveryEmail).toBe(user.RecoveryEmail);
132
- expect(result.FailedLoginAttemptCount).toBe(user.FailedLoginAttemptCount);
133
- expect(result.LastFailedLoginAt).toBe(user.LastFailedLoginAt);
134
- expect(result.LastPasswordChangedAt).toBe(user.LastPasswordChangedAt);
135
- expect(result.NeedToChangePasswordYN).toBe(user.NeedToChangePasswordYN);
136
- expect(result.CreatedById).toBe(user.CreatedById);
137
- expect(result.CreatedAt).toBe(user.CreatedAt);
138
- expect(result.UpdatedById).toBe(user.UpdatedById);
139
- expect(result.UpdatedAt).toBe(user.UpdatedAt);
140
126
  });
141
127
 
142
128
  it('should throw an error when user is not found', async () => {
143
- const findOneMock = jest
144
- .spyOn(UserRepository.prototype, 'findOne')
145
- .mockResolvedValueOnce(null);
129
+ const axiosPostMock = jest
130
+ .spyOn(axios, 'post')
131
+ .mockResolvedValueOnce({ data: { loginUser: null } } as any);
146
132
 
147
133
  await expect(
148
134
  LoginUser.init(sessionService, userId, dbTransaction),
149
135
  ).rejects.toThrow(Error);
150
136
 
151
- expect(findOneMock).toHaveBeenCalledTimes(1);
152
- expect(findOneMock).toHaveBeenCalledWith({
153
- where: {
154
- UserId: userId,
155
- },
156
- include: [
157
- {
158
- model: expect.anything(),
159
- },
160
- ],
161
- transaction: dbTransaction,
162
- });
137
+ expect(axiosPostMock).toHaveBeenCalledTimes(1);
163
138
  });
164
139
  });
165
140
 
@@ -889,10 +864,19 @@ describe('LoginUser', () => {
889
864
  });
890
865
 
891
866
  describe('checkPrivileges', () => {
867
+ const mockLoginApiEndpoint = {
868
+ baseUrl: 'http://sso-api',
869
+ checkPrivileges: '/check-privileges',
870
+ apiKey: 'test-api-key',
871
+ };
872
+
892
873
  beforeEach(() => {
893
874
  jest
894
875
  .spyOn(ApplicationConfig, 'getComponentConfigValue')
895
876
  .mockReturnValue('test-session' as any);
877
+ jest
878
+ .spyOn(ComponentConfig, 'getComponentConfigValue')
879
+ .mockReturnValue(mockLoginApiEndpoint as any);
896
880
  });
897
881
 
898
882
  it('should return true if user has the specified privilege', async () => {
@@ -903,19 +887,10 @@ describe('LoginUser', () => {
903
887
  const loginUser = await LoginUser.init(sessionService);
904
888
  loginUser.ObjectId = '1';
905
889
 
906
- // Mock the necessary methods
907
- const rus = jest
908
- .spyOn(SessionService.prototype, 'retrieveUserSession')
909
- .mockResolvedValueOnce({
910
- systemLogins: [
911
- {
912
- id: '1',
913
- sessionId: 'sessionId',
914
- code: systemCode,
915
- privileges: [privilegeName],
916
- },
917
- ],
918
- });
890
+ const axiosPostMock = jest
891
+ .spyOn(axios, 'post')
892
+ .mockResolvedValueOnce({ data: { hasPrivilege: true } } as any);
893
+
919
894
  // Act
920
895
  const hasPrivilege = await loginUser.checkPrivileges(
921
896
  systemCode,
@@ -924,7 +899,11 @@ describe('LoginUser', () => {
924
899
 
925
900
  // Assert
926
901
  expect(hasPrivilege).toBe(true);
927
- expect(rus).toHaveBeenCalledWith(loginUser.ObjectId, 'test-session');
902
+ expect(axiosPostMock).toHaveBeenCalledWith(
903
+ `${mockLoginApiEndpoint.baseUrl}${mockLoginApiEndpoint.checkPrivileges}`,
904
+ { SystemCode: systemCode, SessionName: 'test-session', PrivilegeName: privilegeName, UserId: '1' },
905
+ { headers: { 'Content-Type': 'application/json', 'x-api-key': mockLoginApiEndpoint.apiKey } },
906
+ );
928
907
  });
929
908
 
930
909
  it('should return false if user does not have the specified privilege', async () => {
@@ -935,19 +914,10 @@ describe('LoginUser', () => {
935
914
  const loginUser = await LoginUser.init(sessionService);
936
915
  loginUser.ObjectId = '1';
937
916
 
938
- // Mock the necessary methods
939
- const rus = jest
940
- .spyOn(SessionService.prototype, 'retrieveUserSession')
941
- .mockResolvedValueOnce({
942
- systemLogins: [
943
- {
944
- id: '1',
945
- sessionId: 'sessionId',
946
- code: systemCode,
947
- privileges: [],
948
- },
949
- ],
950
- });
917
+ const axiosPostMock = jest
918
+ .spyOn(axios, 'post')
919
+ .mockResolvedValueOnce({ data: { hasPrivilege: false } } as any);
920
+
951
921
  // Act
952
922
  const hasPrivilege = await loginUser.checkPrivileges(
953
923
  systemCode,
@@ -956,7 +926,6 @@ describe('LoginUser', () => {
956
926
 
957
927
  // Assert
958
928
  expect(hasPrivilege).toBe(false);
959
- expect(rus).toHaveBeenCalledWith(loginUser.ObjectId, 'test-session');
960
929
  });
961
930
 
962
931
  it('should throw an error if ObjectId is not set', async () => {