Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC-14000 Add Identity Payload Validation Tests #121

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/rvasp/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func parsePayload(payload *protocol.Payload, response bool) (identity *ivms101.I
}

// Validate an identity payload, returning an error if the payload is not valid.
func validateIdentityPayload(identity *ivms101.IdentityPayload, requireBeneficiary bool) *protocol.Error {
func ValidateIdentityPayload(identity *ivms101.IdentityPayload, requireBeneficiary bool) *protocol.Error {
// Verify the identity payload is not nil
if identity == nil {
log.Warn().Msg("identity payload is nil")
Expand Down Expand Up @@ -272,7 +272,7 @@ func validateIdentityPayload(identity *ivms101.IdentityPayload, requireBeneficia
}
default:
log.Warn().Msg(fmt.Sprintf("unknown beneficiary person type: %T", person))
DanielSollis marked this conversation as resolved.
Show resolved Hide resolved
return protocol.Errorf(protocol.ValidationError, "unknown beneficiary person type: %T", person)
return protocol.Errorf(protocol.ValidationError, "unknown beneficiary vasp person type: %T", person)
}
}
return nil
Expand Down
115 changes: 115 additions & 0 deletions pkg/rvasp/transfer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package rvasp_test

import (
"github.com/trisacrypto/testnet/pkg/rvasp"
"github.com/trisacrypto/trisa/pkg/ivms101"
)

func (s *rVASPTestSuite) TestValidateIdentityPayload() {
var err error
require := s.Require()

// Should return an error if the identity payload is nil
err = rvasp.ValidateIdentityPayload(nil, false)
require.EqualError(err, "trisa rejection [INTERNAL_ERROR]: identity payload is nil")

// Should return an error if the originator is nil
req := &ivms101.IdentityPayload{}
err = rvasp.ValidateIdentityPayload(req, false)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing originator")

// Should return an error if the originating vasp is nil
req.Originator = &ivms101.Originator{}
err = rvasp.ValidateIdentityPayload(req, false)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing originating vasp")

// Should return an error if the beneficiary is nil
req.OriginatingVasp = &ivms101.OriginatingVasp{}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing beneficiary")

// Should return an error if the beneficiary person is nil
req.Beneficiary = &ivms101.Beneficiary{BeneficiaryPersons: nil}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing beneficiary person")

// Should return an error with a beneficiary person type other than natural or legal
req.Beneficiary.BeneficiaryPersons = make([]*ivms101.Person, 1)
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: unknown beneficiary person type: <nil>")

// Should return an error if the beneficiary natural person is incomplete
req.Beneficiary.BeneficiaryPersons[0] = &ivms101.Person{
Person: &ivms101.Person_NaturalPerson{
NaturalPerson: &ivms101.NaturalPerson{},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: beneficiary natural person validation error: one or more natural person name identifiers is required")

// Should return an error if the beneficiary legal person is incomplete
req.Beneficiary.BeneficiaryPersons[0] = &ivms101.Person{
Person: &ivms101.Person_LegalPerson{
LegalPerson: &ivms101.LegalPerson{},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: beneficiary legal person validation error: one or more legal person name identifiers is required")

// Should return an error if there are no beneficiary account numbers
req.Beneficiary.BeneficiaryPersons[0].GetLegalPerson().Name = &ivms101.LegalPersonName{
NameIdentifiers: []*ivms101.LegalPersonNameId{
{
LegalPersonName: "LegalPersonName",
LegalPersonNameIdentifierType: ivms101.LegalPersonNameTypeCode_LEGAL_PERSON_NAME_TYPE_CODE_LEGL,
},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing beneficiary account number")

// Should return an error if the beneficiary vasp is nil
req.Beneficiary.AccountNumbers = []string{"AccountNumber"}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing beneficiary vasp")

// Should return an error if the beneficiary vasp entity is nil
req.BeneficiaryVasp = &ivms101.BeneficiaryVasp{}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [INCOMPLETE_IDENTITY]: missing beneficiary vasp entity")

// Should return an error with a beneficiary vasp person type other than natural or legal
req.BeneficiaryVasp.BeneficiaryVasp = &ivms101.Person{}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: unknown beneficiary vasp person type: <nil>")

// Should return an error if the beneficiary vasp natural person is incomplete
req.BeneficiaryVasp.BeneficiaryVasp = &ivms101.Person{
Person: &ivms101.Person_NaturalPerson{
NaturalPerson: &ivms101.NaturalPerson{},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: beneficiary vasp natural person validation error: one or more natural person name identifiers is required")

// Should return an error if the beneficiary vasp legal person is incomplete
req.BeneficiaryVasp.BeneficiaryVasp = &ivms101.Person{
Person: &ivms101.Person_LegalPerson{
LegalPerson: &ivms101.LegalPerson{},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.EqualError(err, "trisa rejection [VALIDATION_ERROR]: beneficiary vasp legal person validation error: one or more legal person name identifiers is required")

// Happy path
req.BeneficiaryVasp.BeneficiaryVasp.GetLegalPerson().Name = &ivms101.LegalPersonName{
NameIdentifiers: []*ivms101.LegalPersonNameId{
{
LegalPersonName: "LegalPersonName",
LegalPersonNameIdentifierType: ivms101.LegalPersonNameTypeCode_LEGAL_PERSON_NAME_TYPE_CODE_LEGL,
},
},
}
err = rvasp.ValidateIdentityPayload(req, true)
require.Nil(err)
}
4 changes: 2 additions & 2 deletions pkg/rvasp/trisa.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (s *TRISA) respondTransfer(in *protocol.SecureEnvelope, peer *peers.Peer, i
return nil, protocol.Errorf(protocol.InternalError, "request could not be processed")
}

if transferError = validateIdentityPayload(identity, requireBeneficiary); transferError != nil {
if transferError = ValidateIdentityPayload(identity, requireBeneficiary); transferError != nil {
log.Warn().Str("message", transferError.Message).Msg("could not validate identity payload")
xfer.SetState(pb.TransactionState_REJECTED)
return nil, transferError
Expand Down Expand Up @@ -676,7 +676,7 @@ func (s *TRISA) sendAsync(tx *db.Transaction) (err error) {
// Repair the beneficiary information if this is the first handshake
if tx.State == pb.TransactionState_PENDING_SENT {
var validationError *protocol.Error
if validationError = validateIdentityPayload(identity, false); validationError != nil {
if validationError = ValidateIdentityPayload(identity, false); validationError != nil {
log.Warn().Str("message", validationError.Message).Msg("could not validate identity payload")
var reject *protocol.SecureEnvelope
if reject, err = envelope.Reject(validationError, envelope.WithEnvelopeID(tx.Envelope)); err != nil {
Expand Down