mirror of
				https://github.com/zitadel/zitadel.git
				synced 2025-11-04 06:38:48 +00:00 
			
		
		
		
	* feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: add execution of targets to grpc calls * feat: split request and response logic to handle the different context information * feat: split request and response logic to handle the different context information * fix: integration test * fix: import alias * fix: refactor execution package * fix: refactor execution interceptor integration and unit tests * fix: refactor execution interceptor integration and unit tests * fix: refactor execution interceptor integration and unit tests * fix: refactor execution interceptor integration and unit tests * fix: refactor execution interceptor integration and unit tests * docs: basic documentation for executions and targets * fix: change order for interceptors * fix: merge back origin/main * fix: change target definition command and query side (#7735) * fix: change target definition command and query side * fix: correct refactoring name changes * fix: correct refactoring name changes * fix: changing execution defintion with target list and type * fix: changing execution definition with target list and type * fix: add back search queries for target and include * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * fix: projections change for execution with targets suffix table * docs: add example to actions v2 * docs: add example to actions v2 * fix: correct integration tests on query for executions * fix: add separate event for execution v2 as content changed * fix: add separate event for execution v2 as content changed * fix: added review comment changes * fix: added review comment changes * fix: added review comment changes --------- Co-authored-by: adlerhurst <silvan.reusser@gmail.com> * fix: added review comment changes * fix: added review comment changes * Update internal/api/grpc/server/middleware/execution_interceptor.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * fix: added review comment changes * fix: added review comment changes * fix: added review comment changes * fix: added review comment changes * fix: added review comment changes * fix: added review comment changes --------- Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: Elio Bischof <elio@zitadel.com>
		
			
				
	
	
		
			270 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			270 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package query
 | 
						|
 | 
						|
import (
 | 
						|
	"database/sql"
 | 
						|
	"database/sql/driver"
 | 
						|
	"errors"
 | 
						|
	"fmt"
 | 
						|
	"regexp"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/zitadel/zitadel/internal/domain"
 | 
						|
	exec "github.com/zitadel/zitadel/internal/repository/execution"
 | 
						|
	"github.com/zitadel/zitadel/internal/zerrors"
 | 
						|
)
 | 
						|
 | 
						|
var (
 | 
						|
	prepareExecutionsStmt = `SELECT projections.executions1.instance_id,` +
 | 
						|
		` projections.executions1.id,` +
 | 
						|
		` projections.executions1.change_date,` +
 | 
						|
		` projections.executions1.sequence,` +
 | 
						|
		` execution_targets.targets,` +
 | 
						|
		` COUNT(*) OVER ()` +
 | 
						|
		` FROM projections.executions1` +
 | 
						|
		` JOIN (` +
 | 
						|
		`SELECT instance_id, execution_id, JSONB_AGG( JSON_OBJECT( 'position' : position, 'include' : include, 'target' : target_id ) ) as targets` +
 | 
						|
		` FROM projections.executions1_targets` +
 | 
						|
		` GROUP BY instance_id, execution_id` +
 | 
						|
		`)` +
 | 
						|
		` AS execution_targets` +
 | 
						|
		` ON execution_targets.instance_id = projections.executions1.instance_id` +
 | 
						|
		` AND execution_targets.execution_id = projections.executions1.id`
 | 
						|
	prepareExecutionsCols = []string{
 | 
						|
		"instance_id",
 | 
						|
		"id",
 | 
						|
		"change_date",
 | 
						|
		"sequence",
 | 
						|
		"targets",
 | 
						|
		"count",
 | 
						|
	}
 | 
						|
 | 
						|
	prepareExecutionStmt = `SELECT projections.executions1.instance_id,` +
 | 
						|
		` projections.executions1.id,` +
 | 
						|
		` projections.executions1.change_date,` +
 | 
						|
		` projections.executions1.sequence,` +
 | 
						|
		` execution_targets.targets` +
 | 
						|
		` FROM projections.executions1` +
 | 
						|
		` JOIN (` +
 | 
						|
		`SELECT instance_id, execution_id, JSONB_AGG( JSON_OBJECT( 'position' : position, 'include' : include, 'target' : target_id ) ) as targets` +
 | 
						|
		` FROM projections.executions1_targets` +
 | 
						|
		` GROUP BY instance_id, execution_id` +
 | 
						|
		`)` +
 | 
						|
		` AS execution_targets` +
 | 
						|
		` ON execution_targets.instance_id = projections.executions1.instance_id` +
 | 
						|
		` AND execution_targets.execution_id = projections.executions1.id`
 | 
						|
	prepareExecutionCols = []string{
 | 
						|
		"instance_id",
 | 
						|
		"id",
 | 
						|
		"change_date",
 | 
						|
		"sequence",
 | 
						|
		"targets",
 | 
						|
	}
 | 
						|
)
 | 
						|
 | 
						|
func Test_ExecutionPrepares(t *testing.T) {
 | 
						|
	type want struct {
 | 
						|
		sqlExpectations sqlExpectation
 | 
						|
		err             checkErr
 | 
						|
	}
 | 
						|
	tests := []struct {
 | 
						|
		name    string
 | 
						|
		prepare interface{}
 | 
						|
		want    want
 | 
						|
		object  interface{}
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionsQuery no result",
 | 
						|
			prepare: prepareExecutionsQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueries(
 | 
						|
					regexp.QuoteMeta(prepareExecutionsStmt),
 | 
						|
					nil,
 | 
						|
					nil,
 | 
						|
				),
 | 
						|
			},
 | 
						|
			object: &Executions{Executions: []*Execution{}},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionsQuery one result",
 | 
						|
			prepare: prepareExecutionsQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueries(
 | 
						|
					regexp.QuoteMeta(prepareExecutionsStmt),
 | 
						|
					prepareExecutionsCols,
 | 
						|
					[][]driver.Value{
 | 
						|
						{
 | 
						|
							"ro",
 | 
						|
							"id",
 | 
						|
							testNow,
 | 
						|
							uint64(20211109),
 | 
						|
							[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
 | 
						|
						},
 | 
						|
					},
 | 
						|
				),
 | 
						|
			},
 | 
						|
			object: &Executions{
 | 
						|
				SearchResponse: SearchResponse{
 | 
						|
					Count: 1,
 | 
						|
				},
 | 
						|
				Executions: []*Execution{
 | 
						|
					{
 | 
						|
						ID: "id",
 | 
						|
						ObjectDetails: domain.ObjectDetails{
 | 
						|
							EventDate:     testNow,
 | 
						|
							ResourceOwner: "ro",
 | 
						|
							Sequence:      20211109,
 | 
						|
						},
 | 
						|
						Targets: []*exec.Target{
 | 
						|
							{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
 | 
						|
							{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
 | 
						|
						},
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionsQuery multiple result",
 | 
						|
			prepare: prepareExecutionsQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueries(
 | 
						|
					regexp.QuoteMeta(prepareExecutionsStmt),
 | 
						|
					prepareExecutionsCols,
 | 
						|
					[][]driver.Value{
 | 
						|
						{
 | 
						|
							"ro",
 | 
						|
							"id-1",
 | 
						|
							testNow,
 | 
						|
							uint64(20211109),
 | 
						|
							[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
 | 
						|
						},
 | 
						|
						{
 | 
						|
							"ro",
 | 
						|
							"id-2",
 | 
						|
							testNow,
 | 
						|
							uint64(20211110),
 | 
						|
							[]byte(`[{"position" : 2, "target" : "target"}, {"position" : 1, "include" : "include"}]`),
 | 
						|
						},
 | 
						|
					},
 | 
						|
				),
 | 
						|
			},
 | 
						|
			object: &Executions{
 | 
						|
				SearchResponse: SearchResponse{
 | 
						|
					Count: 2,
 | 
						|
				},
 | 
						|
				Executions: []*Execution{
 | 
						|
					{
 | 
						|
						ID: "id-1",
 | 
						|
						ObjectDetails: domain.ObjectDetails{
 | 
						|
							EventDate:     testNow,
 | 
						|
							ResourceOwner: "ro",
 | 
						|
							Sequence:      20211109,
 | 
						|
						},
 | 
						|
						Targets: []*exec.Target{
 | 
						|
							{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
 | 
						|
							{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
 | 
						|
						},
 | 
						|
					},
 | 
						|
					{
 | 
						|
						ID: "id-2",
 | 
						|
						ObjectDetails: domain.ObjectDetails{
 | 
						|
							EventDate:     testNow,
 | 
						|
							ResourceOwner: "ro",
 | 
						|
							Sequence:      20211110,
 | 
						|
						},
 | 
						|
						Targets: []*exec.Target{
 | 
						|
							{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
 | 
						|
							{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
 | 
						|
						},
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionsQuery sql err",
 | 
						|
			prepare: prepareExecutionsQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueryErr(
 | 
						|
					regexp.QuoteMeta(prepareExecutionsStmt),
 | 
						|
					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: (*Execution)(nil),
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionQuery no result",
 | 
						|
			prepare: prepareExecutionQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueriesScanErr(
 | 
						|
					regexp.QuoteMeta(prepareExecutionStmt),
 | 
						|
					nil,
 | 
						|
					nil,
 | 
						|
				),
 | 
						|
				err: func(err error) (error, bool) {
 | 
						|
					if !zerrors.IsNotFound(err) {
 | 
						|
						return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
 | 
						|
					}
 | 
						|
					return nil, true
 | 
						|
				},
 | 
						|
			},
 | 
						|
			object: (*Execution)(nil),
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionQuery found",
 | 
						|
			prepare: prepareExecutionQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQuery(
 | 
						|
					regexp.QuoteMeta(prepareExecutionStmt),
 | 
						|
					prepareExecutionCols,
 | 
						|
					[]driver.Value{
 | 
						|
						"ro",
 | 
						|
						"id",
 | 
						|
						testNow,
 | 
						|
						uint64(20211109),
 | 
						|
						[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
 | 
						|
					},
 | 
						|
				),
 | 
						|
			},
 | 
						|
			object: &Execution{
 | 
						|
				ID: "id",
 | 
						|
				ObjectDetails: domain.ObjectDetails{
 | 
						|
					EventDate:     testNow,
 | 
						|
					ResourceOwner: "ro",
 | 
						|
					Sequence:      20211109,
 | 
						|
				},
 | 
						|
				Targets: []*exec.Target{
 | 
						|
					{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
 | 
						|
					{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:    "prepareExecutionQuery sql err",
 | 
						|
			prepare: prepareExecutionQuery,
 | 
						|
			want: want{
 | 
						|
				sqlExpectations: mockQueryErr(
 | 
						|
					regexp.QuoteMeta(prepareExecutionStmt),
 | 
						|
					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: (*Execution)(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...)
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |