Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

3.9 KB
Raw Blame
import Login from './Index.vue';
import routes from '../routes';
import { getLoginRedirectURL } from '../../helpers/AuthHelper';
import { login } from '../../api/auth';

vi.mock('../../api/auth', () => ({
  login: vi.fn(),
}));

describe('login retries', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('preserves the Shopify pricing redirect after resolving a session limit', async () => {
    login.mockResolvedValue(null);
    const context = {
      email: '',
      credentials: {
        email: '[email protected]',
        password: 'Password1!',
      },
      ssoAuthToken: '',
      ssoAccountId: '',
      ssoConversationId: '',
      redirectUrl:
        'settings/billing?plan_handle=growth&shop=store.myshopify.com',
      sessionsLimitReached: true,
      limitedSessions: [{ id: 1 }],
      loginApi: { showLoading: false, hasErrored: false },
      handleImpersonation: vi.fn(),
      showAlertMessage: vi.fn(),
      $t: key => key,
    };

    Login.methods.retryLoginWithParams.call(context, {
      revoke_session_id: 1,
    });
    await Promise.resolve();

    expect(login).toHaveBeenCalledWith(
      expect.objectContaining({
        redirectUrl:
          'settings/billing?plan_handle=growth&shop=store.myshopify.com',
        revoke_session_id: 1,
      })
    );
  });
});

describe('SAML login', () => {
  it('carries the pending Shopify install redirect to the SSO route', () => {
    const redirectUrl = `settings/integrations/shopify?shopify_pending_install=${'a'.repeat(32)}`;

    expect(Login.computed.samlLoginRoute.call({ redirectUrl })).toEqual({
      name: 'sso_login',
      query: { redirect_url: redirectUrl },
    });
  });
});

describe('Shopify signup and password recovery', () => {
  it('shows signup for a pending Shopify installation when public signup is disabled', () => {
    window.chatwootConfig = { signupEnabled: 'false' };

    expect(
      Login.computed.showSignupLink.call({
        signupRoute: {
          name: 'auth_signup',
          query: { shopify_pending_install: 'pending-token' },
        },
      })
    ).toBe(true);
  });

  it('carries the Shopify billing redirect to password recovery', () => {
    const planRedirect = 'settings/billing?plan_handle=growth';
    const route = Login.computed.resetPasswordRoute.call({
      redirectUrl: planRedirect,
      ssoAccountId: '42',
    });
    expect(route).toEqual({
      name: 'auth_reset_password',
      query: { redirect_url: planRedirect, sso_account_id: '42' },
    });
    const resetRoute = routes.find(item => item.name === 'auth_reset_password');
    const props = resetRoute.props(route);
    expect(props).toEqual({ redirectUrl: planRedirect, ssoAccountId: '42' });
    expect(
      getLoginRedirectURL({
        ...props,
        user: { account_id: 1, accounts: [{ id: 1 }, { id: 42 }] },
      })
    ).toBe('/app/accounts/42/settings/billing?plan_handle=growth');
  });

  it('carries a pending install redirect to email verification', async () => {
    const pendingInstallRedirect = `settings/integrations/shopify?shopify_pending_install=${'a'.repeat(32)}`;
    const router = { push: vi.fn() };
    const context = {
      email: '',
      credentials: {
        email: '[email protected]',
        password: 'Password1!',
      },
      ssoAuthToken: '',
      ssoAccountId: '',
      ssoConversationId: '',
      redirectUrl: pendingInstallRedirect,
      loginApi: { showLoading: false, hasErrored: false },
      $router: router,
      handleImpersonation: vi.fn(),
      showAlertMessage: vi.fn(),
      $t: key => key,
    };
    login.mockRejectedValueOnce({ errorCode: 'user_not_confirmed' });

    Login.methods.submitLogin.call(context);
    await Promise.resolve();
    await Promise.resolve();

    expect(router.push).toHaveBeenCalledWith({
      name: 'auth_verify_email',
      state: {
        email: '[email protected]',
        redirectUrl: pendingInstallRedirect,
      },
    });
  });
});