Skip to content

Commit

Permalink
SC-14000 Add Identity Payload Validation Tests (#121)
Browse files Browse the repository at this point in the history
* Initial work

* finished tests

* updated comments

* adding trisa.go

* Update pkg/rvasp/transfer.go

Co-authored-by: Patrick Deziel <42919891+pdeziel@users.noreply.github.com>

---------

Co-authored-by: Patrick Deziel <42919891+pdeziel@users.noreply.github.com>
  • Loading branch information
Daniel Sollis and pdeziel authored Feb 24, 2023
1 parent 51c6689 commit 587e1f7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
6 changes: 3 additions & 3 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 @@ -271,8 +271,8 @@ func validateIdentityPayload(identity *ivms101.IdentityPayload, requireBeneficia
return protocol.Errorf(protocol.ValidationError, "beneficiary vasp legal person validation error: %s", err)
}
default:
log.Warn().Msg(fmt.Sprintf("unknown beneficiary person type: %T", person))
return protocol.Errorf(protocol.ValidationError, "unknown beneficiary person type: %T", person)
log.Warn().Msg(fmt.Sprintf("unknown beneficiary vasp 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

0 comments on commit 587e1f7

Please sign in to comment.