Skip to content

Commit

Permalink
[OTE-730] add test to check register affiliate auth (#2235)
Browse files Browse the repository at this point in the history
Co-authored-by: Teddy Ding <[email protected]>
  • Loading branch information
affanv14 and teddyding authored Sep 11, 2024
1 parent 91714c5 commit 6db737c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
73 changes: 73 additions & 0 deletions protocol/x/affiliates/e2e/register_affiliate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package affiliate_test

import (
"testing"

testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
constants "github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
"github.com/stretchr/testify/require"
)

func TestRegisterAffiliateInvalidSigner(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()

testCases := []struct {
name string
referee string
affiliate string
signer string
expectSuccess bool
}{
{
name: "Valid signer (referee)",
referee: constants.BobAccAddress.String(),
affiliate: constants.AliceAccAddress.String(),
signer: constants.BobAccAddress.String(),
expectSuccess: true,
},
{
name: "Invalid signer (affiliate)",
referee: constants.BobAccAddress.String(),
affiliate: constants.AliceAccAddress.String(),
signer: constants.AliceAccAddress.String(),
expectSuccess: false,
},
{
name: "Invalid signer (non-related address)",
referee: constants.BobAccAddress.String(),
affiliate: constants.AliceAccAddress.String(),
signer: constants.CarlAccAddress.String(),
expectSuccess: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
msgRegisterAffiliate := types.MsgRegisterAffiliate{
Referee: tc.referee,
Affiliate: tc.affiliate,
}

checkTxMsgRegisterAffiliate := testapp.MustMakeCheckTx(
ctx,
tApp.App,
testapp.MustMakeCheckTxOptions{
AccAddressForSigning: tc.signer,
Gas: constants.TestGasLimit,
FeeAmt: constants.TestFeeCoins_5Cents,
},
&msgRegisterAffiliate,
)
checkTxResp := tApp.CheckTx(checkTxMsgRegisterAffiliate)

if tc.expectSuccess {
require.True(t, checkTxResp.IsOK(), "Expected CheckTx to succeed with valid signer")
} else {
require.True(t, checkTxResp.IsErr(), "Expected CheckTx to fail with invalid signer")
require.Contains(t, checkTxResp.Log, "pubKey does not match signer address")
}
})
}
}
5 changes: 4 additions & 1 deletion protocol/x/affiliates/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ type msgServer struct {
}

// RegisterAffiliate implements types.MsgServer.
// This is only valid if a referee signs the message.
// This is only valid if a referee signs the message
// since the referee field is annotated with cosmos.msg.v1.signer
// in protos. This ensures that only referee is returned
// as a signer when GetSigners is called for authentication.
// For example, if Alice is the referee and Bob is the affiliate,
// then only Alice can register Bob as an affiliate. Any
// other signer that sends this message will be rejected.
Expand Down

0 comments on commit 6db737c

Please sign in to comment.