mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
one passing test
This commit is contained in:
parent
112062c0b2
commit
e502da66e2
14 changed files with 1279 additions and 380 deletions
478
server/__tests__/folders.mongodb.test.js
Normal file
478
server/__tests__/folders.mongodb.test.js
Normal file
|
|
@ -0,0 +1,478 @@
|
|||
const Folder = require('../models/folders');
|
||||
const Quiz = require('../models/quizzes');
|
||||
const db = require('../config/db');
|
||||
|
||||
console.log('db:', db); // Debugging line
|
||||
console.log('db.getConnection:', db.getConnection); // Debugging line
|
||||
|
||||
describe('Folders', () => {
|
||||
let database;
|
||||
|
||||
beforeAll(async () => {
|
||||
console.log('beforeAll: db.getConnection:', db.getConnection); // Debugging line
|
||||
database = await db.getConnection();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await db.closeConnection();
|
||||
});
|
||||
|
||||
it('should insert a folder into collection', async () => {
|
||||
const folders = new Folder(db, Quiz);
|
||||
const folderId = await folders.create('Test Folder', '12345');
|
||||
const result = await database.collection('folders').findOne({ _id: folderId });
|
||||
expect(result).toBeTruthy();
|
||||
console.log('found folder result:', result); // Debugging line
|
||||
expect(result.title).toBe('Test Folder');
|
||||
});
|
||||
|
||||
// beforeEach(() => {
|
||||
// jest.clearAllMocks(); // Clear any previous mock calls
|
||||
|
||||
// // Mock the collection object
|
||||
// collection = {
|
||||
// findOne: jest.fn(),
|
||||
// insertOne: jest.fn(),
|
||||
// find: jest.fn().mockReturnValue({ toArray: jest.fn() }), // Mock the find method
|
||||
// deleteOne: jest.fn(),
|
||||
// deleteMany: jest.fn(),
|
||||
// updateOne: jest.fn(),
|
||||
// };
|
||||
|
||||
// // Mock the database connection
|
||||
// db = {
|
||||
// connect: jest.fn(),
|
||||
// getConnection: jest.fn().mockReturnThis(), // Add getConnection method
|
||||
// collection: jest.fn().mockReturnValue(collection),
|
||||
// };
|
||||
|
||||
// quizzes = new Quizzes(db);
|
||||
// folders = new Folders(db, quizzes);
|
||||
|
||||
// });
|
||||
|
||||
// // create
|
||||
// describe('create', () => {
|
||||
// it('should create a new folder and return the new folder ID', async () => {
|
||||
// const title = 'Test Folder';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue(null);
|
||||
// collection.insertOne.mockResolvedValue({ insertedId: new ObjectId() });
|
||||
|
||||
// const result = await folders.create(title, '12345');
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ title, userId: '12345' });
|
||||
// expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ title, userId: '12345' }));
|
||||
// expect(result).toBeDefined();
|
||||
// });
|
||||
|
||||
// // throw an error if userId is undefined
|
||||
// it('should throw an error if userId is undefined', async () => {
|
||||
// const title = 'Test Folder';
|
||||
|
||||
// await expect(folders.create(title, undefined)).rejects.toThrow('Missing required parameter(s)');
|
||||
|
||||
// expect(db.connect).not.toHaveBeenCalled();
|
||||
// });
|
||||
|
||||
// it('should throw an error if the folder already exists', async () => {
|
||||
// const title = 'Existing Folder';
|
||||
// const userId = '66fc70bea1b9e87655cf17c9';
|
||||
|
||||
// // Mock the database response of a found folder
|
||||
// collection.findOne.mockResolvedValue(
|
||||
// // real result from mongosh
|
||||
// {
|
||||
// _id: ObjectId.createFromHexString('66fd33fd81758a882ce99aae'),
|
||||
// userId: userId,
|
||||
// title: title,
|
||||
// created_at: new Date('2024-10-02T11:52:29.797Z')
|
||||
// }
|
||||
// );
|
||||
|
||||
// await expect(folders.create(title, userId)).rejects.toThrow('Folder already exists');
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ title, userId: userId });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // getUserFolders
|
||||
// describe('getUserFolders', () => {
|
||||
// it('should return all folders for a user', async () => {
|
||||
// const userId = '12345';
|
||||
// const userFolders = [
|
||||
// { title: 'Folder 1', userId },
|
||||
// { title: 'Folder 2', userId },
|
||||
// ];
|
||||
|
||||
// // Mock the database response
|
||||
// collection.find().toArray.mockResolvedValue(userFolders);
|
||||
|
||||
// const result = await folders.getUserFolders(userId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.find).toHaveBeenCalledWith({ userId });
|
||||
// expect(result).toEqual(userFolders);
|
||||
// });
|
||||
// });
|
||||
|
||||
// // getOwner
|
||||
// describe('getOwner', () => {
|
||||
// it('should return the owner of a folder', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue({ userId });
|
||||
|
||||
// const result = await folders.getOwner(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// expect(result).toBe(userId);
|
||||
// });
|
||||
// });
|
||||
|
||||
// // write a test for getContent
|
||||
// describe('getContent', () => {
|
||||
// it('should return the content of a folder', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const content = [
|
||||
// { title: 'Quiz 1', content: [] },
|
||||
// { title: 'Quiz 2', content: [] },
|
||||
// ];
|
||||
|
||||
// // Mock the database response
|
||||
// collection.find().toArray.mockResolvedValue(content);
|
||||
|
||||
// const result = await folders.getContent(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('files');
|
||||
// expect(collection.find).toHaveBeenCalledWith({ folderId });
|
||||
// expect(result).toEqual(content);
|
||||
// });
|
||||
|
||||
// it('should return an empty array if the folder has no content', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.find().toArray.mockResolvedValue([]);
|
||||
|
||||
// const result = await folders.getContent(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('files');
|
||||
// expect(collection.find).toHaveBeenCalledWith({ folderId });
|
||||
// expect(result).toEqual([]);
|
||||
// });
|
||||
// });
|
||||
|
||||
// // delete
|
||||
// describe('delete', () => {
|
||||
// it('should delete a folder and return true', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.deleteOne.mockResolvedValue({ deletedCount: 1 });
|
||||
|
||||
|
||||
// // Mock the folders.quizModel.deleteQuizzesByFolderId()
|
||||
// jest.spyOn(quizzes, 'deleteQuizzesByFolderId').mockResolvedValue(true);
|
||||
|
||||
// const result = await folders.delete(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// expect(result).toBe(true);
|
||||
// });
|
||||
|
||||
// it('should return false if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.deleteOne.mockResolvedValue({ deletedCount: 0 });
|
||||
|
||||
// const result = await folders.delete(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.deleteOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// expect(result).toBe(false);
|
||||
// });
|
||||
// });
|
||||
|
||||
// // rename
|
||||
// describe('rename', () => {
|
||||
// it('should rename a folder and return true', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const newTitle = 'New Folder Name';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.updateOne.mockResolvedValue({ modifiedCount: 1 });
|
||||
|
||||
// const result = await folders.rename(folderId, userId, newTitle);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // { _id: ObjectId.createFromHexString(folderId), userId: userId }, { $set: { title: newTitle } }
|
||||
// expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId), userId: userId }, { $set: { title: newTitle } });
|
||||
// expect(result).toBe(true);
|
||||
// });
|
||||
|
||||
// it('should return false if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const newTitle = 'New Folder Name';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
|
||||
|
||||
// const result = await folders.rename(folderId, userId, newTitle);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId), userId: userId }, { $set: { title: newTitle } });
|
||||
// expect(result).toBe(false);
|
||||
// });
|
||||
|
||||
// it('should throw an error if the new title is already in use', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const newTitle = 'Existing Folder';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue({ title: newTitle });
|
||||
// collection.updateOne.mockResolvedValue({ modifiedCount: 0 });
|
||||
|
||||
// await expect(folders.rename(folderId, userId, newTitle)).rejects.toThrow(`Folder with name '${newTitle}' already exists.`);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // expect(collection.updateOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) }, { $set: { title: newTitle } });
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ userId: userId, title: newTitle });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // duplicate
|
||||
// describe('duplicate', () => {
|
||||
// it('should duplicate a folder and return the new folder ID', async () => {
|
||||
// const userId = '12345';
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const sourceFolder = {title: 'SourceFolder', userId: userId, content: []};
|
||||
// const duplicatedFolder = {title: 'SourceFolder (1)', userId: userId, created_at: expect.any(Date), content: []};
|
||||
|
||||
// // Mock the database responses for the folder and the new folder (first one is found, second one is null)
|
||||
// // mock the findOne method
|
||||
// jest.spyOn(collection, 'findOne')
|
||||
// .mockResolvedValueOnce(sourceFolder) // source file exists
|
||||
// .mockResolvedValueOnce(null); // new name is not found
|
||||
|
||||
// // Mock the folder create method
|
||||
// const createSpy = jest.spyOn(folders, 'create').mockResolvedValue(new ObjectId());
|
||||
|
||||
// // mock the folder.getContent method
|
||||
// jest.spyOn(folders, 'getContent').mockResolvedValue([{ title: 'Quiz 1', content: [] }]);
|
||||
|
||||
// // Mock the quizzes.create method
|
||||
// jest.spyOn(quizzes, 'create').mockResolvedValue(new ObjectId());
|
||||
|
||||
// const result = await folders.duplicate(folderId, userId);
|
||||
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
|
||||
// // expect folders.create method was called
|
||||
// expect(createSpy).toHaveBeenCalledWith(duplicatedFolder.title, userId);
|
||||
// // expect the getContent method was called
|
||||
// expect(folders.getContent).toHaveBeenCalledWith(folderId);
|
||||
// // expect the quizzes.create method was called
|
||||
// expect(quizzes.create).toHaveBeenCalledWith('Quiz 1', [], expect.any(String), userId);
|
||||
|
||||
// expect(result).toBeDefined();
|
||||
// });
|
||||
|
||||
// it('should throw an error if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // Mock the database response for the source
|
||||
// collection.findOne.mockResolvedValue(null);
|
||||
|
||||
// await expect(folders.duplicate(folderId, '54321')).rejects.toThrow(`Folder ${folderId} not found`);
|
||||
|
||||
// // expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId), userId: '54321' });
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('folderExists', () => {
|
||||
// it('should return true if folder exists', async () => {
|
||||
// const title = 'Test Folder';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue({ title, userId });
|
||||
|
||||
// const result = await folders.folderExists(title, userId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ title, userId });
|
||||
// expect(result).toBe(true);
|
||||
// });
|
||||
|
||||
// it('should return false if folder does not exist', async () => {
|
||||
// const title = 'Nonexistent Folder';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue(null);
|
||||
|
||||
// const result = await folders.folderExists(title, userId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ title, userId });
|
||||
// expect(result).toBe(false);
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('copy', () => {
|
||||
// it('should copy a folder and return the new folder ID', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const userId = '12345';
|
||||
// const newFolderId = new ObjectId();
|
||||
// // Mock some quizzes that are in folder.content
|
||||
// const sourceFolder = {
|
||||
// title: 'Test Folder',
|
||||
// content: [
|
||||
// { title: 'Quiz 1', content: [] },
|
||||
// { title: 'Quiz 2', content: [] },
|
||||
// ],
|
||||
// };
|
||||
|
||||
// // Mock the response from getFolderWithContent
|
||||
// jest.spyOn(folders, 'getFolderWithContent').mockResolvedValue(sourceFolder);
|
||||
// jest.spyOn(folders, 'create').mockResolvedValue(newFolderId);
|
||||
// // Mock the response from Quiz.createQuiz
|
||||
// jest.spyOn(quizzes, 'create').mockImplementation(() => {});
|
||||
|
||||
// const result = await folders.copy(folderId, userId);
|
||||
|
||||
// // expect(db.connect).toHaveBeenCalled();
|
||||
// // expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// // expect(collection.insertOne).toHaveBeenCalledWith(expect.objectContaining({ userId }));
|
||||
// expect(result).toBe(newFolderId);
|
||||
// });
|
||||
|
||||
// it('should throw an error if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const userId = '12345';
|
||||
|
||||
// // Mock the response from getFolderWithContent
|
||||
// jest.spyOn(folders, 'getFolderWithContent').mockImplementation(() => {
|
||||
// throw new Error(`Folder ${folderId} not found`);
|
||||
// });
|
||||
|
||||
// await expect(folders.copy(folderId, userId)).rejects.toThrow(`Folder ${folderId} not found`);
|
||||
|
||||
// // expect(db.connect).toHaveBeenCalled();
|
||||
// // expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // write a test for getFolderWithContent
|
||||
// describe('getFolderWithContent', () => {
|
||||
// it('should return a folder with content', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const folder = {
|
||||
// _id: new ObjectId(folderId),
|
||||
// title: 'Test Folder',
|
||||
// };
|
||||
// const content = {
|
||||
// content : [
|
||||
// { title: 'Quiz 1', content: [] },
|
||||
// { title: 'Quiz 2', content: [] },
|
||||
// ]};
|
||||
|
||||
// // Mock the response from getFolderById
|
||||
// jest.spyOn(folders, 'getFolderById').mockResolvedValue(folder);
|
||||
|
||||
// // Mock the response from getContent
|
||||
// jest.spyOn(folders, 'getContent').mockResolvedValue(content);
|
||||
|
||||
// const result = await folders.getFolderWithContent(folderId);
|
||||
|
||||
// // expect(db.connect).toHaveBeenCalled();
|
||||
// // expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// expect(result).toEqual({
|
||||
// ...folder,
|
||||
// content: content
|
||||
// });
|
||||
// });
|
||||
|
||||
// it('should throw an error if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // // Mock the database response
|
||||
// // collection.findOne.mockResolvedValue(null);
|
||||
|
||||
// // Mock getFolderById to throw an error
|
||||
// jest.spyOn(folders, 'getFolderById').mockImplementation(() => {
|
||||
// throw new Error(`Folder ${folderId} not found`);
|
||||
// });
|
||||
|
||||
// await expect(folders.getFolderWithContent(folderId)).rejects.toThrow(`Folder ${folderId} not found`);
|
||||
|
||||
// // expect(db.connect).toHaveBeenCalled();
|
||||
// // expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// // expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// });
|
||||
// });
|
||||
|
||||
// // write a test for getFolderById
|
||||
// describe('getFolderById', () => {
|
||||
// it('should return a folder by ID', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
// const folder = {
|
||||
// _id: new ObjectId(folderId),
|
||||
// title: 'Test Folder',
|
||||
// };
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue(folder);
|
||||
|
||||
// const result = await folders.getFolderById(folderId);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// expect(result).toEqual(folder);
|
||||
// });
|
||||
|
||||
// it('should throw an error if the folder does not exist', async () => {
|
||||
// const folderId = '60c72b2f9b1d8b3a4c8e4d3b';
|
||||
|
||||
// // Mock the database response
|
||||
// collection.findOne.mockResolvedValue(null);
|
||||
|
||||
// await expect(folders.getFolderById(folderId)).resolves.toThrow(`Folder ${folderId} not found`);
|
||||
|
||||
// expect(db.connect).toHaveBeenCalled();
|
||||
// expect(db.collection).toHaveBeenCalledWith('folders');
|
||||
// expect(collection.findOne).toHaveBeenCalledWith({ _id: new ObjectId(folderId) });
|
||||
// });
|
||||
// });
|
||||
});
|
||||
|
|
@ -118,8 +118,8 @@ async function start() {
|
|||
const port = process.env.PORT || 4400;
|
||||
|
||||
// Check DB connection
|
||||
await db.connect();
|
||||
db.getConnection();
|
||||
|
||||
await db.getConnection();
|
||||
console.log(`Connexion MongoDB établie`);
|
||||
|
||||
server.listen(port, () => {
|
||||
|
|
|
|||
|
|
@ -5,14 +5,17 @@ dotenv.config();
|
|||
|
||||
class DBConnection {
|
||||
constructor() {
|
||||
this.mongoURI = process.env.MONGO_URI;
|
||||
this.databaseName = process.env.MONGO_DATABASE;
|
||||
// for testing with @shelf/jest-mongodb
|
||||
this.mongoURI = globalThis.__MONGO_URI__ || process.env.MONGO_URI;
|
||||
this.databaseName = globalThis.__MONGO_DB_NAME__ || process.env.MONGO_DATABASE;
|
||||
this.client = null;
|
||||
this.connection = null;
|
||||
console.log(`db.js: Mongo URI: ${this.mongoURI}`);
|
||||
console.log(`db.js: Mongo DB: ${this.databaseName}`);
|
||||
}
|
||||
|
||||
// Connect to the database, but don't reconnect if already connected
|
||||
async connect() {
|
||||
// Return the current database connection, creating it if necessary
|
||||
async getConnection() {
|
||||
if (this.connection) {
|
||||
console.log('Using existing MongoDB connection');
|
||||
return this.connection;
|
||||
|
|
@ -20,7 +23,10 @@ class DBConnection {
|
|||
|
||||
try {
|
||||
// Create the MongoClient only if the connection does not exist
|
||||
this.client = new MongoClient(this.mongoURI);
|
||||
this.client = new MongoClient(this.mongoURI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
await this.client.connect();
|
||||
this.connection = this.client.db(this.databaseName);
|
||||
console.log('MongoDB connected');
|
||||
|
|
@ -31,18 +37,11 @@ class DBConnection {
|
|||
}
|
||||
}
|
||||
|
||||
// Return the current database connection
|
||||
getConnection() {
|
||||
if (!this.connection) {
|
||||
throw new Error('MongoDB connection not established');
|
||||
}
|
||||
return this.connection;
|
||||
}
|
||||
|
||||
// Close the MongoDB connection gracefully
|
||||
async closeConnection() {
|
||||
if (this.client) {
|
||||
await this.client.close();
|
||||
this.connection = null;
|
||||
console.log('MongoDB connection closed');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
server/jest-mongodb-config.js
Normal file
12
server/jest-mongodb-config.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module.exports = {
|
||||
mongodbMemoryServerOptions: {
|
||||
binary: {
|
||||
version: '4.0.3',
|
||||
skipMD5: true,
|
||||
},
|
||||
instance: {
|
||||
dbName: 'jest',
|
||||
},
|
||||
autoStart: false,
|
||||
},
|
||||
};
|
||||
7
server/jest.setup.js
Normal file
7
server/jest.setup.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require('dotenv').config();
|
||||
|
||||
module.exports = {
|
||||
preset: '@shelf/jest-mongodb',
|
||||
};
|
||||
|
||||
console.log('jest.setup.js: MongoDB URI:', process.env.MONGO_URI);
|
||||
|
|
@ -8,8 +8,8 @@ class AuthProvider {
|
|||
}
|
||||
|
||||
async getId(name){
|
||||
await db.connect()
|
||||
const conn = db.getConnection();
|
||||
|
||||
const conn = await db.getConnection();
|
||||
|
||||
const collection = conn.collection('authprovider');
|
||||
|
||||
|
|
@ -22,8 +22,8 @@ class AuthProvider {
|
|||
}
|
||||
|
||||
async create(name) {
|
||||
await db.connect()
|
||||
const conn = db.getConnection();
|
||||
|
||||
const conn = await db.getConnection();
|
||||
|
||||
const collection = conn.collection('authprovider');
|
||||
|
||||
|
|
@ -41,4 +41,4 @@ class AuthProvider {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = new AuthProvider;
|
||||
module.exports = new AuthProvider;
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ class AuthUserAssociation {
|
|||
}
|
||||
|
||||
async find_user_association(provider_name,auth_id){
|
||||
await db.connect()
|
||||
const conn = db.getConnection();
|
||||
|
||||
const conn = await db.getConnection();
|
||||
|
||||
const collection = conn.collection('authUserAssociation');
|
||||
const provider_id = await authProvider.getId(provider_name)
|
||||
|
|
@ -24,8 +24,8 @@ class AuthUserAssociation {
|
|||
}
|
||||
|
||||
async link(provider_name,auth_id,user_id){
|
||||
await db.connect()
|
||||
const conn = db.getConnection();
|
||||
|
||||
const conn = await db.getConnection();
|
||||
|
||||
const collection = conn.collection('authUserAssociation');
|
||||
const provider_id = await authProvider.getId(provider_name)
|
||||
|
|
@ -43,8 +43,8 @@ class AuthUserAssociation {
|
|||
}
|
||||
|
||||
async unlink(provider_name,user_id){
|
||||
await db.connect()
|
||||
const conn = db.getConnection();
|
||||
|
||||
const conn = await db.getConnection();
|
||||
|
||||
const collection = conn.collection('authUserAssociation');
|
||||
const provider_id = await authProvider.getId(provider_name)
|
||||
|
|
@ -56,4 +56,4 @@ class AuthUserAssociation {
|
|||
} else return null
|
||||
}
|
||||
}
|
||||
module.exports = new AuthUserAssociation;
|
||||
module.exports = new AuthUserAssociation;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
const ObjectId = require('mongodb').ObjectId;
|
||||
const { generateUniqueTitle } = require('./utils');
|
||||
|
||||
class Folders {
|
||||
class Folder {
|
||||
constructor(db, quizModel) {
|
||||
this.db = db;
|
||||
this.quizModel = quizModel;
|
||||
|
|
@ -14,8 +14,8 @@ class Folders {
|
|||
throw new Error('Missing required parameter(s)');
|
||||
}
|
||||
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -37,8 +37,8 @@ class Folders {
|
|||
}
|
||||
|
||||
async getUserFolders(userId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -48,8 +48,8 @@ class Folders {
|
|||
}
|
||||
|
||||
async getOwner(folderId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -60,8 +60,8 @@ class Folders {
|
|||
|
||||
// finds all quizzes in a folder
|
||||
async getContent(folderId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const filesCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -71,8 +71,8 @@ class Folders {
|
|||
}
|
||||
|
||||
async delete(folderId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -85,8 +85,8 @@ class Folders {
|
|||
}
|
||||
|
||||
async rename(folderId, userId, newTitle) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ class Folders {
|
|||
}
|
||||
|
||||
async duplicate(folderId, userId) {
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
const sourceFolder = await foldersCollection.findOne({ _id: ObjectId.createFromHexString(folderId), userId: userId });
|
||||
|
|
@ -139,8 +139,7 @@ class Folders {
|
|||
}
|
||||
|
||||
async folderExists(title, userId) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
const existingFolder = await foldersCollection.findOne({ title: title, userId: userId });
|
||||
|
|
@ -163,8 +162,7 @@ class Folders {
|
|||
}
|
||||
|
||||
async getFolderById(folderId) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const foldersCollection = conn.collection('folders');
|
||||
|
||||
|
|
@ -192,4 +190,4 @@ class Folders {
|
|||
|
||||
}
|
||||
|
||||
module.exports = Folders;
|
||||
module.exports = Folder;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ class Images {
|
|||
}
|
||||
|
||||
async upload(file, userId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const imagesCollection = conn.collection('images');
|
||||
|
||||
|
|
@ -26,8 +26,8 @@ class Images {
|
|||
}
|
||||
|
||||
async get(id) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const imagesCollection = conn.collection('images');
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ class Quiz {
|
|||
|
||||
async create(title, content, folderId, userId) {
|
||||
// console.log(`quizzes: create title: ${title}, folderId: ${folderId}, userId: ${userId}`);
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -37,8 +37,8 @@ class Quiz {
|
|||
}
|
||||
|
||||
async getOwner(quizId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -48,8 +48,8 @@ class Quiz {
|
|||
}
|
||||
|
||||
async getContent(quizId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -59,8 +59,8 @@ class Quiz {
|
|||
}
|
||||
|
||||
async delete(quizId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -71,8 +71,7 @@ class Quiz {
|
|||
return true;
|
||||
}
|
||||
async deleteQuizzesByFolderId(folderId) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizzesCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -82,8 +81,8 @@ class Quiz {
|
|||
}
|
||||
|
||||
async update(quizId, newTitle, newContent) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -102,8 +101,8 @@ class Quiz {
|
|||
}
|
||||
|
||||
async move(quizId, newFolderId) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
|
|
@ -118,7 +117,7 @@ class Quiz {
|
|||
}
|
||||
|
||||
async duplicate(quizId, userId) {
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const quizCollection = conn.collection('files');
|
||||
|
||||
const sourceQuiz = await quizCollection.findOne({ _id: ObjectId.createFromHexString(quizId), userId: userId });
|
||||
|
|
@ -141,8 +140,7 @@ class Quiz {
|
|||
}
|
||||
|
||||
async quizExists(title, userId) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const filesCollection = conn.collection('files');
|
||||
const existingFolder = await filesCollection.findOne({ title: title, userId: userId });
|
||||
|
|
@ -17,8 +17,7 @@ class Rooms
|
|||
throw new Error("Room already exists");
|
||||
}
|
||||
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
const newRoom = {
|
||||
|
|
@ -34,8 +33,7 @@ class Rooms
|
|||
|
||||
async getUserRooms(userId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
@ -46,8 +44,7 @@ class Rooms
|
|||
|
||||
async getOwner(roomId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
@ -60,8 +57,7 @@ class Rooms
|
|||
|
||||
async getContent(roomId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
if (!ObjectId.isValid(roomId))
|
||||
{
|
||||
|
|
@ -75,8 +71,7 @@ class Rooms
|
|||
|
||||
async delete(roomId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
@ -91,8 +86,7 @@ class Rooms
|
|||
|
||||
async rename(roomId, userId, newTitle)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
@ -118,8 +112,7 @@ class Rooms
|
|||
{
|
||||
try
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const existingRoom = await conn.collection("rooms").findOne({
|
||||
title: title.toUpperCase(),
|
||||
userId: userId,
|
||||
|
|
@ -132,8 +125,7 @@ class Rooms
|
|||
}
|
||||
async getRoomById(roomId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
@ -159,8 +151,7 @@ class Rooms
|
|||
}
|
||||
async getRoomTitleByUserId(userId)
|
||||
{
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const roomsCollection = conn.collection("rooms");
|
||||
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ class Users {
|
|||
}
|
||||
|
||||
async register(userInfos) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
@ -56,8 +55,7 @@ class Users {
|
|||
async login(email, password) {
|
||||
console.log(`models/users: login: email: ${email}, password: ${password}`);
|
||||
try {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
const user = await userCollection.findOne({ email: email });
|
||||
|
|
@ -90,8 +88,7 @@ class Users {
|
|||
}
|
||||
|
||||
async changePassword(email, newPassword) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
@ -108,8 +105,7 @@ class Users {
|
|||
}
|
||||
|
||||
async delete(email) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
@ -121,8 +117,7 @@ class Users {
|
|||
}
|
||||
|
||||
async getId(email) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
@ -136,8 +131,7 @@ class Users {
|
|||
}
|
||||
|
||||
async getById(id) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
@ -151,8 +145,7 @@ class Users {
|
|||
}
|
||||
|
||||
async editUser(userInfo) {
|
||||
await this.db.connect();
|
||||
const conn = this.db.getConnection();
|
||||
const conn = await this.db.getConnection();
|
||||
|
||||
const userCollection = conn.collection("users");
|
||||
|
||||
|
|
|
|||
963
server/package-lock.json
generated
963
server/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -16,39 +16,41 @@
|
|||
"dependencies": {
|
||||
"bcrypt": "^5.1.1",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.4",
|
||||
"dotenv": "^16.4.7",
|
||||
"express": "^4.18.2",
|
||||
"express-list-endpoints": "^7.1.1",
|
||||
"express-session": "^1.18.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mongodb": "^6.3.0",
|
||||
"mongodb": "^6.14.2",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"nodemailer": "^6.9.9",
|
||||
"nodemailer": "^6.10.0",
|
||||
"passport": "^0.7.0",
|
||||
"passport-oauth2": "^1.8.0",
|
||||
"passport-openidconnect": "^0.1.2",
|
||||
"patch-package": "^8.0.0",
|
||||
"socket.io": "^4.7.2",
|
||||
"socket.io-client": "^4.7.2"
|
||||
"socket.io-client": "^4.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.18.0",
|
||||
"@eslint/js": "^9.22.0",
|
||||
"@shelf/jest-mongodb": "^5.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"eslint": "^9.18.0",
|
||||
"globals": "^15.14.0",
|
||||
"eslint": "^9.22.0",
|
||||
"globals": "^16.0.0",
|
||||
"jest": "^29.7.0",
|
||||
"jest-mock": "^29.7.0",
|
||||
"nodemon": "^3.0.1",
|
||||
"nodemon": "^3.1.9",
|
||||
"supertest": "^6.3.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": "20.x"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node",
|
||||
"preset": "@shelf/jest-mongodb",
|
||||
"testMatch": [
|
||||
"**/__tests__/**/*.js?(x)",
|
||||
"**/?(*.)+(spec|test).js?(x)"
|
||||
]
|
||||
],
|
||||
"setupFilesAfterEnv": ["<rootDir>/jest.setup.js"]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue