diff --git a/api/openapi.yaml b/api/openapi.yaml index c66554ca9e238aa152d7aa465a86ac92042045ed..715f361723fec4fe627cf6436d8dcc8f4394e261 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -30,8 +30,8 @@ paths: post: tags: - graphql - summary: Execué une requête GraphQL - description: Exécute une requête GraphQL + summary: Exécuter une requête GraphQL + description: Exécuter une requête GraphQL operationId: executeGraphQLQuery requestBody: required: true diff --git a/backup/img1.jpg b/backup/img1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..24d86ac1e5b1490106b79d52c1ae63b20c6e0322 Binary files /dev/null and b/backup/img1.jpg differ diff --git a/backup/img2.jpg b/backup/img2.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2dd6b3777384c8a56060d71c6d0fa0eec83ba4f3 Binary files /dev/null and b/backup/img2.jpg differ diff --git a/backup/img3.jpg b/backup/img3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5fa40389bea599abf6343cea575a789b3aa3b9e1 Binary files /dev/null and b/backup/img3.jpg differ diff --git a/backup/maisonavendre.jpg b/backup/maisonavendre.jpg new file mode 100644 index 0000000000000000000000000000000000000000..90ca8a2fb11f6f9f25a4ca5781b4e8cf02b6328a Binary files /dev/null and b/backup/maisonavendre.jpg differ diff --git a/index.js b/index.js index 94faaa815e0f7a90db9ed15349443e43f29eefbe..5b472fe5ea39a67c565b4c514a9343a8807a1dfe 100644 --- a/index.js +++ b/index.js @@ -81,3 +81,5 @@ startServer().then(() => { console.error('Erreur de connexion à la base de données:', error); }); }); + +module.exports = app; \ No newline at end of file diff --git a/test/ad.test.js b/test/ad.test.js new file mode 100644 index 0000000000000000000000000000000000000000..2d48089ddd0579d5db23e22fd4654a5be44c2e83 --- /dev/null +++ b/test/ad.test.js @@ -0,0 +1,72 @@ +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const fs = require('fs'); +const path = require('path'); +const app = require('../index'); +const expect = chai.expect; + +chai.use(chaiHttp); + +let authToken; +let adId; + +/*describe('Ad Routes', () => { + before(async () => { + try { + const loginResponse = await chai + .request(app) + .post('/user/login') + .send({ username: 'usertest', password: 'testpassword' }); + + authToken = loginResponse.body.token; + } catch (error) { + console.error('Error obtaining authentication token:', error); + } + }); + + describe('POST /ads', () => { + it('should add a new ad with photos', (done) => { + chai + .request(app) + .post('/ads') + .set('Authorization', `Bearer ${authToken}`) + .attach('photos', 'path/to/photo1.jpg') + .attach('photos', 'path/to/photo2.jpg') + .field('title', 'Test Ad 2') + .field('propertyType', 'À la vente') + .field('publicationStatus', 'Publiée') + .field('propertyStatus', 'Disponible') + .field('description', 'teest.') + .field('price', 80000) + .field('availabilityDate', '2024-11-01') + .end((err, res) => { + expect(res).to.have.status(200); + done(); + }); + }); + }); + + // Cleanup + after(async () => { + try { + // Delete the ad + await chai + .request(app) + .delete(`/ad/${adId}`) + .set('Authorization', `Bearer ${authToken}`); + + const photo1Path = path.join(__dirname, 'uploads', 'img1.jpg'); + const photo2Path = path.join(__dirname, 'uploads', 'img2.jpg'); + + if (fs.existsSync(photo1Path)) { + fs.unlinkSync(photo1Path); + } + + if (fs.existsSync(photo2Path)) { + fs.unlinkSync(photo2Path); + } + } catch (error) { + console.error('Error cleaning up:', error); + } + }); +});*/ diff --git a/test/userController.test.js b/test/user.test.js similarity index 69% rename from test/userController.test.js rename to test/user.test.js index 8ca764390bf4c9f9bfc27affa04e373d5c48f472..8fe9dac0335f72357b05fe822934761b44d5ee23 100644 --- a/test/userController.test.js +++ b/test/user.test.js @@ -1,14 +1,15 @@ const chai = require('chai'); const chaiHttp = require('chai-http'); -const app = require('../index'); // Adjust the path based on your project structure +const app = require('../index'); const expect = chai.expect; const User = require('../models/User'); const mongoose = require('mongoose'); chai.use(chaiHttp); let testUserToken; + describe('Authentication Routes', () => { - describe('POST /auth', () => { + describe('POST /user', () => { it('should create a new user', (done) => { chai .request(app) @@ -16,13 +17,15 @@ describe('Authentication Routes', () => { .send({ username: 'usertest', password: 'testpassword', isAgent: false }) .end((err, res) => { expect(res).to.have.status(200); - expect(res.body).to.have.property('message').to.equal('Utilisateur créé avec succès'); - expect(res.body).to.have.property('user').to.be.an('object'); + expect(res.body).to.have.property('_id').to.be.a('string'); + expect(res.body).to.have.property('username').to.equal('usertest'); done(); }); }); }); + + describe('POST /user/login', () => { it('should login and return a token', (done) => { chai @@ -32,6 +35,7 @@ describe('Authentication Routes', () => { .end((err, res) => { expect(res).to.have.status(200); expect(res.body).to.have.property('token').to.be.a('string'); + testUserToken = res.body.token; // Save the token for later use done(); }); }); @@ -42,7 +46,7 @@ describe('Authentication Routes', () => { .post('/user/login') .send({ username: 'invaliduser', password: 'invalidpassword' }) .end((err, res) => { - expect(res).to.have.status(401); + expect(res).to.have.status(404); done(); }); }); @@ -52,20 +56,11 @@ describe('Authentication Routes', () => { it('should logout successfully', (done) => { chai .request(app) - .post('/user/login') - .send({ username: 'usertest', password: 'testpassword' }) + .post('/user/logout') + .set('Authorization', `Bearer ${testUserToken}`) .end((err, res) => { expect(res).to.have.status(200); - const token = res.body.token; - chai - .request(app) - .post('/user/logout') - .set('Authorization', `Bearer ${token}`) - .end((err, res) => { - expect(res).to.have.status(200); - expect(res.text).to.equal('Logout successful.'); - done(); - }); + done(); }); }); @@ -79,9 +74,9 @@ describe('Authentication Routes', () => { }); }); }); + after(async () => { try { - // Implement logic to delete the test user using Mongoose await User.deleteMany({ username: 'usertest' }); } catch (error) { console.error('Error deleting test user:', error); @@ -89,5 +84,4 @@ describe('Authentication Routes', () => { await mongoose.connection.close(); } }); - });