|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing';
|
| 2 | +import { BadRequestException } from '@nestjs/common'; |
2 | 3 | import { CloudCapiKeyService } from 'src/modules/cloud/capi-key/cloud-capi-key.service';
|
3 | 4 | import { CloudCapiKeyApiProvider } from 'src/modules/cloud/capi-key/cloud-capi-key.api.provider';
|
4 | 5 | import { CloudCapiKeyRepository } from 'src/modules/cloud/capi-key/repository/cloud-capi-key.repository';
|
5 | 6 | import {
|
6 | 7 | mockCloudCapiKey, mockCloudCapiKeyAnalytics,
|
7 | 8 | mockCloudSessionService, mockCloudUserApiService, mockRepository, mockServerService,
|
| 9 | + mockSessionMetadata, |
| 10 | + mockCloudUser, |
| 11 | + mockUtm, |
| 12 | + MockType, |
8 | 13 | } from 'src/__mocks__';
|
9 | 14 | import { CloudUserApiService } from 'src/modules/cloud/user/cloud-user.api.service';
|
10 | 15 | import { CloudSessionService } from 'src/modules/cloud/session/cloud-session.service';
|
11 | 16 | import { ServerService } from 'src/modules/server/server.service';
|
12 | 17 | import { CloudCapiKeyAnalytics } from 'src/modules/cloud/capi-key/cloud-capi-key.analytics';
|
| 18 | +import { CloudApiBadRequestException } from 'src/modules/cloud/common/exceptions'; |
13 | 19 |
|
14 | 20 | describe('CloudCapiKeyService', () => {
|
15 | 21 | let service: CloudCapiKeyService;
|
| 22 | + let api: CloudCapiKeyApiProvider; |
| 23 | + let repository: CloudCapiKeyRepository; |
| 24 | + let cloudUserApiService: MockType<CloudUserApiService>; |
| 25 | + let cloudSessionService: CloudSessionService; |
| 26 | + let serverService: ServerService; |
| 27 | + let analytics: CloudCapiKeyAnalytics; |
16 | 28 |
|
17 | 29 | beforeEach(async () => {
|
18 | 30 | const module: TestingModule = await Test.createTestingModule({
|
@@ -45,13 +57,40 @@ describe('CloudCapiKeyService', () => {
|
45 | 57 | ],
|
46 | 58 | }).compile();
|
47 | 59 |
|
48 |
| - service = module.get(CloudCapiKeyService); |
| 60 | + service = await module.get(CloudCapiKeyService); |
| 61 | + api = await module.get(CloudCapiKeyApiProvider); |
| 62 | + repository = await module.get(CloudCapiKeyRepository); |
| 63 | + cloudUserApiService = await module.get(CloudUserApiService); |
| 64 | + cloudSessionService = await module.get(CloudSessionService); |
| 65 | + serverService = await module.get(ServerService); |
| 66 | + analytics = await module.get(CloudCapiKeyAnalytics); |
49 | 67 | });
|
50 | 68 |
|
51 | 69 | describe('generateName', () => {
|
52 |
| - it('successfully get cloud databases', async () => { |
| 70 | + it('successfully generate capi key name', async () => { |
53 | 71 | expect(await service['generateName'](mockCloudCapiKey))
|
54 | 72 | .toEqual(mockCloudCapiKey.name);
|
55 | 73 | });
|
56 | 74 | });
|
| 75 | + |
| 76 | + describe('ensureCapiKeys', () => { |
| 77 | + it('Should return exist capi key', async () => { |
| 78 | + expect(await service['ensureCapiKeys'](mockSessionMetadata, mockUtm)) |
| 79 | + .toEqual(mockCloudUser.capiKey); |
| 80 | + }); |
| 81 | + it('Should throw CloudApiBadRequestException', async () => { |
| 82 | + cloudUserApiService.me.mockResolvedValue(null); |
| 83 | + CloudUserApiService.getCurrentAccount(null); |
| 84 | + await expect(service['ensureCapiKeys'](mockSessionMetadata, mockUtm)) |
| 85 | + .rejects.toThrowError(CloudApiBadRequestException); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('handleCapiKeyUnauthorizedError', () => { |
| 90 | + it('should show BadRequestException error', async () => { |
| 91 | + const mockError = new BadRequestException('error'); |
| 92 | + expect(await service.handleCapiKeyUnauthorizedError(mockError, mockSessionMetadata)) |
| 93 | + .toEqual(new BadRequestException('error')); |
| 94 | + }); |
| 95 | + }); |
57 | 96 | });
|
0 commit comments