Closed mufidu closed 5 months ago
027755b9b4
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
test/bookCategory.test.js
✓ https://github.com/mufidu/booku/commit/2d419484cc84e1c72bb43ba70d9052876fd22236 Edit
Modify test/bookCategory.test.js with contents:
• Before each test case in `bookCategory.test.js`, add a `before` hook to login and obtain a JWT token.
• Use `chai.request(server).post('/user/login').send({email: 'mufid.to@gmail.com', password: 'password'})` to send a login request.
• Extract the JWT token from the login response and store it in a variable.
• In each test case, modify the `.get('/books/category/Fantasy')` and similar requests to include an `Authorization` header with the value `Bearer ${token}`, where `token` is the variable storing the JWT token.
--- +++ @@ -5,11 +5,24 @@ chai.use(chaiHttp); +let token; + +before(done => { + chai.request(server) + .post('/user/login') + .send({email: 'mufid.to@gmail.com', password: 'password'}) + .end((err, res) => { + token = res.body.token; + done(); + }); +}); + describe('GET /books/category/:categoryName', () => { describe('Successfully retrieving books by category', () => { it('should return all books for a valid category', done => { chai.request(server) .get('/books/category/Fantasy') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { expect(res).to.have.status(200); expect(res.body).to.be.an('array'); @@ -26,6 +39,7 @@ it('should return a 404 status code with an appropriate error message', done => { chai.request(server) .get('/books/category/Unknown') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { expect(res).to.have.status(200); expect(res.body).to.be.an('array').that.is.empty; @@ -38,6 +52,7 @@ it('should return books with the correct structure for a valid category', done => { chai.request(server) .get('/books/category/Science') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { expect(res).to.have.status(200); expect(res.body).to.be.an('array');
test/bookCategory.test.js
✓ Edit
Check test/bookCategory.test.js with contents:
Ran GitHub Actions for 2d419484cc84e1c72bb43ba70d9052876fd22236:
test/booksSearch.test.js
✓ https://github.com/mufidu/booku/commit/2b72970ee3e0e6155a27eafec9b161a940b9e3a0 Edit
Modify test/booksSearch.test.js with contents:
• Add a `before` hook at the beginning of the file to login and obtain a JWT token using the same method described above.
• Store the JWT token in a variable and use it in the `Authorization` header for all requests in this test file, similar to the modifications in `bookCategory.test.js`.
--- +++ @@ -4,10 +4,23 @@ const should = chai.should(); chai.use(chaiHttp); +let token; + +before(done => { + chai.request(server) + .post('/user/login') + .send({email: 'mufid.to@gmail.com', password: 'password'}) + .end((err, res) => { + token = res.body.token; + done(); + }); +}); + describe('GET /books search functionality', () => { it('should return books matching the title query', (done) => { chai.request(server) .get('/books?title=Harry Potter') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('array'); @@ -21,6 +34,7 @@ it('should return books matching the author query', (done) => { chai.request(server) .get('/books?author=J.K. Rowling') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('array'); @@ -34,6 +48,7 @@ it('should return books matching the category query', (done) => { chai.request(server) .get('/books?category=Fantasy') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('array'); @@ -47,6 +62,7 @@ it('should return books matching a combination of title, author, and category queries', (done) => { chai.request(server) .get('/books?title=Harry Potter&author=J.K. Rowling&category=Fantasy') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('array'); @@ -62,6 +78,7 @@ it('should return all books when no query parameters are provided', (done) => { chai.request(server) .get('/books') + .set('Authorization', `Bearer ${token}`) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('array');
test/booksSearch.test.js
✓ Edit
Check test/booksSearch.test.js with contents:
Ran GitHub Actions for 2b72970ee3e0e6155a27eafec9b161a940b9e3a0:
test/booksByAuthor.test.js
✓ https://github.com/mufidu/booku/commit/124690284d5772a50a5ff34b5a6025207ab4100c Edit
Modify test/booksByAuthor.test.js with contents:
• Implement a `before` hook to perform a login operation and obtain a JWT token, following the same steps as outlined for the other test files.
• Ensure that each request to the `/books/author/:authorName` endpoint includes the `Authorization` header with the JWT token.
--- +++ @@ -5,10 +5,22 @@ chai.use(chaiHttp); +let token; + +before(done => { + chai.request(server) + .post('/user/login') + .send({email: 'mufid.to@gmail.com', password: 'password'}) + .end((err, res) => { + token = res.body.token; + done(); + }); +}); + describe('GET /books/author/:authorName', () => { it('should retrieve books by an existing author', async () => { const authorName = 'Tere Liye'; - const res = await chai.request(server).get(`/books/author/${authorName}`); + const res = await chai.request(server).get(`/books/author/${authorName}`).set('Authorization', `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.be.an('array'); res.body.forEach(book => { @@ -18,7 +30,7 @@ it('should return an empty array for a non-existing author', async () => { const authorName = 'NonExisting Author'; - const res = await chai.request(server).get(`/books/author/${authorName}`); + const res = await chai.request(server).get(`/books/author/${authorName}`).set('Authorization', `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.be.an('array').that.is.empty; });
test/booksByAuthor.test.js
✓ Edit
Check test/booksByAuthor.test.js with contents:
Ran GitHub Actions for 124690284d5772a50a5ff34b5a6025207ab4100c:
I have finished reviewing the code for completeness. I did not find errors for sweep/use_jwt_token_in_tests
.
💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.
This is an automated message generated by Sweep AI.
The /books route requires JWT token to be accessed, which can be retrieved from
/user/login
endpoint usingmufid.to@gmail.com
as email andpassword
as password. Currently all/books
tests fail because the routes are forbidden. Fix that.Checklist
- [X] Modify `test/bookCategory.test.js` ✓ https://github.com/mufidu/booku/commit/2d419484cc84e1c72bb43ba70d9052876fd22236 [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/bookCategory.test.js) - [X] Running GitHub Actions for `test/bookCategory.test.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/bookCategory.test.js) - [X] Modify `test/booksSearch.test.js` ✓ https://github.com/mufidu/booku/commit/2b72970ee3e0e6155a27eafec9b161a940b9e3a0 [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/booksSearch.test.js) - [X] Running GitHub Actions for `test/booksSearch.test.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/booksSearch.test.js) - [X] Modify `test/booksByAuthor.test.js` ✓ https://github.com/mufidu/booku/commit/124690284d5772a50a5ff34b5a6025207ab4100c [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/booksByAuthor.test.js) - [X] Running GitHub Actions for `test/booksByAuthor.test.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/use_jwt_token_in_tests/test/booksByAuthor.test.js)