Closed mufidu closed 5 months ago
bbdb1959f6
)[!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/app.test.js
✓ https://github.com/mufidu/booku/commit/a78dfd2cbc37b80f59f4eeaf8eefcb326707114b Edit
Modify test/app.test.js with contents:
• At the beginning of the `app.test.js` file, add a `before` hook similar to the one in `user.test.js`. This hook will log in using the provided credentials (`mufid.to@gmail.com` and `password`) to obtain a token. Use `chai.request(app).post("/auth/login").send({email: "mufid.to@gmail.com", password: "password"})` to perform the login and store the token in a variable.
• In each test that requires authentication (POST, PUT, DELETE routes for `/books` and `/books/:id`), modify the `chai.request` calls to include the `.set("Authorization", `Bearer ${token}`)` header, using the token obtained in the `before` hook. This will ensure that the requests are authenticated.
• Ensure that the `before` hook properly handles any errors during the login process, such as by logging the error or failing the test if the login does not succeed.
• Update the comments within the file to reflect the addition of authentication to the tests, making it clear which operations require a token and how it is being used.
--- +++ @@ -5,6 +5,20 @@ chai.use(chaiHttp); const { expect } = chai; + +let token; + +before(async () => { + try { + const res = await chai.request(app) + .post("/auth/login") + .send({email: "mufid.to@gmail.com", password: "password"}); + token = res.body.jwt; + } catch (error) { + console.error("Login failed", error); + throw error; + } +}); describe("Book API", () => { // Test for GET route "/books" @@ -23,7 +37,7 @@ category: "Science", cover: "Test Cover", }; - const res = await chai.request(app).post("/books").send(book); + const res = await chai.request(app).post("/books").send(book).set("Authorization", `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.be.a("object"); expect(res.body).to.have.property("title").eql(book.title); @@ -62,7 +76,8 @@ const res = await chai .request(app) .put(`/books/${book.id}`) - .send({ title: "Updated Test Book" }); + .send({ title: "Updated Test Book" }) + .set("Authorization", `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.be.a("object"); expect(res.body).to.have.property("title").eql("Updated Test Book"); @@ -78,7 +93,7 @@ cover: "Test Cover", }); await book.save(); - const res = await chai.request(app).delete(`/books/${book.id}`); + const res = await chai.request(app).delete(`/books/${book.id}`).set("Authorization", `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.equal(`${book.title} deleted`); });
test/app.test.js
✓ Edit
Check test/app.test.js with contents:
Ran GitHub Actions for a78dfd2cbc37b80f59f4eeaf8eefcb326707114b:
I have finished reviewing the code for completeness. I did not find errors for sweep/refactor_apptestjs_to_implement_token
.
💡 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.
Details
Refactor app.test.js so it uses token, by logging in via /auth/login, using
mufid.to@gmail.com
as email andpassword
as password. Format it like other tests in thetest
folder (for example, make it similar touser.test.js
)Checklist
- [X] Modify `test/app.test.js` ✓ https://github.com/mufidu/booku/commit/a78dfd2cbc37b80f59f4eeaf8eefcb326707114b [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_apptestjs_to_implement_token/test/app.test.js) - [X] Running GitHub Actions for `test/app.test.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/refactor_apptestjs_to_implement_token/test/app.test.js)