mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
init gestion img
This commit is contained in:
parent
8240de0a44
commit
c6c830ef74
3 changed files with 122 additions and 0 deletions
|
|
@ -7,6 +7,9 @@
|
|||
|
||||
// const BASE_URL = '/image'
|
||||
|
||||
const Images = require('../models/images');
|
||||
const ObjectId = require('mongodb').ObjectId;
|
||||
|
||||
describe.skip("POST /upload", () => {
|
||||
|
||||
describe("when the jwt is not sent", () => {
|
||||
|
|
@ -64,3 +67,83 @@ describe.skip("GET /get", () => {
|
|||
})
|
||||
|
||||
})
|
||||
|
||||
describe('Images', () => {
|
||||
let db;
|
||||
let images;
|
||||
let dbConn;
|
||||
let mockImagesCollection;
|
||||
|
||||
beforeEach(() => {
|
||||
mockImagesCollection = {
|
||||
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
|
||||
findOne: jest.fn()
|
||||
};
|
||||
|
||||
dbConn = {
|
||||
collection: jest.fn().mockReturnValue(mockImagesCollection)
|
||||
};
|
||||
|
||||
db = {
|
||||
connect: jest.fn().mockResolvedValue(),
|
||||
getConnection: jest.fn().mockReturnValue(dbConn)
|
||||
};
|
||||
|
||||
images = new Images(db);
|
||||
});
|
||||
|
||||
describe('upload', () => {
|
||||
it('should upload an image and return the inserted ID', async () => {
|
||||
const testFile = { originalname: 'test.png', buffer: Buffer.from('dummydata'), mimetype: 'image/png' };
|
||||
const userId = 'user123';
|
||||
|
||||
const result = await images.upload(testFile, userId);
|
||||
|
||||
expect(db.connect).toHaveBeenCalled();
|
||||
expect(db.getConnection).toHaveBeenCalled();
|
||||
expect(dbConn.collection).toHaveBeenCalledWith('images');
|
||||
expect(mockImagesCollection.insertOne).toHaveBeenCalledWith({
|
||||
userId: userId,
|
||||
file_name: 'test.png',
|
||||
file_content: testFile.buffer.toString('base64'),
|
||||
mime_type: 'image/png',
|
||||
created_at: expect.any(Date)
|
||||
});
|
||||
expect(result).toBe('image123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('should retrieve the image if found', async () => {
|
||||
const imageId = '65d9a8f9b5e8d1a5e6a8c9f0';
|
||||
const testImage = {
|
||||
file_name: 'test.png',
|
||||
file_content: Buffer.from('dummydata').toString('base64'),
|
||||
mime_type: 'image/png'
|
||||
};
|
||||
mockImagesCollection.findOne.mockResolvedValue(testImage);
|
||||
|
||||
const result = await images.get(imageId);
|
||||
|
||||
expect(db.connect).toHaveBeenCalled();
|
||||
expect(db.getConnection).toHaveBeenCalled();
|
||||
expect(dbConn.collection).toHaveBeenCalledWith('images');
|
||||
expect(mockImagesCollection.findOne).toHaveBeenCalledWith({ _id: ObjectId.createFromHexString(imageId) });
|
||||
expect(result).toEqual({
|
||||
file_name: 'test.png',
|
||||
file_content: Buffer.from('dummydata'),
|
||||
mime_type: 'image/png'
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null if image is not found', async () => {
|
||||
const imageId = '65d9a8f9b5e8d1a5e6a8c9f0';
|
||||
mockImagesCollection.findOne.mockResolvedValue(null);
|
||||
|
||||
const result = await images.get(imageId);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,23 @@ class ImagesController {
|
|||
}
|
||||
};
|
||||
|
||||
//TODO TEST
|
||||
getAll = async (req, res, next) => {
|
||||
try {
|
||||
|
||||
const images = await this.images.getAll();
|
||||
|
||||
if (!images || images.length === 0) {
|
||||
throw new AppError(IMAGE_NOT_FOUND);
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
return res.status(200).json(imagesName);
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
module.exports = ImagesController;
|
||||
|
|
|
|||
|
|
@ -42,6 +42,28 @@ class Images {
|
|||
};
|
||||
}
|
||||
|
||||
//TODO TEST
|
||||
async getAll() {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const imagesCollection = conn.collection('images');
|
||||
|
||||
const result = await imagesCollection.find({});
|
||||
|
||||
if (!result) return null;
|
||||
|
||||
//TODO latency issues -> images > 20
|
||||
const imagesName = result.map(image => ({
|
||||
id: image.id,
|
||||
file_name: image.file_name,
|
||||
file_content: Buffer.from(image.file_content, 'base64'),
|
||||
mime_type: image.mime_type
|
||||
}));
|
||||
|
||||
return imagesName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Images;
|
||||
|
|
|
|||
Loading…
Reference in a new issue