-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MLPAB-1269: Support CERTIFICATE_PROVIDER_OPT_OUT update (#195)
- Loading branch information
Showing
7 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "CERTIFICATE_PROVIDER_OPT_OUT", | ||
"changes": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters