diff --git a/x/registry/keeper/msg_server_update_top_level_domain_registration_policy_test.go b/x/registry/keeper/msg_server_update_top_level_domain_registration_policy_test.go new file mode 100644 index 00000000..6c20b812 --- /dev/null +++ b/x/registry/keeper/msg_server_update_top_level_domain_registration_policy_test.go @@ -0,0 +1,92 @@ +package keeper_test + +import ( + "fmt" + + errorsmod "cosmossdk.io/errors" + + "github.com/mycel-domain/mycel/testutil" + "github.com/mycel-domain/mycel/x/registry/types" +) + +func (suite *KeeperTestSuite) TestUpdateTopLevelDomainRegistrationPolicy() { + testCases := []struct { + creator string + name string + registrationPolicy string + expErr error + fn func() + }{ + { + creator: testutil.Alice, + name: "foo", + registrationPolicy: "PUBLIC", + expErr: nil, + fn: func() {}, + }, + { + creator: testutil.Alice, + name: "foo", + registrationPolicy: "PRIVATE", + expErr: nil, + fn: func() {}, + }, + { + creator: testutil.Alice, + name: "foo", + registrationPolicy: "INVALID", + expErr: errorsmod.Wrapf(types.ErrInvalidRegistrationPolicy, "INVALID"), + fn: func() {}, + }, + { + creator: testutil.Alice, + name: "invalid", + registrationPolicy: "PUBLIC", + expErr: errorsmod.Wrapf(types.ErrSecondLevelDomainNotFound, "invalid"), + fn: func() {}, + }, + { + creator: testutil.Bob, + name: "foo", + registrationPolicy: "PUBLIC", + expErr: errorsmod.Wrapf(types.ErrSecondLevelDomainNotEditable, "%s", testutil.Bob), + fn: func() {}, + }, + } + for i, tc := range testCases { + suite.Run(fmt.Sprintf("Case %d", i), func() { + suite.SetupTest() + + // NOTE: cel TLD is registered by Alice by default + + // Run test case function + tc.fn() + + // Update dns record + msgUpdateTopLevelDomainRegistrationPolicy := &types.MsgUpdateTopLevelDomainRegistrationPolicy{ + Creator: tc.creator, + Name: tc.name, + RegistrationPolicy: tc.registrationPolicy, + } + + _, err := suite.msgServer.UpdateTopLevelDomainRegistrationPolicy(suite.ctx, msgUpdateTopLevelDomainRegistrationPolicy) + + if tc.expErr == nil { + // Check if the record is updated + suite.Require().Nil(err) + res, _ := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, "cel") + suite.Require().Equal(tc.registrationPolicy, res.GetRegistrationPolicy()) + // Evalute events + events, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), types.EventTypeUpdateTopLevelDomainRegistrationPolicy) + suite.Require().True(found) + + for _, event := range events { + suite.Require().Equal(tc.name, event.Attributes[0].Value) + suite.Require().Equal(tc.registrationPolicy, event.Attributes[1].Value) + } + } else { + suite.Require().EqualError(err, tc.expErr.Error()) + } + }) + } +}