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.

10.9 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  getSecurityFeatureCardPresentation,
  getSecurityFeatureStatePresentation,
  getSecurityHardeningActions,
  getSecurityNetworkAccessSubtitle,
  getSecurityPostureItems,
  getSecurityScoreIconComponent,
  getSecurityScorePresentation,
  getSecurityScoreSymbol,
  getSecurityScoreTextClass,
  getSecurityWarningPresentation,
  shouldShowGlobalSecurityWarning,
} from '@/utils/securityScorePresentation';

describe('securityScorePresentation', () => {
  it('returns strong posture presentation for high scores', () => {
    expect(getSecurityScorePresentation(92)).toMatchObject({
      label: 'Strong',
      icon: 'shield-check',
    });
    expect(getSecurityScorePresentation(92).tone.badge).toContain('emerald');
  });

  it('returns moderate posture presentation for medium scores', () => {
    expect(getSecurityScorePresentation(65)).toMatchObject({
      label: 'Moderate',
      icon: 'shield',
    });
    expect(getSecurityScorePresentation(65).tone.badge).toContain('amber');
  });

  it('returns weak posture presentation for low scores', () => {
    expect(getSecurityScorePresentation(25)).toMatchObject({
      label: 'Weak',
      icon: 'shield-alert',
    });
    expect(getSecurityScorePresentation(25).tone.badge).toContain('rose');
  });

  it('returns matching text classes and symbols for score tiers', () => {
    expect(getSecurityScoreTextClass(90)).toContain('emerald');
    expect(getSecurityScoreSymbol(90)).toBe('✓');
    expect(getSecurityScoreTextClass(60)).toContain('amber');
    expect(getSecurityScoreSymbol(60)).toBe('!');
    expect(getSecurityScoreTextClass(20)).toContain('rose');
    expect(getSecurityScoreSymbol(20)).toBe('!!');
  });

  it('returns a critical public-access warning presentation', () => {
    expect(
      getSecurityWarningPresentation({
        score: 40,
        publicAccess: true,
        hasAuthentication: false,
        apiTokenConfigured: false,
        exportProtected: false,
        hasHTTPS: false,
      }),
    ).toMatchObject({
      background: 'bg-red-50 dark:bg-red-900/25',
      border: 'border-red-200 dark:border-red-800',
      messageClass: 'font-semibold text-red-700 dark:text-red-300',
    });
  });

  it('returns a shared low-score warning presentation for non-public setups', () => {
    expect(
      getSecurityWarningPresentation({
        score: 35,
        publicAccess: false,
        hasAuthentication: false,
        apiTokenConfigured: false,
        exportProtected: false,
        hasHTTPS: false,
      }),
    ).toMatchObject({
      background: 'bg-red-50 dark:bg-red-900/25',
      border: 'border-red-200 dark:border-red-800',
      messageClass: 'text-base-content',
    });
  });

  it('returns a shared moderate warning presentation when score is only moderately low', () => {
    expect(
      getSecurityWarningPresentation({
        score: 60,
        publicAccess: false,
        hasAuthentication: false,
        apiTokenConfigured: false,
        exportProtected: false,
        hasHTTPS: false,
      }),
    ).toMatchObject({
      background: 'bg-yellow-50 dark:bg-yellow-900/25',
      border: 'border-yellow-200 dark:border-yellow-800',
      messageClass: 'text-base-content',
    });
  });

  it('describes missing controls when authentication is enabled', () => {
    expect(
      getSecurityWarningPresentation({
        score: 60,
        publicAccess: false,
        hasAuthentication: true,
        apiTokenConfigured: false,
        exportProtected: true,
        hasHTTPS: false,
      }).message,
    ).toBe(
      'Authentication is enabled, but this Pulse instance is still missing HTTPS and an API token.',
    );
  });

  it('keeps global warnings off for private authenticated setup debt', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        hasAuthentication: true,
        exportProtected: true,
        hasHTTPS: false,
        publicAccess: false,
      }),
    ).toBe(false);
  });

  it('keeps global warnings on for active exposure states', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        hasAuthentication: false,
        exportProtected: true,
        hasHTTPS: true,
        publicAccess: false,
      }),
    ).toBe(true);
    expect(
      shouldShowGlobalSecurityWarning({
        hasAuthentication: true,
        exportProtected: false,
        hasHTTPS: true,
        publicAccess: false,
      }),
    ).toBe(true);
    expect(
      shouldShowGlobalSecurityWarning({
        hasAuthentication: true,
        exportProtected: true,
        hasHTTPS: false,
        publicAccess: true,
      }),
    ).toBe(true);
  });

  it('returns recommended hardening steps for private authenticated setups', () => {
    expect(
      getSecurityHardeningActions({
        hasAuthentication: true,
        apiTokenConfigured: false,
        exportProtected: true,
        hasAuditLogging: false,
        requiresAuth: true,
        publicAccess: false,
        isPrivateNetwork: true,
        hasHTTPS: false,
      }),
    ).toEqual([
      expect.objectContaining({
        key: 'configure-https',
        severity: 'recommended',
      }),
      expect.objectContaining({
        key: 'create-api-token',
        severity: 'recommended',
      }),
    ]);
  });

  it('returns critical hardening steps for exposed setups', () => {
    expect(
      getSecurityHardeningActions({
        hasAuthentication: false,
        apiTokenConfigured: false,
        exportProtected: false,
        unprotectedExportAllowed: true,
        hasAuditLogging: false,
        requiresAuth: false,
        publicAccess: true,
        isPrivateNetwork: false,
        hasHTTPS: false,
      }).map((action) => action.key),
    ).toEqual(['enable-authentication', 'protect-exports', 'configure-https']);
  });

  it('returns canonical yes/no feature-state presentation', () => {
    expect(getSecurityFeatureStatePresentation(true)).toEqual({
      label: 'Yes',
      className: 'text-green-600',
    });
    expect(getSecurityFeatureStatePresentation(false)).toEqual({
      label: 'No',
      className: 'text-red-600',
    });
  });

  it('returns canonical score icon components for posture tiers', () => {
    expect(getSecurityScoreIconComponent(90).name).toBe('ShieldCheck');
    expect(getSecurityScoreIconComponent(60).name).toBe('Shield');
    expect(getSecurityScoreIconComponent(20).name).toBe('ShieldAlert');
  });

  it('returns canonical feature-card presentation for enabled, critical, and optional states', () => {
    expect(
      getSecurityFeatureCardPresentation({
        enabled: true,
        critical: true,
      }),
    ).toMatchObject({
      cardClassName:
        'border-emerald-200 dark:border-emerald-800 bg-emerald-50 dark:bg-emerald-950/25',
      iconClassName: 'text-emerald-500 dark:text-emerald-400',
      statusLabel: 'Enabled',
    });

    expect(
      getSecurityFeatureCardPresentation({
        enabled: false,
        critical: true,
      }),
    ).toMatchObject({
      cardClassName: 'border-rose-200 dark:border-rose-800 bg-rose-50 dark:bg-rose-950/25',
      iconClassName: 'text-rose-500 dark:text-rose-400',
      statusLabel: 'Disabled',
    });

    expect(
      getSecurityFeatureCardPresentation({
        enabled: false,
        critical: false,
      }),
    ).toMatchObject({
      cardClassName: 'border-border bg-surface-alt',
      iconClassName: 'text-muted',
      statusLabel: 'Disabled',
    });
  });

  it('returns canonical security posture items and network subtitle', () => {
    expect(
      getSecurityPostureItems({
        hasAuthentication: true,
        ssoEnabled: false,
        hasProxyAuth: false,
        apiTokenConfigured: true,
        exportProtected: true,
        unprotectedExportAllowed: false,
        hasHTTPS: true,
        hasAuditLogging: false,
        requiresAuth: true,
        publicAccess: true,
        isPrivateNetwork: false,
      }),
    ).toEqual(
      expect.arrayContaining([
        expect.objectContaining({
          key: 'password',
          label: 'Password login',
          description: 'Active',
          critical: true,
        }),
        expect.objectContaining({
          key: 'export',
          description: 'Token + passphrase required',
        }),
        // Publicly reachable, so HTTPS counts as critical.
        expect.objectContaining({ key: 'https', critical: true }),
      ]),
    );

    expect(
      getSecurityNetworkAccessSubtitle({
        hasAuthentication: true,
        apiTokenConfigured: true,
        exportProtected: true,
        hasAuditLogging: true,
        requiresAuth: true,
        publicAccess: true,
        isPrivateNetwork: false,
      }),
    ).toBe('Public network access detected');
    expect(
      getSecurityNetworkAccessSubtitle({
        hasAuthentication: true,
        apiTokenConfigured: true,
        exportProtected: true,
        hasAuditLogging: true,
        requiresAuth: true,
        publicAccess: false,
        isPrivateNetwork: true,
      }),
    ).toBe('Private network access');
  });
});

