Skip to content

Commit bad1681

Browse files
committed
Add Test Cases for VendorInfo
1 parent c2aa603 commit bad1681

File tree

7 files changed

+517
-1
lines changed

7 files changed

+517
-1
lines changed

integration_tests/constants/constants.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ var (
3535

3636
// Model Info.
3737
VID uint16 = 1
38+
VendorName = "Vendor Name"
39+
CompanyLegalName = "Legal Company Name"
40+
CompanyPreferredName = "Company Preferred Name"
41+
VendorLandingPageUrl = "https://www.example.com"
3842
PID uint16 = 22
3943
DeviceTypeID uint16 = 12345
4044
Version = "1.0"

x/model/client/rest/rest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
6464
).Methods("PUT")
6565

6666
r.HandleFunc(
67-
fmt.Sprintf("/%s/versions/{%s}/{%s}", storeName, vid, pid),
67+
fmt.Sprintf("/%s/version/{%s}/{%s}", storeName, vid, pid),
6868
getModelVersionsHandler(cliCtx, storeName),
6969
).Methods("GET")
7070

x/vendorinfo/handler_test.go

+150
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,156 @@
1616
package vendorinfo
1717

1818
import (
19+
"fmt"
20+
"testing"
1921

22+
sdk "github.com/cosmos/cosmos-sdk/types"
23+
"github.com/stretchr/testify/require"
24+
abci "github.com/tendermint/tendermint/abci/types"
25+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
26+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/auth"
27+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/vendorinfo/internal/keeper"
28+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/vendorinfo/internal/types"
2029
)
2130

