Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

3.5 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { createPortalAPI } from './api';
import { installAuthController } from './auth_controller';
import { createPortalStore } from './store';
import type { PortalBootstrapData } from './types';

async function flushAsync() {
  await Promise.resolve();
  await Promise.resolve();
  await Promise.resolve();
  await new Promise(function(resolve) {
    setTimeout(resolve, 0);
  });
}

const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
  has_self_hosted_commercial: false,
  email_sign_in_available: true,
  provider_hosted_mode: false,
  public_site_url: 'https://pulserelay.pro',
  support_email: '[email protected]',
  commercial_api_base_url: '/api/portal/commercial',
  portal_path: '/portal',
  bootstrap_path: '/api/portal/bootstrap',
  magic_link_request_path: '/magic-link',
  signup_path: '/signup',
  logout_path: '/logout',
  account_api_base_path: '/api/accounts',
  portal_api_base_path: '/api/portal',
};

describe('auth controller', function() {
  beforeEach(function() {
    document.body.innerHTML = '';
    vi.restoreAllMocks();
  });

  it('syncs bootstrap email without overwriting local input state', function() {
    var store = createPortalStore(bootstrapDefaults, {
      email: '[email protected]',
    });
    var controller = installAuthController({
      api: createPortalAPI({ getBootstrap: function() { return store.getBootstrap(); } }),
      store,
    });

    expect(controller.getLoginState().emailValue).toBe('[email protected]');

    controller.getLoginState().emailValue = '[email protected]';
    store.setBootstrap({
      email: '[email protected]',
    });
    expect(controller.getLoginState().emailValue).toBe('[email protected]');
  });

  it('tracks login email input and completes magic-link request state', async function() {
    var store = createPortalStore(bootstrapDefaults, {});
    vi.stubGlobal(
      'fetch',
      vi.fn().mockResolvedValue({
        ok: true,
        status: 200,
        headers: new Headers({ 'content-type': 'application/json' }),
        json: async function() {
          return { message: 'If that email is registered, you will receive a magic link shortly.' };
        },
        text: async function() {
          return '';
        },
      })
    );

    var controller = installAuthController({
      api: createPortalAPI({ getBootstrap: function() { return store.getBootstrap(); } }),
      store,
    });

    document.body.innerHTML =
      '<input id="portal-login-email" data-portal-input="login-email">' +
      '<button id="send" data-portal-action="send-magic-link">Send</button>';

    var input = document.getElementById('portal-login-email') as HTMLInputElement;
    input.value = '[email protected]';
    input.dispatchEvent(new Event('input', { bubbles: true }));
    expect(controller.getLoginState().emailValue).toBe('[email protected]');

    document.getElementById('send')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));

    await flushAsync();
    expect(controller.getLoginState().request.pending).toBe(false);
    expect(controller.getLoginState().request.error).toBe('');
    expect(controller.getLoginState().success).toBe(true);
    expect(controller.getLoginState().successMessage).toContain('If that email is registered');
    expect(fetch).toHaveBeenCalledWith(
      '/magic-link',
      expect.objectContaining({
        method: 'POST',
        body: JSON.stringify({ email: '[email protected]', target: 'portal' }),
      })
    );
  });
});