mufidu / booku

0 stars 1 forks source link

Sweep: Use JWT token in tests. #79

Closed mufidu closed 5 months ago

mufidu commented 5 months ago

The /books route requires JWT token to be accessed, which can be retrieved from /user/login endpoint using mufid.to@gmail.com as email and password 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)
sweep-ai[bot] commented 5 months ago

🚀 Here's the PR! #82

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 027755b9b4)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/mufidu/booku/blob/4de681cadf6f44300cdb88448ffa49ecd23fcd6d/test/bookCategory.test.js#L1-L50 https://github.com/mufidu/booku/blob/4de681cadf6f44300cdb88448ffa49ecd23fcd6d/test/booksSearch.test.js#L1-L71 https://github.com/mufidu/booku/blob/4de681cadf6f44300cdb88448ffa49ecd23fcd6d/test/booksByAuthor.test.js#L1-L26 https://github.com/mufidu/booku/blob/4de681cadf6f44300cdb88448ffa49ecd23fcd6d/routes/book.routes.js#L1-L103 https://github.com/mufidu/booku/blob/4de681cadf6f44300cdb88448ffa49ecd23fcd6d/app.js#L1-L50

Step 2: ⌨️ Coding

--- 
+++ 
@@ -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');

Ran GitHub Actions for 2d419484cc84e1c72bb43ba70d9052876fd22236:

--- 
+++ 
@@ -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');

Ran GitHub Actions for 2b72970ee3e0e6155a27eafec9b161a940b9e3a0:

--- 
+++ 
@@ -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;
   });

Ran GitHub Actions for 124690284d5772a50a5ff34b5a6025207ab4100c:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/use_jwt_token_in_tests.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 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.