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.

11.8 KB
Raw Blame
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { OrgsAPI } from '../orgs';
import { apiFetchJSON } from '@/utils/apiClient';

vi.mock('@/utils/apiClient', () => ({
  apiFetchJSON: vi.fn(),
}));

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

  describe('list', () => {
    it('fetches all organizations', async () => {
      const mockOrgs = [{ id: 'org-1', displayName: 'Org 1' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockOrgs);

      const result = await OrgsAPI.list();

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockOrgs);
    });
  });

  describe('create', () => {
    it('creates a new organization', async () => {
      const newOrg = { id: 'org-new', displayName: 'New Org' };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(newOrg);

      const result = await OrgsAPI.create({ id: 'org-new', displayName: 'New Org' });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({ id: 'org-new', displayName: 'New Org' }),
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
      expect(result).toEqual(newOrg);
    });
  });

  describe('get', () => {
    it('fetches a single organization', async () => {
      const mockOrg = { id: 'org-1', displayName: 'Org 1' };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockOrg);

      const result = await OrgsAPI.get('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org-1', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockOrg);
    });
  });

  describe('update', () => {
    it('updates organization display name', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ id: 'org-1', displayName: 'Updated' });

      await OrgsAPI.update('org-1', { displayName: 'Updated' });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1',
        expect.objectContaining({
          method: 'PUT',
          body: JSON.stringify({ displayName: 'Updated' }),
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  it('binds a name change to its accepted selection with no automatic retry', async () => {
    const controller = new AbortController();
    const options = { expectedOrgID: 'org/one', signal: controller.signal, retry: false as const };
    vi.mocked(apiFetchJSON).mockResolvedValueOnce({ id: 'org/one', displayName: 'Renamed' });
    await OrgsAPI.update('org/one', { displayName: 'Renamed' }, options);
    expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org%2Fone', {
      ...options,
      method: 'PUT',
      body: JSON.stringify({ displayName: 'Renamed' }),
      preferSessionAuth: true,
      skipOrgContext: true,
    });
  });

  describe('remove', () => {
    it('deletes an organization', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.remove('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('listMembers', () => {
    it('fetches organization members', async () => {
      const mockMembers = [{ userId: 'user-1', role: 'admin' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockMembers);

      const result = await OrgsAPI.listMembers('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org-1/members', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockMembers);
    });
  });

  describe('listPendingInvitations', () => {
    it('fetches pending organization invitations', async () => {
      const mockInvitations = [{ userId: 'user-1', role: 'viewer' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockInvitations);

      const result = await OrgsAPI.listPendingInvitations('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org-1/invitations', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockInvitations);
    });
  });

  describe('listMyInvitations', () => {
    it('fetches the current user invitation inbox', async () => {
      const mockInvitations = [{ orgId: 'org-1', orgDisplayName: 'Org 1', userId: 'user-1' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockInvitations);

      const result = await OrgsAPI.listMyInvitations();

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/org-invitations', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockInvitations);
    });
  });

  describe('inviteMember', () => {
    it('invites a member to organization', async () => {
      const mockInvitation = {
        kind: 'invitation',
        invitation: { userId: 'user-1', role: 'viewer' },
      };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockInvitation);

      const result = await OrgsAPI.inviteMember('org-1', { userId: 'user-1', role: 'viewer' });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/members',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({ userId: 'user-1', role: 'viewer' }),
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
      expect(result).toEqual(mockInvitation);
    });
  });

  describe('acceptMyInvitation', () => {
    it('accepts an invitation for the current user', async () => {
      const mockMember = { kind: 'member', member: { userId: 'user-1', role: 'viewer' } };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockMember);

      const result = await OrgsAPI.acceptMyInvitation('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/org-invitations/org-1/accept',
        expect.objectContaining({
          method: 'POST',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
      expect(result).toEqual(mockMember);
    });
  });

  describe('declineMyInvitation', () => {
    it('declines an invitation for the current user', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.declineMyInvitation('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/org-invitations/org-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('revokeInvitation', () => {
    it('revokes a pending invitation from an organization', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.revokeInvitation('org-1', 'user-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/invitations/user-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('removeMember', () => {
    it('removes a member from organization', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.removeMember('org-1', 'user-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/members/user-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('listShares', () => {
    it('fetches organization shares', async () => {
      const mockShares = [{ id: 'share-1', targetOrgId: 'org-2', resourceType: 'alert' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockShares);

      const result = await OrgsAPI.listShares('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org-1/shares', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockShares);
    });
  });

  describe('listIncomingShares', () => {
    it('fetches incoming organization shares', async () => {
      const mockShares = [{ id: 'share-1', sourceOrgId: 'org-2' }];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockShares);

      const result = await OrgsAPI.listIncomingShares('org-1');

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/orgs/org-1/shares/incoming', {
        preferSessionAuth: true,
        skipOrgContext: true,
      });
      expect(result).toEqual(mockShares);
    });
  });

  describe('createShare', () => {
    it('creates a resource share', async () => {
      const mockShare = { id: 'share-1', targetOrgId: 'org-2', resourceType: 'alert' };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockShare);

      const result = await OrgsAPI.createShare('org-1', {
        targetOrgId: 'org-2',
        resourceType: 'alert',
        resourceId: 'resource-1',
        accessRole: 'viewer',
      });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/shares',
        expect.objectContaining({
          method: 'POST',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
      expect(result).toEqual(mockShare);
    });
  });

  describe('acceptIncomingShare', () => {
    it('accepts a pending incoming share', async () => {
      const mockShare = { id: 'share-1', status: 'accepted' };
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockShare);

      const result = await OrgsAPI.acceptIncomingShare('org-1', 'share-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/shares/incoming/share-1/accept',
        expect.objectContaining({
          method: 'POST',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
      expect(result).toEqual(mockShare);
    });
  });

  describe('declineIncomingShare', () => {
    it('declines or removes an incoming share', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.declineIncomingShare('org-1', 'share-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/shares/incoming/share-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('deleteShare', () => {
    it('deletes a resource share', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);

      await OrgsAPI.deleteShare('org-1', 'share-1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/org-1/shares/share-1',
        expect.objectContaining({
          method: 'DELETE',
          preferSessionAuth: true,
          skipOrgContext: true,
        }),
      );
    });
  });

  describe('default organization members', () => {
    it('keeps the server-reported member management capability', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        id: 'default',
        displayName: 'default',
        canManageMembers: true,
      });

      const result = await OrgsAPI.get('default');

      expect(result.canManageMembers).toBe(true);
    });

    it('adds a default organization member through the members route', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        kind: 'member',
        member: { userId: 'bob', role: 'viewer' },
      });

      await OrgsAPI.inviteMember('default', { userId: 'bob', role: 'viewer' });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/orgs/default/members',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({ userId: 'bob', role: 'viewer' }),
        }),
      );
    });
  });
});