31+
func TestHandler_AddVendorInfo(t *testing.T) {
32+
setup := Setup()
33+
34+
// add new vendorInfo
35+
vendorInfo := TestMsgAddVendorInfo(setup.Vendor)
36+
result := setup.Handler(setup.Ctx, vendorInfo)
37+
require.Equal(t, sdk.CodeOK, result.Code)
38+
39+
// query vendorInfo
40+
receivedVendorInfo := queryVendorInfo(setup, vendorInfo.VendorId)
41+
42+
// check
43+
require.Equal(t, vendorInfo.VendorId, receivedVendorInfo.VendorId)
44+
require.Equal(t, vendorInfo.VendorName, receivedVendorInfo.VendorName)
45+
require.Equal(t, vendorInfo.CompanyLegalName, receivedVendorInfo.CompanyLegalName)
46+
require.Equal(t, vendorInfo.CompanyPreferredName, receivedVendorInfo.CompanyPreferredName)
47+
require.Equal(t, vendorInfo.VendorLandingPageUrl, receivedVendorInfo.VendorLandingPageUrl)
48+
49+
}
50+
51+
func TestHandler_UpdateVendorInfo(t *testing.T) {
52+
setup := Setup()
53+
54+
// try update vendor for non existant vendor
55+
msgUpdateVendorInfo := TestMsgUpdateVendorInfo(setup.Vendor)
56+
result := setup.Handler(setup.Ctx, msgUpdateVendorInfo)
57+
require.Equal(t, types.CodeVendorDoesNotExist, result.Code)
58+
59+
// add new vendorInfo
60+
msgAddVendorInfo := TestMsgAddVendorInfo(setup.Vendor)
61+
result = setup.Handler(setup.Ctx, msgAddVendorInfo)
62+
require.Equal(t, sdk.CodeOK, result.Code)
63+
64+
// update existing vendorInfo
65+
result = setup.Handler(setup.Ctx, msgUpdateVendorInfo)
66+
require.Equal(t, sdk.CodeOK, result.Code)
67+
68+
// query updated vendorInfo
69+
recievedVendorInfo := queryVendorInfo(setup, msgUpdateVendorInfo.VendorId)
70+
71+
// check
72+
require.Equal(t, msgUpdateVendorInfo.VendorId, recievedVendorInfo.VendorId)
73+
require.Equal(t, msgUpdateVendorInfo.VendorName, recievedVendorInfo.VendorName)
74+
require.Equal(t, msgUpdateVendorInfo.CompanyLegalName, recievedVendorInfo.CompanyLegalName)
75+
require.Equal(t, msgUpdateVendorInfo.CompanyPreferredName, recievedVendorInfo.CompanyPreferredName)
76+
require.Equal(t, msgUpdateVendorInfo.VendorLandingPageUrl, recievedVendorInfo.VendorLandingPageUrl)
77+
}
78+
79+
func TestHandler_OnlyOwnerCanUpdateVendorInfo(t *testing.T) {
80+
setup := Setup()
81+
82+
// add new vendorInfo
83+
msgAddVendorInfo := TestMsgAddVendorInfo(setup.Vendor)
84+
result := setup.Handler(setup.Ctx, msgAddVendorInfo)
85+
require.Equal(t, sdk.CodeOK, result.Code)
86+
87+
for _, role := range []auth.AccountRole{auth.Trustee, auth.TestHouse, auth.Vendor} {
88+
// store account
89+
account := auth.NewAccount(testconstants.Address3, testconstants.PubKey3, auth.AccountRoles{role}, testconstants.VendorId3)
90+
setup.authKeeper.SetAccount(setup.Ctx, account)
91+
92+
// update existing VendorInfo by not owner
93+
msgUpdateVendorInfo := TestMsgUpdateVendorInfo(testconstants.Address3)
94+
result = setup.Handler(setup.Ctx, msgUpdateVendorInfo)
95+
require.Equal(t, sdk.CodeUnauthorized, result.Code)
96+
}
97+
98+
// owner update existing VendorInfo
99+
msgUpdateVendorInfo := TestMsgUpdateVendorInfo(setup.Vendor)
100+
result = setup.Handler(setup.Ctx, msgUpdateVendorInfo)
101+
require.Equal(t, sdk.CodeOK, result.Code)
102+
}
103+
104+
func TestHandler_AddVendorInfoWithEmptyOptionalFields(t *testing.T) {
105+
setup := Setup()
106+
107+
// add new vendorInfo
108+
vendorInfo := TestMsgAddVendorInfo(setup.Vendor)
109+
vendorInfo.CompanyLegalName = ""
110+
111+
result := setup.Handler(setup.Ctx, vendorInfo)
112+
require.Equal(t, sdk.CodeOK, result.Code)
113+
114+
// query vendorInfo
115+
receivedVendorInfo := queryVendorInfo(setup, testconstants.VID)
116+
117+
// check
118+
require.Equal(t, receivedVendorInfo.CompanyLegalName, "")
119+
120+
}
121+
122+
func TestHandler_AddVendorByNonVendor(t *testing.T) {
123+
setup := Setup()
124+
125+
for _, role := range []auth.AccountRole{auth.Trustee, auth.TestHouse} {
126+
// store account
127+
account := auth.NewAccount(testconstants.Address3, testconstants.PubKey3, auth.AccountRoles{role}, testconstants.VendorId3)
128+
setup.authKeeper.SetAccount(setup.Ctx, account)
129+
130+
// add new VendorInfo
131+
VendorInfo := TestMsgAddVendorInfo(testconstants.Address3)
132+
result := setup.Handler(setup.Ctx, VendorInfo)
133+
require.Equal(t, sdk.CodeUnauthorized, result.Code)
134+
}
135+
}
136+
137+
func TestHandler_PartiallyUpdateVendor(t *testing.T) {
138+
setup := Setup()
139+
140+
// add new vendorInfo
141+
msgAddVendorInfo := TestMsgAddVendorInfo(setup.Vendor)
142+
result := setup.Handler(setup.Ctx, msgAddVendorInfo)
143+
144+
// owner update Description of existing vendorInfo
145+
msgUpdateVendorInfo := TestMsgUpdateVendorInfo(setup.Vendor)
146+
147+
msgUpdateVendorInfo.CompanyPreferredName = "New Preferred Name"
148+
msgUpdateVendorInfo.VendorLandingPageUrl = "https://new.example.com"
149+
result = setup.Handler(setup.Ctx, msgUpdateVendorInfo)
150+
require.Equal(t, sdk.CodeOK, result.Code)
151+
152+
// query VendorInfo
153+
receivedVendorInfo := queryVendorInfo(setup, msgUpdateVendorInfo.VendorId)
154+
155+
// check
156+
require.Equal(t, receivedVendorInfo.CompanyPreferredName, msgUpdateVendorInfo.CompanyPreferredName)
157+
require.Equal(t, receivedVendorInfo.VendorLandingPageUrl, msgUpdateVendorInfo.VendorLandingPageUrl)
158+
}
159+
160+
func queryVendorInfo(setup TestSetup, vid uint16) types.VendorInfo {
161+
result, _ := setup.Querier(
162+
setup.Ctx,
163+
[]string{keeper.QueryVendor, fmt.Sprintf("%v", vid)},
164+
abci.RequestQuery{},
165+
)
166+
167+
var receivedVendorInfo types.VendorInfo
168+
_ = setup.Cdc.UnmarshalJSON(result, &receivedVendorInfo)
169+
170+
return receivedVendorInfo
171+
}

