Conversation
ethanszeto
left a comment
There was a problem hiding this comment.
By the nature of integration tests, we want to call multiple endpoints in succession. Since there is no precedent yet in the codebase I will break it down.
Integration tests are different than API tests because they test a logical order of multiple endpoints together whilst API tests simply target and test one endpoint at a time.
In this case, we want to call 4 different endpoints in a row, to make sure that they all work in combination with one another:
- User Creates An Account (
/user/signup) - Admin Approves Account (
/user/resolve-status) - User Logs Into Account (
/user/login) - User Requests Profile (
/user/me)
You will want to call all four endpoints in one test, to ensure that this entire integration works properly.
It is important to note that you cannot execute the flow across multiple tests, since the database is reset before each individual test, and thus you would lose any 'created' data from the previous call.
Okay got it. I got the user signup to work, but I'm having a bit of trouble with the user approval. I'll submit a pull request again. It's giving me undefined userid. |
I made it so that because the signupResponse body only contains a message, it skips the approval step and now the tests pass.
ethanszeto
left a comment
There was a problem hiding this comment.
Looks mostly loog Ben, just an adjustment on remove some old debugging logic, as well as the way that login ultimately works near the end of the test file
| if (signupResponse.status !== 201) { | ||
| console.error("Signup failed:", signupResponse.body); | ||
| console.error("Request body:", { | ||
| username: "testuser", | ||
| password: "testpass", | ||
| status: AccountStatus.Pending.toString(), | ||
| email: "testuser@example.com", | ||
| graduationYear: 2023, | ||
| lastName: "User", | ||
| firstName: "Test" | ||
| }); | ||
| console.error("Response status:", signupResponse.status); | ||
| } |
There was a problem hiding this comment.
Traditionally we don't leave "potentially failing" branches in test files, even for debugging. When the expect clauses don't pass, we know there's a failure.
| if (!signupResponse.body.username) { | ||
| console.error("User ID is undefined in the signup response: ", signupResponse.body); | ||
|
|
||
| return; | ||
| } |
| if (approvalResponse.status !== 200) { | ||
| console.error("Approval failed:", approvalResponse.body); | ||
| console.error("Request body:", listOfUsers); | ||
| console.error("Response status:", approvalResponse.status); | ||
| } |
| const loginResponse = await request(app) | ||
| .post("/user/login") | ||
| .send({ username: "testuser", password: "testpass" }); | ||
|
|
||
| expect(loginResponse.status).toBe(200); | ||
| expect(loginResponse.body).toHaveProperty("token"); | ||
|
|
||
| // Step 4: User requests their profile | ||
| const profileResponse = await request(app) | ||
| .get("/user/me") | ||
| .set("Authorization", `Bearer ${loginResponse.body.token}`); | ||
|
|
||
| expect(profileResponse.status).toBe(200); | ||
| expect(profileResponse.body).toHaveProperty("username", "testuser"); |
There was a problem hiding this comment.
I'm pretty sure the way it works is that the login response will contain a 'set-cookie' header with the token on it. After, that token is passed into requests as an HTTPONLY cookie, not as an Authorization header
|
I'm not sure why but the approve user test is still failing. It should pass based on the logic. |
Pull Request
Brief Summary
-
Questions / Considerations for the Future
-
API Changes
-
Database Changes
-
New Tests
-
Closes #