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

[OTE-730] add test to check register affiliate auth #2235

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 TestMsgServer_RegisterAffiliateInvalidSigner(t *testing.T) {
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading