auth.service.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { TestBed } from '@angular/core/testing';
  2. import { of } from 'rxjs';
  3. import { ApiService } from './api.service';
  4. import { AuthService } from './auth.service';
  5. describe('AuthService', () => {
  6. let auth: AuthService;
  7. let api: jasmine.SpyObj<ApiService>;
  8. beforeEach(() => {
  9. api = jasmine.createSpyObj<ApiService>('ApiService', ['post', 'callCloud']);
  10. TestBed.configureTestingModule({ providers: [AuthService, { provide: ApiService, useValue: api }] });
  11. auth = TestBed.inject(AuthService);
  12. });
  13. it('uses the Parse session token to load the member after password login', () => {
  14. api.post.and.returnValue(of({ retcode: 0, result: { userId: 42, userName: 'tree', sessionToken: 'r:session' } }));
  15. api.callCloud.and.returnValue(of({ retcode: 0, result: { userId: 42, honeyName: '小树' }, addon: { State: 1 } }));
  16. let actual: Record<string, unknown> | undefined;
  17. auth.login('user_login_passwd', { name: 'tree', passwd: 'password' }).subscribe((user) => actual = user);
  18. expect(api.callCloud).toHaveBeenCalledWith('user_get', { uid: 42 }, 'r:session');
  19. expect(actual?.['sessionToken']).toBe('r:session');
  20. expect(actual?.['honeyName']).toBe('小树');
  21. });
  22. it('rejects migrated logins that do not return a real Parse session', () => {
  23. api.post.and.returnValue(of({ retcode: 0, result: { userId: 9, userName: 'legacy' } }));
  24. let message = '';
  25. auth.login('user_login_passwd', { name: 'legacy', passwd: 'password' }).subscribe({
  26. error: (error: Error) => message = error.message,
  27. });
  28. expect(message).toContain('未生成有效会话');
  29. expect(api.callCloud).not.toHaveBeenCalled();
  30. });
  31. });