x/vendorinfo/handler_test_setup.go

+109
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,112 @@
1313
// limitations under the License.
1414

1515
package vendorinfo
16+
17+
import (
18+
"github.com/cosmos/cosmos-sdk/codec"
19+
"github.com/cosmos/cosmos-sdk/store"
20+
sdk "github.com/cosmos/cosmos-sdk/types"
21+
"github.com/tendermint/go-amino"
22+
abci "github.com/tendermint/tendermint/abci/types"
23+
"github.com/tendermint/tendermint/libs/log"
24+
dbm "github.com/tendermint/tm-db"
25+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
26+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/auth"
27+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/vendorinfo/internal/types"
28+
)
29+
30+
type TestSetup struct {
31+
Cdc *amino.Codec
32+
Ctx sdk.Context
33+
VendorInfoKeeper Keeper
34+
authKeeper auth.Keeper
35+
Handler sdk.Handler
36+
Querier sdk.Querier
37+
Vendor sdk.AccAddress
38+
VendorId uint16
39+
}
40+
41+
func Setup() TestSetup {
42+
// Init Codec
43+
cdc := codec.New()
44+
sdk.RegisterCodec(cdc)
45+
codec.RegisterCrypto(cdc)
46+
47+
// Init KVSore
48+
db := dbm.NewMemDB()
49+
50+
dbStore := store.NewCommitMultiStore(db)
51+
52+
vendorInfoKey := sdk.NewKVStoreKey(StoreKey)
53+
dbStore.MountStoreWithDB(vendorInfoKey, sdk.StoreTypeIAVL, nil)
54+
55+
authKey := sdk.NewKVStoreKey(auth.StoreKey)
56+
dbStore.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, nil)
57+
58+
_ = dbStore.LoadLatestVersion()
59+
60+
// Init Keepers
61+
VendorInfoKeeper := NewKeeper(vendorInfoKey, cdc)
62+
authKeeper := auth.NewKeeper(authKey, cdc)
63+
64+
// Create context
65+
ctx := sdk.NewContext(dbStore, abci.Header{ChainID: testconstants.ChainID}, false, log.NewNopLogger())
66+
67+
// Create Handler and Querier
68+
querier := NewQuerier(VendorInfoKeeper)
69+
handler := NewHandler(VendorInfoKeeper, authKeeper)
70+
71+
account := auth.NewAccount(testconstants.Address1, testconstants.PubKey1, auth.AccountRoles{auth.Vendor}, testconstants.VendorId1)
72+
account.AccountNumber = authKeeper.GetNextAccountNumber(ctx)
73+
authKeeper.SetAccount(ctx, account)
74+
75+
setup := TestSetup{
76+
Cdc: cdc,
77+
Ctx: ctx,
78+
VendorInfoKeeper: VendorInfoKeeper,
79+
authKeeper: authKeeper,
80+
Handler: handler,
81+
Querier: querier,
82+
Vendor: account.Address,
83+
}
84+
85+
return setup
86+
}
87+
88+
func getTestVendor() types.VendorInfo {
89+
vendorInfo := types.VendorInfo{
90+
VendorId: testconstants.VendorId1,
91+
VendorName: testconstants.VendorName,
92+
CompanyLegalName: testconstants.CompanyLegalName,
93+
CompanyPreferredName: testconstants.CompanyPreferredName,
94+
VendorLandingPageUrl: testconstants.VendorLandingPageUrl,
95+
}
96+
97+
return vendorInfo
98+
}
99+
100+
func getTestVendorForUpdate() types.VendorInfo {
101+
vendorInfo := types.VendorInfo{
102+
VendorId: testconstants.VendorId1,
103+
VendorName: testconstants.VendorName + "-updated",
104+
CompanyLegalName: testconstants.CompanyLegalName + "-updated",
105+
CompanyPreferredName: testconstants.CompanyPreferredName + "-updated",
106+
VendorLandingPageUrl: testconstants.VendorLandingPageUrl + "-updated",
107+
}
108+
109+
return vendorInfo
110+
}
111+
112+
func TestMsgAddVendorInfo(signer sdk.AccAddress) MsgAddVendorInfo {
113+
return MsgAddVendorInfo{
114+
VendorInfo: getTestVendor(),
115+
Signer: signer,
116+
}
117+
}
118+
119+
func TestMsgUpdateVendorInfo(signer sdk.AccAddress) MsgUpdateVendorInfo {
120+
return MsgUpdateVendorInfo{
121+
VendorInfo: getTestVendorForUpdate(),
122+
Signer: signer,
123+
}
124+
}

