Skip to content

Commit

Permalink
MLPAB-1269: Support CERTIFICATE_PROVIDER_OPT_OUT update (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored May 9, 2024
1 parent af79b94 commit 8a483d6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ test-api:

# get lpas
./api-test/tester -expectedStatus=200 REQUEST POST $(URL)/lpas '{"uids": [$(LPA_UID)]}'

# certificate provider opt out
$(eval LPA_UID := "$(shell ./api-test/tester UID)")
cat ./docs/example-lpa.json | ./api-test/tester -expectedStatus=201 REQUEST PUT $(URL)/lpas/$(LPA_UID) "`xargs -0`"
./api-test/tester -expectedStatus=200 REQUEST GET $(URL)/lpas/$(LPA_UID) ''
cat ./docs/certificate-provider-opt-out.json | ./api-test/tester -expectedStatus=201 REQUEST POST $(URL)/lpas/$(LPA_UID)/updates "`xargs -0`"

.PHONY: test-api

test-pact:
Expand Down
4 changes: 4 additions & 0 deletions docs/certificate-provider-opt-out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "CERTIFICATE_PROVIDER_OPT_OUT",
"changes": []
}
1 change: 1 addition & 0 deletions docs/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ components:
- TRUST_CORPORATION_SIGN
- PERFECT
- REGISTER
- CERTIFICATE_PROVIDER_OPT_OUT
changes:
type: array
items:
Expand Down
7 changes: 4 additions & 3 deletions internal/shared/lpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (e LpaType) IsValid() bool {
type LpaStatus string

const (
LpaStatusProcessing = LpaStatus("processing")
LpaStatusPerfect = LpaStatus("perfect")
LpaStatusRegistered = LpaStatus("registered")
LpaStatusProcessing = LpaStatus("processing")
LpaStatusPerfect = LpaStatus("perfect")
LpaStatusRegistered = LpaStatus("registered")
LpaStatusCannotRegister = LpaStatus("cannot-register")
)
23 changes: 23 additions & 0 deletions lambda/update/certificate_provider_opt_out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "github.com/ministryofjustice/opg-data-lpa-store/internal/shared"

type CertificateProviderOptOut struct{}

func (c CertificateProviderOptOut) Apply(lpa *shared.Lpa) []shared.FieldError {
if lpa.CertificateProvider.SignedAt != nil && !lpa.CertificateProvider.SignedAt.IsZero() {
return []shared.FieldError{{Source: "/type", Detail: "certificate provider cannot opt out after providing certificate"}}
}

lpa.Status = shared.LpaStatusCannotRegister

return nil
}

func validateCertificateProviderOptOut(changes []shared.Change) (CertificateProviderOptOut, []shared.FieldError) {
if len(changes) > 0 {
return CertificateProviderOptOut{}, []shared.FieldError{{Source: "/changes", Detail: "expected empty"}}
}

return CertificateProviderOptOut{}, nil
}
70 changes: 70 additions & 0 deletions lambda/update/certificate_provider_opt_out_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"encoding/json"
"testing"
"time"

"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
"github.com/stretchr/testify/assert"
)

func TestCertificateProviderOptOutApply(t *testing.T) {
lpa := &shared.Lpa{Status: shared.LpaStatusProcessing}
c := CertificateProviderOptOut{}

errors := c.Apply(lpa)

assert.Empty(t, errors)
assert.Equal(t, shared.LpaStatusCannotRegister, lpa.Status)
}

func TestCertificateProviderOptOutApplyWhenCertificateProvided(t *testing.T) {
now := time.Now()
certificateProvider := shared.CertificateProvider{Email: "a@example", SignedAt: &now}
lpa := &shared.Lpa{LpaInit: shared.LpaInit{
CertificateProvider: certificateProvider},
}

errors := CertificateProviderOptOut{}.Apply(lpa)

assert.Equal(t, errors, []shared.FieldError{{Source: "/type", Detail: "certificate provider cannot opt out after providing certificate"}})
assert.Equal(t, certificateProvider, lpa.CertificateProvider)
}

func TestValidateUpdateCertificateProviderOptOut(t *testing.T) {
testcases := map[string]struct {
update shared.Update
lpa *shared.Lpa
errors []shared.FieldError
}{
"valid": {
update: shared.Update{
Type: "CERTIFICATE_PROVIDER_OPT_OUT",
Changes: []shared.Change{},
},
},
"with changes": {
update: shared.Update{
Type: "CERTIFICATE_PROVIDER_OPT_OUT",
Changes: []shared.Change{
{
Key: "/something/someValue",
New: json.RawMessage(`"not expected"`),
Old: jsonNull,
},
},
},
errors: []shared.FieldError{
{Source: "/changes", Detail: "expected empty"},
},
},
}

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
_, errors := validateUpdate(tc.update, &shared.Lpa{})
assert.ElementsMatch(t, tc.errors, errors)
})
}
}
10 changes: 6 additions & 4 deletions lambda/update/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type Applyable interface {

func validateUpdate(update shared.Update, lpa *shared.Lpa) (Applyable, []shared.FieldError) {
switch update.Type {
case "CERTIFICATE_PROVIDER_SIGN":
return validateCertificateProviderSign(update.Changes, lpa)
case "ATTORNEY_SIGN":
return validateAttorneySign(update.Changes, lpa)
case "TRUST_CORPORATION_SIGN":
return validateTrustCorporationSign(update.Changes, lpa)
case "CERTIFICATE_PROVIDER_OPT_OUT":
return validateCertificateProviderOptOut(update.Changes)
case "CERTIFICATE_PROVIDER_SIGN":
return validateCertificateProviderSign(update.Changes, lpa)
case "PERFECT":
return validatePerfect(update.Changes)
case "REGISTER":
return validateRegister(update.Changes)
case "TRUST_CORPORATION_SIGN":
return validateTrustCorporationSign(update.Changes, lpa)
default:
return nil, []shared.FieldError{{Source: "/type", Detail: "invalid value"}}
}
Expand Down

0 comments on commit 8a483d6

Please sign in to comment.