From 671cbb4b50f609843818f68c68631a1afa37f915 Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Sun, 10 Jun 2018 01:42:35 +0100 Subject: [PATCH] mgr/dashboard: Add unit test for frontend api services Signed-off-by: Tiago Melo --- .../src/app/shared/api/auth.service.spec.ts | 56 ++++++ .../src/app/shared/api/cephfs.service.spec.ts | 51 ++++- .../shared/api/configuration.service.spec.ts | 34 +++- .../app/shared/api/dashboard.service.spec.ts | 34 +++- .../src/app/shared/api/host.service.spec.ts | 41 ++++ .../app/shared/api/logging.service.spec.ts | 39 ++++ .../src/app/shared/api/logging.service.ts | 5 +- .../app/shared/api/monitor.service.spec.ts | 34 +++- .../src/app/shared/api/osd.service.spec.ts | 51 ++++- .../api/performance-counter.service.spec.ts | 47 +++-- .../src/app/shared/api/pool.service.spec.ts | 60 ++++++ .../shared/api/rbd-mirroring.service.spec.ts | 34 +++- .../src/app/shared/api/rbd.service.spec.ts | 129 +++++++++++++ .../app/shared/api/rgw-bucket.service.spec.ts | 102 +++++++++- .../app/shared/api/rgw-daemon.service.spec.ts | 40 +++- .../app/shared/api/rgw-user.service.spec.ts | 181 ++++++++++++++++-- .../app/shared/api/tcmu-iscsi.service.spec.ts | 43 +++++ 17 files changed, 876 insertions(+), 105 deletions(-) create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts new file mode 100644 index 00000000000..f8d71b38f99 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/auth.service.spec.ts @@ -0,0 +1,56 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; + +import { AuthStorageService } from '../services/auth-storage.service'; + +import { configureTestBed } from '../unit-test-helper'; +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [AuthService, AuthStorageService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(AuthService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it( + 'should login and save the user', + fakeAsync(() => { + const fakeCredentials = { username: 'foo', password: 'bar' }; + service.login(fakeCredentials); + const req = httpTesting.expectOne('api/auth'); + expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual(fakeCredentials); + req.flush(fakeCredentials); + tick(); + expect(localStorage.getItem('dashboard_username')).toBe('foo'); + }) + ); + + it( + 'should logout and remove the user', + fakeAsync(() => { + service.logout(); + const req = httpTesting.expectOne('api/auth'); + expect(req.request.method).toBe('DELETE'); + req.flush({ username: 'foo' }); + tick(); + expect(localStorage.getItem('dashboard_username')).toBe(null); + }) + ); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/cephfs.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/cephfs.service.spec.ts index 91fc8df7ecc..1c8c14fee4f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/cephfs.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/cephfs.service.spec.ts @@ -1,19 +1,52 @@ -import { HttpClientModule } from '@angular/common/http'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { CephfsService } from './cephfs.service'; describe('CephfsService', () => { + let service: CephfsService; + let httpTesting: HttpTestingController; + configureTestBed({ - imports: [HttpClientModule], + imports: [HttpClientTestingModule], providers: [CephfsService] }); - it( - 'should be created', - inject([CephfsService], (service: CephfsService) => { - expect(service).toBeTruthy(); - }) - ); + beforeEach(() => { + service = TestBed.get(CephfsService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call list', () => { + service.list().subscribe(); + const req = httpTesting.expectOne('api/cephfs'); + expect(req.request.method).toBe('GET'); + }); + + it('should call getCephfs', () => { + service.getCephfs(1).subscribe(); + const req = httpTesting.expectOne('api/cephfs/1'); + expect(req.request.method).toBe('GET'); + }); + + it('should call getClients', () => { + service.getClients(1).subscribe(); + const req = httpTesting.expectOne('api/cephfs/1/clients'); + expect(req.request.method).toBe('GET'); + }); + + it('should call getMdsCounters', () => { + service.getMdsCounters(1).subscribe(); + const req = httpTesting.expectOne('api/cephfs/1/mds_counters'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/configuration.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/configuration.service.spec.ts index be451104083..fa07822b508 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/configuration.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/configuration.service.spec.ts @@ -1,20 +1,34 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { ConfigurationService } from './configuration.service'; describe('ConfigurationService', () => { + let service: ConfigurationService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [ConfigurationService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(ConfigurationService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([ConfigurationService], (service: ConfigurationService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call getConfigData', () => { + service.getConfigData().subscribe(); + const req = httpTesting.expectOne('api/cluster_conf/'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/dashboard.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/dashboard.service.spec.ts index d11d2b22447..9062ba501b8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/dashboard.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/dashboard.service.spec.ts @@ -1,20 +1,34 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { DashboardService } from './dashboard.service'; describe('DashboardService', () => { + let service: DashboardService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [DashboardService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(DashboardService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([DashboardService], (service: DashboardService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call getHealth', () => { + service.getHealth().subscribe(); + const req = httpTesting.expectOne('api/dashboard/health'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts new file mode 100644 index 00000000000..445386f8f78 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/host.service.spec.ts @@ -0,0 +1,41 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; + +import { configureTestBed } from '../unit-test-helper'; +import { HostService } from './host.service'; + +describe('HostService', () => { + let service: HostService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [HostService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(HostService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it( + 'should call list', + fakeAsync(() => { + let result; + service.list().then((resp) => (result = resp)); + const req = httpTesting.expectOne('api/host'); + expect(req.request.method).toBe('GET'); + req.flush(['foo', 'bar']); + tick(); + expect(result).toEqual(['foo', 'bar']); + }) + ); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts new file mode 100644 index 00000000000..fa8f0e918b1 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.spec.ts @@ -0,0 +1,39 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; + +import { configureTestBed } from '../unit-test-helper'; +import { LoggingService } from './logging.service'; + +describe('LoggingService', () => { + let service: LoggingService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [LoggingService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(LoggingService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call jsError', () => { + service.jsError('foo', 'bar', 'baz').subscribe(); + const req = httpTesting.expectOne('ui-api/logging/js-error'); + expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual({ + url: 'foo', + message: 'bar', + stack: 'baz' + }); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts index 097f86edfe8..3ed2a172643 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/logging.service.ts @@ -7,9 +7,7 @@ import { ApiModule } from './api.module'; providedIn: ApiModule }) export class LoggingService { - - constructor(private http: HttpClient) { - } + constructor(private http: HttpClient) {} jsError(url, message, stack) { const request = { @@ -19,5 +17,4 @@ export class LoggingService { }; return this.http.post('ui-api/logging/js-error', request); } - } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/monitor.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/monitor.service.spec.ts index fd59f30cab7..e1813b983cd 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/monitor.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/monitor.service.spec.ts @@ -1,20 +1,34 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { MonitorService } from './monitor.service'; describe('MonitorService', () => { + let service: MonitorService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [MonitorService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(MonitorService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([MonitorService], (service: MonitorService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call getMonitor', () => { + service.getMonitor().subscribe(); + const req = httpTesting.expectOne('api/monitor'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/osd.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/osd.service.spec.ts index 848296c6eb6..d8237e71467 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/osd.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/osd.service.spec.ts @@ -1,19 +1,52 @@ -import { HttpClientModule } from '@angular/common/http'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { OsdService } from './osd.service'; describe('OsdService', () => { + let service: OsdService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [OsdService], - imports: [HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(OsdService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([OsdService], (service: OsdService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call getList', () => { + service.getList().subscribe(); + const req = httpTesting.expectOne('api/osd'); + expect(req.request.method).toBe('GET'); + }); + + it('should call getDetails', () => { + service.getDetails(1).subscribe(); + const req = httpTesting.expectOne('api/osd/1'); + expect(req.request.method).toBe('GET'); + }); + + it('should call scrub, with deep=true', () => { + service.scrub('foo', true).subscribe(); + const req = httpTesting.expectOne('api/osd/foo/scrub?deep=true'); + expect(req.request.method).toBe('POST'); + }); + + it('should call scrub, with deep=false', () => { + service.scrub('foo', false).subscribe(); + const req = httpTesting.expectOne('api/osd/foo/scrub?deep=false'); + expect(req.request.method).toBe('POST'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/performance-counter.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/performance-counter.service.spec.ts index 5b616ffd21e..ec87677237b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/performance-counter.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/performance-counter.service.spec.ts @@ -1,22 +1,45 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; - -import { BsDropdownModule } from 'ngx-bootstrap'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { PerformanceCounterService } from './performance-counter.service'; describe('PerformanceCounterService', () => { + let service: PerformanceCounterService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [PerformanceCounterService], - imports: [HttpClientTestingModule, BsDropdownModule.forRoot(), HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(PerformanceCounterService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); }); - it( - 'should be created', - inject([PerformanceCounterService], (service: PerformanceCounterService) => { - expect(service).toBeTruthy(); - }) - ); + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call list', () => { + service.list().subscribe(); + const req = httpTesting.expectOne('api/perf_counters'); + expect(req.request.method).toBe('GET'); + }); + + it('should call get', () => { + let result; + service.get('foo', '1').subscribe((resp) => { + result = resp; + }); + const req = httpTesting.expectOne('api/perf_counters/foo/1'); + expect(req.request.method).toBe('GET'); + req.flush({ counters: [{ foo: 'bar' }] }); + expect(result).toEqual([{ foo: 'bar' }]); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts new file mode 100644 index 00000000000..693fd891f21 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/pool.service.spec.ts @@ -0,0 +1,60 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; + +import { configureTestBed } from '../unit-test-helper'; +import { PoolService } from './pool.service'; + +describe('PoolService', () => { + let service: PoolService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [PoolService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(PoolService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call getList', () => { + service.getList().subscribe(); + const req = httpTesting.expectOne('api/pool'); + expect(req.request.method).toBe('GET'); + }); + + it( + 'should call list without parameter', + fakeAsync(() => { + let result; + service.list().then((resp) => (result = resp)); + const req = httpTesting.expectOne('api/pool?attrs='); + expect(req.request.method).toBe('GET'); + req.flush(['foo', 'bar']); + tick(); + expect(result).toEqual(['foo', 'bar']); + }) + ); + + it( + 'should call list with a list', + fakeAsync(() => { + let result; + service.list(['foo']).then((resp) => (result = resp)); + const req = httpTesting.expectOne('api/pool?attrs=foo'); + expect(req.request.method).toBe('GET'); + req.flush(['foo', 'bar']); + tick(); + expect(result).toEqual(['foo', 'bar']); + }) + ); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts index 0a6321f9bce..827a185c9ba 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts @@ -1,20 +1,34 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { RbdMirroringService } from './rbd-mirroring.service'; describe('RbdMirroringService', () => { + let service: RbdMirroringService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [RbdMirroringService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(RbdMirroringService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([RbdMirroringService], (service: RbdMirroringService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call get', () => { + service.get().subscribe(); + const req = httpTesting.expectOne('api/rbdmirror'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts new file mode 100644 index 00000000000..4d311c86a90 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd.service.spec.ts @@ -0,0 +1,129 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; + +import { configureTestBed } from '../unit-test-helper'; +import { RbdService } from './rbd.service'; + +describe('RbdService', () => { + let service: RbdService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [RbdService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(RbdService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call create', () => { + service.create('foo').subscribe(); + const req = httpTesting.expectOne('api/block/image'); + expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual('foo'); + }); + + it('should call delete', () => { + service.delete('poolName', 'rbdName').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call update', () => { + service.update('poolName', 'rbdName', 'foo').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName'); + expect(req.request.body).toEqual('foo'); + expect(req.request.method).toBe('PUT'); + }); + + it('should call get', () => { + service.get('poolName', 'rbdName').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName'); + expect(req.request.method).toBe('GET'); + }); + + it('should call list', () => { + service.list().subscribe(); + const req = httpTesting.expectOne('api/block/image'); + expect(req.request.method).toBe('GET'); + }); + + it('should call copy', () => { + service.copy('poolName', 'rbdName', 'foo').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/copy'); + expect(req.request.body).toEqual('foo'); + expect(req.request.method).toBe('POST'); + }); + + it('should call flatten', () => { + service.flatten('poolName', 'rbdName').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/flatten'); + expect(req.request.body).toEqual(null); + expect(req.request.method).toBe('POST'); + }); + + it('should call defaultFeatures', () => { + service.defaultFeatures().subscribe(); + const req = httpTesting.expectOne('api/block/image/default_features'); + expect(req.request.method).toBe('GET'); + }); + + it('should call createSnapshot', () => { + service.createSnapshot('poolName', 'rbdName', 'snapshotName').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap'); + expect(req.request.body).toEqual({ + snapshot_name: 'snapshotName' + }); + expect(req.request.method).toBe('POST'); + }); + + it('should call renameSnapshot', () => { + service.renameSnapshot('poolName', 'rbdName', 'snapshotName', 'foo').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName'); + expect(req.request.body).toEqual({ + new_snap_name: 'foo' + }); + expect(req.request.method).toBe('PUT'); + }); + + it('should call protectSnapshot', () => { + service.protectSnapshot('poolName', 'rbdName', 'snapshotName', true).subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName'); + expect(req.request.body).toEqual({ + is_protected: true + }); + expect(req.request.method).toBe('PUT'); + }); + + it('should call rollbackSnapshot', () => { + service.rollbackSnapshot('poolName', 'rbdName', 'snapshotName').subscribe(); + const req = httpTesting.expectOne( + 'api/block/image/poolName/rbdName/snap/snapshotName/rollback' + ); + expect(req.request.body).toEqual(null); + expect(req.request.method).toBe('POST'); + }); + + it('should call cloneSnapshot', () => { + service.cloneSnapshot('poolName', 'rbdName', 'snapshotName', null).subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName/clone'); + expect(req.request.body).toEqual(null); + expect(req.request.method).toBe('POST'); + }); + + it('should call deleteSnapshot', () => { + service.deleteSnapshot('poolName', 'rbdName', 'snapshotName').subscribe(); + const req = httpTesting.expectOne('api/block/image/poolName/rbdName/snap/snapshotName'); + expect(req.request.method).toBe('DELETE'); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.spec.ts index 50890f1bed3..1a7c7c73173 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.spec.ts @@ -1,20 +1,102 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { RgwBucketService } from './rgw-bucket.service'; describe('RgwBucketService', () => { + let service: RgwBucketService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [RgwBucketService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(RgwBucketService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); }); - it( - 'should be created', - inject([RgwBucketService], (service: RgwBucketService) => { - expect(service).toBeTruthy(); - }) - ); + it('should call list, with enumerate returning empty', () => { + let result; + service.list().subscribe((resp) => { + result = resp; + }); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket'); + req.flush([]); + expect(req.request.method).toBe('GET'); + expect(result).toEqual([]); + }); + + it('should call list, with enumerate returning 2 elements', () => { + let result; + service.list().subscribe((resp) => { + result = resp; + }); + let req = httpTesting.expectOne('/api/rgw/proxy/bucket'); + req.flush(['foo', 'bar']); + + req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo'); + req.flush({ name: 'foo' }); + + req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=bar'); + req.flush({ name: 'bar' }); + + expect(req.request.method).toBe('GET'); + expect(result).toEqual([{ name: 'foo' }, { name: 'bar' }]); + }); + + it('should call get', () => { + service.get('foo').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo'); + expect(req.request.method).toBe('GET'); + }); + + it('should call create', () => { + service.create('foo', 'bar').subscribe(); + const req = httpTesting.expectOne('/api/rgw/bucket'); + expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual({ + bucket: 'foo', + uid: 'bar' + }); + }); + + it('should call update', () => { + service.update('foo', 'bar', 'baz').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=bar&bucket-id=foo&uid=baz'); + expect(req.request.method).toBe('PUT'); + }); + + it('should call delete, with purgeObjects = true', () => { + service.delete('foo').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo&purge-objects=true'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call delete, with purgeObjects = false', () => { + service.delete('foo', false).subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket?bucket=foo&purge-objects=false'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call exists', () => { + let result; + service.exists('foo').subscribe((resp) => { + result = resp; + }); + const req = httpTesting.expectOne('/api/rgw/proxy/bucket'); + expect(req.request.method).toBe('GET'); + req.flush(['foo', 'bar']); + expect(result).toBe(true); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts index aa3e52219e7..362e474fe58 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-daemon.service.spec.ts @@ -1,20 +1,40 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; import { configureTestBed } from '../unit-test-helper'; import { RgwDaemonService } from './rgw-daemon.service'; describe('RgwDaemonService', () => { + let service: RgwDaemonService; + let httpTesting: HttpTestingController; + configureTestBed({ providers: [RgwDaemonService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(RgwDaemonService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); }); - it( - 'should be created', - inject([RgwDaemonService], (service: RgwDaemonService) => { - expect(service).toBeTruthy(); - }) - ); + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call list', () => { + service.list().subscribe(); + const req = httpTesting.expectOne('api/rgw/daemon'); + expect(req.request.method).toBe('GET'); + }); + + it('should call get', () => { + service.get('foo').subscribe(); + const req = httpTesting.expectOne('api/rgw/daemon/foo'); + expect(req.request.method).toBe('GET'); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts index 59f1b22f448..e9569ef19ec 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-user.service.spec.ts @@ -1,20 +1,179 @@ -import { HttpClientModule } from '@angular/common/http'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { inject } from '@angular/core/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; + +import { of as observableOf } from 'rxjs'; import { configureTestBed } from '../unit-test-helper'; import { RgwUserService } from './rgw-user.service'; describe('RgwUserService', () => { + let service: RgwUserService; + let httpTesting: HttpTestingController; + configureTestBed({ - providers: [RgwUserService], - imports: [HttpClientTestingModule, HttpClientModule] + imports: [HttpClientTestingModule], + providers: [RgwUserService] + }); + + beforeEach(() => { + service = TestBed.get(RgwUserService); + httpTesting = TestBed.get(HttpTestingController); }); - it( - 'should be created', - inject([RgwUserService], (service: RgwUserService) => { - expect(service).toBeTruthy(); - }) - ); + 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/proxy/metadata/user'); + 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/proxy/metadata/user'); + expect(req.request.method).toBe('GET'); + req.flush(['foo', 'bar']); + + req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo'); + expect(req.request.method).toBe('GET'); + req.flush({ name: 'foo' }); + + req = httpTesting.expectOne('/api/rgw/proxy/user?uid=bar'); + 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/proxy/metadata/user'); + expect(req.request.method).toBe('GET'); + }); + + it('should call get', () => { + service.get('foo').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo'); + expect(req.request.method).toBe('GET'); + }); + + it('should call getQuota', () => { + service.getQuota('foo').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?quota&uid=foo'); + expect(req.request.method).toBe('GET'); + }); + + it('should call put', () => { + service.put({ foo: 'bar' }).subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?foo=bar'); + expect(req.request.method).toBe('PUT'); + }); + + it('should call putQuota', () => { + service.putQuota({ foo: 'bar' }).subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?quota&foo=bar'); + expect(req.request.method).toBe('PUT'); + }); + + it('should call post', () => { + service.post({ foo: 'bar' }).subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?foo=bar'); + expect(req.request.method).toBe('POST'); + }); + + it('should call delete', () => { + service.delete('foo').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call addSubuser with unrecognized permission', () => { + service.addSubuser('foo', 'bar', 'baz', null, true).subscribe(); + const req = httpTesting.expectOne( + '/api/rgw/proxy/user?uid=foo&subuser=bar&key-type=swift&access=baz&generate-secret=true' + ); + expect(req.request.method).toBe('PUT'); + }); + + it('should call addSubuser with mapped permission', () => { + service.addSubuser('foo', 'bar', 'full-control', 'baz', false).subscribe(); + const req = httpTesting.expectOne( + '/api/rgw/proxy/user?uid=foo&subuser=bar&key-type=swift&access=full&secret-key=baz' + ); + expect(req.request.method).toBe('PUT'); + }); + + it('should call deleteSubuser', () => { + service.deleteSubuser('foo', 'bar').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?uid=foo&subuser=bar&purge-keys=true'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call addCapability', () => { + service.addCapability('foo', 'bar', 'baz').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?caps&uid=foo&user-caps=bar=baz'); + expect(req.request.method).toBe('PUT'); + }); + + it('should call deleteCapability', () => { + service.deleteCapability('foo', 'bar', 'baz').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?caps&uid=foo&user-caps=bar=baz'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call addS3Key, with generateKey true', () => { + service.addS3Key('1', 'foo', 'bar', 'baz', true).subscribe(); + const req = httpTesting.expectOne( + '/api/rgw/proxy/user?key&uid=1&key-type=s3&generate-key=true&subuser=foo' + ); + expect(req.request.method).toBe('PUT'); + }); + + it('should call addS3Key, with generateKey false', () => { + service.addS3Key('1', 'foo', 'bar', 'baz', false).subscribe(); + const req = httpTesting.expectOne( + '/api/rgw/proxy/user?key' + + '&uid=1&key-type=s3&generate-key=false&access-key=bar&secret-key=baz&subuser=foo' + ); + expect(req.request.method).toBe('PUT'); + }); + + it('should call deleteS3Key', () => { + service.deleteS3Key('foo', 'bar').subscribe(); + const req = httpTesting.expectOne('/api/rgw/proxy/user?key&uid=foo&key-type=s3&access-key=bar'); + expect(req.request.method).toBe('DELETE'); + }); + + it('should call exists with an existent uid', () => { + spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar'])); + let result; + service.exists('foo').subscribe((res) => { + result = res; + }); + expect(result).toBe(true); + }); + + it('should call exists with a non existent uid', () => { + spyOn(service, 'enumerate').and.returnValue(observableOf(['foo', 'bar'])); + let result; + service.exists('baz').subscribe((res) => { + result = res; + }); + expect(result).toBe(false); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts new file mode 100644 index 00000000000..b917923d8af --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/tcmu-iscsi.service.spec.ts @@ -0,0 +1,43 @@ +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; + +import { configureTestBed } from '../unit-test-helper'; +import { TcmuIscsiService } from './tcmu-iscsi.service'; + +describe('TcmuIscsiService', () => { + let service: TcmuIscsiService; + let httpTesting: HttpTestingController; + + configureTestBed({ + providers: [TcmuIscsiService], + imports: [HttpClientTestingModule] + }); + + beforeEach(() => { + service = TestBed.get(TcmuIscsiService); + httpTesting = TestBed.get(HttpTestingController); + }); + + afterEach(() => { + httpTesting.verify(); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it( + 'should call tcmuiscsi', + fakeAsync(() => { + let result; + service.tcmuiscsi().then((resp) => { + result = resp; + }); + const req = httpTesting.expectOne('api/tcmuiscsi'); + expect(req.request.method).toBe('GET'); + req.flush(['foo']); + tick(); + expect(result).toEqual(['foo']); + }) + ); +}); -- 2.39.5