Skip to content

Commit

Permalink
is: Define default limit in tests and call new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Oct 23, 2024
1 parent 1db7f35 commit 31c2dbb
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ For details about compatibility between different releases, see the **Commitment

- Potential leak of end devices of other (owned) applications in the top end devices panel in the application overview of the Console.
- Fix reversed Join Server dev nonce metrics.
- Default page limit on IS List RPCs if not provided in the request.
- Enforce default page limit on IS List RPCs if a value is not provided in the request.

### Security

Expand Down
9 changes: 9 additions & 0 deletions pkg/identityserver/bunstore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestApplicationStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestApplicationStoreCRUD(t)
st.TestApplicationStorePagination(t)
st.TestAPIKeyStorePaginationDefaults(t)
}

func TestClientStore(t *testing.T) {
Expand All @@ -80,6 +81,7 @@ func TestClientStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestClientStoreCRUD(t)
st.TestClientStorePagination(t)
st.TestClientStorePaginationDefaults(t)
}

func TestEndDeviceStore(t *testing.T) {
Expand Down Expand Up @@ -108,6 +110,7 @@ func TestOrganizationStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestOrganizationStoreCRUD(t)
st.TestOrganizationStorePagination(t)
st.TestOrganizationStorePaginationDefaults(t)
}

func TestUserStore(t *testing.T) {
Expand All @@ -116,6 +119,7 @@ func TestUserStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestUserStoreCRUD(t)
st.TestUserStorePagination(t)
st.TestUserSessionStorePaginationDefaults(t)
}

func TestUserSessionStore(t *testing.T) {
Expand All @@ -124,6 +128,7 @@ func TestUserSessionStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestUserSessionStore(t)
st.TestUserSessionStorePagination(t)
st.TestUserSessionStorePaginationDefaults(t)
}

func TestUserBookmarkStore(t *testing.T) {
Expand All @@ -140,6 +145,7 @@ func TestAPIKeyStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestAPIKeyStoreCRUD(t)
st.TestAPIKeyStorePagination(t)
st.TestAPIKeyStorePaginationDefaults(t)
}

func TestMembershipStore(t *testing.T) {
Expand All @@ -148,6 +154,7 @@ func TestMembershipStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestMembershipStoreCRUD(t)
st.TestMembershipStorePagination(t)
st.TestMembershipStorePaginationDefaults(t)
}

func TestEmailValidationStore(t *testing.T) {
Expand All @@ -163,6 +170,7 @@ func TestInvitationStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestInvitationStore(t)
st.TestInvitationStorePagination(t)
st.TestInvitationStorePaginationDefaults(t)
}

func TestLoginTokenStore(t *testing.T) {
Expand All @@ -178,6 +186,7 @@ func TestOAuthStore(t *testing.T) {
st := storetest.New(t, newTestStore)
st.TestOAuthStore(t)
st.TestOAuthStorePagination(t)
st.TestOAuthStorePaginationDefaults(t)
}

func TestEUIStore(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/api_key_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,14 @@ func (st *StoreTest) TestAPIKeyStorePagination(t *T) {

// TestAPIKeyStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestAPIKeyStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

app1 := st.population.NewApplication(nil)

var all []*ttnpb.APIKey
for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
_, key := st.population.NewAPIKey(app1.GetEntityIdentifiers(), ttnpb.Right_RIGHT_APPLICATION_ALL)
key.Name = fmt.Sprintf("Key %d", i)
all = append(all, key)
Expand All @@ -296,10 +300,10 @@ func (st *StoreTest) TestAPIKeyStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)
got, err := s.FindAPIKeys(paginateCtx, app1.GetEntityIdentifiers())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/application_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,13 @@ func (st *StoreTest) TestApplicationStorePagination(t *T) {

// TestApplicationStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestApplicationStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

usr1 := st.population.NewUser()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
st.population.NewApplication(usr1.GetOrganizationOrUserIdentifiers())
}

Expand All @@ -385,10 +389,10 @@ func (st *StoreTest) TestApplicationStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)
got, err := s.FindApplications(paginateCtx, nil, mask)
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/client_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,13 @@ func (st *StoreTest) TestClientStorePagination(t *T) {

// TestClientStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestClientStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

usr1 := st.population.NewUser()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
st.population.NewClient(usr1.GetOrganizationOrUserIdentifiers())
}

Expand All @@ -373,10 +377,10 @@ func (st *StoreTest) TestClientStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)
got, err := s.FindClients(paginateCtx, nil, mask)
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/invitation_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (st *StoreTest) TestInvitationStorePagination(t *T) {

// TestInvitationStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestInvitationStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

a, ctx := test.New(t)
start := time.Now().Truncate(time.Second)

Expand All @@ -267,7 +271,7 @@ func (st *StoreTest) TestInvitationStorePaginationDefaults(t *T) {
}
defer s.Close()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
_, err := s.CreateInvitation(ctx, &ttnpb.Invitation{
Email: fmt.Sprintf("user%[email protected]", i+1),
Token: fmt.Sprintf("TOKEN%d", i+1),
Expand All @@ -282,10 +286,10 @@ func (st *StoreTest) TestInvitationStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(store.WithOrder(ctx, "email"), 0, 1, &total)
paginateCtx := store.WithPagination(store.WithOrder(ctx, "email"), 0, 0, &total)
got, err := s.FindInvitations(paginateCtx)
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
18 changes: 11 additions & 7 deletions pkg/identityserver/storetest/membership_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,23 @@ func (st *StoreTest) TestMembershipStorePagination(t *T) {

// TestMembershipStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestMembershipStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

var apps []*ttnpb.Application
for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
apps = append(apps, st.population.NewApplication(nil))
}

var memberIDs []*ttnpb.OrganizationOrUserIdentifiers
for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
ids := st.population.NewUser().GetOrganizationOrUserIdentifiers()
memberIDs = append(memberIDs, ids)
st.population.NewMembership(ids, apps[0].GetEntityIdentifiers(), ttnpb.Right_RIGHT_APPLICATION_ALL)
}

for i := 1; i < 102; i++ {
for i := 1; i < 10; i++ {
st.population.NewMembership(memberIDs[0], apps[i].GetEntityIdentifiers(), ttnpb.Right_RIGHT_APPLICATION_ALL)
}

Expand All @@ -423,23 +427,23 @@ func (st *StoreTest) TestMembershipStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)

got, err := s.FindMembers(paginateCtx, apps[0].GetEntityIdentifiers())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})

