mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-17 10:55:12 +00:00
chore(test): streamline integration tests for instance management (#12243)
- Removed redundant relational instance handling in integration tests for adding and removing custom domains. - Simplified test cases by consolidating instance creation and context management. - Enhanced clarity and maintainability of test logic by reducing complexity in test structures. # Which Problems Are Solved `backed/v3` tests are flaky, but the code is not productive. Development is paused for the moment. Disabling flaky tests helps us to maintain the product easier. # How the Problems Are Solved - Removed redundant relational instance handling in integration tests for adding and removing custom domains. - Simplified test cases by consolidating instance creation and context management. - Enhanced clarity and maintainability of test logic by reducing complexity in test structures.newly introduced terms). # Additional Context - related to https://github.com/zitadel/zitadel/pull/12241 --------- Co-authored-by: Livio Spring <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This commit is contained in:
co-authored by
Livio Spring
Copilot Autofix powered by AI
parent
7019a03ec4
commit
f85a2373c5
@@ -15,8 +15,6 @@ import (
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/integration"
|
||||
featurepb "github.com/zitadel/zitadel/pkg/grpc/feature/v2"
|
||||
instancepb "github.com/zitadel/zitadel/pkg/grpc/instance/v2"
|
||||
objpb "github.com/zitadel/zitadel/pkg/grpc/object"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/session/v2"
|
||||
@@ -250,8 +248,7 @@ type sessionAttr struct {
|
||||
Details *object.Details
|
||||
}
|
||||
|
||||
// listSessionsEnv holds the environment context for ListSessions tests,
|
||||
// allowing the same test cases to run against both the eventstore and relational backends.
|
||||
// listSessionsEnv holds the environment context for ListSessions tests.
|
||||
type listSessionsEnv struct {
|
||||
ownerCtx context.Context // IAMOwner-level context; can list all sessions
|
||||
loginCtx context.Context // login-user context; used for session creation in deps
|
||||
@@ -260,13 +257,9 @@ type listSessionsEnv struct {
|
||||
loginUserID string
|
||||
testUser *user.AddHumanUserResponse
|
||||
newUser func(ctx context.Context) *user.AddHumanUserResponse
|
||||
// isRelational controls behavior that currently differs between the eventstore
|
||||
// and relational backends (e.g. sequence field, permission-check semantics).
|
||||
isRelational bool
|
||||
}
|
||||
|
||||
// runListSessionsTestCases defines and runs the full ListSessions test suite against env.
|
||||
// This is called by both [TestServer_ListSessions] (eventstore) and [TestServer_ListSessions_Relational].
|
||||
func runListSessionsTestCases(t *testing.T, env listSessionsEnv) {
|
||||
t.Helper()
|
||||
|
||||
@@ -331,29 +324,17 @@ func runListSessionsTestCases(t *testing.T, env listSessionsEnv) {
|
||||
},
|
||||
},
|
||||
{
|
||||
// In the eventstore path, the session exists (TotalResult=1) but the permission
|
||||
// callback hides its content (Sessions=[]). In the relational path the permission
|
||||
// check is not yet implemented (returns SQL true), so the session is fully visible.
|
||||
// TODO(IAM-Marco): align expectations once relational permission checks are implemented.
|
||||
name: "list sessions, no permission",
|
||||
ctx: env.noPermCtx,
|
||||
req: &session.ListSessionsRequest{Queries: []*session.SearchQuery{}},
|
||||
dep: func(t *testing.T, req *session.ListSessionsRequest) []*sessionAttr {
|
||||
info := newSess(t, env.loginCtx, "", "", nil, nil)
|
||||
req.Queries = append(req.Queries, &session.SearchQuery{Query: &session.SearchQuery_IdsQuery{IdsQuery: &session.IDsQuery{Ids: []string{info.ID}}}})
|
||||
if env.isRelational {
|
||||
return []*sessionAttr{info}
|
||||
}
|
||||
return []*sessionAttr{}
|
||||
},
|
||||
want: &session.ListSessionsResponse{
|
||||
Details: &object.ListDetails{TotalResult: 1, Timestamp: timestamppb.Now()},
|
||||
Sessions: func() []*session.Session {
|
||||
if env.isRelational {
|
||||
return []*session.Session{{}}
|
||||
}
|
||||
return []*session.Session{}
|
||||
}(),
|
||||
Details: &object.ListDetails{TotalResult: 1, Timestamp: timestamppb.Now()},
|
||||
Sessions: []*session.Session{},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -670,11 +651,7 @@ func runListSessionsTestCases(t *testing.T, env listSessionsEnv) {
|
||||
|
||||
for i := range infos {
|
||||
tt.want.Sessions[i].Id = infos[i].ID
|
||||
if env.isRelational {
|
||||
tt.want.Sessions[i].Sequence = 0
|
||||
} else {
|
||||
tt.want.Sessions[i].Sequence = infos[i].Details.GetSequence()
|
||||
}
|
||||
tt.want.Sessions[i].Sequence = infos[i].Details.GetSequence()
|
||||
tt.want.Sessions[i].CreationDate = infos[i].Details.GetChangeDate()
|
||||
tt.want.Sessions[i].ChangeDate = infos[i].Details.GetChangeDate()
|
||||
|
||||
@@ -765,37 +742,3 @@ func TestServer_ListSessions_with_expiration_date_filter(t *testing.T) {
|
||||
assert.Equal(t, session2.SessionId, listSessionsResponse2.Sessions[0].Id)
|
||||
}
|
||||
|
||||
func TestServer_ListSessions_Relational(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
sysAuthZ := integration.WithSystemAuthorization(ctx)
|
||||
|
||||
inst := integration.NewInstance(sysAuthZ)
|
||||
integration.EnsureInstanceFeature(t, sysAuthZ, inst,
|
||||
&featurepb.SetInstanceFeaturesRequest{EnableRelationalTables: gu.Ptr(true)},
|
||||
func(tCollect *assert.CollectT, got *featurepb.GetInstanceFeaturesResponse) {
|
||||
assert.True(tCollect, got.GetEnableRelationalTables().GetEnabled())
|
||||
},
|
||||
)
|
||||
t.Cleanup(func() {
|
||||
inst.Client.InstanceV2.DeleteInstance(sysAuthZ, &instancepb.DeleteInstanceRequest{InstanceId: inst.ID()})
|
||||
})
|
||||
|
||||
instOwnerCtx := inst.WithAuthorizationToken(context.Background(), integration.UserTypeIAMOwner)
|
||||
loginCtx := inst.WithAuthorizationToken(context.Background(), integration.UserTypeLogin)
|
||||
noPermCtx := inst.WithAuthorizationToken(context.Background(), integration.UserTypeNoPermission)
|
||||
|
||||
runListSessionsTestCases(t, listSessionsEnv{
|
||||
ownerCtx: instOwnerCtx,
|
||||
loginCtx: loginCtx,
|
||||
noPermCtx: noPermCtx,
|
||||
client: inst.Client.SessionV2,
|
||||
loginUserID: inst.Users.Get(integration.UserTypeLogin).ID,
|
||||
testUser: inst.CreateHumanUser(instOwnerCtx),
|
||||
newUser: func(ctx context.Context) *user.AddHumanUserResponse { return inst.CreateHumanUser(ctx) },
|
||||
isRelational: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ import (
|
||||
"google.golang.org/protobuf/types/known/durationpb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||
"github.com/zitadel/zitadel/internal/integration"
|
||||
"github.com/zitadel/zitadel/internal/integration/sink"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/feature/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/instance/v2"
|
||||
mgmt "github.com/zitadel/zitadel/pkg/grpc/management"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/session/v2"
|
||||
@@ -901,120 +901,106 @@ func TestServer_SetSession_expired(t *testing.T) {
|
||||
func TestServer_DeleteSession_token(t *testing.T) {
|
||||
sysAuthZ := integration.WithSystemAuthorization(CTX)
|
||||
|
||||
relTableState := integration.RelationalTablesEnableMatrix(t, IAMOwnerCTX, sysAuthZ)
|
||||
for _, stateCase := range relTableState {
|
||||
t.Run(stateCase.Name, func(t *testing.T) {
|
||||
loginCTX := stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
createResp, err := stateCase.Inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{})
|
||||
require.NoError(t, err)
|
||||
inst := integration.NewInstance(sysAuthZ)
|
||||
instOwner := inst.WithAuthorizationToken(IAMOwnerCTX, integration.UserTypeIAMOwner)
|
||||
t.Cleanup(func() {
|
||||
_, err := inst.Client.InstanceV2.DeleteInstance(integration.WithSystemAuthorization(context.Background()), &instance.DeleteInstanceRequest{InstanceId: inst.ID()})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
//TODO: remove after create session also uses the relational tables directly
|
||||
if stateCase.Enabled {
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
loginCTX := inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
createResp, err := inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(stateCase.InstOwner, &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr("invalid"),
|
||||
})
|
||||
require.Error(t, err)
|
||||
if stateCase.Enabled {
|
||||
integration.RequireStatusError(t, err, domain.ErrSessionTokenInvalid(nil))
|
||||
}
|
||||
_, err = inst.Client.SessionV2.DeleteSession(instOwner, &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr("invalid"),
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(stateCase.InstOwner, &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr(createResp.GetSessionToken()),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
_, err = inst.Client.SessionV2.DeleteSession(instOwner, &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr(createResp.GetSessionToken()),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestServer_DeleteSession_own_session(t *testing.T) {
|
||||
sysAuthZ := integration.WithSystemAuthorization(CTX)
|
||||
|
||||
relTableState := integration.RelationalTablesEnableMatrix(t, IAMOwnerCTX, sysAuthZ)
|
||||
for _, stateCase := range relTableState {
|
||||
t.Run(stateCase.Name, func(t *testing.T) {
|
||||
loginCTX := stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
// create two users for the test and a session each to get tokens for authorization
|
||||
user1 := stateCase.Inst.CreateUserTypeHuman(stateCase.InstOwner, integration.Email())
|
||||
stateCase.Inst.SetUserPassword(stateCase.InstOwner, user1.GetId(), integration.UserPassword, false)
|
||||
_, token1, _, _ := stateCase.Inst.CreatePasswordSession(t, loginCTX, user1.GetId(), integration.UserPassword)
|
||||
inst := integration.NewInstance(sysAuthZ)
|
||||
instOwner := inst.WithAuthorizationToken(IAMOwnerCTX, integration.UserTypeIAMOwner)
|
||||
t.Cleanup(func() {
|
||||
_, err := inst.Client.InstanceV2.DeleteInstance(integration.WithSystemAuthorization(context.Background()), &instance.DeleteInstanceRequest{InstanceId: inst.ID()})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
user2 := stateCase.Inst.CreateUserTypeHuman(stateCase.InstOwner, integration.Email())
|
||||
stateCase.Inst.SetUserPassword(stateCase.InstOwner, user2.GetId(), integration.UserPassword, false)
|
||||
_, token2, _, _ := stateCase.Inst.CreatePasswordSession(t, loginCTX, user2.GetId(), integration.UserPassword)
|
||||
loginCTX := inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
// create two users for the test and a session each to get tokens for authorization
|
||||
user1 := inst.CreateUserTypeHuman(instOwner, integration.Email())
|
||||
inst.SetUserPassword(instOwner, user1.GetId(), integration.UserPassword, false)
|
||||
_, token1, _, _ := inst.CreatePasswordSession(t, loginCTX, user1.GetId(), integration.UserPassword)
|
||||
|
||||
// create a new session for the first user
|
||||
createResp, err := stateCase.Inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Checks: &session.Checks{
|
||||
User: &session.CheckUser{
|
||||
Search: &session.CheckUser_UserId{
|
||||
UserId: user1.GetId(),
|
||||
},
|
||||
},
|
||||
user2 := inst.CreateUserTypeHuman(instOwner, integration.Email())
|
||||
inst.SetUserPassword(instOwner, user2.GetId(), integration.UserPassword, false)
|
||||
_, token2, _, _ := inst.CreatePasswordSession(t, loginCTX, user2.GetId(), integration.UserPassword)
|
||||
|
||||
// create a new session for the first user
|
||||
createResp, err := inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Checks: &session.Checks{
|
||||
User: &session.CheckUser{
|
||||
Search: &session.CheckUser_UserId{
|
||||
UserId: user1.GetId(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
//TODO: remove after create session also uses the relational tables directly
|
||||
if stateCase.Enabled {
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
// delete the new (user1) session must not be possible with user (has no permission)
|
||||
_, err = inst.Client.SessionV2.DeleteSession(integration.WithAuthorizationToken(t.Context(), token2), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
// delete the new (user1) session must not be possible with user (has no permission)
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(integration.WithAuthorizationToken(t.Context(), token2), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
})
|
||||
require.Error(t, err)
|
||||
if stateCase.Enabled {
|
||||
integration.RequireStatusError(t, err, domain.ErrAuthMissingPermission(nil, "", domain.SessionDeletePermission))
|
||||
}
|
||||
|
||||
// delete the new (user1) session by themselves
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(integration.WithAuthorizationToken(t.Context(), token1), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
// delete the new (user1) session by themselves
|
||||
_, err = inst.Client.SessionV2.DeleteSession(integration.WithAuthorizationToken(t.Context(), token1), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestServer_DeleteSession_with_permission(t *testing.T) {
|
||||
sysAuthZ := integration.WithSystemAuthorization(CTX)
|
||||
|
||||
relTableState := integration.RelationalTablesEnableMatrix(t, IAMOwnerCTX, sysAuthZ)
|
||||
inst := integration.NewInstance(sysAuthZ)
|
||||
instOwner := inst.WithAuthorizationToken(IAMOwnerCTX, integration.UserTypeIAMOwner)
|
||||
t.Cleanup(func() {
|
||||
_, err := inst.Client.InstanceV2.DeleteInstance(integration.WithSystemAuthorization(context.Background()), &instance.DeleteInstanceRequest{InstanceId: inst.ID()})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
for _, stateCase := range relTableState {
|
||||
t.Run(stateCase.Name, func(t *testing.T) {
|
||||
loginCTX := stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
user1 := stateCase.Inst.CreateUserTypeHuman(stateCase.InstOwner, integration.Email())
|
||||
createResp, err := stateCase.Inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Checks: &session.Checks{
|
||||
User: &session.CheckUser{
|
||||
Search: &session.CheckUser_UserId{
|
||||
UserId: user1.GetId(),
|
||||
},
|
||||
},
|
||||
loginCTX := inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
user1 := inst.CreateUserTypeHuman(instOwner, integration.Email())
|
||||
createResp, err := inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Checks: &session.Checks{
|
||||
User: &session.CheckUser{
|
||||
Search: &session.CheckUser_UserId{
|
||||
UserId: user1.GetId(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
if stateCase.Enabled {
|
||||
t.Skip("TODO: remove after permission checks are implemented in the relational tables")
|
||||
}
|
||||
// delete the new session by ORG_OWNER
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(
|
||||
stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner),
|
||||
&session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
// delete the new session by ORG_OWNER
|
||||
_, err = inst.Client.SessionV2.DeleteSession(
|
||||
inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner),
|
||||
&session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestServer_DeleteSession_expired(t *testing.T) {
|
||||
@@ -1024,32 +1010,31 @@ func TestServer_DeleteSession_expired(t *testing.T) {
|
||||
})
|
||||
sysAuthZ := integration.WithSystemAuthorization(CTX)
|
||||
|
||||
relTableState := integration.RelationalTablesEnableMatrix(t, IAMOwnerCTX, sysAuthZ)
|
||||
inst := integration.NewInstance(sysAuthZ)
|
||||
t.Cleanup(func() {
|
||||
_, err := inst.Client.InstanceV2.DeleteInstance(integration.WithSystemAuthorization(context.Background()), &instance.DeleteInstanceRequest{InstanceId: inst.ID()})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
for _, stateCase := range relTableState {
|
||||
t.Run(stateCase.Name, func(t *testing.T) {
|
||||
loginCTX := stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
createResp, err := stateCase.Inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Lifetime: durationpb.New(5 * time.Second),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
loginCTX := inst.WithAuthorizationToken(t.Context(), integration.UserTypeLogin)
|
||||
createResp, err := inst.Client.SessionV2.CreateSession(loginCTX, &session.CreateSessionRequest{
|
||||
Lifetime: durationpb.New(5 * time.Second),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// wait until the token expires
|
||||
time.Sleep(10 * time.Second)
|
||||
_, err = stateCase.Inst.Client.SessionV2.DeleteSession(stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr(createResp.GetSessionToken()),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
// wait until the token expires
|
||||
time.Sleep(10 * time.Second)
|
||||
_, err = inst.Client.SessionV2.DeleteSession(inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner), &session.DeleteSessionRequest{
|
||||
SessionId: createResp.GetSessionId(),
|
||||
SessionToken: gu.Ptr(createResp.GetSessionToken()),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// get session should return an error
|
||||
sessionResp, err := stateCase.Inst.Client.SessionV2.GetSession(stateCase.Inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner),
|
||||
&session.GetSessionRequest{SessionId: createResp.GetSessionId()})
|
||||
require.Error(t, err)
|
||||
// TODO: implement error check once the query side is relational
|
||||
require.Nil(t, sessionResp)
|
||||
})
|
||||
}
|
||||
// get session should return an error
|
||||
sessionResp, err := inst.Client.SessionV2.GetSession(inst.WithAuthorizationToken(t.Context(), integration.UserTypeOrgOwner),
|
||||
&session.GetSessionRequest{SessionId: createResp.GetSessionId()})
|
||||
require.Error(t, err)
|
||||
require.Nil(t, sessionResp)
|
||||
}
|
||||
|
||||
func Test_ZITADEL_API_missing_authentication(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user