x/vendorinfo/internal/keeper/keeper_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,76 @@
1414

1515
//nolint:testpackage
1616
package keeper
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/require"
22+
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
23+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/vendorinfo/internal/types"
24+
)
25+
26+
func TestKeeper_VendorInfoGetSet(t *testing.T) {
27+
setup := Setup()
28+
29+
// check if vendor is present
30+
require.False(t, setup.VendorInfoKeeper.IsVendorInfoPresent(setup.Ctx, testconstants.VID))
31+
32+
// no vendor info before its created
33+
require.Panics(t, func() {
34+
setup.VendorInfoKeeper.GetVendorInfo(setup.Ctx, testconstants.VID)
35+
})
36+
37+
// create vendor info
38+
setup.VendorInfoKeeper.SetVendorInfo(setup.Ctx, DefaultVendorInfo())
39+
40+
// check if vendor info is present
41+
require.True(t, setup.VendorInfoKeeper.IsVendorInfoPresent(setup.Ctx, testconstants.VID))
42+
43+
// get vendorInfo info
44+
vendorInfo := setup.VendorInfoKeeper.GetVendorInfo(setup.Ctx, testconstants.VID)
45+
require.NotNil(t, vendorInfo)
46+
require.Equal(t, testconstants.VID, vendorInfo.VendorId)
47+
require.Equal(t, testconstants.VendorName, vendorInfo.VendorName)
48+
require.Equal(t, testconstants.CompanyLegalName, vendorInfo.CompanyLegalName)
49+
require.Equal(t, testconstants.CompanyPreferredName, vendorInfo.CompanyPreferredName)
50+
require.Equal(t, testconstants.VendorLandingPageUrl, vendorInfo.VendorLandingPageUrl)
51+
52+
// Update the vendorInfo record with new values
53+
vendorInfo.VendorName = testconstants.VendorName + "updated"
54+
vendorInfo.CompanyLegalName = testconstants.CompanyLegalName + "updated"
55+
vendorInfo.CompanyPreferredName = testconstants.CompanyPreferredName + "updated"
56+
vendorInfo.VendorLandingPageUrl = testconstants.VendorLandingPageUrl + "updated"
57+
setup.VendorInfoKeeper.SetVendorInfo(setup.Ctx, vendorInfo)
58+
59+
updateVendorInfo := setup.VendorInfoKeeper.GetVendorInfo(setup.Ctx, testconstants.VID)
60+
require.Equal(t, testconstants.VID, updateVendorInfo.VendorId)
61+
require.Equal(t, testconstants.VendorName+"updated", updateVendorInfo.VendorName)
62+
require.Equal(t, testconstants.CompanyLegalName+"updated", updateVendorInfo.CompanyLegalName)
63+
require.Equal(t, testconstants.CompanyPreferredName+"updated", updateVendorInfo.CompanyPreferredName)
64+
require.Equal(t, testconstants.VendorLandingPageUrl+"updated", updateVendorInfo.VendorLandingPageUrl)
65+
66+
}
67+
68+
func TestKeeper_VendorInfoIterator(t *testing.T) {
69+
setup := Setup()
70+
71+
count := 10
72+
73+
// add 10 models infos with same VID and check associated products
74+
PopulateStoreWithVendorInfo(setup, count)
75+
76+
// get total count
77+
totalVendorInfos := setup.VendorInfoKeeper.CountTotalVendorInfos(setup.Ctx)
78+
require.Equal(t, count, totalVendorInfos)
79+
80+
// get iterator
81+
var expectedRecords []types.VendorInfo
82+
83+
setup.VendorInfoKeeper.IterateVendorInfos(setup.Ctx, func(model types.VendorInfo) (stop bool) {
84+
expectedRecords = append(expectedRecords, model)
85+
86+
return false
87+
})
88+
require.Equal(t, count, len(expectedRecords))
89+
}

0 commit comments

Comments
 (0)