summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts
blob: 7884f2385f22d35c8f2b7831fbd5c2d09f28a114 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { of as observableOf, throwError } from 'rxjs';

import { configureTestBed, RgwHelper } from '~/testing/unit-test-helper';
import { RgwUserService } from './rgw-user.service';

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

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

  beforeEach(() => {
    service = TestBed.inject(RgwUserService);
    httpTesting = TestBed.inject(HttpTestingController);
    RgwHelper.selectDaemon();
  });

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

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

  it('should call list with empty result', () => {
    let result;
    service.list().subscribe((resp) => {
      result = resp;
    });
    const req = httpTesting.expectOne(`api/rgw/user?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
    req.flush([]);
    expect(result).toEqual([]);
  });

  it('should call list with result', () => {
    let result;
    service.list().subscribe((resp) => {
      result = resp;
    });
    let req = httpTesting.expectOne(`api/rgw/user?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
    req.flush(['foo', 'bar']);

    req = httpTesting.expectOne(`api/rgw/user/foo?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
    req.flush({ name: 'foo' });

    req = httpTesting.expectOne(`api/rgw/user/bar?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
    req.flush({ name: 'bar' });

    expect(result).toEqual([{ name: 'foo' }, { name: 'bar' }]);
  });

  it('should call enumerate', () => {
    service.enumerate().subscribe();
    const req = httpTesting.expectOne(`api/rgw/user?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
  });

  it('should call get', () => {
    service.get('foo').subscribe();
    const req = httpTesting.expectOne(`api/rgw/user/foo?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
  });

  it('should call getQuota', () => {
    service.getQuota('foo').subscribe();
    const req = httpTesting.expectOne(`api/rgw/user/foo/quota?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('GET');
  });

  it('should call update', () => {
    service.update('foo', { xxx: 'yyy' }).subscribe();
    const req = httpTesting.expectOne(`api/rgw/user/foo?${RgwHelper.DAEMON_QUERY_PARAM}&xxx=yyy`);
    expect(req.request.method).toBe('PUT');
  });

  it('should call updateQuota', () => {
    service.updateQuota('foo', { xxx: 'yyy' }).subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/quota?${RgwHelper.DAEMON_QUERY_PARAM}&xxx=yyy`
    );
    expect(req.request.method).toBe('PUT');
  });

  it('should call create', () => {
    service.create({ foo: 'bar' }).subscribe();
    const req = httpTesting.expectOne(`api/rgw/user?${RgwHelper.DAEMON_QUERY_PARAM}&foo=bar`);
    expect(req.request.method).toBe('POST');
  });

  it('should call delete', () => {
    service.delete('foo').subscribe();
    const req = httpTesting.expectOne(`api/rgw/user/foo?${RgwHelper.DAEMON_QUERY_PARAM}`);
    expect(req.request.method).toBe('DELETE');
  });

  it('should call createSubuser', () => {
    service.createSubuser('foo', { xxx: 'yyy' }).subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/subuser?${RgwHelper.DAEMON_QUERY_PARAM}&xxx=yyy`
    );
    expect(req.request.method).toBe('POST');
  });

  it('should call deleteSubuser', () => {
    service.deleteSubuser('foo', 'bar').subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/subuser/bar?${RgwHelper.DAEMON_QUERY_PARAM}`
    );
    expect(req.request.method).toBe('DELETE');
  });

  it('should call addCapability', () => {
    service.addCapability('foo', 'bar', 'baz').subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/capability?${RgwHelper.DAEMON_QUERY_PARAM}&type=bar&perm=baz`
    );
    expect(req.request.method).toBe('POST');
  });

  it('should call deleteCapability', () => {
    service.deleteCapability('foo', 'bar', 'baz').subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/capability?${RgwHelper.DAEMON_QUERY_PARAM}&type=bar&perm=baz`
    );
    expect(req.request.method).toBe('DELETE');
  });

  it('should call addS3Key', () => {
    service.addS3Key('foo', { xxx: 'yyy' }).subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/key?${RgwHelper.DAEMON_QUERY_PARAM}&key_type=s3&xxx=yyy`
    );
    expect(req.request.method).toBe('POST');
  });

  it('should call deleteS3Key', () => {
    service.deleteS3Key('foo', 'bar').subscribe();
    const req = httpTesting.expectOne(
      `api/rgw/user/foo/key?${RgwHelper.DAEMON_QUERY_PARAM}&key_type=s3&access_key=bar`
    );
    expect(req.request.method).toBe('DELETE');
  });

  it('should call exists with an existent uid', (done) => {
    spyOn(service, 'get').and.returnValue(observableOf({}));
    service.exists('foo').subscribe((res) => {
      expect(res).toBe(true);
      done();
    });
  });

  it('should call exists with a non existent uid', (done) => {
    spyOn(service, 'get').and.returnValue(throwError('bar'));
    service.exists('baz').subscribe((res) => {
      expect(res).toBe(false);
      done();
    });
  });
});