summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/user.service.spec.ts
blob: ba038a72553b864e9289677304a25e3a8ca7f007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { UserFormModel } from '~/app/core/auth/user-form/user-form.model';
import { configureTestBed } from '~/testing/unit-test-helper';
import { UserService } from './user.service';

describe('UserService', () => {
  let service: UserService;
  let httpTesting: HttpTestingController;

  configureTestBed({
    providers: [UserService],
    imports: [HttpClientTestingModule]
  });

  beforeEach(() => {
    service = TestBed.inject(UserService);
    httpTesting = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    httpTesting.verify();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should call create', () => {
    const user = new UserFormModel();
    user.username = 'user0';
    user.password = 'pass0';
    user.name = 'User 0';
    user.email = 'user0@email.com';
    user.roles = ['administrator'];
    service.create(user).subscribe();
    const req = httpTesting.expectOne('api/user');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual(user);
  });

  it('should call delete', () => {
    service.delete('user0').subscribe();
    const req = httpTesting.expectOne('api/user/user0');
    expect(req.request.method).toBe('DELETE');
  });

  it('should call update', () => {
    const user = new UserFormModel();
    user.username = 'user0';
    user.password = 'pass0';
    user.name = 'User 0';
    user.email = 'user0@email.com';
    user.roles = ['administrator'];
    service.update(user).subscribe();
    const req = httpTesting.expectOne('api/user/user0');
    expect(req.request.body).toEqual(user);
    expect(req.request.method).toBe('PUT');
  });

  it('should call get', () => {
    service.get('user0').subscribe();
    const req = httpTesting.expectOne('api/user/user0');
    expect(req.request.method).toBe('GET');
  });

  it('should call list', () => {
    service.list().subscribe();
    const req = httpTesting.expectOne('api/user');
    expect(req.request.method).toBe('GET');
  });

  it('should call changePassword', () => {
    service.changePassword('user0', 'foo', 'bar').subscribe();
    const req = httpTesting.expectOne('api/user/user0/change_password');
    expect(req.request.body).toEqual({
      old_password: 'foo',
      new_password: 'bar'
    });
    expect(req.request.method).toBe('POST');
  });

  it('should call validatePassword', () => {
    service.validatePassword('foo').subscribe();
    const req = httpTesting.expectOne('api/user/validate_password');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual({ password: 'foo', old_password: null, username: null });
  });

  it('should call validatePassword (incl. name)', () => {
    service.validatePassword('foo_bar', 'bar').subscribe();
    const req = httpTesting.expectOne('api/user/validate_password');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual({ password: 'foo_bar', username: 'bar', old_password: null });
  });

  it('should call validatePassword (incl. old password)', () => {
    service.validatePassword('foo', null, 'foo').subscribe();
    const req = httpTesting.expectOne('api/user/validate_password');
    expect(req.request.method).toBe('POST');
    expect(req.request.body).toEqual({ password: 'foo', old_password: 'foo', username: null });
  });
});