Skip to content

Commit c0abe76

Browse files
committed
Added unit tests and added migration
1 parent 0d299e4 commit c0abe76

7 files changed

+356
-0
lines changed

app/app.go

+7
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,13 @@ func New(
686686
},
687687
)
688688

689+
app.UpgradeKeeper.SetUpgradeHandler(
690+
"v1.4.4",
691+
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
692+
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
693+
},
694+
)
695+
689696
return app
690697
}
691698

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package keeper_test
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/cosmos/cosmos-sdk/types/query"
9+
"github.com/stretchr/testify/require"
10+
keepertest "github.com/zigbee-alliance/distributed-compliance-ledger/testutil/keeper"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/testutil/nullify"
12+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
13+
"google.golang.org/grpc/codes"
14+
"google.golang.org/grpc/status"
15+
)
16+
17+
// Prevent strconv unused error.
18+
var _ = strconv.IntSize
19+
20+
func TestCertificatesQuerySingle(t *testing.T) {
21+
keeper, ctx := keepertest.PkiKeeper(t, nil)
22+
wctx := sdk.WrapSDKContext(ctx)
23+
msgs := createNCertificates(keeper, ctx, 2)
24+
for _, tc := range []struct {
25+
desc string
26+
request *types.QueryGetCertificatesRequest
27+
response *types.QueryGetCertificatesResponse
28+
err error
29+
}{
30+
{
31+
desc: "First",
32+
request: &types.QueryGetCertificatesRequest{
33+
Subject: msgs[0].Subject,
34+
SubjectKeyId: msgs[0].SubjectKeyId,
35+
},
36+
response: &types.QueryGetCertificatesResponse{Certificates: msgs[0]},
37+
},
38+
{
39+
desc: "Second",
40+
request: &types.QueryGetCertificatesRequest{
41+
Subject: msgs[1].Subject,
42+
SubjectKeyId: msgs[1].SubjectKeyId,
43+
},
44+
response: &types.QueryGetCertificatesResponse{Certificates: msgs[1]},
45+
},
46+
{
47+
desc: "KeyNotFound",
48+
request: &types.QueryGetCertificatesRequest{
49+
Subject: strconv.Itoa(100000),
50+
SubjectKeyId: strconv.Itoa(100000),
51+
},
52+
err: status.Error(codes.NotFound, "not found"),
53+
},
54+
{
55+
desc: "InvalidRequest",
56+
err: status.Error(codes.InvalidArgument, "invalid request"),
57+
},
58+
} {
59+
t.Run(tc.desc, func(t *testing.T) {
60+
response, err := keeper.Certificates(wctx, tc.request)
61+
if tc.err != nil {
62+
require.ErrorIs(t, err, tc.err)
63+
} else {
64+
require.NoError(t, err)
65+
require.Equal(t,
66+
nullify.Fill(tc.response),
67+
nullify.Fill(response),
68+
)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestCertificatesQueryPaginated(t *testing.T) {
75+
keeper, ctx := keepertest.PkiKeeper(t, nil)
76+
wctx := sdk.WrapSDKContext(ctx)
77+
msgs := createNCertificates(keeper, ctx, 5)
78+
79+
request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllCertificatesRequest {
80+
return &types.QueryAllCertificatesRequest{
81+
Pagination: &query.PageRequest{
82+
Key: next,
83+
Offset: offset,
84+
Limit: limit,
85+
CountTotal: total,
86+
},
87+
}
88+
}
89+
t.Run("ByOffset", func(t *testing.T) {
90+
step := 2
91+
for i := 0; i < len(msgs); i += step {
92+
resp, err := keeper.CertificatesAll(wctx, request(nil, uint64(i), uint64(step), false))
93+
require.NoError(t, err)
94+
require.LessOrEqual(t, len(resp.Certificates), step)
95+
require.Subset(t,
96+
nullify.Fill(msgs),
97+
nullify.Fill(resp.Certificates),
98+
)
99+
}
100+
})
101+
t.Run("ByKey", func(t *testing.T) {
102+
step := 2
103+
var next []byte
104+
for i := 0; i < len(msgs); i += step {
105+
resp, err := keeper.CertificatesAll(wctx, request(next, 0, uint64(step), false))
106+
require.NoError(t, err)
107+
require.LessOrEqual(t, len(resp.Certificates), step)
108+
require.Subset(t,
109+
nullify.Fill(msgs),
110+
nullify.Fill(resp.Certificates),
111+
)
112+
next = resp.Pagination.NextKey
113+
}
114+
})
115+
t.Run("Total", func(t *testing.T) {
116+
resp, err := keeper.CertificatesAll(wctx, request(nil, 0, 0, true))
117+
require.NoError(t, err)
118+
require.Equal(t, len(msgs), int(resp.Pagination.Total))
119+
require.ElementsMatch(t,
120+
nullify.Fill(msgs),
121+
nullify.Fill(resp.Certificates),
122+
)
123+
})
124+
t.Run("InvalidRequest", func(t *testing.T) {
125+
_, err := keeper.ApprovedCertificatesAll(wctx, nil)
126+
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
127+
})
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package keeper_test
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/stretchr/testify/require"
9+
keepertest "github.com/zigbee-alliance/distributed-compliance-ledger/testutil/keeper"
10+
"github.com/zigbee-alliance/distributed-compliance-ledger/testutil/nullify"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
12+
"google.golang.org/grpc/codes"
13+
"google.golang.org/grpc/status"
14+
)
15+
16+
// Prevent strconv unused error.
17+
var _ = strconv.IntSize
18+
19+
func TestNocCertificatesBySubjectQuerySingle(t *testing.T) {
20+
keeper, ctx := keepertest.PkiKeeper(t, nil)
21+
wctx := sdk.WrapSDKContext(ctx)
22+
msgs := createNNocCertificatesBySubject(keeper, ctx, 2)
23+
for _, tc := range []struct {
24+
desc string
25+
request *types.QueryGetNocCertificatesBySubjectRequest
26+
response *types.QueryGetNocCertificatesBySubjectResponse
27+
err error
28+
}{
29+
{
30+
desc: "First",
31+
request: &types.QueryGetNocCertificatesBySubjectRequest{
32+
Subject: msgs[0].Subject,
33+
},
34+
response: &types.QueryGetNocCertificatesBySubjectResponse{NocCertificatesBySubject: msgs[0]},
35+
},
36+
{
37+
desc: "Second",
38+
request: &types.QueryGetNocCertificatesBySubjectRequest{
39+
Subject: msgs[1].Subject,
40+
},
41+
response: &types.QueryGetNocCertificatesBySubjectResponse{NocCertificatesBySubject: msgs[1]},
42+
},
43+
{
44+
desc: "KeyNotFound",
45+
request: &types.QueryGetNocCertificatesBySubjectRequest{
46+
Subject: strconv.Itoa(100000),
47+
},
48+
err: status.Error(codes.NotFound, "not found"),
49+
},
50+
{
51+
desc: "InvalidRequest",
52+
err: status.Error(codes.InvalidArgument, "invalid request"),
53+
},
54+
} {
55+
t.Run(tc.desc, func(t *testing.T) {
56+
response, err := keeper.NocCertificatesBySubject(wctx, tc.request)
57+
if tc.err != nil {
58+
require.ErrorIs(t, err, tc.err)
59+
} else {
60+
require.NoError(t, err)
61+
require.Equal(t,
62+
nullify.Fill(tc.response),
63+
nullify.Fill(response),
64+
)
65+
}
66+
})
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package keeper_test
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/cosmos/cosmos-sdk/types/query"
9+
"github.com/stretchr/testify/require"
10+
keepertest "github.com/zigbee-alliance/distributed-compliance-ledger/testutil/keeper"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/testutil/nullify"
12+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
13+
"google.golang.org/grpc/codes"
14+
"google.golang.org/grpc/status"
15+
)
16+
17+
// Prevent strconv unused error.
18+
var _ = strconv.IntSize
19+
20+
func TestNocCertificatesQuerySingle(t *testing.T) {
21+
keeper, ctx := keepertest.PkiKeeper(t, nil)
22+
wctx := sdk.WrapSDKContext(ctx)
23+
msgs := createNNocCertificates(keeper, ctx, 2)
24+
for _, tc := range []struct {
25+
desc string
26+
request *types.QueryGetNocCertificatesRequest
27+
response *types.QueryGetNocCertificatesResponse
28+
err error
29+
}{
30+
{
31+
desc: "First",
32+
request: &types.QueryGetNocCertificatesRequest{
33+
Subject: msgs[0].Subject,
34+
SubjectKeyId: msgs[0].SubjectKeyId,
35+
},
36+
response: &types.QueryGetNocCertificatesResponse{NocCertificates: msgs[0]},
37+
},
38+
{
39+
desc: "Second",
40+
request: &types.QueryGetNocCertificatesRequest{
41+
Subject: msgs[1].Subject,
42+
SubjectKeyId: msgs[1].SubjectKeyId,
43+
},
44+
response: &types.QueryGetNocCertificatesResponse{NocCertificates: msgs[1]},
45+
},
46+
{
47+
desc: "KeyNotFound",
48+
request: &types.QueryGetNocCertificatesRequest{
49+
Subject: strconv.Itoa(100000),
50+
SubjectKeyId: strconv.Itoa(100000),
51+
},
52+
err: status.Error(codes.NotFound, "not found"),
53+
},
54+
{
55+
desc: "InvalidRequest",
56+
err: status.Error(codes.InvalidArgument, "invalid request"),
57+
},
58+
} {
59+
t.Run(tc.desc, func(t *testing.T) {
60+
response, err := keeper.NocCertificates(wctx, tc.request)
61+
if tc.err != nil {
62+
require.ErrorIs(t, err, tc.err)
63+
} else {
64+
require.NoError(t, err)
65+
require.Equal(t,
66+
nullify.Fill(tc.response),
67+
nullify.Fill(response),
68+
)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestQueryNocCertificatesQueryPaginated(t *testing.T) {
75+
keeper, ctx := keepertest.PkiKeeper(t, nil)
76+
wctx := sdk.WrapSDKContext(ctx)
77+
msgs := createNNocCertificates(keeper, ctx, 5)
78+
79+
request := func(next []byte, offset, limit uint64, total bool, subjectKeyId string) *types.QueryNocCertificatesRequest {
80+
return &types.QueryNocCertificatesRequest{
81+
Pagination: &query.PageRequest{
82+
Key: next,
83+
Offset: offset,
84+
Limit: limit,
85+
CountTotal: total,
86+
},
87+
SubjectKeyId: subjectKeyId,
88+
}
89+
}
90+
t.Run("ByOffset", func(t *testing.T) {
91+
step := 2
92+
for i := 0; i < len(msgs); i += step {
93+
resp, err := keeper.NocCertificatesAll(wctx, request(nil, uint64(i), uint64(step), false, ""))
94+
require.NoError(t, err)
95+
require.LessOrEqual(t, len(resp.NocCertificates), step)
96+
require.Subset(t,
97+
nullify.Fill(msgs),
98+
nullify.Fill(resp.NocCertificates),
99+
)
100+
}
101+
})
102+
t.Run("ByKey", func(t *testing.T) {
103+
step := 2
104+
var next []byte
105+
for i := 0; i < len(msgs); i += step {
106+
resp, err := keeper.NocCertificatesAll(wctx, request(next, 0, uint64(step), false, ""))
107+
require.NoError(t, err)
108+
require.LessOrEqual(t, len(resp.NocCertificates), step)
109+
require.Subset(t,
110+
nullify.Fill(msgs),
111+
nullify.Fill(resp.NocCertificates),
112+
)
113+
next = resp.Pagination.NextKey
114+
}
115+
})
116+
t.Run("Total", func(t *testing.T) {
117+
resp, err := keeper.NocCertificatesAll(wctx, request(nil, 0, 0, true, ""))
118+
require.NoError(t, err)
119+
require.Equal(t, len(msgs), int(resp.Pagination.Total))
120+
require.ElementsMatch(t,
121+
nullify.Fill(msgs),
122+
nullify.Fill(resp.NocCertificates),
123+
)
124+
})
125+
t.Run("By subjectkey-id", func(t *testing.T) {
126+
resp, err := keeper.NocCertificatesAll(wctx, request(nil, 0, 0, true, "0"))
127+
require.NoError(t, err)
128+
require.Equal(t, 1, len(resp.NocCertificates))
129+
require.Equal(t, msgs[0].SubjectKeyId, resp.NocCertificates[0].SubjectKeyId)
130+
})
131+
t.Run("InvalidRequest", func(t *testing.T) {
132+
_, err := keeper.NocCertificatesAll(wctx, nil)
133+
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
134+
})
135+
}

x/pki/keeper/migrations.go

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ func (m Migrator) Migrate3to4(ctx sdk.Context) error {
3636

3737
return nil
3838
}
39+
40+
// Migrate2to4 migrates from version 2 to 4.
41+
func (m Migrator) Migrate2to4(ctx sdk.Context) error {
42+
err := m.Migrate2to3(ctx)
43+
if err != nil {
44+
return err
45+
}
46+
err = m.Migrate3to4(ctx)
47+
if err != nil {
48+
return err
49+
}
50+
return nil
51+
}

x/pki/keeper/noc_certificates_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func createNNocCertificates(keeper *keeper.Keeper, ctx sdk.Context, n int) []typ
2222
items[i].SubjectKeyId = strconv.Itoa(i)
2323

2424
keeper.SetNocCertificates(ctx, items[i])
25+
keeper.SetNocCertificatesBySubjectKeyId(ctx, types.NocCertificatesBySubjectKeyId{
26+
SubjectKeyId: items[i].SubjectKeyId,
27+
Certs: items[i].Certs,
28+
})
2529
}
2630
return items
2731
}

x/pki/module.go

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
122122
m := keeper.NewMigrator(am.keeper)
123123
_ = cfg.RegisterMigration(pkitypes.ModuleName, 1, m.Migrate1to2)
124124
_ = cfg.RegisterMigration(pkitypes.ModuleName, 2, m.Migrate2to3)
125+
_ = cfg.RegisterMigration(pkitypes.ModuleName, 2, m.Migrate2to4)
125126
_ = cfg.RegisterMigration(pkitypes.ModuleName, 3, m.Migrate3to4)
126127
}
127128

0 commit comments

Comments
 (0)