describe('Issue1650 truncated security status must not raise the global warning', () => {
  // /api/security/status truncates its payload by authority. A kiosk API token
  // (monitoring:read only) gets detailLevel 'authenticated' with no posture
  // fields at all, which used to read as "exports unprotected" and produced a
  // false 1/5 security banner linking into settings the token cannot open.
  const truncatedKioskPayload = {
    detailLevel: 'authenticated' as const,
    hasAuthentication: true,
    exportProtected: false,
    hasHTTPS: false,
    publicAccess: false,
  };

  it('stays silent for an authenticated-but-unprivileged payload', () => {
    expect(shouldShowGlobalSecurityWarning(truncatedKioskPayload)).toBe(false);
  });

  it('stays silent for an authenticated payload that looks publicly exposed', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        ...truncatedKioskPayload,
        publicAccess: true,
      }),
    ).toBe(false);
  });

  it('still warns an unauthenticated instance from its public payload', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        detailLevel: 'public',
        hasAuthentication: false,
        exportProtected: false,
      }),
    ).toBe(true);
  });

  it('does not invent posture debt from a public payload that has authentication', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        detailLevel: 'public',
        hasAuthentication: true,
        exportProtected: false,
      }),
    ).toBe(false);
  });

  it('keeps the full assessment for a privileged payload', () => {
    expect(
      shouldShowGlobalSecurityWarning({
        detailLevel: 'privileged',
        hasAuthentication: true,
        exportProtected: false,
        hasHTTPS: true,
        publicAccess: false,
      }),
    ).toBe(true);
    expect(
      shouldShowGlobalSecurityWarning({
        detailLevel: 'privileged',
        hasAuthentication: true,
        exportProtected: true,
        hasHTTPS: true,
        publicAccess: false,
      }),
    ).toBe(false);
  });
});