| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import { TestBed } from '@angular/core/testing';
- import { of } from 'rxjs';
- import { ApiService } from './api.service';
- import { AuthService } from './auth.service';
- describe('AuthService', () => {
- let auth: AuthService;
- let api: jasmine.SpyObj<ApiService>;
- beforeEach(() => {
- api = jasmine.createSpyObj<ApiService>('ApiService', ['post', 'callCloud']);
- TestBed.configureTestingModule({ providers: [AuthService, { provide: ApiService, useValue: api }] });
- auth = TestBed.inject(AuthService);
- });
- it('uses the Parse session token to load the member after password login', () => {
- api.post.and.returnValue(of({ retcode: 0, result: { userId: 42, userName: 'tree', sessionToken: 'r:session' } }));
- api.callCloud.and.returnValue(of({ retcode: 0, result: { userId: 42, honeyName: '小树' }, addon: { State: 1 } }));
- let actual: Record<string, unknown> | undefined;
- auth.login('user_login_passwd', { name: 'tree', passwd: 'password' }).subscribe((user) => actual = user);
- expect(api.callCloud).toHaveBeenCalledWith('user_get', { uid: 42 }, 'r:session');
- expect(actual?.['sessionToken']).toBe('r:session');
- expect(actual?.['honeyName']).toBe('小树');
- });
- it('rejects migrated logins that do not return a real Parse session', () => {
- api.post.and.returnValue(of({ retcode: 0, result: { userId: 9, userName: 'legacy' } }));
- let message = '';
- auth.login('user_login_passwd', { name: 'legacy', passwd: 'password' }).subscribe({
- error: (error: Error) => message = error.message,
- });
- expect(message).toContain('未生成有效会话');
- expect(api.callCloud).not.toHaveBeenCalled();
- });
- });
|