2023-07-10 13:27:00 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2023-11-21 12:11:38 +00:00
|
|
|
"database/sql"
|
2023-07-10 13:27:00 +00:00
|
|
|
"testing"
|
2023-11-21 12:11:38 +00:00
|
|
|
"time"
|
2023-07-10 13:27:00 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-21 12:11:38 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository/mock"
|
|
|
|
action_repo "github.com/zitadel/zitadel/internal/repository/action"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/authrequest"
|
2023-12-20 12:21:08 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
2023-11-21 12:11:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/feature"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpintent"
|
|
|
|
iam_repo "github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
key_repo "github.com/zitadel/zitadel/internal/repository/keypair"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/limits"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/oidcsession"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
proj_repo "github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
quota_repo "github.com/zitadel/zitadel/internal/repository/quota"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/session"
|
|
|
|
usr_repo "github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/usergrant"
|
2023-07-10 13:27:00 +00:00
|
|
|
)
|
|
|
|
|
2023-11-21 12:11:38 +00:00
|
|
|
type expect func(mockRepository *mock.MockRepository)
|
|
|
|
|
|
|
|
func expectEventstore(expects ...expect) func(*testing.T) *eventstore.Eventstore {
|
|
|
|
return func(t *testing.T) *eventstore.Eventstore {
|
|
|
|
m := mock.NewRepo(t)
|
|
|
|
for _, e := range expects {
|
|
|
|
e(m)
|
|
|
|
}
|
|
|
|
es := eventstore.NewEventstore(
|
|
|
|
&eventstore.Config{
|
|
|
|
Querier: m.MockQuerier,
|
|
|
|
Pusher: m.MockPusher,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
iam_repo.RegisterEventMappers(es)
|
|
|
|
org.RegisterEventMappers(es)
|
|
|
|
usr_repo.RegisterEventMappers(es)
|
|
|
|
proj_repo.RegisterEventMappers(es)
|
|
|
|
usergrant.RegisterEventMappers(es)
|
|
|
|
key_repo.RegisterEventMappers(es)
|
|
|
|
action_repo.RegisterEventMappers(es)
|
|
|
|
session.RegisterEventMappers(es)
|
|
|
|
idpintent.RegisterEventMappers(es)
|
|
|
|
authrequest.RegisterEventMappers(es)
|
|
|
|
oidcsession.RegisterEventMappers(es)
|
|
|
|
quota_repo.RegisterEventMappers(es)
|
|
|
|
limits.RegisterEventMappers(es)
|
|
|
|
feature.RegisterEventMappers(es)
|
2023-12-20 12:21:08 +00:00
|
|
|
deviceauth.RegisterEventMappers(es)
|
2023-11-21 12:11:38 +00:00
|
|
|
return es
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectFilter(events ...eventstore.Event) expect {
|
|
|
|
return func(m *mock.MockRepository) {
|
|
|
|
m.ExpectFilterEvents(events...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func expectFilterError(err error) expect {
|
|
|
|
return func(m *mock.MockRepository) {
|
|
|
|
m.ExpectFilterEventsError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventFromEventPusher(event eventstore.Command) *repository.Event {
|
|
|
|
data, _ := eventstore.EventData(event)
|
|
|
|
return &repository.Event{
|
|
|
|
InstanceID: event.Aggregate().InstanceID,
|
|
|
|
ID: "",
|
|
|
|
Seq: 0,
|
|
|
|
CreationDate: time.Time{},
|
|
|
|
Typ: event.Type(),
|
|
|
|
Data: data,
|
|
|
|
EditorUser: event.Creator(),
|
|
|
|
Version: event.Aggregate().Version,
|
|
|
|
AggregateID: event.Aggregate().ID,
|
|
|
|
AggregateType: event.Aggregate().Type,
|
|
|
|
ResourceOwner: sql.NullString{String: event.Aggregate().ResourceOwner, Valid: event.Aggregate().ResourceOwner != ""},
|
|
|
|
Constraints: event.UniqueConstraints(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:27:00 +00:00
|
|
|
func Test_cleanStaticQueries(t *testing.T) {
|
|
|
|
query := `select
|
|
|
|
foo,
|
|
|
|
bar
|
|
|
|
from table;`
|
|
|
|
want := "select foo, bar from table;"
|
|
|
|
cleanStaticQueries(&query)
|
|
|
|
assert.Equal(t, want, query)
|
|
|
|
}
|