Playwright e2e testing. What to do with oauth2 or email-passwordless authentication
Asked Answered
G

2

12

I am starting to do e2e tests with playwright. Most of the application requires to be authenticated. Automating this with a username/password mechanism is quite easy. The problem is that the app I want to test has only two authentication mechanisms: Github OAuth and email authentication links. Specifically I am using next-auth in a NextJS project.

I don't know how should I e2e test this with Playwright, the options I have considered are:

  • Completly mocking the authentication using a mock user and a mock session and then append the session token in the test context (as it is described here in the official docs).
  • Creating a Github user and/or an email account for the tests and somehow truly use them in playwrigth.

The first option is much easier to implement but then it's not e2e testing anymore. The second option looks difficult to implement and I don't know if it is recommended. I don't know if there are smarter or more standarized ways to proceed with this.

Gabby answered 2/11, 2021 at 9:2 Comment(0)
H
2

I disagree with @mostafa as an e2e test should be as closely to production as possible. Even if you're using a well tested library, you could still use it wrongly. Cypress has the mentioned issue recognized and developed a solution with a new feature: https://cypress.io/blog/2022/04/25/cypress-9-6-0-easily-test-multi-domain-workflows-with-cy-origin/

It's especially useful for login workflows provided by external services.

Halsy answered 27/12, 2022 at 20:34 Comment(0)
H
0

The keynote here is that you don't need to write e2e test to test the authentication flow because you're using next-auth ( which I think is a well-tested library). So I think you should go for the first option .

Hypophosphate answered 10/11, 2021 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.