-
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-1777 Add PERFECT update type and status (#180)
- Loading branch information
Showing
7 changed files
with
185 additions
and
113 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
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,47 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared" | ||
) | ||
|
||
type Perfect struct{} | ||
|
||
func (r Perfect) Apply(lpa *shared.Lpa) []shared.FieldError { | ||
if lpa.Status != shared.LpaStatusProcessing { | ||
return []shared.FieldError{{Source: "/type", Detail: "status must be processing to make perfect"}} | ||
} | ||
|
||
if lpa.SignedAt.IsZero() { | ||
return []shared.FieldError{{Source: "/type", Detail: "lpa must be signed"}} | ||
} | ||
|
||
if lpa.CertificateProvider.SignedAt == nil || lpa.CertificateProvider.SignedAt.IsZero() { | ||
return []shared.FieldError{{Source: "/type", Detail: "lpa must have a certificate"}} | ||
} | ||
|
||
for _, a := range lpa.Attorneys { | ||
if a.SignedAt == nil || a.SignedAt.IsZero() { | ||
return []shared.FieldError{{Source: "/type", Detail: "lpa must be signed by attorneys"}} | ||
} | ||
} | ||
|
||
for _, t := range lpa.TrustCorporations { | ||
for _, s := range t.Signatories { | ||
if s.SignedAt.IsZero() { | ||
return []shared.FieldError{{Source: "/type", Detail: "lpa must be signed by trust corporations"}} | ||
} | ||
} | ||
} | ||
|
||
lpa.Status = shared.LpaStatusPerfect | ||
|
||
return nil | ||
} | ||
|
||
func validatePerfect(changes []shared.Change) (Perfect, []shared.FieldError) { | ||
if len(changes) > 0 { | ||
return Perfect{}, []shared.FieldError{{Source: "/changes", Detail: "expected empty"}} | ||
} | ||
|
||
return Perfect{}, 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,126 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPerfectApply(t *testing.T) { | ||
now := time.Now() | ||
|
||
lpa := &shared.Lpa{ | ||
Status: shared.LpaStatusProcessing, | ||
LpaInit: shared.LpaInit{ | ||
SignedAt: now, | ||
CertificateProvider: shared.CertificateProvider{ | ||
SignedAt: &now, | ||
}, | ||
Attorneys: []shared.Attorney{{ | ||
SignedAt: &now, | ||
}}, | ||
TrustCorporations: []shared.TrustCorporation{{ | ||
Signatories: []shared.Signatory{{ | ||
SignedAt: now, | ||
}}, | ||
}}, | ||
}, | ||
} | ||
|
||
errors := Perfect{}.Apply(lpa) | ||
assert.Nil(t, errors) | ||
assert.Equal(t, shared.LpaStatusPerfect, lpa.Status) | ||
} | ||
|
||
func TestPerfectApplyWhenUnsigned(t *testing.T) { | ||
now := time.Now() | ||
|
||
testcases := map[string]struct { | ||
lpa *shared.Lpa | ||
errors []shared.FieldError | ||
}{ | ||
"lpa": { | ||
lpa: &shared.Lpa{ | ||
Status: shared.LpaStatusProcessing, | ||
LpaInit: shared.LpaInit{ | ||
CertificateProvider: shared.CertificateProvider{SignedAt: &now}, | ||
Attorneys: []shared.Attorney{{SignedAt: &now}}, | ||
TrustCorporations: []shared.TrustCorporation{{ | ||
Signatories: []shared.Signatory{{SignedAt: now}}, | ||
}}, | ||
}, | ||
}, | ||
errors: []shared.FieldError{{Source: "/type", Detail: "lpa must be signed"}}, | ||
}, | ||
"certificate provider": { | ||
lpa: &shared.Lpa{ | ||
Status: shared.LpaStatusProcessing, | ||
LpaInit: shared.LpaInit{ | ||
SignedAt: now, | ||
CertificateProvider: shared.CertificateProvider{}, | ||
Attorneys: []shared.Attorney{{SignedAt: &now}}, | ||
TrustCorporations: []shared.TrustCorporation{{ | ||
Signatories: []shared.Signatory{{SignedAt: now}}, | ||
}}, | ||
}, | ||
}, | ||
errors: []shared.FieldError{{Source: "/type", Detail: "lpa must have a certificate"}}, | ||
}, | ||
"attorney": { | ||
lpa: &shared.Lpa{ | ||
Status: shared.LpaStatusProcessing, | ||
LpaInit: shared.LpaInit{ | ||
SignedAt: now, | ||
CertificateProvider: shared.CertificateProvider{SignedAt: &now}, | ||
Attorneys: []shared.Attorney{{SignedAt: &now}, {}}, | ||
TrustCorporations: []shared.TrustCorporation{{ | ||
Signatories: []shared.Signatory{{SignedAt: now}}, | ||
}}, | ||
}, | ||
}, | ||
errors: []shared.FieldError{{Source: "/type", Detail: "lpa must be signed by attorneys"}}, | ||
}, | ||
"trust corporation": { | ||
lpa: &shared.Lpa{ | ||
Status: shared.LpaStatusProcessing, | ||
LpaInit: shared.LpaInit{ | ||
SignedAt: now, | ||
CertificateProvider: shared.CertificateProvider{SignedAt: &now}, | ||
Attorneys: []shared.Attorney{{SignedAt: &now}}, | ||
TrustCorporations: []shared.TrustCorporation{{ | ||
Signatories: []shared.Signatory{{SignedAt: now}, {}}, | ||
}}, | ||
}, | ||
}, | ||
errors: []shared.FieldError{{Source: "/type", Detail: "lpa must be signed by trust corporations"}}, | ||
}, | ||
} | ||
|
||
for name, tc := range testcases { | ||
t.Run(name, func(t *testing.T) { | ||
errors := Perfect{}.Apply(tc.lpa) | ||
assert.Equal(t, tc.errors, errors) | ||
}) | ||
} | ||
} | ||
|
||
func TestRegisterApplyWhenNotProcessing(t *testing.T) { | ||
for _, status := range []shared.LpaStatus{shared.LpaStatusPerfect, shared.LpaStatusRegistered} { | ||
t.Run(string(status), func(t *testing.T) { | ||
errors := Perfect{}.Apply(&shared.Lpa{Status: status}) | ||
assert.Equal(t, []shared.FieldError{{Source: "/type", Detail: "status must be processing to make perfect"}}, errors) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidatePerfect(t *testing.T) { | ||
_, errors := validatePerfect(nil) | ||
assert.Nil(t, errors) | ||
} | ||
|
||
func TestValidatePerfectWhenChanges(t *testing.T) { | ||
_, errors := validatePerfect([]shared.Change{{}}) | ||
assert.Equal(t, []shared.FieldError{{Source: "/changes", Detail: "expected empty"}}, 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
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