-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
14 changed files
with
291 additions
and
619 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
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
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
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
|
||
import { BlobService } from "@/services/blob.service"; | ||
import { ConfigFactoryService } from "@/services/config-factory.service"; | ||
|
||
import { ObjectController } from "./blob.controller"; | ||
|
||
describe("ObjectController", () => { | ||
let controller: ObjectController; | ||
let blobService: BlobService; | ||
let factoryService: ConfigFactoryService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ObjectController], | ||
providers: [ | ||
{ | ||
provide: BlobService, | ||
useValue: { | ||
getObject: jest.fn(), | ||
putObject: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: ConfigFactoryService, | ||
useValue: { | ||
minio: { bucket: "test-bucket" }, | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<ObjectController>(ObjectController); | ||
blobService = module.get<BlobService>(BlobService); | ||
factoryService = module.get<ConfigFactoryService>(ConfigFactoryService); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
|
||
describe("getObject", () => { | ||
it("should call blobService.getObject with correct cuid2 and bucket", async () => { | ||
const cuid2 = "test-cuid2"; | ||
const result = { file: "test-file" as any }; | ||
jest.spyOn(blobService, "getObject").mockResolvedValueOnce(result as any); | ||
await expect(controller.getObject(cuid2)).resolves.toEqual(result.file); | ||
expect(blobService.getObject).toHaveBeenCalledWith( | ||
cuid2, | ||
factoryService.minio.bucket, | ||
); | ||
}); | ||
}); | ||
|
||
describe("putObjects", () => { | ||
it("should call blobService.putObject for each file and return uploaded blobs", async () => { | ||
const request = { | ||
files: jest.fn().mockImplementation(() => [ | ||
{ | ||
toBuffer: jest.fn().mockResolvedValueOnce(Buffer.from("test-data")), | ||
filename: "test.txt", | ||
mimetype: "text/plain", | ||
}, | ||
{ | ||
toBuffer: jest.fn().mockResolvedValueOnce(Buffer.from("test-data")), | ||
filename: "test2.txt", | ||
mimetype: "text/plain", | ||
}, | ||
]), | ||
}; | ||
const expectedUploadedBlobs = [ | ||
{ | ||
filename: "test.txt", | ||
mimetype: "text/plain", | ||
cuid2: expect.any(String), | ||
}, | ||
{ | ||
filename: "test2.txt", | ||
mimetype: "text/plain", | ||
cuid2: expect.any(String), | ||
}, | ||
]; | ||
|
||
await expect(controller.putObjects(request)).resolves.toEqual( | ||
expectedUploadedBlobs, | ||
); | ||
|
||
expect(blobService.putObject).toHaveBeenCalledTimes(2); | ||
expect(blobService.putObject).toHaveBeenCalledWith( | ||
factoryService.minio.bucket, | ||
expect.any(String), | ||
expect.any(Buffer), | ||
expect.any(Number), | ||
expect.objectContaining({ | ||
filename: "test.txt", | ||
mimetype: "text/plain", | ||
}), | ||
); | ||
expect(blobService.putObject).toHaveBeenCalledWith( | ||
factoryService.minio.bucket, | ||
expect.any(String), | ||
expect.any(Buffer), | ||
expect.any(Number), | ||
expect.objectContaining({ | ||
filename: "test2.txt", | ||
mimetype: "text/plain", | ||
}), | ||
); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
|
||
import { MINIO_CLIENT } from "@/modules/minio-client.module"; | ||
|
||
import { BlobService } from "./blob.service"; | ||
|
||
describe("BlobService", () => { | ||
let service: BlobService; | ||
const mockMinioClient = { | ||
getObject: jest.fn(), | ||
createBucketIfNotExists: jest.fn(), | ||
putObject: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
BlobService, | ||
{ | ||
provide: MINIO_CLIENT, | ||
useValue: mockMinioClient, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<BlobService>(BlobService); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe("getObject", () => { | ||
it("should call minioClient.getObject with correct parameters", async () => { | ||
const id = "test-id"; | ||
const bucket = "test-bucket"; | ||
await service.getObject(id, bucket); | ||
expect(mockMinioClient.getObject).toHaveBeenCalledWith(bucket, id); | ||
}); | ||
}); | ||
|
||
describe("putObject", () => { | ||
it("should call minioClient.createBucketIfNotExists and minioClient.putObject with correct parameters", async () => { | ||
const bucketName = "test-bucket"; | ||
const objectName = "test-object"; | ||
const stream = "test-stream"; | ||
const size = 123; | ||
const metaData = { key: "value" }; | ||
await service.putObject(bucketName, objectName, stream, size, metaData); | ||
expect(mockMinioClient.createBucketIfNotExists).toHaveBeenCalledWith( | ||
bucketName, | ||
); | ||
expect(mockMinioClient.putObject).toHaveBeenCalledWith( | ||
bucketName, | ||
objectName, | ||
stream, | ||
size, | ||
metaData, | ||
); | ||
}); | ||
}); | ||
}); |
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
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
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
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
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
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
Oops, something went wrong.