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.

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

import { createPortalAPI } from './api';
import type { PortalBootstrapData } from './types';

const bootstrap: PortalBootstrapData = {
  authenticated: true,
  email: '[email protected]',
  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: '/api/public/magic-link/request',
  signup_path: '/signup',
  logout_path: '/auth/logout',
  account_api_base_path: '/api/accounts',
  portal_api_base_path: '/api/portal',
  accounts: [],
};

describe('portal api', function() {
  beforeEach(function() {
    vi.restoreAllMocks();
  });

  it('parses bootstrap JSON through the shared api client', async function() {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
      ok: true,
      headers: new Headers({ 'content-type': 'application/json' }),
      json: async function() {
        return {
          authenticated: true,
          email: '[email protected]',
          accounts: [],
        };
      },
    }));

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    var result = await api.fetchBootstrap();
    expect(result.email).toBe('[email protected]');
  });

  it('raises typed api errors with payload-derived messages', async function() {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
      ok: false,
      status: 409,
      headers: new Headers({ 'content-type': 'application/json' }),
      json: async function() {
        return { error: 'Member already exists.' };
      },
    }));

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    await expect(api.inviteMember('acct_1', { email: '[email protected]', role: 'admin' })).rejects.toMatchObject({
      name: 'PortalAPIError',
      status: 409,
      message: 'Member already exists.',
    });
  });

  // A code-plus-sentence payload must show the sentence: the provider saw
  // "provider_msp_license_lapsed" instead of what to do about it.
  it('prefers the human message over the machine error code', async function() {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
      ok: false,
      status: 403,
      headers: new Headers({ 'content-type': 'application/json' }),
      json: async function() {
        return { error: 'provider_msp_license_lapsed', message: 'Your Pulse MSP plan has ended, so no new clients can be added.' };
      },
    }));

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    await expect(api.createWorkspace('acct_1', { display_name: 'Acme' })).rejects.toMatchObject({
      name: 'PortalAPIError',
      status: 403,
      message: 'Your Pulse MSP plan has ended, so no new clients can be added.',
    });
  });

  it('keeps task-specific fallback copy on network failures', async function() {
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('socket hang up')));

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    await expect(api.listMembers('acct_1')).rejects.toMatchObject({
      name: 'PortalAPIError',
      status: 0,
      message: 'Failed to load access roster.',
    });
  });

  it('sends portal-targeted magic-link requests and returns the server message', async function() {
    var fetchMock = 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.' };
      },
    });
    vi.stubGlobal('fetch', fetchMock);

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    var result = await api.requestMagicLink('[email protected]');
    expect(fetchMock).toHaveBeenCalledWith(
      '/api/public/magic-link/request',
      expect.objectContaining({
        method: 'POST',
        body: JSON.stringify({ email: '[email protected]', target: 'portal' }),
      })
    );
    expect(result.message).toContain('If that email is registered');
  });

  it('supports commercial GET requests for pricing and checkout resolution', async function() {
    var fetchMock = vi.fn().mockResolvedValue({
      ok: true,
      status: 200,
      headers: new Headers({ 'content-type': 'application/json' }),
      json: async function() {
        return { title: 'pricing' };
      },
    });
    vi.stubGlobal('fetch', fetchMock);

    var api = createPortalAPI({
      getBootstrap: function() {
        return bootstrap;
      },
    });

    var result = await api.getCommercialJSON<{ title: string }>('/v1/public/pricing-model?track=v6');
    expect(fetchMock).toHaveBeenCalledWith(
      '/api/portal/commercial/v1/public/pricing-model?track=v6',
      expect.objectContaining({
        headers: { Accept: 'application/json' },
      }),
    );
    expect(result.title).toBe('pricing');
  });
});