@umituz/react-native-auth 3.4.22 → 3.4.24

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.
@@ -1,247 +0,0 @@
1
- /**
2
- * AuthCoreService Tests
3
- */
4
-
5
- import { AuthCoreService } from '../../../src/infrastructure/services/AuthCoreService';
6
- import { DEFAULT_AUTH_CONFIG } from '../../../src/domain/value-objects/AuthConfig';
7
- import type { IAuthProvider } from '../../../src/application/ports/IAuthProvider';
8
- import type { AuthUser } from '../../../src/domain/entities/AuthUser';
9
-
10
- describe('AuthCoreService', () => {
11
- let authCoreService: AuthCoreService;
12
- let mockProvider: jest.Mocked<IAuthProvider>;
13
-
14
- beforeEach(() => {
15
- mockProvider = {
16
- initialize: jest.fn(),
17
- isInitialized: jest.fn().mockReturnValue(true),
18
- signIn: jest.fn(),
19
- signUp: jest.fn(),
20
- signOut: jest.fn(),
21
- getCurrentUser: jest.fn(),
22
- onAuthStateChange: jest.fn().mockReturnValue(jest.fn()),
23
- };
24
-
25
- authCoreService = new AuthCoreService(DEFAULT_AUTH_CONFIG);
26
- });
27
-
28
- describe('constructor', () => {
29
- it('should initialize with provided config', () => {
30
- const customConfig = {
31
- ...DEFAULT_AUTH_CONFIG,
32
- password: {
33
- ...DEFAULT_AUTH_CONFIG.password,
34
- minLength: 12,
35
- },
36
- };
37
-
38
- const service = new AuthCoreService(customConfig);
39
- expect(service.getConfig()).toEqual(customConfig);
40
- });
41
- });
42
-
43
- describe('initialize', () => {
44
- it('should initialize with IAuthProvider', async () => {
45
- await authCoreService.initialize(mockProvider);
46
- expect(mockProvider.initialize).toHaveBeenCalled();
47
- });
48
-
49
- it('should initialize with Firebase Auth instance', async () => {
50
- const mockFirebaseAuth = {
51
- currentUser: null,
52
- } as any;
53
-
54
- await expect(authCoreService.initialize(mockFirebaseAuth)).rejects.toThrow();
55
- });
56
-
57
- it('should throw error when no provider provided', async () => {
58
- await expect(authCoreService.initialize(null as any)).rejects.toThrow(
59
- 'Auth provider or Firebase Auth instance is required'
60
- );
61
- });
62
- });
63
-
64
- describe('isInitialized', () => {
65
- it('should return false when not initialized', () => {
66
- expect(authCoreService.isInitialized()).toBe(false);
67
- });
68
-
69
- it('should return true when initialized', async () => {
70
- await authCoreService.initialize(mockProvider);
71
- expect(authCoreService.isInitialized()).toBe(true);
72
- });
73
- });
74
-
75
- describe('signUp', () => {
76
- const mockUser: AuthUser = {
77
- uid: 'test-uid',
78
- email: 'test@example.com',
79
- displayName: 'Test User',
80
- isAnonymous: false,
81
- emailVerified: false,
82
- photoURL: null,
83
- };
84
-
85
- beforeEach(async () => {
86
- await authCoreService.initialize(mockProvider);
87
- });
88
-
89
- it('should sign up successfully with valid credentials', async () => {
90
- mockProvider.signUp.mockResolvedValue(mockUser);
91
-
92
- const result = await authCoreService.signUp({
93
- email: 'test@example.com',
94
- password: 'password123',
95
- displayName: 'Test User',
96
- });
97
-
98
- expect(result).toEqual(mockUser);
99
- expect(mockProvider.signUp).toHaveBeenCalledWith({
100
- email: 'test@example.com',
101
- password: 'password123',
102
- displayName: 'Test User',
103
- });
104
- });
105
-
106
- it('should sign up without display name', async () => {
107
- mockProvider.signUp.mockResolvedValue(mockUser);
108
-
109
- await authCoreService.signUp({
110
- email: 'test@example.com',
111
- password: 'password123',
112
- });
113
-
114
- expect(mockProvider.signUp).toHaveBeenCalledWith({
115
- email: 'test@example.com',
116
- password: 'password123',
117
- displayName: undefined,
118
- });
119
- });
120
-
121
- it('should throw error when not initialized', async () => {
122
- const uninitializedService = new AuthCoreService(DEFAULT_AUTH_CONFIG);
123
-
124
- await expect(uninitializedService.signUp({
125
- email: 'test@example.com',
126
- password: 'password123',
127
- })).rejects.toThrow('Auth service is not initialized');
128
- });
129
- });
130
-
131
- describe('signIn', () => {
132
- const mockUser: AuthUser = {
133
- uid: 'test-uid',
134
- email: 'test@example.com',
135
- displayName: 'Test User',
136
- isAnonymous: false,
137
- emailVerified: false,
138
- photoURL: null,
139
- };
140
-
141
- beforeEach(async () => {
142
- await authCoreService.initialize(mockProvider);
143
- });
144
-
145
- it('should sign in successfully with valid credentials', async () => {
146
- mockProvider.signIn.mockResolvedValue(mockUser);
147
-
148
- const result = await authCoreService.signIn({
149
- email: 'test@example.com',
150
- password: 'password123',
151
- });
152
-
153
- expect(result).toEqual(mockUser);
154
- expect(mockProvider.signIn).toHaveBeenCalledWith({
155
- email: 'test@example.com',
156
- password: 'password123',
157
- });
158
- });
159
-
160
- it('should throw error when not initialized', async () => {
161
- const uninitializedService = new AuthCoreService(DEFAULT_AUTH_CONFIG);
162
-
163
- await expect(uninitializedService.signIn({
164
- email: 'test@example.com',
165
- password: 'password123',
166
- })).rejects.toThrow('Auth service is not initialized');
167
- });
168
- });
169
-
170
- describe('signOut', () => {
171
- beforeEach(async () => {
172
- await authCoreService.initialize(mockProvider);
173
- });
174
-
175
- it('should sign out successfully', async () => {
176
- await authCoreService.signOut();
177
- expect(mockProvider.signOut).toHaveBeenCalled();
178
- });
179
-
180
- it('should handle sign out when not initialized', async () => {
181
- const uninitializedService = new AuthCoreService(DEFAULT_AUTH_CONFIG);
182
-
183
- await expect(uninitializedService.signOut()).resolves.not.toThrow();
184
- });
185
- });
186
-
187
- describe('getCurrentUser', () => {
188
- const mockUser: AuthUser = {
189
- uid: 'test-uid',
190
- email: 'test@example.com',
191
- displayName: 'Test User',
192
- isAnonymous: false,
193
- emailVerified: false,
194
- photoURL: null,
195
- };
196
-
197
- it('should return null when not initialized', () => {
198
- const result = authCoreService.getCurrentUser();
199
- expect(result).toBeNull();
200
- });
201
-
202
- it('should return current user when initialized', async () => {
203
- mockProvider.getCurrentUser.mockReturnValue(mockUser);
204
- await authCoreService.initialize(mockProvider);
205
-
206
- const result = authCoreService.getCurrentUser();
207
- expect(result).toEqual(mockUser);
208
- });
209
-
210
- it('should return null when no current user', async () => {
211
- mockProvider.getCurrentUser.mockReturnValue(null);
212
- await authCoreService.initialize(mockProvider);
213
-
214
- const result = authCoreService.getCurrentUser();
215
- expect(result).toBeNull();
216
- });
217
- });
218
-
219
- describe('onAuthStateChange', () => {
220
- it('should return cleanup function when not initialized', () => {
221
- const callback = jest.fn();
222
- const cleanup = authCoreService.onAuthStateChange(callback);
223
-
224
- expect(callback).toHaveBeenCalledWith(null);
225
- expect(typeof cleanup).toBe('function');
226
- });
227
-
228
- it('should subscribe to auth state changes when initialized', async () => {
229
- const callback = jest.fn();
230
- const mockCleanup = jest.fn();
231
- mockProvider.onAuthStateChange.mockReturnValue(mockCleanup);
232
-
233
- await authCoreService.initialize(mockProvider);
234
- const cleanup = authCoreService.onAuthStateChange(callback);
235
-
236
- expect(mockProvider.onAuthStateChange).toHaveBeenCalledWith(callback);
237
- expect(cleanup).toBe(mockCleanup);
238
- });
239
- });
240
-
241
- describe('getConfig', () => {
242
- it('should return the current config', () => {
243
- const config = authCoreService.getConfig();
244
- expect(config).toEqual(DEFAULT_AUTH_CONFIG);
245
- });
246
- });
247
- });
@@ -1,270 +0,0 @@
1
- /**
2
- * AuthValidation Tests
3
- */
4
-
5
- import {
6
- validateEmail,
7
- validatePasswordForLogin,
8
- validatePasswordForRegister,
9
- validatePasswordConfirmation,
10
- validateDisplayName,
11
- } from '../../../src/infrastructure/utils/AuthValidation';
12
- import { DEFAULT_PASSWORD_CONFIG } from '../../../src/domain/value-objects/AuthConfig';
13
- import { initializeAuthPackage } from '../../../src/infrastructure/services/AuthPackage';
14
-
15
- describe('AuthValidation', () => {
16
- beforeEach(() => {
17
- // Reset package config before each test
18
- initializeAuthPackage();
19
- });
20
-
21
- describe('validateEmail', () => {
22
- it('should reject empty email', () => {
23
- const result = validateEmail('');
24
- expect(result.isValid).toBe(false);
25
- expect(result.error).toBe('Email is required');
26
- });
27
-
28
- it('should reject whitespace-only email', () => {
29
- const result = validateEmail(' ');
30
- expect(result.isValid).toBe(false);
31
- expect(result.error).toBe('Email is required');
32
- });
33
-
34
- it('should reject invalid email format', () => {
35
- const result = validateEmail('invalid-email');
36
- expect(result.isValid).toBe(false);
37
- expect(result.error).toBe('Please enter a valid email address');
38
- });
39
-
40
- it('should accept valid email format', () => {
41
- const result = validateEmail('test@example.com');
42
- expect(result.isValid).toBe(true);
43
- expect(result.error).toBeUndefined();
44
- });
45
-
46
- it('should accept valid email with subdomain', () => {
47
- const result = validateEmail('test@mail.example.com');
48
- expect(result.isValid).toBe(true);
49
- expect(result.error).toBeUndefined();
50
- });
51
-
52
- it('should trim whitespace', () => {
53
- const result = validateEmail(' test@example.com ');
54
- expect(result.isValid).toBe(true);
55
- expect(result.error).toBeUndefined();
56
- });
57
- });
58
-
59
- describe('validatePasswordForLogin', () => {
60
- it('should reject empty password', () => {
61
- const result = validatePasswordForLogin('');
62
- expect(result.isValid).toBe(false);
63
- expect(result.error).toBe('Password is required');
64
- });
65
-
66
- it('should accept any non-empty password', () => {
67
- const result = validatePasswordForLogin('any');
68
- expect(result.isValid).toBe(true);
69
- expect(result.error).toBeUndefined();
70
- });
71
-
72
- it('should accept password with spaces', () => {
73
- const result = validatePasswordForLogin(' password ');
74
- expect(result.isValid).toBe(true);
75
- expect(result.error).toBeUndefined();
76
- });
77
- });
78
-
79
- describe('validatePasswordForRegister', () => {
80
- const config = DEFAULT_PASSWORD_CONFIG;
81
-
82
- it('should reject empty password', () => {
83
- const result = validatePasswordForRegister('', config);
84
- expect(result.isValid).toBe(false);
85
- expect(result.error).toBe('Password is required');
86
- expect(result.requirements.hasMinLength).toBe(false);
87
- });
88
-
89
- it('should reject password that is too short', () => {
90
- const result = validatePasswordForRegister('123', config);
91
- expect(result.isValid).toBe(false);
92
- expect(result.error).toBe(`Password must be at least ${config.minLength} characters`);
93
- expect(result.requirements.hasMinLength).toBe(false);
94
- });
95
-
96
- it('should accept password that meets minimum length', () => {
97
- const result = validatePasswordForRegister('12345678', config);
98
- expect(result.requirements.hasMinLength).toBe(true);
99
- });
100
-
101
- it('should validate uppercase requirement', () => {
102
- const configWithUppercase = {
103
- ...config,
104
- requireUppercase: true,
105
- };
106
-
107
- const result = validatePasswordForRegister('password', configWithUppercase);
108
- expect(result.isValid).toBe(false);
109
- expect(result.error).toBe('Password must contain at least one uppercase letter');
110
- expect(result.requirements.hasUppercase).toBe(false);
111
-
112
- const validResult = validatePasswordForRegister('Password', configWithUppercase);
113
- expect(validResult.requirements.hasUppercase).toBe(true);
114
- });
115
-
116
- it('should validate lowercase requirement', () => {
117
- const configWithLowercase = {
118
- ...config,
119
- requireLowercase: true,
120
- };
121
-
122
- const result = validatePasswordForRegister('PASSWORD', configWithLowercase);
123
- expect(result.isValid).toBe(false);
124
- expect(result.error).toBe('Password must contain at least one lowercase letter');
125
- expect(result.requirements.hasLowercase).toBe(false);
126
-
127
- const validResult = validatePasswordForRegister('Password', configWithLowercase);
128
- expect(validResult.requirements.hasLowercase).toBe(true);
129
- });
130
-
131
- it('should validate number requirement', () => {
132
- const configWithNumber = {
133
- ...config,
134
- requireNumber: true,
135
- };
136
-
137
- const result = validatePasswordForRegister('Password', configWithNumber);
138
- expect(result.isValid).toBe(false);
139
- expect(result.error).toBe('Password must contain at least one number');
140
- expect(result.requirements.hasNumber).toBe(false);
141
-
142
- const validResult = validatePasswordForRegister('Password1', configWithNumber);
143
- expect(validResult.requirements.hasNumber).toBe(true);
144
- });
145
-
146
- it('should validate special character requirement', () => {
147
- const configWithSpecial = {
148
- ...config,
149
- requireSpecialChar: true,
150
- };
151
-
152
- const result = validatePasswordForRegister('Password1', configWithSpecial);
153
- expect(result.isValid).toBe(false);
154
- expect(result.error).toBe('Password must contain at least one special character');
155
- expect(result.requirements.hasSpecialChar).toBe(false);
156
-
157
- const validResult = validatePasswordForRegister('Password1!', configWithSpecial);
158
- expect(validResult.requirements.hasSpecialChar).toBe(true);
159
- });
160
-
161
- it('should accept password that meets all requirements', () => {
162
- const strictConfig = {
163
- ...config,
164
- requireUppercase: true,
165
- requireLowercase: true,
166
- requireNumber: true,
167
- requireSpecialChar: true,
168
- };
169
-
170
- const result = validatePasswordForRegister('Password1!', strictConfig);
171
- expect(result.isValid).toBe(true);
172
- expect(result.requirements).toEqual({
173
- hasMinLength: true,
174
- hasUppercase: true,
175
- hasLowercase: true,
176
- hasNumber: true,
177
- hasSpecialChar: true,
178
- });
179
- });
180
-
181
- it('should skip requirements when disabled', () => {
182
- const lenientConfig = {
183
- ...config,
184
- requireUppercase: false,
185
- requireLowercase: false,
186
- requireNumber: false,
187
- requireSpecialChar: false,
188
- };
189
-
190
- const result = validatePasswordForRegister('password', lenientConfig);
191
- expect(result.requirements).toEqual({
192
- hasMinLength: true,
193
- hasUppercase: true, // Should be true when requirement is disabled
194
- hasLowercase: true, // Should be true when requirement is disabled
195
- hasNumber: true, // Should be true when requirement is disabled
196
- hasSpecialChar: true, // Should be true when requirement is disabled
197
- });
198
- });
199
- });
200
-
201
- describe('validatePasswordConfirmation', () => {
202
- it('should reject empty confirmation', () => {
203
- const result = validatePasswordConfirmation('password', '');
204
- expect(result.isValid).toBe(false);
205
- expect(result.error).toBe('Please confirm your password');
206
- });
207
-
208
- it('should reject mismatched passwords', () => {
209
- const result = validatePasswordConfirmation('password', 'different');
210
- expect(result.isValid).toBe(false);
211
- expect(result.error).toBe('Passwords do not match');
212
- });
213
-
214
- it('should accept matching passwords', () => {
215
- const result = validatePasswordConfirmation('password', 'password');
216
- expect(result.isValid).toBe(true);
217
- expect(result.error).toBeUndefined();
218
- });
219
-
220
- it('should handle empty passwords that match', () => {
221
- const result = validatePasswordConfirmation('', '');
222
- expect(result.isValid).toBe(true);
223
- expect(result.error).toBeUndefined();
224
- });
225
- });
226
-
227
- describe('validateDisplayName', () => {
228
- it('should reject empty name', () => {
229
- const result = validateDisplayName('');
230
- expect(result.isValid).toBe(false);
231
- expect(result.error).toBe('Name is required');
232
- });
233
-
234
- it('should reject whitespace-only name', () => {
235
- const result = validateDisplayName(' ');
236
- expect(result.isValid).toBe(false);
237
- expect(result.error).toBe('Name is required');
238
- });
239
-
240
- it('should reject name that is too short', () => {
241
- const result = validateDisplayName('A');
242
- expect(result.isValid).toBe(false);
243
- expect(result.error).toBe('Name must be at least 2 characters');
244
- });
245
-
246
- it('should accept name that meets minimum length', () => {
247
- const result = validateDisplayName('Al');
248
- expect(result.isValid).toBe(true);
249
- expect(result.error).toBeUndefined();
250
- });
251
-
252
- it('should use custom minimum length', () => {
253
- const result = validateDisplayName('Al', 3);
254
- expect(result.isValid).toBe(false);
255
- expect(result.error).toBe('Name must be at least 3 characters');
256
- });
257
-
258
- it('should trim whitespace', () => {
259
- const result = validateDisplayName(' John Doe ');
260
- expect(result.isValid).toBe(true);
261
- expect(result.error).toBeUndefined();
262
- });
263
-
264
- it('should accept name with special characters', () => {
265
- const result = validateDisplayName('John-O\'Connor');
266
- expect(result.isValid).toBe(true);
267
- expect(result.error).toBeUndefined();
268
- });
269
- });
270
- });