zitadel/internal/query/project_grant_member_test.go
Tim Möhlmann ba9b807854
perf(oidc): optimize the introspection endpoint (#6909)
* get key by id and cache them

* userinfo from events for v2 tokens

* improve keyset caching

* concurrent token and client checks

* client and project in single query

* logging and otel

* drop owner_removed column on apps and authN tables

* userinfo and project roles in go routines

* get  oidc user info from projections and add actions

* add avatar URL

* some cleanup

* pull oidc work branch

* remove storage from server

* add config flag for experimental introspection

* legacy introspection flag

* drop owner_removed column on user projections

* drop owner_removed column on useer_metadata

* query userinfo unit test

* query introspection client test

* add user_grants to the userinfo query

* handle PAT scopes

* bring triggers back

* test instance keys query

* add userinfo unit tests

* unit test keys

* go mod tidy

* solve some bugs

* fix missing preferred login name

* do not run triggers in go routines, they seem to deadlock

* initialize the trigger handlers late with a sync.OnceValue

* Revert "do not run triggers in go routines, they seem to deadlock"

This reverts commit 2a03da2127.

* add missing translations

* chore: update go version for linting

* pin oidc version

* parse a global time location for query test

* fix linter complains

* upgrade go lint

* fix more linting issues

---------

Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2023-11-21 13:11:38 +01:00

299 lines
7.7 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/domain"
)
var (
projectGrantMembersQuery = regexp.QuoteMeta("SELECT" +
" members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.user_id" +
", members.roles" +
", projections.login_names3.login_name" +
", projections.users9_humans.email" +
", projections.users9_humans.first_name" +
", projections.users9_humans.last_name" +
", projections.users9_humans.display_name" +
", projections.users9_machines.name" +
", projections.users9_humans.avatar_key" +
", projections.users9.type" +
", COUNT(*) OVER () " +
"FROM projections.project_grant_members4 AS members " +
"LEFT JOIN projections.users9_humans " +
"ON members.user_id = projections.users9_humans.user_id " +
"AND members.instance_id = projections.users9_humans.instance_id " +
"LEFT JOIN projections.users9_machines " +
"ON members.user_id = projections.users9_machines.user_id " +
"AND members.instance_id = projections.users9_machines.instance_id " +
"LEFT JOIN projections.users9 " +
"ON members.user_id = projections.users9.id " +
"AND members.instance_id = projections.users9.instance_id " +
"LEFT JOIN projections.login_names3 " +
"ON members.user_id = projections.login_names3.user_id " +
"AND members.instance_id = projections.login_names3.instance_id " +
"LEFT JOIN projections.project_grants4 " +
"ON members.grant_id = projections.project_grants4.grant_id " +
"AND members.instance_id = projections.project_grants4.instance_id " +
`AS OF SYSTEM TIME '-1 ms' ` +
"WHERE projections.login_names3.is_primary = $1")
projectGrantMembersColumns = []string{
"creation_date",
"change_date",
"sequence",
"resource_owner",
"user_id",
"roles",
"login_name",
"email",
"first_name",
"last_name",
"display_name",
"name",
"avatar_key",
"type",
"count",
}
)
func Test_ProjectGrantMemberPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareProjectGrantMembersQuery no result",
prepare: prepareProjectGrantMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectGrantMembersQuery,
nil,
nil,
),
},
object: &Members{
Members: []*Member{},
},
},
{
name: "prepareProjectGrantMembersQuery human found",
prepare: prepareProjectGrantMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectGrantMembersQuery,
projectGrantMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id",
database.TextArray[string]{"role-1", "role-2"},
"gigi@caos-ag.zitadel.ch",
"gigi@caos.ch",
"first-name",
"last-name",
"display name",
nil,
nil,
domain.UserTypeHuman,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 1,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id",
Roles: database.TextArray[string]{"role-1", "role-2"},
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
Email: "gigi@caos.ch",
FirstName: "first-name",
LastName: "last-name",
DisplayName: "display name",
AvatarURL: "",
UserType: domain.UserTypeHuman,
},
},
},
},
{
name: "prepareProjectGrantMembersQuery machine found",
prepare: prepareProjectGrantMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectGrantMembersQuery,
projectGrantMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id",
database.TextArray[string]{"role-1", "role-2"},
"machine@caos-ag.zitadel.ch",
nil,
nil,
nil,
nil,
"machine-name",
nil,
domain.UserTypeMachine,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 1,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id",
Roles: database.TextArray[string]{"role-1", "role-2"},
PreferredLoginName: "machine@caos-ag.zitadel.ch",
Email: "",
FirstName: "",
LastName: "",
DisplayName: "machine-name",
AvatarURL: "",
UserType: domain.UserTypeMachine,
},
},
},
},
{
name: "prepareProjectGrantMembersQuery multiple users",
prepare: prepareProjectGrantMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectGrantMembersQuery,
projectGrantMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id-1",
database.TextArray[string]{"role-1", "role-2"},
"gigi@caos-ag.zitadel.ch",
"gigi@caos.ch",
"first-name",
"last-name",
"display name",
nil,
nil,
domain.UserTypeHuman,
},
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id-2",
database.TextArray[string]{"role-1", "role-2"},
"machine@caos-ag.zitadel.ch",
nil,
nil,
nil,
nil,
"machine-name",
nil,
domain.UserTypeMachine,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 2,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id-1",
Roles: database.TextArray[string]{"role-1", "role-2"},
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
Email: "gigi@caos.ch",
FirstName: "first-name",
LastName: "last-name",
DisplayName: "display name",
AvatarURL: "",
UserType: domain.UserTypeHuman,
},
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id-2",
Roles: database.TextArray[string]{"role-1", "role-2"},
PreferredLoginName: "machine@caos-ag.zitadel.ch",
Email: "",
FirstName: "",
LastName: "",
DisplayName: "machine-name",
AvatarURL: "",
UserType: domain.UserTypeMachine,
},
},
},
},
{
name: "prepareProjectGrantMembersQuery sql err",
prepare: prepareProjectGrantMembersQuery,
want: want{
sqlExpectations: mockQueryErr(
projectGrantMembersQuery,
sql.ErrConnDone,
),
err: func(err error) (error, bool) {
if !errors.Is(err, sql.ErrConnDone) {
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
}
return nil, true
},
},
object: (*ProjectGrantMembership)(nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
})
}
}