✅How to login without a UI using a HTML web forms
✅How to login without a UI using JWT
We will use https://github.com/nadvolod/cypress-example-recipes only for login testing
- Stop the other processes from previous exercises
Ctrl + C
- Fork https://github.com/nadvolod/cypress-example-recipes
- Clone your fork to your machine
- Start cypress and the app
cd cypress-example-recipes
npm install
cd examples/logging-in__html-web-forms/
npm run dev
We will use https://github.com/nadvolod/cypress-example-recipes only for login testing
Our simple web app is protected by a HTML web form
- Try to open the URL
http://localhost:7077/
- Try to login with valid credentials
jane.lane
andpassword123
. Pay attention to the requests and behavior of the application - Also try to login with invalid credentials.
Some expected app behaviors
- /admin only allowed for authenticated users
- /users only allowed for authenticated users
- /dashboard only allowed for authenticated users
- Only user
jane.lane
andpassword123
can access the app
Open inneficient.spec.js
UI tests in general are inneficient! Hence, so many models exist to discourage us from writing so many UI tests (autoamtion pyramid, automation trophy, automation diamond...)
So let's improve our tests...
- Open
exercise.spec.js
- Create a test that can visit
/dashboard
without a UI login (We'll do this together) - Create a test that can visit
/users
without a UI login - Create a test that can visit
/admin
without a UI login
💡 You already have some helpful code to make your life easier
✅Fast
✅Reliable
✖️Need to learn how your API works (maybe a good thing)
- We bypassed one login using
sessionStorage
in a browser. This case is not typical as most websites don't have authentication code that happens in the front-end. - We bypassed another login using an HTML Web Form. This is a common approach that we can use to tackle other types of logins
Ctrl + C
to kill all of the processess running from previous session (server and cypress)cd ../logging-in_jwt
npm run dev
- App runs on http://localhost:8081/
Authenticate User:
POST to http://localhost:4000/users/authenticate
Get list of users:
GET to http://localhost:4000/users with a bearer token as auth
Explore the application at http://localhost:8081/
and notice the behavior of authentication
How does JWT work?
- Form fires
handleSubmit()
- Reads form values
- Fires a web request for authentication
//LoginPage.vue
if (username && password) {
dispatch('authentication/login', { username, password });
}
- Save JWT token to local storage. No cookies.
//user.service.js
.then((user) => {
// login successful if there's a jwt token in the response
if (user.token) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('user', JSON.stringify(user))
}
Building and testing an auth API with JWT tutorial
Open cypress/integration/exercise.spec.js
Your challenge is to create 2 tests:
- Create a test to login with a JWT and assert that user is successfully logged in
- Assert that a user can successfully log out. You're not allowed to login through the UI
💡 JWT authentication is already implemented for you
✅Pretty much all authentication can be bypassed without using a UI
✅Authenticating using non-UI methods is more stable and efficient
✅We learned 3 types of authentication: HTML Web forms, JWT, directly setting session storage (not realistic)
There are numerous other ways that we can authenticate:
- SSO
- Using app code
- XHR web forms
- CSRF tokens
- Basic auth