github.com/KiraCore/sekai@v0.3.43/x/gov/keeper/msg_server_test.go (about) 1 package keeper_test 2 3 import ( 4 "testing" 5 6 simapp "github.com/KiraCore/sekai/app" 7 "github.com/KiraCore/sekai/x/gov/keeper" 8 "github.com/KiraCore/sekai/x/gov/types" 9 tmproto "github.com/cometbft/cometbft/proto/tendermint/types" 10 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" 11 sdk "github.com/cosmos/cosmos-sdk/types" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestMsgServerClaimCouncilor(t *testing.T) { 16 app := simapp.Setup(false) 17 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 18 19 addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) 20 21 // try claim with not allowed user 22 msgServer := keeper.NewMsgServerImpl(app.CustomGovKeeper) 23 _, err := msgServer.ClaimCouncilor(sdk.WrapSDKContext(ctx), types.NewMsgClaimCouncilor( 24 addr, "moniker", "username", "description", "social", "contact", "avatar", 25 )) 26 require.Error(t, err) 27 28 // try claim with allowed user 29 actor := types.NewDefaultActor(addr) 30 err = app.CustomGovKeeper.AddWhitelistPermission(ctx, actor, types.PermClaimCouncilor) 31 require.NoError(t, err) 32 _, err = msgServer.ClaimCouncilor(sdk.WrapSDKContext(ctx), types.NewMsgClaimCouncilor( 33 addr, "moniker", "username", "description", "social", "contact", "avatar", 34 )) 35 require.NoError(t, err) 36 37 // check councilor created 38 councilor, found := app.CustomGovKeeper.GetCouncilor(ctx, addr) 39 require.True(t, found) 40 require.Equal(t, councilor, types.Councilor{ 41 Address: addr, 42 Status: types.CouncilorActive, 43 Rank: 0, 44 AbstentionCounter: 0, 45 }) 46 47 // check moniker, username, description, social, contact, avatar registered correctly 48 recordId := app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "moniker") 49 record := app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 50 require.Equal(t, record.Key, "moniker") 51 require.Equal(t, record.Value, "moniker") 52 53 recordId = app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "username") 54 record = app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 55 require.Equal(t, record.Key, "username") 56 require.Equal(t, record.Value, "username") 57 58 recordId = app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "description") 59 record = app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 60 require.Equal(t, record.Key, "description") 61 require.Equal(t, record.Value, "description") 62 63 recordId = app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "social") 64 record = app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 65 require.Equal(t, record.Key, "social") 66 require.Equal(t, record.Value, "social") 67 68 recordId = app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "contact") 69 record = app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 70 require.Equal(t, record.Key, "contact") 71 require.Equal(t, record.Value, "contact") 72 73 recordId = app.CustomGovKeeper.GetIdentityRecordIdByAddressKey(ctx, addr, "avatar") 74 record = app.CustomGovKeeper.GetIdentityRecordById(ctx, recordId) 75 require.Equal(t, record.Key, "avatar") 76 require.Equal(t, record.Value, "avatar") 77 } 78 79 func TestMsgServerCouncilorPause(t *testing.T) { 80 app := simapp.Setup(false) 81 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 82 83 addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) 84 85 // try pause with not available councilor 86 msgServer := keeper.NewMsgServerImpl(app.CustomGovKeeper) 87 _, err := msgServer.CouncilorPause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorPause( 88 addr, 89 )) 90 require.Error(t, err) 91 92 // test jailed councilor pause 93 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 94 Address: addr, 95 Status: types.CouncilorJailed, 96 Rank: 0, 97 AbstentionCounter: 0, 98 }) 99 _, err = msgServer.CouncilorPause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorPause( 100 addr, 101 )) 102 require.Error(t, err) 103 104 // test inactive councilor 105 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 106 Address: addr, 107 Status: types.CouncilorInactive, 108 Rank: 0, 109 AbstentionCounter: 0, 110 }) 111 _, err = msgServer.CouncilorPause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorPause( 112 addr, 113 )) 114 require.Error(t, err) 115 116 // test paused councilor pause 117 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 118 Address: addr, 119 Status: types.CouncilorPaused, 120 Rank: 0, 121 AbstentionCounter: 0, 122 }) 123 _, err = msgServer.CouncilorPause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorPause( 124 addr, 125 )) 126 require.Error(t, err) 127 128 // active councilor pause 129 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 130 Address: addr, 131 Status: types.CouncilorActive, 132 Rank: 0, 133 AbstentionCounter: 0, 134 }) 135 _, err = msgServer.CouncilorPause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorPause( 136 addr, 137 )) 138 require.NoError(t, err) 139 councilor, found := app.CustomGovKeeper.GetCouncilor(ctx, addr) 140 require.True(t, found) 141 require.Equal(t, councilor.Status, types.CouncilorPaused) 142 } 143 144 func TestMsgServerCouncilorUnpause(t *testing.T) { 145 app := simapp.Setup(false) 146 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 147 148 addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) 149 150 // try unpause with not available councilor 151 msgServer := keeper.NewMsgServerImpl(app.CustomGovKeeper) 152 _, err := msgServer.CouncilorUnpause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorUnpause( 153 addr, 154 )) 155 require.Error(t, err) 156 157 // test active councilor unpause 158 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 159 Address: addr, 160 Status: types.CouncilorActive, 161 Rank: 0, 162 AbstentionCounter: 0, 163 }) 164 _, err = msgServer.CouncilorUnpause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorUnpause( 165 addr, 166 )) 167 require.Error(t, err) 168 169 // test paused councilor pause 170 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 171 Address: addr, 172 Status: types.CouncilorPaused, 173 Rank: 0, 174 AbstentionCounter: 0, 175 }) 176 _, err = msgServer.CouncilorUnpause(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorUnpause( 177 addr, 178 )) 179 require.NoError(t, err) 180 181 councilor, found := app.CustomGovKeeper.GetCouncilor(ctx, addr) 182 require.True(t, found) 183 require.Equal(t, councilor.Status, types.CouncilorActive) 184 } 185 186 func TestMsgServerCouncilorActivate(t *testing.T) { 187 app := simapp.Setup(false) 188 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 189 190 addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes()) 191 192 // try activate with not available councilor 193 msgServer := keeper.NewMsgServerImpl(app.CustomGovKeeper) 194 _, err := msgServer.CouncilorActivate(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorActivate( 195 addr, 196 )) 197 require.Error(t, err) 198 199 // test active councilor activate 200 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 201 Address: addr, 202 Status: types.CouncilorActive, 203 Rank: 0, 204 AbstentionCounter: 100, 205 }) 206 _, err = msgServer.CouncilorActivate(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorActivate( 207 addr, 208 )) 209 require.Error(t, err) 210 211 // test inactive councilor activate 212 app.CustomGovKeeper.SaveCouncilor(ctx, types.Councilor{ 213 Address: addr, 214 Status: types.CouncilorInactive, 215 Rank: 0, 216 AbstentionCounter: 100, 217 }) 218 _, err = msgServer.CouncilorActivate(sdk.WrapSDKContext(ctx), types.NewMsgCouncilorActivate( 219 addr, 220 )) 221 require.NoError(t, err) 222 223 councilor, found := app.CustomGovKeeper.GetCouncilor(ctx, addr) 224 require.True(t, found) 225 // check status change 226 require.Equal(t, councilor.Status, types.CouncilorActive) 227 // check absention counter change 228 require.Equal(t, councilor.AbstentionCounter, int64(0)) 229 }