t.Run("FindMemberships_PageLimit", func(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)

got, err := s.FindMemberships(paginateCtx, memberIDs[0], "application", false)
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
18 changes: 11 additions & 7 deletions pkg/identityserver/storetest/oauth_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,16 @@ func (st *StoreTest) TestOAuthStorePagination(t *T) {

// TestOAuthStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestOAuthStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

a, ctx := test.New(t)

usr1 := st.population.NewUser()

var clients []*ttnpb.Client
for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
clients = append(clients, st.population.NewClient(usr1.GetOrganizationOrUserIdentifiers()))
}

Expand All @@ -473,7 +477,7 @@ func (st *StoreTest) TestOAuthStorePaginationDefaults(t *T) {
}
defer s.Close()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
_, err := s.Authorize(ctx, &ttnpb.OAuthClientAuthorization{
UserIds: usr1.GetIds(),
ClientIds: clients[i].GetIds(),
Expand All @@ -485,7 +489,7 @@ func (st *StoreTest) TestOAuthStorePaginationDefaults(t *T) {
time.Sleep(test.Delay)
}

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
_, err := s.CreateAccessToken(ctx, &ttnpb.OAuthAccessToken{
UserIds: usr1.GetIds(),
ClientIds: clients[0].GetIds(),
Expand All @@ -502,23 +506,23 @@ func (st *StoreTest) TestOAuthStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 1, &total)
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 0, &total)

got, err := s.ListAuthorizations(paginateCtx, usr1.GetIds())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})

t.Run("ListAccessTokens_PageLimit", func(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 1, &total)
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 0, &total)

got, err := s.ListAccessTokens(paginateCtx, usr1.GetIds(), clients[0].GetIds())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/organization_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,13 @@ func (st *StoreTest) TestOrganizationStorePagination(t *T) {

// TestOrganizationStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestOrganizationStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

usr1 := st.population.NewUser()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
st.population.NewOrganization(usr1.GetOrganizationOrUserIdentifiers())
}

Expand All @@ -375,11 +379,11 @@ func (st *StoreTest) TestOrganizationStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(ctx, 0, 1, &total)
paginateCtx := store.WithPagination(ctx, 0, 0, &total)

got, err := s.FindOrganizations(paginateCtx, nil, mask)
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
10 changes: 7 additions & 3 deletions pkg/identityserver/storetest/user_session_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func (st *StoreTest) TestUserSessionStorePagination(t *T) {

// TestUserSessionStorePaginationDefaults tests the default pagination values.
func (st *StoreTest) TestUserSessionStorePaginationDefaults(t *T) {
store.SetPaginationDefaults(store.PaginationDefaults{
DefaultLimit: 7,
})

a, ctx := test.New(t)

usr1 := st.population.NewUser()
Expand All @@ -246,7 +250,7 @@ func (st *StoreTest) TestUserSessionStorePaginationDefaults(t *T) {
}
defer s.Close()

for i := 0; i < 102; i++ {
for i := 0; i < 10; i++ {
_, err := s.CreateSession(ctx, &ttnpb.UserSession{
UserIds: usr1.GetIds(),
SessionId: fmt.Sprintf("SESS%d", i+1),
Expand All @@ -262,11 +266,11 @@ func (st *StoreTest) TestUserSessionStorePaginationDefaults(t *T) {
a, ctx := test.New(t)

var total uint64
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 1, &total)
paginateCtx := store.WithPagination(store.WithOrder(ctx, "created_at"), 0, 0, &total)

got, err := s.FindSessions(paginateCtx, usr1.GetIds())
if a.So(err, should.BeNil) && a.So(got, should.NotBeNil) {
a.So(got, should.HaveLength, 100)
a.So(got, should.HaveLength, 7)
}
})
}
Loading

0 comments on commit 31c2dbb

Please sign in to comment.