2021-12-08 15:16:48 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-12-08 15:16:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthNKeyProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
2022-01-03 08:19:07 +00:00
|
|
|
event func(t *testing.T) eventstore.Event
|
2021-12-08 15:16:48 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2022-01-03 08:19:07 +00:00
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
2021-12-08 15:16:48 +00:00
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyAdded app",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ApplicationKeyAddedEventType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"applicationId": "appId", "clientId":"clientId","keyId": "keyId", "type": 1, "expirationDate": "2021-11-30T15:00:00Z", "publicKey": "cHVibGljS2V5"}`),
|
|
|
|
), project.ApplicationKeyAddedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyAdded,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.authn_keys (id, creation_date, resource_owner, instance_id, aggregate_id, sequence, object_id, expiration, identifier, public_key, type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2021-12-08 15:16:48 +00:00
|
|
|
"agg-id",
|
|
|
|
uint64(15),
|
|
|
|
"appId",
|
|
|
|
anyArg{},
|
|
|
|
"clientId",
|
|
|
|
[]byte("publicKey"),
|
|
|
|
domain.AuthNKeyTypeJSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyAdded user",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.MachineKeyAddedEventType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId", "type": 1, "expirationDate": "2021-11-30T15:00:00Z", "publicKey": "cHVibGljS2V5"}`),
|
|
|
|
), user.MachineKeyAddedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyAdded,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.authn_keys (id, creation_date, resource_owner, instance_id, aggregate_id, sequence, object_id, expiration, identifier, public_key, type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2021-12-08 15:16:48 +00:00
|
|
|
"agg-id",
|
|
|
|
uint64(15),
|
|
|
|
"agg-id",
|
|
|
|
anyArg{},
|
|
|
|
"agg-id",
|
|
|
|
[]byte("publicKey"),
|
|
|
|
domain.AuthNKeyTypeJSON,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved app key",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ApplicationKeyRemovedEventType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId"}`),
|
|
|
|
), project.ApplicationKeyRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged api no change",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.APIConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId"}`),
|
|
|
|
), project.APIConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged api config basic",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.APIConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId", "authMethodType": 0}`),
|
|
|
|
), project.APIConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "UPDATE projections.authn_keys SET (enabled) = ($1) WHERE (object_id = $2)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
false,
|
|
|
|
"appId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged api config jwt",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.APIConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId", "authMethodType": 1}`),
|
|
|
|
), project.APIConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "UPDATE projections.authn_keys SET (enabled) = ($1) WHERE (object_id = $2)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
true,
|
|
|
|
"appId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved app key",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.MachineKeyRemovedEventType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId"}`),
|
|
|
|
), user.MachineKeyRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged oidc no change",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.OIDCConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId"}`),
|
|
|
|
), project.OIDCConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged oidc config basic",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.OIDCConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId", "authMethodType": 0}`),
|
|
|
|
), project.OIDCConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "UPDATE projections.authn_keys SET (enabled) = ($1) WHERE (object_id = $2)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
false,
|
|
|
|
"appId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyEnabledChanged oidc config jwt",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.OIDCConfigChangedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId", "authMethodType": 3}`),
|
|
|
|
), project.OIDCConfigChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyEnabledChanged,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "UPDATE projections.authn_keys SET (enabled) = ($1) WHERE (object_id = $2)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
true,
|
|
|
|
"appId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved app key removed",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ApplicationKeyRemovedEventType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId"}`),
|
|
|
|
), project.ApplicationKeyRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved app removed",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ApplicationRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
[]byte(`{"appId": "appId"}`),
|
|
|
|
), project.ApplicationRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (object_id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"appId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved project removed",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(project.ProjectRemovedType),
|
|
|
|
project.AggregateType,
|
|
|
|
nil,
|
|
|
|
), project.ProjectRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("project"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (aggregate_id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved machine key removed",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.MachineKeyRemovedEventType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId"}`),
|
|
|
|
), user.MachineKeyRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"keyId",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceAuthNKeyRemoved user removed",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(user.UserRemovedType),
|
|
|
|
user.AggregateType,
|
|
|
|
[]byte(`{"keyId": "keyId"}`),
|
|
|
|
), user.UserRemovedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&authNKeyProjection{}).reduceAuthNKeyRemoved,
|
2021-12-08 15:16:48 +00:00
|
|
|
want: wantReduce{
|
|
|
|
projection: AuthNKeyTable,
|
|
|
|
aggregateType: eventstore.AggregateType("user"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.authn_keys WHERE (aggregate_id = $1)",
|
2021-12-08 15:16:48 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
|
|
|
if !errors.IsErrorInvalidArgument(err) {
|
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
|
|
|
assertReduce(t, got, err, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|