2021-12-16 13:25:38 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2021-12-16 13:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery = regexp.QuoteMeta("SELECT" +
|
2021-12-16 13:25:38 +00:00
|
|
|
" members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
|
|
|
", members.user_id" +
|
|
|
|
", members.roles" +
|
2022-03-23 08:02:39 +00:00
|
|
|
", projections.login_names.login_name" +
|
2022-11-07 09:33:22 +00:00
|
|
|
", projections.users5_humans.email" +
|
|
|
|
", projections.users5_humans.first_name" +
|
|
|
|
", projections.users5_humans.last_name" +
|
|
|
|
", projections.users5_humans.display_name" +
|
|
|
|
", projections.users5_machines.name" +
|
|
|
|
", projections.users5_humans.avatar_key" +
|
2021-12-16 13:25:38 +00:00
|
|
|
", COUNT(*) OVER () " +
|
2022-08-31 07:52:43 +00:00
|
|
|
"FROM projections.instance_members2 AS members " +
|
2022-11-07 09:33:22 +00:00
|
|
|
"LEFT JOIN projections.users5_humans " +
|
|
|
|
"ON members.user_id = projections.users5_humans.user_id AND members.instance_id = projections.users5_humans.instance_id " +
|
|
|
|
"LEFT JOIN projections.users5_machines " +
|
|
|
|
"ON members.user_id = projections.users5_machines.user_id AND members.instance_id = projections.users5_machines.instance_id " +
|
2022-03-23 08:02:39 +00:00
|
|
|
"LEFT JOIN projections.login_names " +
|
2022-10-27 06:08:36 +00:00
|
|
|
"ON members.user_id = projections.login_names.user_id AND members.instance_id = projections.login_names.instance_id " +
|
2022-03-23 08:02:39 +00:00
|
|
|
"WHERE projections.login_names.is_primary = $1")
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersColumns = []string{
|
2021-12-16 13:25:38 +00:00
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
"resource_owner",
|
|
|
|
"user_id",
|
|
|
|
"roles",
|
|
|
|
"login_name",
|
|
|
|
"email",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"display_name",
|
|
|
|
"name",
|
2022-01-14 09:45:50 +00:00
|
|
|
"avatar_key",
|
2021-12-16 13:25:38 +00:00
|
|
|
"count",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_IAMMemberPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "prepareInstanceMembersQuery no result",
|
|
|
|
prepare: prepareInstanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
Members: []*Member{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "prepareInstanceMembersQuery human found",
|
|
|
|
prepare: prepareInstanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery,
|
|
|
|
instanceMembersColumns,
|
2021-12-16 13:25:38 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"gigi@caos-ag.zitadel.ch",
|
|
|
|
"gigi@caos.ch",
|
|
|
|
"first-name",
|
|
|
|
"last-name",
|
|
|
|
"display name",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
|
|
|
|
Email: "gigi@caos.ch",
|
|
|
|
FirstName: "first-name",
|
|
|
|
LastName: "last-name",
|
|
|
|
DisplayName: "display name",
|
|
|
|
AvatarURL: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "prepareInstanceMembersQuery machine found",
|
|
|
|
prepare: prepareInstanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery,
|
|
|
|
instanceMembersColumns,
|
2021-12-16 13:25:38 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"machine@caos-ag.zitadel.ch",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"machine-name",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "machine@caos-ag.zitadel.ch",
|
|
|
|
Email: "",
|
|
|
|
FirstName: "",
|
|
|
|
LastName: "",
|
|
|
|
DisplayName: "machine-name",
|
|
|
|
AvatarURL: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareIAMMembersQuery multiple users",
|
2022-03-24 16:21:34 +00:00
|
|
|
prepare: prepareInstanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery,
|
|
|
|
instanceMembersColumns,
|
2021-12-16 13:25:38 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id-1",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"gigi@caos-ag.zitadel.ch",
|
|
|
|
"gigi@caos.ch",
|
|
|
|
"first-name",
|
|
|
|
"last-name",
|
|
|
|
"display name",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id-2",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"machine@caos-ag.zitadel.ch",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"machine-name",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id-1",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
|
|
|
|
Email: "gigi@caos.ch",
|
|
|
|
FirstName: "first-name",
|
|
|
|
LastName: "last-name",
|
|
|
|
DisplayName: "display name",
|
|
|
|
AvatarURL: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id-2",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "machine@caos-ag.zitadel.ch",
|
|
|
|
Email: "",
|
|
|
|
FirstName: "",
|
|
|
|
LastName: "",
|
|
|
|
DisplayName: "machine-name",
|
|
|
|
AvatarURL: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareIAMMembersQuery sql err",
|
2022-03-24 16:21:34 +00:00
|
|
|
prepare: prepareInstanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceMembersQuery,
|
2021-12-16 13:25:38 +00:00
|
|
|
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: 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|