-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
enrique
committed
Jan 25, 2023
1 parent
321a0eb
commit d94e1f5
Showing
1 changed file
with
72 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,83 @@ | ||
import {Test, TestingModule} from '@nestjs/testing' | ||
import {NeverminedService} from './nvm.service' | ||
import {ConfigService} from '../config/config.service' | ||
import {HttpService} from '@nestjs/axios' | ||
import {createMock} from "@golevelup/ts-jest" | ||
import {BadRequestException} from "@nestjs/common" | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { NeverminedService } from './nvm.service' | ||
import { ConfigService } from '../config/config.service' | ||
import { HttpService } from '@nestjs/axios' | ||
import { createMock } from '@golevelup/ts-jest' | ||
import { BadRequestException } from '@nestjs/common' | ||
import * as AWSsdkMock from 'aws-sdk-mock' | ||
describe('NeverminedService', () => { | ||
let neverminedService: NeverminedService | ||
let configServiceMock | ||
let httpServiceMock | ||
let neverminedService: NeverminedService | ||
let configServiceMock | ||
let httpServiceMock | ||
|
||
beforeEach(async () => { | ||
AWSsdkMock.mock('S3', 'upload', jest.fn().mockResolvedValue({ Location: 'https://example.com/file.jpg' })) | ||
configServiceMock = createMock<ConfigService>({ | ||
computeConfig: () => ({ | ||
argo_namespace: 'argo', | ||
}), | ||
cryptoConfig: () => ({ | ||
provider_key: './keyfile.json', | ||
provider_password: 'password', | ||
}), | ||
nvm: () => ({ | ||
neverminedNodeAddress: '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0', | ||
}), | ||
}) | ||
beforeEach(async () => { | ||
AWSsdkMock.mock( | ||
'S3', | ||
'upload', | ||
jest.fn().mockResolvedValue({ Location: 'https://example.com/file.jpg' }), | ||
) | ||
configServiceMock = createMock<ConfigService>({ | ||
computeConfig: () => ({ | ||
argo_namespace: 'argo', | ||
}), | ||
cryptoConfig: () => ({ | ||
provider_key: './keyfile.json', | ||
provider_password: 'password', | ||
}), | ||
nvm: () => ({ | ||
neverminedNodeAddress: '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0', | ||
}), | ||
}) | ||
|
||
httpServiceMock = createMock<HttpService>({ | ||
get: jest.fn().mockReturnValue({ | ||
data: { | ||
files: [ | ||
{ | ||
url: 'https://example.com/file.png', | ||
contentType: 'image/png', | ||
name: 'file.png', | ||
}, | ||
], | ||
}, | ||
}), | ||
}) | ||
httpServiceMock = createMock<HttpService>({ | ||
get: jest.fn().mockReturnValue({ | ||
data: { | ||
files: [ | ||
{ | ||
url: 'https://example.com/file.png', | ||
contentType: 'image/png', | ||
name: 'file.png', | ||
}, | ||
], | ||
}, | ||
}), | ||
}) | ||
|
||
const nvmModule: TestingModule = await Test.createTestingModule({ | ||
providers: [NeverminedService, | ||
{ | ||
provide: ConfigService, | ||
useValue: configServiceMock, | ||
}, | ||
{ | ||
provide: HttpService, | ||
useValue: httpServiceMock, | ||
}, | ||
], | ||
}).compile() | ||
const nvmModule: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
NeverminedService, | ||
{ | ||
provide: ConfigService, | ||
useValue: configServiceMock, | ||
}, | ||
{ | ||
provide: HttpService, | ||
useValue: httpServiceMock, | ||
}, | ||
], | ||
}).compile() | ||
|
||
neverminedService = nvmModule.get<NeverminedService>(NeverminedService) | ||
}) | ||
neverminedService = nvmModule.get<NeverminedService>(NeverminedService) | ||
}) | ||
|
||
afterEach(() => { | ||
AWSsdkMock.restore() | ||
}) | ||
afterEach(() => { | ||
AWSsdkMock.restore() | ||
}) | ||
|
||
it('NeverminedService should be defined', () => { | ||
expect(neverminedService).toBeDefined() | ||
}) | ||
it('NeverminedService should be defined', () => { | ||
expect(neverminedService).toBeDefined() | ||
}) | ||
|
||
it('should throw an exception when the DID is not valid', async () => { | ||
await expect(neverminedService.getAssetUrl('did:ethr:0x123', 0)).rejects.toThrow(BadRequestException) | ||
} | ||
it('should throw an exception when the DID is not valid', async () => { | ||
await expect(neverminedService.getAssetUrl('did:ethr:0x123', 0)).rejects.toThrow( | ||
BadRequestException, | ||
) | ||
}) | ||
|
||
it('should throw an error if the bucket does not exist', async () => { | ||
AWSsdkMock.mock('S3', 'upload', jest.fn().mockRejectedValue({ code: 'NoSuchBucket' })) | ||
await expect(neverminedService.uploadS3(Buffer.from('file content'), 'file.jpg')).rejects.toThrowError('Cannot convert object to primitive value') | ||
}) | ||
|
||
}) | ||
it('should throw an error if the bucket does not exist', async () => { | ||
AWSsdkMock.mock('S3', 'upload', jest.fn().mockRejectedValue({ code: 'NoSuchBucket' })) | ||
await expect( | ||
neverminedService.uploadS3(Buffer.from('file content'), 'file.jpg'), | ||
).rejects.toThrowError('Cannot convert object to primitive value') | ||
}) | ||
}) |