All files / src/components/widgets/implementationguide SecurityVisualizationWidget.tsx

96.7% Statements 235/243
72.72% Branches 8/11
100% Functions 2/2
96.7% Lines 235/243

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 3711x 1x 1x   1x       1x       1x       1x 1x 1x                                                                                   1x   1x 54x 54x 54x 54x 54x 54x   54x 54x     54x   26x 26x 26x 26x     26x 26x 26x 26x 26x   26x 26x 26x   26x 54x     54x   26x 26x 26x 26x 26x     26x 54x     54x 54x 54x 54x     54x 54x 52x 54x 52x                         52x     54x 54x 54x 10x 10x     10x 10x   54x 54x 54x     54x 54x   54x 8x 8x     8x 8x   54x   54x   54x 54x 54x 54x 54x 54x 54x 54x   54x   54x 54x 54x 54x 54x 54x   54x 54x 54x 54x   54x 54x 54x 54x 54x   54x 54x 54x 54x 54x 54x   54x 54x 54x 54x     54x 54x   54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x     54x 54x     54x 54x 54x 54x 54x 54x 54x 54x 54x     54x 54x 54x   54x 54x 54x   54x 54x   54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x     54x 54x 54x   54x 54x   54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x     54x 54x 54x   54x 54x   54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x     54x     54x 54x   54x 54x       54x 54x 54x   54x 54x   54x 54x   54x 54x   54x 54x   54x 54x 54x 54x 54x   54x   1x  
import React, { useMemo } from "react";
import { WIDGET_ICONS, WIDGET_TITLES } from "../../../constants/appConstants";
import { useSecurityMetricsService } from "../../../hooks/useSecurityMetricsService";
import { SecurityLevel } from "../../../types/cia";
import {
  getRiskLevelColor,
  getSecurityScoreColorClass,
} from "../../../utils/colorUtils";
import {
  calculateBusinessImpactLevel,
  getRiskLevelFromImpactLevel,
} from "../../../utils/riskUtils";
import {
  getSecurityLevelValue,
  normalizeSecurityLevel,
} from "../../../utils/securityLevelUtils";
import RadarChart from "../../charts/RadarChart";
import SecurityLevelIndicator from "../../common/SecurityLevelIndicator";
import WidgetContainer from "../../common/WidgetContainer";
 
/**
 * Props for SecurityVisualizationWidget component
 */
export interface SecurityVisualizationWidgetProps {
  /**
   * Selected availability level
   */
  availabilityLevel: SecurityLevel;
 
  /**
   * Selected integrity level
   */
  integrityLevel: SecurityLevel;
 
  /**
   * Selected confidentiality level
   */
  confidentialityLevel: SecurityLevel;
 
  /**
   * Optional CSS class name
   */
  className?: string;
 
  /**
   * Optional test ID for testing
   */
  testId?: string;
}
 
/**
 * SecurityVisualizationWidget displays security metrics in visual form
 *
 * ## Business Perspective
 *
 * This widget helps stakeholders visualize the current security posture
 * and understand the relationship between security controls and risk levels.
 * It provides at-a-glance metrics and visual indicators to support
 * decision-making around security investments. 📊
 */
const SecurityVisualizationWidget: React.FC<
  SecurityVisualizationWidgetProps
