Skip to content

[Question] How to launch an error in autoLogin custom step #2600

@rsc1975

Description

@rsc1975

I've implemented an autoLogin based on REST calls, to optimize the time in login and check processes, the problem is that when the token is invalid I don't know how to mark the check process as invalid so the workflow continues with login stage, if I throw an Error() the entire autoLogin process is halt due to an unexpected error. All the examples I saw use the existing API based on user interface where the user fill-in the credentials in a form and so on, but I didn't find anything using REST helper.

My autoLogin config:

autoLogin: {
      enabled: true,
      saveToFile: true,
      inject: 'login',
      users: {
        admin: {
          login: async (I) => {
            I.amOnPage('/login');
            await I.loginWithCredentials(adminUser.username, adminUser.password);            
          },
          check: (I) => {
            const token = codeceptjs.store['admin_session'];
            I.validateToken(token);
          },
          fetch: async (I) => {
            const cookie = await I.grabCookie('token');
            return cookie.value;
          },
          restore: (I, sessionToken) => {
            I.amOnPage('/login');
            I.saveTokenData(sessionToken);
          }
        },
     }
   }

The custom validationToken() step is something like:

module.exports = function() {
  return actor({

    async validateToken(token) {
    
      let response = await this.sendGetRequest(`/api/session/validate?token=${token}`);
      
      if (response.status === 200) {
        if (response.data === true) {
          return true;
        }
      }
      throw new Error('Invalid token !!');
    }
});

As (ugly) workaround I've got the following code in autoLogin.check implementation:

check: async (I) => {
  console.log('admin.check');
  const token = codeceptjs.store['admin_session'];
  const isValid = await I.validateToken(token);
  if (!isValid) {
    I.see("NOTHING TO SEE HERE, so It'll fail");
  }
},

By the way, I launched the same question in StackOverflow with a bounty, but without luck by the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions