Skip to content

Commit

Permalink
improve the check method (#3935)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Oct 28, 2023
1 parent e39b82b commit dc036b8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
36 changes: 35 additions & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Scenario('log me in', ( {I, login} ) => {
#### How It Works

1. `restore` method is executed. It should open a page and set credentials.
2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged in user.
2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged-in user. When you pass the second args `session`, you could perform the validation using passed session.
3. If `restore` and `check` were not successful, `login` is executed
4. `login` should fill in login form
5. After successful login, `fetch` is executed to save cookies into memory or file.
Expand Down Expand Up @@ -240,6 +240,40 @@ Scenario('login', async ( {I, login} ) => {
})
```

#### Tips: Using session to validate user

Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`

```js
autoLogin: {
enabled: true,
saveToFile: true,
inject: 'login',
users: {
admin: {
login: async (I) => { // If you use async function in the autoLogin plugin
const phrase = await I.grabTextFrom('#phrase')
I.fillField('username', 'admin'),
I.fillField('password', 'password')
I.fillField('phrase', phrase)
},
check: (I, session) => {
// Throwing an error in `check` will make CodeceptJS perform the login step for the user
if (session.profile.email !== the.email.you.expect@some-mail.com) {
throw new Error ('Wrong user signed in');
}
},
}
}
}
```

```js
Scenario('login', async ( {I, login} ) => {
await login('admin') // you should use `await`
})
```

### Parameters

- `config`
Expand Down
38 changes: 35 additions & 3 deletions lib/plugin/autoLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const defaultConfig = {
* #### How It Works
*
* 1. `restore` method is executed. It should open a page and set credentials.
* 2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged in user.
* 2. `check` method is executed. It should reload a page (so cookies are applied) and check that this page belongs to logged-in user. When you pass the second args `session`, you could perform the validation using passed session.
* 3. If `restore` and `check` were not successful, `login` is executed
* 4. `login` should fill in login form
* 5. After successful login, `fetch` is executed to save cookies into memory or file.
Expand Down Expand Up @@ -212,6 +212,38 @@ const defaultConfig = {
* })
* ```
*
* #### Tips: Using session to validate user
*
* Instead of asserting on page elements for the current user in `check`, you can use the `session` you saved in `fetch`
*
* ```js
* autoLogin: {
* enabled: true,
* saveToFile: true,
* inject: 'login',
* users: {
* admin: {
* login: async (I) => { // If you use async function in the autoLogin plugin
* const phrase = await I.grabTextFrom('#phrase')
* I.fillField('username', 'admin'),
* I.fillField('password', 'password')
* I.fillField('phrase', phrase)
* },
* check: (I, session) => {
* // Throwing an error in `check` will make CodeceptJS perform the login step for the user
* if (session.profile.email !== the.email.you.expect@some-mail.com) {
* throw new Error ('Wrong user signed in');
* }
* },
* }
* }
* }
* ```
*
* ```js
* Scenario('login', async ( {I, login} ) => {
* await login('admin') // you should use `await`
* })
*
*
*/
Expand Down Expand Up @@ -264,10 +296,10 @@ module.exports = function (config) {
recorder.session.start('check login');
if (shouldAwait) {
await userSession.restore(I, cookies);
await userSession.check(I);
await userSession.check(I, cookies);
} else {
userSession.restore(I, cookies);
userSession.check(I);
userSession.check(I, cookies);
}
recorder.session.catch((err) => {
debug(`Failed auto login for ${name} due to ${err}`);
Expand Down

0 comments on commit dc036b8

Please sign in to comment.