1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { TestBed } from '@angular/core/testing';
4 import { configureTestBed } from '../../../testing/unit-test-helper';
5 import { UserFormModel } from '../../core/auth/user-form/user-form.model';
6 import { UserService } from './user.service';
8 describe('UserService', () => {
9 let service: UserService;
10 let httpTesting: HttpTestingController;
13 providers: [UserService],
14 imports: [HttpClientTestingModule]
18 service = TestBed.get(UserService);
19 httpTesting = TestBed.get(HttpTestingController);
26 it('should be created', () => {
27 expect(service).toBeTruthy();
30 it('should call create', () => {
31 const user = new UserFormModel();
32 user.username = 'user0';
33 user.password = 'pass0';
35 user.email = 'user0@email.com';
36 user.roles = ['administrator'];
37 service.create(user).subscribe();
38 const req = httpTesting.expectOne('api/user');
39 expect(req.request.method).toBe('POST');
40 expect(req.request.body).toEqual(user);
43 it('should call delete', () => {
44 service.delete('user0').subscribe();
45 const req = httpTesting.expectOne('api/user/user0');
46 expect(req.request.method).toBe('DELETE');
49 it('should call update', () => {
50 const user = new UserFormModel();
51 user.username = 'user0';
52 user.password = 'pass0';
54 user.email = 'user0@email.com';
55 user.roles = ['administrator'];
56 service.update(user).subscribe();
57 const req = httpTesting.expectOne('api/user/user0');
58 expect(req.request.body).toEqual(user);
59 expect(req.request.method).toBe('PUT');
62 it('should call get', () => {
63 service.get('user0').subscribe();
64 const req = httpTesting.expectOne('api/user/user0');
65 expect(req.request.method).toBe('GET');
68 it('should call list', () => {
69 service.list().subscribe();
70 const req = httpTesting.expectOne('api/user');
71 expect(req.request.method).toBe('GET');
74 it('should call changePassword', () => {
75 service.changePassword('user0', 'foo', 'bar').subscribe();
76 const req = httpTesting.expectOne('api/user/user0/change_password');
77 expect(req.request.body).toEqual({
81 expect(req.request.method).toBe('POST');
84 it('should call validatePassword', () => {
85 service.validatePassword('foo').subscribe();
86 const req = httpTesting.expectOne('api/user/validate_password');
87 expect(req.request.method).toBe('POST');
88 expect(req.request.body).toEqual({ password: 'foo', old_password: null, username: null });
91 it('should call validatePassword (incl. name)', () => {
92 service.validatePassword('foo_bar', 'bar').subscribe();
93 const req = httpTesting.expectOne('api/user/validate_password');
94 expect(req.request.method).toBe('POST');
95 expect(req.request.body).toEqual({ password: 'foo_bar', username: 'bar', old_password: null });
98 it('should call validatePassword (incl. old password)', () => {
99 service.validatePassword('foo', null, 'foo').subscribe();
100 const req = httpTesting.expectOne('api/user/validate_password');
101 expect(req.request.method).toBe('POST');
102 expect(req.request.body).toEqual({ password: 'foo', old_password: 'foo', username: null });