> = ({
  availabilityLevel,
  integrityLevel,
  confidentialityLevel,
  className = "",
  testId = "security-visualization-widget",
}) => {
  // Get the security metrics service
  const { securityMetricsService, error, isLoading } =
    useSecurityMetricsService();
 
  // Calculate security score based on selected levels
  const securityScore = useMemo(() => {
    // Normalize security levels to ensure consistent case handling
    const normalizedAvailability = normalizeSecurityLevel(availabilityLevel);
    const normalizedIntegrity = normalizeSecurityLevel(integrityLevel);
    const normalizedConfidentiality =
      normalizeSecurityLevel(confidentialityLevel);
 
    // Get security level values using normalized values
    const availabilityValue = getSecurityLevelValue(normalizedAvailability);
    const integrityValue = getSecurityLevelValue(normalizedIntegrity);
    const confidentialityValue = getSecurityLevelValue(
      normalizedConfidentiality
    );
 
    const totalScore =
      availabilityValue + integrityValue + confidentialityValue;
    const maxPossibleScore = 12; // 3 components x maximum value of 4
 
    return Math.round((totalScore / maxPossibleScore) * 100);
  }, [availabilityLevel, integrityLevel, confidentialityLevel]);
 
  // Use the business impact level to determine risk using utility functions
  const riskLevel = useMemo(() => {
    // Calculate impact level using the utility function
    const impactLevel = calculateBusinessImpactLevel(
      availabilityLevel,
      integrityLevel,
      confidentialityLevel
    );
 
    // Use the centralized utility to map impact level to risk level string
    return getRiskLevelFromImpactLevel(impactLevel);
  }, [availabilityLevel, integrityLevel, confidentialityLevel]);
 
  // Get security recommendations based on security levels
  const getSecurityRecommendations = () => {
    const availabilityValue = getSecurityLevelValue(availabilityLevel);
    const integrityValue = getSecurityLevelValue(integrityLevel);
    const confidentialityValue = getSecurityLevelValue(confidentialityLevel);
 
    // If all security levels are the same and high, show balanced recommendation
    if (
      availabilityLevel === integrityLevel &&
      integrityLevel === confidentialityLevel
    ) {
      if (availabilityLevel === "High" || availabilityLevel === "Very High") {
        return (
          <div
            data-testid="balanced-recommendation"
            className="p-3 bg-green-50 dark:bg-green-900 dark:bg-opacity-20 rounded-lg mt-4"
          >
            <p className="text-sm">
              Your security posture is well-balanced with strong controls across
              all CIA components.
            </p>
          </div>
        );
      }
    }
 
    // Component-specific recommendations
    return (
      <div className="space-y-2 mt-4" data-testid="security-recommendations">
        {availabilityValue < 2 && (
          <div className="p-2 bg-blue-50 dark:bg-blue-900 dark:bg-opacity-20 rounded">
            <p className="text-sm">
              Consider improving availability controls to ensure business
              continuity.
            </p>
          </div>
        )}
        {integrityValue < 3 && (
          <div className="p-2 bg-green-50 dark:bg-green-900 dark:bg-opacity-20 rounded">
            <p className="text-sm">
              Enhance data validation mechanisms and change management to
              improve integrity.
            </p>
          </div>
        )}
        {confidentialityValue < 2 && (
          <div className="p-2 bg-purple-50 dark:bg-purple-900 dark:bg-opacity-20 rounded">
            <p className="text-sm">
              Strengthen access controls and data protection to improve
              confidentiality.
            </p>
          </div>
        )}
      </div>
    );
  };
 
  return (
    <WidgetContainer
      title={WIDGET_TITLES.SECURITY_VISUALIZATION || "Security Visualization"}
      icon={WIDGET_ICONS.SECURITY_VISUALIZATION || "📊"}
      className={className}
      testId={testId}
      isLoading={isLoading}
      error={error}
    >
      <div className="p-4">
        {/* Security score section */}
        <div className="mb-6">
          <h3 className="text-lg font-medium mb-3">Security Posture</h3>
          <div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
            <div className="flex justify-between items-center mb-4">
              <div>
                <div className="text-sm text-gray-600 dark:text-gray-400">
                  Security Score
                </div>
                <div
                  className="text-3xl font-bold"
                  data-testid="security-score-value"
                >
                  {securityScore}
                </div>
              </div>
              <div>
                <div className="text-sm text-gray-600 dark:text-gray-400">
                  Risk Level
                </div>
                <div
                  className={`text-xl font-bold ${getRiskLevelColor(
                    riskLevel
                  )}`}
                  data-testid="risk-level"
                >
                  {riskLevel}
                </div>
              </div>
            </div>
 
            {/* Score gauge */}
            <div className="mb-2">
              <div className="text-xs text-gray-600 dark:text-gray-400 mb-1">
                Security Level
              </div>
              <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2.5">
                <div
                  className={`h-2.5 rounded-full ${getSecurityScoreColorClass(
                    securityScore
                  )}`}
                  style={{ width: `${securityScore}%` }}
                  data-testid="security-score-bar"
                ></div>
              </div>
              <div className="flex justify-between text-xs text-gray-600 dark:text-gray-400 mt-1">
                <div>Critical</div>
                <div>High</div>
                <div>Medium</div>
                <div>Low</div>
                <div>Minimal</div>
              </div>
            </div>
          </div>
        </div>
 
        {/* Security Radar Chart using existing component */}
        <div className="mb-6">
          <h3 className="text-lg font-medium mb-3">Security Components</h3>
 
          {/* Reuse the RadarChart component */}
          <div className="p-4 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
            <RadarChart
              availabilityLevel={availabilityLevel}
              integrityLevel={integrityLevel}
              confidentialityLevel={confidentialityLevel}
              testId={`${testId}-radar-chart`}
            />
          </div>
        </div>
 
        {/* Component Details Section */}
        <div className="mb-6">
          <h3 className="text-lg font-medium mb-3">Component Details</h3>
          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            {/* Confidentiality component */}
            <div
              className="p-3 bg-purple-50 dark:bg-purple-900 dark:bg-opacity-20 rounded-lg"
              data-testid="confidentiality-component"
            >
              <div className="flex items-center justify-between mb-2">
                <div className="text-md font-medium text-purple-800 dark:text-purple-300">
                  Confidentiality
                </div>
                <SecurityLevelIndicator
                  level={confidentialityLevel}
                  testId="confidentiality-level-indicator"
                />
              </div>
              <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
                <div
                  className="bg-purple-500 h-1.5 rounded-full"
                  style={{
                    width: `${
                      (getSecurityLevelValue(confidentialityLevel) / 4) * 100
                    }%`,
                  }}
                  data-testid="confidentiality-gauge"
                ></div>
              </div>
            </div>
 
            {/* Integrity component */}
            <div
              className="p-3 bg-green-50 dark:bg-green-900 dark:bg-opacity-20 rounded-lg"
              data-testid="integrity-component"
            >
              <div className="flex items-center justify-between mb-2">
                <div className="text-md font-medium text-green-800 dark:text-green-300">
                  Integrity
                </div>
                <SecurityLevelIndicator
                  level={integrityLevel}
                  testId="integrity-level-indicator"
                />
              </div>
              <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
                <div
                  className="bg-green-500 h-1.5 rounded-full"
                  style={{
                    width: `${
                      (getSecurityLevelValue(integrityLevel) / 4) * 100
                    }%`,
                  }}
                  data-testid="integrity-gauge"
                ></div>
              </div>
            </div>
 
            {/* Availability component */}
            <div
              className="p-3 bg-blue-50 dark:bg-blue-900 dark:bg-opacity-20 rounded-lg"
              data-testid="availability-component"
            >
              <div className="flex items-center justify-between mb-2">
                <div className="text-md font-medium text-blue-800 dark:text-blue-300">
                  Availability
                </div>
                <SecurityLevelIndicator
                  level={availabilityLevel}
                  testId="availability-level-indicator"
                />
              </div>
              <div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
                <div
                  className="bg-blue-500 h-1.5 rounded-full"
                  style={{
                    width: `${
                      (getSecurityLevelValue(availabilityLevel) / 4) * 100
                    }%`,
                  }}
                  data-testid="availability-gauge"
                ></div>
              </div>
            </div>
          </div>
        </div>
 
        {/* Recommendations Section - Added for test expectation */}
        {getSecurityRecommendations()}
 
        {/* Explanation section */}
        <div className="p-3 bg-blue-50 dark:bg-blue-900 dark:bg-opacity-20 rounded-lg mt-4">
          <h3 className="text-md font-medium mb-2">
            Security Visualization Key
          </h3>
          <p className="text-sm text-gray-600 dark:text-gray-400 mb-2">
            The security score represents your overall security posture based on
            your CIA triad levels. The risk level indicates the potential
            business risk associated with your current security posture.
          </p>
          <div className="grid grid-cols-2 md:grid-cols-5 gap-2 text-xs">
            <div className="p-1 bg-red-100 text-red-800 dark:bg-red-900 dark:bg-opacity-30 dark:text-red-300 rounded text-center">
              Critical Risk
            </div>
            <div className="p-1 bg-orange-100 text-orange-800 dark:bg-orange-900 dark:bg-opacity-30 dark:text-orange-300 rounded text-center">
              High Risk
            </div>
            <div className="p-1 bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:bg-opacity-30 dark:text-yellow-300 rounded text-center">
              Medium Risk
            </div>
            <div className="p-1 bg-green-100 text-green-800 dark:bg-green-900 dark:bg-opacity-30 dark:text-green-300 rounded text-center">
              Low Risk
            </div>
            <div className="p-1 bg-blue-100 text-blue-800 dark:bg-blue-900 dark:bg-opacity-30 dark:text-blue-300 rounded text-center">
              Minimal Risk
            </div>
          </div>
        </div>
      </div>
    </WidgetContainer>
  );
};
 
export default SecurityVisualizationWidget;