Skip to content

Integration tests to test user flow#293

Open
benniu04 wants to merge 4 commits intomainfrom
Integration-Test---User-Sign-Up---Approval---Login---View-Profile-#247
Open

Integration tests to test user flow#293
benniu04 wants to merge 4 commits intomainfrom
Integration-Test---User-Sign-Up---Approval---Login---View-Profile-#247

Conversation

@benniu04
Copy link
Contributor

Pull Request

Brief Summary

-

Questions / Considerations for the Future

-

API Changes

-

Database Changes

-

New Tests

-

Closes #

Copy link
Member

@ethanszeto ethanszeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@benniu04
Copy link
Contributor Author

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.
Copy link
Member

@ethanszeto ethanszeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines 34 to 46
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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 51 to 55
if (!signupResponse.body.username) {
console.error("User ID is undefined in the signup response: ", signupResponse.body);

return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines 68 to 72
if (approvalResponse.status !== 200) {
console.error("Approval failed:", approvalResponse.body);
console.error("Request body:", listOfUsers);
console.error("Response status:", approvalResponse.status);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines 78 to 91
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@benniu04
Copy link
Contributor Author

I'm not sure why but the approve user test is still failing. It should pass based on the logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants