Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/jimmy test cookies #9

Merged
merged 26 commits into from Sep 15, 2020
Merged

Feat/jimmy test cookies #9

merged 26 commits into from Sep 15, 2020

Conversation

jpqy
Copy link
Contributor

@jpqy jpqy commented Sep 13, 2020

Current tests use the apollo-server-testing library which provides a query and mutation hook against the ApolloServer directly without going through HTTP. However, this library sucks (see apollographql/apollo-server#2277), and doesn't handle cookies at all. For thorough testing of routes that set cookies, we need to test whether cookies are set properly.

The goal for this branch is to instead instantiate the express server loaded with app (i.e. app) and use the supertest library to run .post against the express server. Similarly, this framework doesn't require an http service to be started.

  • Finish "todos" refactoring after code-review with Monarch
  • Convert testManager to use supertest instead of apollo-server-testing
  • Write login/logout tests that check cookies

This makes it consistent with the other methods in the context and
possibly makes it easier to mock in tests.
UserService should only handle operations done on the User entity.
Logout doesn't do any such operations and should thus belong in the
resolver (i.e. controller).
Context should be handled in the resolver/controller layer.
The ServerContext interface should be agnostic about implementation
details of how JWTs are set and cleared.
@jpqy jpqy self-assigned this Sep 13, 2020
Doing this modularizes the express server to be used by supertest.
Since we are working with supertest, which is a bit lower-level than the
previous testClient hook from Apollo-Server-Testing, the query method
now takes in the GraphQL DocumentNode directly, converted to string
using graphql's `print` method.

The raw response now includes http headers, and the actual data is now
in a JSON string, so we must parse it to achieve what testClient was
previously doing for us.
We no longer need to mock/hardcode setting of cookies etc since our
tests now actually use an express server, which handles these things.
We need to parse the `set-cookie` header in the raw response in our
cookies tests.
@jpqy jpqy marked this pull request as ready for review September 14, 2020 21:28
@jpqy jpqy requested a review from a team as a code owner September 14, 2020 21:28
Copy link
Contributor

@clairefro clairefro left a comment

Choose a reason for hiding this comment

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

Looks really good and works. One question - are we still mocking the db on queries (not actually making calls)? I can see testClient is an instance of supertest but it looks like its test app argument is built with the same express server context as normal development server. I might not be understanding correctly

@jpqy
Copy link
Contributor Author

jpqy commented Sep 15, 2020

No, we are actually not mocking anything at the moment. Our index.ts takes app, an express server integrated with apollo-server, and calls .listen to start the http server. In our test, we actually use the exact same app as in index.ts and instead of .listen, we use supertest to create a hook against it that lets us run .post on the app and get responses, without starting the http server.

@jpqy jpqy merged commit f90faef into master Sep 15, 2020
@jpqy jpqy deleted the feat/jimmy-test-cookies branch September 15, 2020 17:59
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.

None yet

2 participants