Compare commits

..

No commits in common. "1512f320c87c510085b6725479043b363650732a" and "f6afb1f7ed092461a70e1188da404c31a3311be1" have entirely different histories.

2 changed files with 28 additions and 39 deletions

View file

@ -73,22 +73,12 @@ describe('Images', () => {
let images; let images;
let dbConn; let dbConn;
let mockImagesCollection; let mockImagesCollection;
let mockFindCursor;
beforeEach(() => { beforeEach(() => {
mockFindCursor = {
sort: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
toArray: jest.fn(),
};
mockImagesCollection = { mockImagesCollection = {
insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }), insertOne: jest.fn().mockResolvedValue({ insertedId: 'image123' }),
findOne: jest.fn(), findOne: jest.fn(),
find: jest.fn().mockReturnValue(mockFindCursor), find: jest.fn().mockReturnValue({ sort: jest.fn().mockReturnValue([]) })
countDocuments: jest.fn()
}; };
dbConn = { dbConn = {
@ -157,41 +147,35 @@ describe('Images', () => {
}); });
}); });
describe('getImages', () => { describe('getAll', () => {
it('should retrieve a paginated list of images', async () => { it('should retrieve a paginated list of images', async () => {
const mockImages = [ const mockImages = [
{ _id: '1', userId: 'user1', file_name: 'image1.png', file_content: Buffer.from('data1'), mime_type: 'image/png' }, { id: '1', file_name: 'image1.png', file_content: Buffer.from('data1').toString('base64'), mime_type: 'image/png' },
{ _id: '2', userId: 'user2', file_name: 'image2.png', file_content: Buffer.from('data2'), mime_type: 'image/png' } { id: '2', file_name: 'image2.png', file_content: Buffer.from('data2').toString('base64'), mime_type: 'image/png' }
]; ];
mockImagesCollection.find.mockReturnValue({ sort: jest.fn().mockReturnValue(mockImages) });
mockImagesCollection.countDocuments.mockResolvedValue(2);
mockFindCursor.toArray.mockResolvedValue(mockImages); const result = await images.getAll(1, 10);
const result = await images.getImages(1, 10);
expect(db.connect).toHaveBeenCalled(); expect(db.connect).toHaveBeenCalled();
expect(db.getConnection).toHaveBeenCalled(); expect(db.getConnection).toHaveBeenCalled();
expect(dbConn.collection).toHaveBeenCalledWith('images'); expect(dbConn.collection).toHaveBeenCalledWith('images');
expect(mockImagesCollection.find).toHaveBeenCalledWith({}); expect(mockImagesCollection.find).toHaveBeenCalledWith({});
expect(mockFindCursor.sort).toHaveBeenCalledWith({ created_at: 1 }); expect(result.length).toEqual(mockImages.length);
expect(mockFindCursor.skip).toHaveBeenCalledWith(0); expect(result).toEqual([
expect(mockFindCursor.limit).toHaveBeenCalledWith(10); { id: '1', file_name: 'image1.png', file_content: Buffer.from('data1'), mime_type: 'image/png' },
expect(result).toEqual({ { id: '2', file_name: 'image2.png', file_content: Buffer.from('data2'), mime_type: 'image/png' }
images: [ ]);
{ id: '1', user: 'user1', file_name: 'image1.png', file_content: 'ZGF0YTE=', mime_type: 'image/png' },
{ id: '2', user: 'user2', file_name: 'image2.png', file_content: 'ZGF0YTI=', mime_type: 'image/png' }
],
total: 2,
});
}); });
it('should return an empty array if no images are found', async () => { it('should return null if not images is not found', async () => {
mockImagesCollection.countDocuments.mockResolvedValue(0); mockImagesCollection.find.mockReturnValue({ sort: jest.fn().mockReturnValue(undefined) });
mockFindCursor.toArray.mockResolvedValue([]); const result = await images.getAll(1, 10);
expect(db.connect).toHaveBeenCalled();
const result = await images.getImages(1, 10); expect(db.getConnection).toHaveBeenCalled();
expect(dbConn.collection).toHaveBeenCalledWith('images');
expect(result).toEqual({ images: [], total: 0 }); expect(mockImagesCollection.find).toHaveBeenCalledWith({});
expect(result).toEqual(null);
}); });
}); });
}); });

View file

@ -54,7 +54,12 @@ class UsersController {
const token = jwt.create(user.email, user._id); const token = jwt.create(user.email, user._id);
return res.status(200).json({ token }); let result = {
token: token,
userId: user._id
}
return res.status(200).json({ result });
} catch (error) { } catch (error) {
next(error); next(error);
} }