All files / modules/30-secrets/modals/CreateSSHCredModal/views VerifySecret.tsx

80.33% Statements 49/61
26.92% Branches 21/78
85.71% Functions 6/7
80.33% Lines 49/61

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              233x 233x 233x 233x 233x 233x 233x                       233x 233x 233x 233x     233x 233x 233x 233x 233x     233x             12x 12x 12x           12x         12x       12x 12x 12x   12x 4x   2x 2x 2x 2x   2x 2x 2x   2x         2x 2x 2x 2x                     2x       12x 2x   2x           2x 2x 2x 2x       12x                               233x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { StepsProgress, ModalErrorHandler, ModalErrorHandlerBinding } from '@wings-software/uicore'
import { Intent } from '@harness/design-system'
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import { SSHKeyValidationMetadata, useValidateSecret, ResponseSecretValidationResultDTO } from 'services/cd-ng'
import { useGetDelegatesStatus, RestResponseDelegateStatus } from 'services/portal'
import { useStrings } from 'framework/strings'
import type { UseGetMockData } from '@common/utils/testUtils'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
 
interface VerifySecretProps {
  validationMetadata?: SSHKeyValidationMetadata
  identifier: string
  onFinish?: (status: Status) => void
  mockDelegateStatus?: UseGetMockData<RestResponseDelegateStatus>
  mockValidateSecret?: UseGetMockData<ResponseSecretValidationResultDTO>
}
 
enum Step {
  ZERO,
  ONE,
  TWO
}
 
export enum Status {
  WAIT = 'WAIT',
  PROCESS = 'PROCESS',
  DONE = 'DONE',
  ERROR = 'ERROR'
}
 
const VerifySecret: React.FC<VerifySecretProps> = ({
  identifier,
  validationMetadata,
  onFinish,
  mockDelegateStatus,
  mockValidateSecret
}) => {
  const { accountId: accountIdentifier, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const { getString } = useStrings()
  const {
    data: delegateStatus,
    loading: loadingDelegateStatus,
    error: delegateStatusError,
    refetch: getDelegatesStatus
  } = useGetDelegatesStatus({
    queryParams: { accountId: accountIdentifier },
    lazy: true,
    mock: mockDelegateStatus
  })
  const { mutate: validateSecret } = useValidateSecret({
    queryParams: { identifier, accountIdentifier, projectIdentifier, orgIdentifier },
    mock: mockValidateSecret
  })
  const [currentStep, setCurrentStep] = useState<Step>(Step.ONE)
  const [currentStatus, setCurrentStatus] = useState<Status>(Status.WAIT)
  const [currentIntent, setCurrentIntent] = useState<Intent>(Intent.WARNING)
 
  useEffect(() => {
    switch (currentStep) {
      case Step.ONE:
        setCurrentStatus(Status.PROCESS)
        setCurrentIntent(Intent.WARNING)
        getDelegatesStatus()
        break
      case Step.TWO:
        setCurrentStatus(Status.PROCESS)
        Eif (validationMetadata) {
          validateSecret(validationMetadata).then(
            response => {
              Iif (response.data?.success) {
                setCurrentStatus(Status.DONE)
                setCurrentIntent(Intent.SUCCESS)
                onFinish?.(currentStatus)
              } else {
                setCurrentStatus(Status.ERROR)
                setCurrentIntent(Intent.DANGER)
                response.data?.message && modalErrorHandler?.showDanger(response.data.message)
                onFinish?.(currentStatus)
              }
            },
            _error => {
              setCurrentStatus(Status.ERROR)
              setCurrentIntent(Intent.DANGER)
              _error?.data?.message && modalErrorHandler?.showDanger(_error.data.message)
              onFinish?.(currentStatus)
            }
          )
        }
        break
    }
  }, [currentStep])
 
  useEffect(() => {
    Iif (loadingDelegateStatus) {
      // wait. do nothing
    } else Iif (delegateStatusError) {
      setCurrentStatus(Status.ERROR)
      setCurrentIntent(Intent.DANGER)
      const err = (delegateStatusError.data as any)?.responseMessages?.[0]?.message
      err && modalErrorHandler?.showDanger(err)
      onFinish?.(currentStatus)
    } else Eif (delegateStatus) {
      setCurrentStatus(Status.DONE)
      setCurrentIntent(Intent.SUCCESS)
      setCurrentStep(Step.TWO)
    }
  }, [delegateStatus, loadingDelegateStatus, delegateStatusError])
 
  return (
    <>
      <StepsProgress
        current={currentStep}
        steps={[
          getString('secrets.createSSHCredWizard.verifyStepOne'),
          getString('secrets.createSSHCredWizard.verifyStepTwo')
        ]}
        currentStatus={currentStatus}
        intent={currentIntent}
      />
      <ModalErrorHandler bind={setModalErrorHandler} style={{ marginTop: 'var(--spacing-large)' }} />
    </>
  )
}
 
export default VerifySecret