diff --git a/x/reports/keeper/common_test.go b/x/reports/keeper/common_test.go index d040fdc199..018f49c512 100644 --- a/x/reports/keeper/common_test.go +++ b/x/reports/keeper/common_test.go @@ -3,14 +3,8 @@ package keeper_test import ( "testing" - authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - - profileskeeper "github.com/desmos-labs/desmos/v5/x/profiles/keeper" - profilestypes "github.com/desmos-labs/desmos/v5/x/profiles/types" - - postskeeper "github.com/desmos-labs/desmos/v5/x/posts/keeper" - poststypes "github.com/desmos-labs/desmos/v5/x/posts/types" + "github.com/golang/mock/gomock" db "github.com/cometbft/cometbft-db" "github.com/cometbft/cometbft/libs/log" @@ -19,43 +13,32 @@ import ( "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/stretchr/testify/suite" "github.com/desmos-labs/desmos/v5/app" - relationshipskeeper "github.com/desmos-labs/desmos/v5/x/relationships/keeper" - relationshipstypes "github.com/desmos-labs/desmos/v5/x/relationships/types" "github.com/desmos-labs/desmos/v5/x/reports/keeper" + "github.com/desmos-labs/desmos/v5/x/reports/testutil" "github.com/desmos-labs/desmos/v5/x/reports/types" - subspaceskeeper "github.com/desmos-labs/desmos/v5/x/subspaces/keeper" - subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types" ) type KeeperTestSuite struct { suite.Suite - cdc codec.Codec - legacyAminoCdc *codec.LegacyAmino - ctx sdk.Context + cdc codec.Codec + ctx sdk.Context storeKey storetypes.StoreKey k keeper.Keeper - ak profileskeeper.Keeper - sk subspaceskeeper.Keeper - rk relationshipskeeper.Keeper - pk postskeeper.Keeper + ak *testutil.MockProfilesKeeper + sk *testutil.MockSubspacesKeeper + rk *testutil.MockRelationshipsKeeper + pk *testutil.MockPostsKeeper } func (suite *KeeperTestSuite) SetupTest() { // Define store keys - keys := sdk.NewMemoryStoreKeys( - paramstypes.StoreKey, authtypes.StoreKey, - profilestypes.StoreKey, relationshipstypes.StoreKey, - subspacestypes.StoreKey, poststypes.StoreKey, - types.StoreKey, - ) - tKeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) + keys := sdk.NewMemoryStoreKeys(types.StoreKey) suite.storeKey = keys[types.StoreKey] // Create an in-memory db @@ -64,23 +47,22 @@ func (suite *KeeperTestSuite) SetupTest() { for _, key := range keys { ms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, memDB) } - for _, tKey := range tKeys { - ms.MountStoreWithDB(tKey, storetypes.StoreTypeTransient, memDB) - } - if err := ms.LoadLatestVersion(); err != nil { panic(err) } suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger()) - suite.cdc, suite.legacyAminoCdc = app.MakeCodecs() + suite.cdc, _ = app.MakeCodecs() + + // Mocks initializations + ctrl := gomock.NewController(suite.T()) + defer ctrl.Finish() - // Define keeper - suite.sk = subspaceskeeper.NewKeeper(suite.cdc, keys[subspacestypes.StoreKey], nil, nil, authtypes.NewModuleAddress("gov").String()) - suite.rk = relationshipskeeper.NewKeeper(suite.cdc, keys[relationshipstypes.StoreKey], suite.sk) - authKeeper := authkeeper.NewAccountKeeper(suite.cdc, keys[authtypes.StoreKey], authtypes.ProtoBaseAccount, app.GetMaccPerms(), "cosmos", authtypes.NewModuleAddress("gov").String()) - suite.ak = profileskeeper.NewKeeper(suite.cdc, suite.legacyAminoCdc, keys[profilestypes.StoreKey], authKeeper, suite.rk, nil, nil, nil, authtypes.NewModuleAddress("gov").String()) - suite.pk = postskeeper.NewKeeper(suite.cdc, keys[poststypes.StoreKey], suite.ak, suite.sk, suite.rk, authtypes.NewModuleAddress("gov").String()) + // Setup keepers + suite.sk = testutil.NewMockSubspacesKeeper(ctrl) + suite.rk = testutil.NewMockRelationshipsKeeper(ctrl) + suite.ak = testutil.NewMockProfilesKeeper(ctrl) + suite.pk = testutil.NewMockPostsKeeper(ctrl) suite.k = keeper.NewKeeper( suite.cdc, suite.storeKey, diff --git a/x/reports/keeper/external_hooks.go b/x/reports/keeper/external_hooks.go index 8f426442aa..96941c6b61 100644 --- a/x/reports/keeper/external_hooks.go +++ b/x/reports/keeper/external_hooks.go @@ -36,15 +36,6 @@ func (h Hooks) AfterSubspaceSaved(ctx sdk.Context, subspaceID uint64) { // AfterSubspaceDeleted implements subspacestypes.Hooks func (h Hooks) AfterSubspaceDeleted(ctx sdk.Context, subspaceID uint64) { - // Delete the reason id key - h.k.DeleteNextReasonID(ctx, subspaceID) - - // Delete all the reasons related to this subspace - h.k.IterateSubspaceReasons(ctx, subspaceID, func(reason types.Reason) (stop bool) { - h.k.DeleteReason(ctx, reason.SubspaceID, reason.ID) - return false - }) - // Delete the report id key h.k.DeleteNextReportID(ctx, subspaceID) @@ -53,6 +44,15 @@ func (h Hooks) AfterSubspaceDeleted(ctx sdk.Context, subspaceID uint64) { h.k.DeleteReport(ctx, report.SubspaceID, report.ID) return false }) + + // Delete the reason id key + h.k.DeleteNextReasonID(ctx, subspaceID) + + // Delete all the reasons related to this subspace + h.k.IterateSubspaceReasons(ctx, subspaceID, func(reason types.Reason) (stop bool) { + h.k.DeleteReason(ctx, reason.SubspaceID, reason.ID) + return false + }) } // AfterSubspaceGroupSaved implements subspacestypes.Hooks diff --git a/x/reports/keeper/external_hooks_test.go b/x/reports/keeper/external_hooks_test.go index 5dd368ef74..483a1c104b 100644 --- a/x/reports/keeper/external_hooks_test.go +++ b/x/reports/keeper/external_hooks_test.go @@ -3,33 +3,21 @@ package keeper_test import ( "time" - poststypes "github.com/desmos-labs/desmos/v5/x/posts/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/desmos-labs/desmos/v5/x/reports/types" - subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types" ) func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() { testCases := []struct { - name string - store func(ctx sdk.Context) - subspace subspacestypes.Subspace - check func(ctx sdk.Context) + name string + store func(ctx sdk.Context) + subspaceID uint64 + check func(ctx sdk.Context) }{ { - name: "saving a subspaces adds the correct keys", - subspace: subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC), - nil, - ), + name: "saving a subspaces adds the correct keys", + subspaceID: 1, check: func(ctx sdk.Context) { storedReasonID, err := suite.k.GetNextReasonID(ctx, 1) suite.Require().NoError(err) @@ -43,29 +31,10 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() { { name: "reason and report ids are not overwritten", store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 2) suite.k.SetNextReasonID(ctx, 1, 2) }, - subspace: subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - "cosmos1a0cj0j6ujn2xap8p40y6648d0w2npytw3xvenm", - time.Date(2020, 1, 2, 12, 00, 00, 000, time.UTC), - nil, - ), + subspaceID: 1, check: func(ctx sdk.Context) { storedReasonID, err := suite.k.GetNextReasonID(ctx, 1) suite.Require().NoError(err) @@ -78,9 +47,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() { }, } - // Set the hooks - suite.sk.SetHooks(suite.k.Hooks()) - for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { @@ -89,7 +55,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceSaved() { tc.store(ctx) } - suite.sk.SaveSubspace(ctx, tc.subspace) + suite.k.Hooks().AfterSubspaceSaved(ctx, tc.subspaceID) if tc.check != nil { tc.check(ctx) } @@ -141,9 +107,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceDeleted() { }, } - // Set the hooks - suite.sk.SetHooks(suite.k.Hooks()) - for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { @@ -152,7 +115,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterSubspaceDeleted() { tc.store(ctx) } - suite.sk.DeleteSubspace(ctx, tc.subspaceID) + suite.k.Hooks().AfterSubspaceDeleted(ctx, tc.subspaceID) if tc.check != nil { tc.check(ctx) } @@ -171,23 +134,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() { { name: "deleting a post removes all the associated reports", store: func(ctx sdk.Context) { - suite.pk.SavePost(ctx, poststypes.NewPost( - 1, - 0, - 1, - "External ID", - "This is a text", - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - 1, - nil, - nil, - nil, - poststypes.REPLY_SETTING_EVERYONE, - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - )) - suite.k.SaveReport(ctx, types.NewReport( 1, 1, @@ -209,9 +155,6 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() { }, } - // Set the hooks - suite.pk.SetHooks(suite.k.Hooks()) - for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { @@ -220,7 +163,7 @@ func (suite *KeeperTestSuite) TestKeeper_AfterPostDeleted() { tc.store(ctx) } - suite.pk.DeletePost(ctx, tc.subspaceID, tc.postID) + suite.k.Hooks().AfterPostDeleted(ctx, tc.subspaceID, tc.postID) if tc.check != nil { tc.check(ctx) } diff --git a/x/reports/keeper/genesis_test.go b/x/reports/keeper/genesis_test.go index 633eb5b287..93a8491458 100644 --- a/x/reports/keeper/genesis_test.go +++ b/x/reports/keeper/genesis_test.go @@ -4,6 +4,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/golang/mock/gomock" "github.com/desmos-labs/desmos/v5/x/reports/types" subspacestypes "github.com/desmos-labs/desmos/v5/x/subspaces/types" @@ -12,35 +13,47 @@ import ( func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) expGenesis *types.GenesisState }{ { name: "subspaces data entries are exported properly", + setup: func() { + subspaces := []subspacestypes.Subspace{ + subspacestypes.NewSubspace( + 1, + "Test subspace", + "This is a test subspace", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + ), + subspacestypes.NewSubspace( + 2, + "Another test subspace", + "This is another test subspace", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + ), + } + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) suite.k.SetNextReportID(ctx, 1, 2) - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 2, - "Another test subspace", - "This is another test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 2, 3) suite.k.SetNextReportID(ctx, 2, 4) @@ -53,6 +66,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { }, { name: "reasons are exported properly", + setup: func() { + subspaces := []subspacestypes.Subspace{} + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { suite.k.SaveReason(ctx, types.NewReason( 1, @@ -74,6 +97,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { }, { name: "reports are exported properly", + setup: func() { + subspaces := []subspacestypes.Subspace{} + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, @@ -101,6 +134,16 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { }, { name: "params are exported properly", + setup: func() { + subspaces := []subspacestypes.Subspace{} + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { suite.k.SetParams(ctx, types.NewParams([]types.StandardReason{ types.NewStandardReason(1, "Spam", "This content is spam"), @@ -116,6 +159,9 @@ func (suite *KeeperTestSuite) TestKeeper_ExportGenesis() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } diff --git a/x/reports/keeper/invariants_test.go b/x/reports/keeper/invariants_test.go index 9e1cd63c1c..a8816d7302 100644 --- a/x/reports/keeper/invariants_test.go +++ b/x/reports/keeper/invariants_test.go @@ -3,7 +3,7 @@ package keeper_test import ( "time" - poststypes "github.com/desmos-labs/desmos/v5/x/posts/types" + "github.com/golang/mock/gomock" sdk "github.com/cosmos/cosmos-sdk/types" @@ -15,38 +15,59 @@ import ( func (suite *KeeperTestSuite) TestValidSubspacesInvariant() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) expBroken bool }{ { name: "non existing next reason id breaks invariant", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + subspaces := []subspacestypes.Subspace{ + subspacestypes.NewSubspace( + 1, + "Test subspace", + "This is a test subspace", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + ), + } + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) }, expBroken: true, }, { name: "non existing next report id breaks invariant", + setup: func() { + subspaces := []subspacestypes.Subspace{ + subspacestypes.NewSubspace( + 1, + "Test subspace", + "This is a test subspace", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + ), + } + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) }, @@ -54,17 +75,28 @@ func (suite *KeeperTestSuite) TestValidSubspacesInvariant() { }, { name: "valid data does not break invariant", + setup: func() { + subspaces := []subspacestypes.Subspace{ + subspacestypes.NewSubspace( + 1, + "Test subspace", + "This is a test subspace", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", + "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + ), + } + suite.sk.EXPECT(). + IterateSubspaces(gomock.Any(), gomock.Any()). + Do(func(ctx sdk.Context, fn func(subspace subspacestypes.Subspace) (stop bool)) { + for _, subspace := range subspaces { + fn(subspace) + } + }) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) suite.k.SetNextReportID(ctx, 1, 1) }, @@ -76,6 +108,9 @@ func (suite *KeeperTestSuite) TestValidSubspacesInvariant() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } @@ -89,11 +124,17 @@ func (suite *KeeperTestSuite) TestValidSubspacesInvariant() { func (suite *KeeperTestSuite) TestValidReasonsInvariant() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) expBroken bool }{ { name: "non existing subspace breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, store: func(ctx sdk.Context) { suite.k.SaveReason(ctx, types.NewReason( 1, @@ -106,18 +147,12 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { }, { name: "non existing next reason id breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -129,17 +164,12 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { }, { name: "invalid reason id compared to next reason id breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) suite.k.SaveReason(ctx, types.NewReason( @@ -153,17 +183,12 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { }, { name: "invalid reason breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 2) suite.k.SaveReason(ctx, types.NewReason( @@ -177,17 +202,12 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { }, { name: "valid data does not break invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 2) suite.k.SaveReason(ctx, types.NewReason( @@ -205,6 +225,9 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } @@ -218,11 +241,17 @@ func (suite *KeeperTestSuite) TestValidReasonsInvariant() { func (suite *KeeperTestSuite) TestValidReportsInvariant() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) expBroken bool }{ { name: "missing subspace breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, @@ -238,18 +267,12 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "missing reason breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - suite.k.SaveReport(ctx, types.NewReport( 1, 1, @@ -264,18 +287,12 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "missing next report id breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -297,17 +314,12 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "invalid report id compared to next report id breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 1) suite.k.SaveReason(ctx, types.NewReason( @@ -331,17 +343,16 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "missing post breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.pk.EXPECT(). + HasPost(gomock.Any(), uint64(1), uint64(1)). + Return(false) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 2) suite.k.SaveReason(ctx, types.NewReason( @@ -365,17 +376,12 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "invalid report breaks invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 2) suite.k.SaveReason(ctx, types.NewReason( @@ -399,17 +405,16 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { }, { name: "valid data does not break invariant", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.pk.EXPECT(). + HasPost(gomock.Any(), uint64(1), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - "cosmos1s0he0z3g92zwsxdj83h0ky9w463sx7gq9mqtgn", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 2) suite.k.SaveReason(ctx, types.NewReason( @@ -419,23 +424,6 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { "This content is spam", )) - suite.pk.SavePost(ctx, poststypes.NewPost( - 1, - 0, - 1, - "External ID", - "This is a text", - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - 1, - nil, - nil, - nil, - poststypes.REPLY_SETTING_EVERYONE, - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - )) - suite.k.SaveReport(ctx, types.NewReport( 1, 1, @@ -454,6 +442,9 @@ func (suite *KeeperTestSuite) TestValidReportsInvariant() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } diff --git a/x/reports/keeper/msg_server_test.go b/x/reports/keeper/msg_server_test.go index 0afac73b14..d939ad174f 100644 --- a/x/reports/keeper/msg_server_test.go +++ b/x/reports/keeper/msg_server_test.go @@ -3,7 +3,7 @@ package keeper_test import ( "time" - "github.com/desmos-labs/desmos/v5/testutil/profilestesting" + "github.com/golang/mock/gomock" poststypes "github.com/desmos-labs/desmos/v5/x/posts/types" @@ -18,6 +18,7 @@ import ( func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { testCases := []struct { name string + setup func() setupCtx func(ctx sdk.Context) sdk.Context store func(ctx sdk.Context) msg *types.MsgCreateReport @@ -28,6 +29,11 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }{ { name: "user without profile returns error", + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(false) + }, msg: types.NewMsgCreateReport( 1, []uint32{1}, @@ -39,9 +45,14 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "non existing subspace returns error", - store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) + + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) }, msg: types.NewMsgCreateReport( 1, @@ -54,20 +65,16 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "non existing reason returns error", - store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, + store: func(ctx sdk.Context) { suite.k.SetNextReportID(ctx, 1, 1) }, msg: types.NewMsgCreateReport( @@ -81,20 +88,26 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "no permission returns error", - store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionReportContent, + ). + Return(false) + }, + store: func(ctx sdk.Context) { suite.k.SetNextReportID(ctx, 1, 1) suite.k.SaveReason(ctx, types.NewReason( @@ -115,32 +128,31 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "invalid report data returns error", + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) + + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionReportContent, + ). + Return(true) + }, setupCtx: func(ctx sdk.Context) sdk.Context { return ctx.WithBlockTime(time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC)) }, store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) - - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 1) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionReportContent), - ) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -159,32 +171,31 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "duplicated report returns error", + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) + + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionReportContent, + ). + Return(true) + }, setupCtx: func(ctx sdk.Context) sdk.Context { return ctx.WithBlockTime(time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC)) }, store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) - - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 1) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionReportContent), - ) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -213,32 +224,41 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "valid request works properly - user target", + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) + + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionReportContent, + ). + Return(true) + + suite.rk. + EXPECT(). + HasUserBlocked( + gomock.Any(), + "cosmos1ggzk8tnte9lmzgpvyzzdtmwmn6rjlct4spmjjd", + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + uint64(1), + ). + Return(false) + }, setupCtx: func(ctx sdk.Context) sdk.Context { return ctx.WithBlockTime(time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC)) }, store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) - - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 1) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionReportContent), - ) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -287,55 +307,66 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { }, { name: "valid request works properly - post target", + setup: func() { + suite.ak.EXPECT(). + HasProfile(gomock.Any(), "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh"). + Return(true) + + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionReportContent, + ). + Return(true) + + suite.pk.EXPECT(). + GetPost(gomock.Any(), uint64(1), uint64(1)). + Return(poststypes.NewPost( + 1, + 0, + 1, + "External ID", + "This is a text", + "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + 1, + nil, + nil, + nil, + poststypes.REPLY_SETTING_EVERYONE, + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + ), true) + + suite.rk. + EXPECT(). + HasUserBlocked( + gomock.Any(), + "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + uint64(1), + ). + Return(false) + }, setupCtx: func(ctx sdk.Context) sdk.Context { return ctx.WithBlockTime(time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC)) }, store: func(ctx sdk.Context) { - err := suite.ak.SaveProfile(ctx, profilestesting.ProfileFromAddr("cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh")) - suite.Require().NoError(err) - - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReportID(ctx, 1, 1) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionReportContent), - ) - suite.k.SaveReason(ctx, types.NewReason( 1, 1, "Spam", "This content is spam, or the user is spamming", )) - - suite.pk.SavePost(ctx, poststypes.NewPost( - 1, - 0, - 1, - "External ID", - "This is a text", - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - 1, - nil, - nil, - nil, - poststypes.REPLY_SETTING_EVERYONE, - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - "cosmos13t6y2nnugtshwuy0zkrq287a95lyy8vzleaxmd", - )) }, msg: types.NewMsgCreateReport( 1, @@ -382,6 +413,9 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.setupCtx != nil { ctx = tc.setupCtx(ctx) } @@ -409,6 +443,7 @@ func (suite *KeeperTestSuite) TestMsgServer_CreateReport() { func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) msg *types.MsgDeleteReport shouldErr bool @@ -416,6 +451,11 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { }{ { name: "non existing subspace returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, msg: types.NewMsgDeleteReport( 1, 1, @@ -425,17 +465,10 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { }, { name: "non existing report returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) }, msg: types.NewMsgDeleteReport( 1, @@ -445,26 +478,30 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { shouldErr: true, }, { - name: "no permission returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + name: "invalid signer returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReports, + ). + Return(false) + }, + store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, 1, []uint32{1}, "This content is spam", types.NewUserTarget("cosmos1pjffdtweghpyxru9alssyqtdkq8mn6sepgstgm"), - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + "cosmos1zkmf50jq4lzvhvp5ekl0sdf2p4g3v9v8edt24z", time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), )) }, @@ -476,26 +513,40 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { shouldErr: true, }, { - name: "invalid signer returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + name: "no permission returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReports, + ). + Return(false) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionDeleteOwnReports, + ). + Return(false) + }, + store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, 1, []uint32{1}, "This content is spam", types.NewUserTarget("cosmos1pjffdtweghpyxru9alssyqtdkq8mn6sepgstgm"), - "cosmos1zkmf50jq4lzvhvp5ekl0sdf2p4g3v9v8edt24z", + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), )) }, @@ -508,25 +559,32 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { }, { name: "report creator can delete the report properly", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionDeleteOwnReports), - ) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReports, + ). + Return(false) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionDeleteOwnReports, + ). + Return(true) + }, + store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, 1, @@ -559,25 +617,22 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { }, { name: "moderator can delete the report properly", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReports), - ) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReports, + ). + Return(true) + }, + store: func(ctx sdk.Context) { suite.k.SaveReport(ctx, types.NewReport( 1, 1, @@ -614,6 +669,9 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } @@ -632,6 +690,7 @@ func (suite *KeeperTestSuite) TestMsgServer_DeleteReport() { func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) msg *types.MsgSupportStandardReason shouldErr bool @@ -641,6 +700,11 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { }{ { name: "non existing subspace returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, msg: types.NewMsgSupportStandardReason( 1, 1, @@ -650,18 +714,12 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { }, { name: "non existing standard reason returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - suite.k.SetParams(ctx, types.NewParams(nil)) }, msg: types.NewMsgSupportStandardReason( @@ -673,18 +731,22 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { }, { name: "no permission returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(false) + }, + store: func(ctx sdk.Context) { suite.k.SetParams(ctx, types.NewParams([]types.StandardReason{ types.NewStandardReason(1, "Spam", "This content is spam"), })) @@ -698,25 +760,22 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { }, { name: "not found next reason id returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) - - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) + }, + store: func(ctx sdk.Context) { suite.k.SetParams(ctx, types.NewParams([]types.StandardReason{ types.NewStandardReason(1, "Spam", "This content is spam"), })) @@ -730,26 +789,24 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { }, { name: "valid request returns no error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) - suite.k.SetParams(ctx, types.NewParams([]types.StandardReason{ types.NewStandardReason(1, "Spam", "This content is spam"), })) @@ -789,6 +846,9 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } @@ -813,6 +873,7 @@ func (suite *KeeperTestSuite) TestMsgServer_SupportStandardReason() { func (suite *KeeperTestSuite) TestMsgServer_AddReason() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) msg *types.MsgAddReason shouldErr bool @@ -822,6 +883,11 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { }{ { name: "non existing subspace returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, msg: types.NewMsgAddReason( 1, "Spam", @@ -832,17 +898,20 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { }, { name: "no permission returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(false) }, msg: types.NewMsgAddReason( 1, @@ -854,24 +923,20 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { }, { name: "no next reason id returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) }, msg: types.NewMsgAddReason( 1, @@ -883,25 +948,23 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { }, { name: "invalid reason returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) - - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) }, msg: types.NewMsgAddReason( 1, @@ -913,25 +976,23 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { }, { name: "correct request returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) + }, store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) suite.k.SetNextReasonID(ctx, 1, 1) - - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) }, msg: types.NewMsgAddReason( 1, @@ -968,6 +1029,9 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } @@ -988,6 +1052,7 @@ func (suite *KeeperTestSuite) TestMsgServer_AddReason() { func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { testCases := []struct { name string + setup func() store func(ctx sdk.Context) msg *types.MsgRemoveReason shouldErr bool @@ -995,6 +1060,11 @@ func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { }{ { name: "non existing subspace returns error", + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(false) + }, msg: types.NewMsgRemoveReason( 1, 1, @@ -1004,17 +1074,10 @@ func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { }, { name: "non existing reason returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) }, msg: types.NewMsgRemoveReason( 1, @@ -1025,18 +1088,22 @@ func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { }, { name: "no permission returns error", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(false) + }, + store: func(ctx sdk.Context) { suite.k.SaveReason(ctx, types.NewReason( 1, 1, @@ -1053,31 +1120,28 @@ func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { }, { name: "valid request works properly", - store: func(ctx sdk.Context) { - suite.sk.SaveSubspace(ctx, subspacestypes.NewSubspace( - 1, - "Test subspace", - "This is a test subspace", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - "cosmos1m0czrla04f7rp3zg7dsgc4kla54q7pc4xt00l5", - "cosmos1qzskhrcjnkdz2ln4yeafzsdwht8ch08j4wed69", - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - )) + setup: func() { + suite.sk.EXPECT(). + HasSubspace(gomock.Any(), uint64(1)). + Return(true) + suite.sk.EXPECT(). + HasPermission( + gomock.Any(), + uint64(1), + uint32(subspacestypes.RootSectionID), + "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", + types.PermissionManageReasons, + ). + Return(true) + }, + store: func(ctx sdk.Context) { suite.k.SaveReason(ctx, types.NewReason( 1, 1, "Spam", "This content is spam", )) - - suite.sk.SetUserPermissions(ctx, - 1, - 0, - "cosmos1qycmg40ju50fx2mcc82qtkzuswjs3mj3mqekeh", - subspacestypes.NewPermissions(types.PermissionManageReasons), - ) }, msg: types.NewMsgRemoveReason( 1, @@ -1105,6 +1169,9 @@ func (suite *KeeperTestSuite) TestMsgServer_RemoveReason() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() + if tc.setup != nil { + tc.setup() + } if tc.store != nil { tc.store(ctx) } diff --git a/x/reports/keeper/reports_test.go b/x/reports/keeper/reports_test.go index c84e89a704..b2fb9ba45b 100644 --- a/x/reports/keeper/reports_test.go +++ b/x/reports/keeper/reports_test.go @@ -4,7 +4,7 @@ import ( "time" poststypes "github.com/desmos-labs/desmos/v5/x/posts/types" - relationshipstypes "github.com/desmos-labs/desmos/v5/x/relationships/types" + "github.com/golang/mock/gomock" sdk "github.com/cosmos/cosmos-sdk/types" @@ -144,7 +144,7 @@ func (suite *KeeperTestSuite) TestKeeper_DeleteNextReportID() { func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { testCases := []struct { name string - store func(ctx sdk.Context) + setup func() report types.Report shouldErr bool }{ @@ -163,13 +163,14 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { }, { name: "UserTarget - blocked reporter returns error", - store: func(ctx sdk.Context) { - suite.rk.SaveUserBlock(ctx, relationshipstypes.NewUserBlock( - "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", - "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", - "", - 1, - )) + setup: func() { + suite.rk.EXPECT(). + HasUserBlocked(gomock.Any(), + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", + uint64(1), + ). + Return(true) }, report: types.NewReport( 1, @@ -184,6 +185,15 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { }, { name: "UserTarget - valid data returns no error", + setup: func() { + suite.rk.EXPECT(). + HasUserBlocked(gomock.Any(), + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", + uint64(1), + ). + Return(false) + }, report: types.NewReport( 1, 1, @@ -197,6 +207,11 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { }, { name: "PostTarget - not found post returns error", + setup: func() { + suite.pk.EXPECT(). + GetPost(gomock.Any(), uint64(1), uint64(1)). + Return(poststypes.Post{}, false) + }, report: types.NewReport( 1, 1, @@ -210,30 +225,33 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { }, { name: "PostTarget - blocked user returns error", - store: func(ctx sdk.Context) { - suite.pk.SavePost(ctx, poststypes.NewPost( - 1, - 0, - 1, - "", - "This is a new post", - "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", - 0, - nil, - nil, - nil, - poststypes.REPLY_SETTING_EVERYONE, - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", - )) - - suite.rk.SaveUserBlock(ctx, relationshipstypes.NewUserBlock( - "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", - "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", - "", - 1, - )) + setup: func() { + suite.pk.EXPECT(). + GetPost(gomock.Any(), uint64(1), uint64(1)). + Return(poststypes.NewPost( + 1, + 0, + 1, + "", + "This is a new post", + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + 0, + nil, + nil, + nil, + poststypes.REPLY_SETTING_EVERYONE, + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + ), true) + + suite.rk.EXPECT(). + HasUserBlocked(gomock.Any(), + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", + uint64(1), + ). + Return(true) }, report: types.NewReport( 1, @@ -248,23 +266,33 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { }, { name: "PostsData - valid data returns no error", - store: func(ctx sdk.Context) { - suite.pk.SavePost(ctx, poststypes.NewPost( - 1, - 0, - 1, - "", - "This is a new post", - "cosmos1r9jamre0x0qqy562rhhckt6sryztwhnvhafyz4", - 0, - nil, - nil, - nil, - poststypes.REPLY_SETTING_EVERYONE, - time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), - nil, - "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", - )) + setup: func() { + suite.pk.EXPECT(). + GetPost(gomock.Any(), uint64(1), uint64(1)). + Return(poststypes.NewPost( + 1, + 0, + 1, + "", + "This is a new post", + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + 0, + nil, + nil, + nil, + poststypes.REPLY_SETTING_EVERYONE, + time.Date(2020, 1, 1, 12, 00, 00, 000, time.UTC), + nil, + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + ), true) + + suite.rk.EXPECT(). + HasUserBlocked(gomock.Any(), + "cosmos10s22qjua2n3law0ymstm3txm7764mfk2cjawq5", + "cosmos1wprgptc8ktt0eemrn2znpxv8crdxm8tdpkdr7w", + uint64(1), + ). + Return(false) }, report: types.NewReport( 1, @@ -283,8 +311,8 @@ func (suite *KeeperTestSuite) TestKeeper_ValidateReport() { tc := tc suite.Run(tc.name, func() { ctx, _ := suite.ctx.CacheContext() - if tc.store != nil { - tc.store(ctx) + if tc.setup != nil { + tc.setup() } err := suite.k.ValidateReport(ctx, tc.report) diff --git a/x/reports/testutil/expected_keepers_mocks.go b/x/reports/testutil/expected_keepers_mocks.go index 79b62a83ad..33d8ab5ab4 100644 --- a/x/reports/testutil/expected_keepers_mocks.go +++ b/x/reports/testutil/expected_keepers_mocks.go @@ -73,6 +73,34 @@ func (m *MockSubspacesKeeper) EXPECT() *MockSubspacesKeeperMockRecorder { return m.recorder } +// GetAllSubspaces mocks base method. +func (m *MockSubspacesKeeper) GetAllSubspaces(ctx types.Context) []types1.Subspace { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAllSubspaces", ctx) + ret0, _ := ret[0].([]types1.Subspace) + return ret0 +} + +// GetAllSubspaces indicates an expected call of GetAllSubspaces. +func (mr *MockSubspacesKeeperMockRecorder) GetAllSubspaces(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllSubspaces", reflect.TypeOf((*MockSubspacesKeeper)(nil).GetAllSubspaces), ctx) +} + +// GetUsersWithRootPermissions mocks base method. +func (m *MockSubspacesKeeper) GetUsersWithRootPermissions(ctx types.Context, subspaceID uint64, permission types1.Permissions) []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetUsersWithRootPermissions", ctx, subspaceID, permission) + ret0, _ := ret[0].([]string) + return ret0 +} + +// GetUsersWithRootPermissions indicates an expected call of GetUsersWithRootPermissions. +func (mr *MockSubspacesKeeperMockRecorder) GetUsersWithRootPermissions(ctx, subspaceID, permission interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersWithRootPermissions", reflect.TypeOf((*MockSubspacesKeeper)(nil).GetUsersWithRootPermissions), ctx, subspaceID, permission) +} + // HasPermission mocks base method. func (m *MockSubspacesKeeper) HasPermission(ctx types.Context, subspaceID uint64, sectionID uint32, user string, permission types1.Permission) bool { m.ctrl.T.Helper() @@ -188,6 +216,20 @@ func (mr *MockPostsKeeperMockRecorder) GetPost(ctx, subspaceID, postID interface return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPost", reflect.TypeOf((*MockPostsKeeper)(nil).GetPost), ctx, subspaceID, postID) } +// GetSubspacePosts mocks base method. +func (m *MockPostsKeeper) GetSubspacePosts(ctx types.Context, subspaceID uint64) []types0.Post { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSubspacePosts", ctx, subspaceID) + ret0, _ := ret[0].([]types0.Post) + return ret0 +} + +// GetSubspacePosts indicates an expected call of GetSubspacePosts. +func (mr *MockPostsKeeperMockRecorder) GetSubspacePosts(ctx, subspaceID interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubspacePosts", reflect.TypeOf((*MockPostsKeeper)(nil).GetSubspacePosts), ctx, subspaceID) +} + // HasPost mocks base method. func (m *MockPostsKeeper) HasPost(ctx types.Context, subspaceID, postID uint64) bool { m.ctrl.T.Helper()