2022-02-08 08:37:28 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
2022-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2022-02-08 08:37:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPersonalAccessTokenProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event func(t *testing.T) eventstore.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reducePersonalAccessTokenAdded",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.PersonalAccessTokenAddedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"tokenId": "tokenID", "expiration": "9999-12-31T23:59:59Z", "scopes": ["openid"]}`),
|
|
|
|
), user.PersonalAccessTokenAddedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&personalAccessTokenProjection{}).reducePersonalAccessTokenAdded,
|
2022-02-08 08:37:28 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.personal_access_tokens2 (id, creation_date, change_date, resource_owner, instance_id, sequence, user_id, expiration, scopes) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
2022-02-08 08:37:28 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"tokenID",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-02-08 08:37:28 +00:00
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
|
|
|
time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC),
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"openid"},
|
2022-02-08 08:37:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reducePersonalAccessTokenRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.PersonalAccessTokenRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"tokenId": "tokenID"}`),
|
|
|
|
), user.PersonalAccessTokenRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&personalAccessTokenProjection{}).reducePersonalAccessTokenRemoved,
|
2022-02-08 08:37:28 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.personal_access_tokens2 WHERE (id = $1)",
|
2022-02-08 08:37:28 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"tokenID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceUserRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.PersonalAccessTokenRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
nil,
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&personalAccessTokenProjection{}).reduceUserRemoved,
|
2022-02-08 08:37:28 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-08-31 07:52:43 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.personal_access_tokens2 WHERE (user_id = $1)",
|
2022-02-08 08:37:28 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
name: "instance.reduceInstanceRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(instance.InstanceRemovedEventType),
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{"name": "Name"}`),
|
|
|
|
), instance.InstanceRemovedEventMapper),
|
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(PersonalAccessTokenColumnInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "DELETE FROM projections.personal_access_tokens2 WHERE (instance_id = $1)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
|
|
|
if _, ok := err.(errors.InvalidArgument); !ok {
|
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
2022-10-20 12:36:52 +00:00
|
|
|
assertReduce(t, got, err, PersonalAccessTokenProjectionTable, tt.want)
|
2022-02-08 08:37:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|