From 95e57349849c7bc84841cb10f4e4342d22a3f9f3 Mon Sep 17 00:00:00 2001 From: affan Date: Wed, 11 Sep 2024 07:39:53 -0400 Subject: [PATCH 1/3] add test to check register affiliate auth --- protocol/x/affiliates/keeper/msg_server.go | 5 +- .../x/affiliates/keeper/msg_server_test.go | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/protocol/x/affiliates/keeper/msg_server.go b/protocol/x/affiliates/keeper/msg_server.go index 00780ed7fb..b21094d20b 100644 --- a/protocol/x/affiliates/keeper/msg_server.go +++ b/protocol/x/affiliates/keeper/msg_server.go @@ -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. diff --git a/protocol/x/affiliates/keeper/msg_server_test.go b/protocol/x/affiliates/keeper/msg_server_test.go index 404b554391..cb0e609430 100644 --- a/protocol/x/affiliates/keeper/msg_server_test.go +++ b/protocol/x/affiliates/keeper/msg_server_test.go @@ -132,3 +132,66 @@ func TestMsgServer_UpdateAffiliateTiers(t *testing.T) { }) } } + +func TestMsgServer_RegisterAffiliateInvalidSigner(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") + } + }) + } +} From c8305cdcf8da66e73b61ccf7abf8bea6cceba923 Mon Sep 17 00:00:00 2001 From: affan Date: Wed, 11 Sep 2024 15:59:08 -0400 Subject: [PATCH 2/3] move to e2e folder --- .../affiliates/e2e/register_affiliate_test.go | 73 +++++++++++++++++++ .../x/affiliates/keeper/msg_server_test.go | 63 ---------------- 2 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 protocol/x/affiliates/e2e/register_affiliate_test.go diff --git a/protocol/x/affiliates/e2e/register_affiliate_test.go b/protocol/x/affiliates/e2e/register_affiliate_test.go new file mode 100644 index 0000000000..53094669e2 --- /dev/null +++ b/protocol/x/affiliates/e2e/register_affiliate_test.go @@ -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 TestMsgServer_RegisterAffiliateInvalidSigner(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") + } + }) + } +} diff --git a/protocol/x/affiliates/keeper/msg_server_test.go b/protocol/x/affiliates/keeper/msg_server_test.go index cb0e609430..404b554391 100644 --- a/protocol/x/affiliates/keeper/msg_server_test.go +++ b/protocol/x/affiliates/keeper/msg_server_test.go @@ -132,66 +132,3 @@ func TestMsgServer_UpdateAffiliateTiers(t *testing.T) { }) } } - -func TestMsgServer_RegisterAffiliateInvalidSigner(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") - } - }) - } -} From 808872dbbe4b69d89f444c3a6c3a8fcc9fd38bba Mon Sep 17 00:00:00 2001 From: Mohammed Affan Date: Wed, 11 Sep 2024 16:06:20 -0400 Subject: [PATCH 3/3] Update protocol/x/affiliates/e2e/register_affiliate_test.go Co-authored-by: Teddy Ding --- protocol/x/affiliates/e2e/register_affiliate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/x/affiliates/e2e/register_affiliate_test.go b/protocol/x/affiliates/e2e/register_affiliate_test.go index 53094669e2..5408d56b02 100644 --- a/protocol/x/affiliates/e2e/register_affiliate_test.go +++ b/protocol/x/affiliates/e2e/register_affiliate_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestMsgServer_RegisterAffiliateInvalidSigner(t *testing.T) { +func TestRegisterAffiliateInvalidSigner(t *testing.T) { tApp := testapp.NewTestAppBuilder(t).Build() ctx := tApp.InitChain()