All files / modules/33-auth-settings/modals/PasswordExpiration/views PasswordExpirationForm.tsx

100% Statements 28/28
90% Branches 9/10
100% Functions 4/4
100% Lines 28/28

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              4x 4x 4x 4x                     4x 4x   4x   4x 4x                           4x 6x 6x 6x 6x 6x 6x   6x             6x 2x           2x 2x       2x 2x     2x             6x                                                     2x       10x                                                                   4x  
/*
 * Copyright 2022 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 React from 'react'
import * as yup from 'yup'
import { useParams } from 'react-router-dom'
import {
  Layout,
  Text,
  Formik,
  FormikForm,
  FormInput,
  Button,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  ButtonVariation
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useToaster } from '@common/components'
import type { LoginSettings, PasswordExpirationPolicy } from 'services/cd-ng'
import { usePutLoginSettings } from 'services/cd-ng'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
 
interface Props {
  onSubmit?: () => void
  onCancel: () => void
  loginSettings: LoginSettings
  editing: boolean
}
 
interface FormValues {
  daysBeforePasswordExpire: number
  daysBeforeUserNotified: number
}
 
const PasswordExpirationForm: React.FC<Props> = ({ onSubmit, onCancel, loginSettings, editing }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { showSuccess } = useToaster()
  const passwordExpirationSettings = loginSettings.passwordExpirationPolicy
  const [modalErrorHandler, setModalErrorHandler] = React.useState<ModalErrorHandlerBinding>()
 
  const { mutate: updateLoginSettings, loading: updatingLoginSettings } = usePutLoginSettings({
    loginSettingsId: loginSettings.uuid,
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const handleSubmit = async (values: FormValues): Promise<void> => {
    const passwordExpirationPolicy: PasswordExpirationPolicy = {
      enabled: editing ? passwordExpirationSettings.enabled : /* istanbul ignore next */ true,
      daysBeforePasswordExpires: values.daysBeforePasswordExpire,
      daysBeforeUserNotifiedOfPasswordExpiration: values.daysBeforeUserNotified
    }
 
    try {
      const response = await updateLoginSettings({
        ...loginSettings,
        passwordExpirationPolicy
      })
      /* istanbul ignore else */ if (response) {
        showSuccess(
          getString(editing ? 'authSettings.passwordExpirationUpdated' : 'authSettings.passwordExpirationEnabled')
        )
        onSubmit?.()
      }
    } catch (e) {
      /* istanbul ignore next */ modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
 
  return (
    <Layout.Vertical padding={{ left: 'huge', right: 'huge' }}>
      <ModalErrorHandler bind={setModalErrorHandler} />
      <Text color={Color.GREY_900} font={{ size: 'medium', weight: 'semi-bold' }} margin={{ bottom: 'xxlarge' }}>
        {getString('authSettings.passwordExpiration')}
      </Text>
      <Formik
        formName="passwordExpirationForm"
        initialValues={{
          daysBeforePasswordExpire:
            passwordExpirationSettings.daysBeforePasswordExpires || /* istanbul ignore next */ 30,
          daysBeforeUserNotified:
            passwordExpirationSettings.daysBeforeUserNotifiedOfPasswordExpiration || /* istanbul ignore next */ 3
        }}
        validationSchema={yup.object().shape({
          daysBeforePasswordExpire: yup
            .number()
            .typeError(getString('common.validation.valueMustBeANumber'))
            .min(1, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: 1 }))
            .required(getString('validation.thisIsARequiredField')),
          daysBeforeUserNotified: yup
            .number()
            .typeError(getString('common.validation.valueMustBeANumber'))
            .min(1, getString('common.validation.valueMustBeGreaterThanOrEqualToN', { n: 1 }))
            .required(getString('validation.thisIsARequiredField'))
        })}
        onSubmit={values => {
          handleSubmit(values)
        }}
      >
        {() => (
          <FormikForm>
            <Layout.Vertical spacing="xlarge">
              <FormInput.Text
                name="daysBeforePasswordExpire"
                label={getString('authSettings.daysBeforePasswordExpires')}
                inputGroup={{
                  type: 'number'
                }}
              />
              <FormInput.Text
                name="daysBeforeUserNotified"
                label={getString('authSettings.daysBeforeUserNotified')}
                inputGroup={{
                  type: 'number'
                }}
              />
            </Layout.Vertical>
            <Layout.Horizontal margin={{ top: 'xxxlarge', bottom: 'xlarge' }}>
              <Button
                text={getString('save')}
                type="submit"
                variation={ButtonVariation.PRIMARY}
                margin={{ right: 'small' }}
                disabled={updatingLoginSettings}
              />
              <Button text={getString('cancel')} onClick={onCancel} variation={ButtonVariation.TERTIARY} />
            </Layout.Horizontal>
          </FormikForm>
        )}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default PasswordExpirationForm