mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:37:30 +00:00
trusted domain event test
This commit is contained in:
@@ -10,7 +10,12 @@ import (
|
|||||||
type InstanceDomain struct {
|
type InstanceDomain struct {
|
||||||
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
|
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
|
||||||
Domain string `json:"domain,omitempty" db:"domain"`
|
Domain string `json:"domain,omitempty" db:"domain"`
|
||||||
IsPrimary bool `json:"isPrimary,omitempty" db:"is_primary"`
|
// IsPrimary indicates if the domain is the primary domain of the instance.
|
||||||
|
// It is only set for custom domains.
|
||||||
|
IsPrimary *bool `json:"isPrimary,omitempty" db:"is_primary"`
|
||||||
|
// IsGenerated indicates if the domain is a generated domain.
|
||||||
|
// It is only set for custom domains.
|
||||||
|
IsGenerated *bool `json:"isGenerated,omitempty" db:"is_generated"`
|
||||||
Type DomainType `json:"type,omitempty" db:"type"`
|
Type DomainType `json:"type,omitempty" db:"type"`
|
||||||
|
|
||||||
CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"`
|
CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"`
|
||||||
@@ -20,16 +25,16 @@ type InstanceDomain struct {
|
|||||||
type AddInstanceDomain struct {
|
type AddInstanceDomain struct {
|
||||||
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
|
InstanceID string `json:"instanceId,omitempty" db:"instance_id"`
|
||||||
Domain string `json:"domain,omitempty" db:"domain"`
|
Domain string `json:"domain,omitempty" db:"domain"`
|
||||||
IsPrimary bool `json:"isPrimary,omitempty" db:"is_primary"`
|
IsPrimary *bool `json:"isPrimary,omitempty" db:"is_primary"`
|
||||||
IsGenerated bool `json:"isGenerated,omitempty" db:"is_generated"`
|
IsGenerated *bool `json:"isGenerated,omitempty" db:"is_generated"`
|
||||||
Type DomainType `json:"type,omitempty" db:"type"`
|
Type DomainType `json:"type,omitempty" db:"type"`
|
||||||
|
|
||||||
// CreatedAt is the time when the domain was added.
|
// CreatedAt is the time when the domain was added.
|
||||||
// It is set by the repository and should not be set by the caller.
|
// It is set by the repository and should not be set by the caller.
|
||||||
CreatedAt *time.Time `json:"createdAt,omitzero" db:"created_at"`
|
CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"`
|
||||||
// UpdatedAt is the time when the domain was last updated.
|
// UpdatedAt is the time when the domain was last updated.
|
||||||
// It is set by the repository and should not be set by the caller.
|
// It is set by the repository and should not be set by the caller.
|
||||||
UpdatedAt *time.Time `json:"updatedAt,omitzero" db:"updated_at"`
|
UpdatedAt time.Time `json:"updatedAt,omitzero" db:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type instanceDomainColumns interface {
|
type instanceDomainColumns interface {
|
||||||
|
@@ -15,8 +15,8 @@ CREATE TABLE zitadel.instance_domains(
|
|||||||
, is_generated BOOLEAN
|
, is_generated BOOLEAN
|
||||||
, type zitadel.domain_type NOT NULL
|
, type zitadel.domain_type NOT NULL
|
||||||
|
|
||||||
, created_at TIMESTAMPTZ DEFAULT NOW()
|
, created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
|
||||||
, updated_at TIMESTAMPTZ DEFAULT NOW()
|
, updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
|
||||||
|
|
||||||
, PRIMARY KEY (domain)
|
, PRIMARY KEY (domain)
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ CREATE TABLE zitadel.instance_domains(
|
|||||||
|
|
||||||
, CONSTRAINT primary_cannot_be_trusted CHECK (is_primary IS NULL OR type != 'trusted')
|
, CONSTRAINT primary_cannot_be_trusted CHECK (is_primary IS NULL OR type != 'trusted')
|
||||||
, CONSTRAINT generated_cannot_be_trusted CHECK (is_generated IS NULL OR type != 'trusted')
|
, CONSTRAINT generated_cannot_be_trusted CHECK (is_generated IS NULL OR type != 'trusted')
|
||||||
, CONSTRAINT custom_values_set CHECK (type = 'custom' AND is_primary IS NOT NULL AND is_generated IS NOT NULL)
|
, CONSTRAINT custom_values_set CHECK ((is_primary IS NOT NULL AND is_generated IS NOT NULL) OR type != 'custom')
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX idx_instance_domain_instance ON zitadel.instance_domains(instance_id);
|
CREATE INDEX idx_instance_domain_instance ON zitadel.instance_domains(instance_id);
|
||||||
@@ -37,8 +37,8 @@ CREATE TABLE zitadel.org_domains(
|
|||||||
, is_primary BOOLEAN NOT NULL DEFAULT FALSE
|
, is_primary BOOLEAN NOT NULL DEFAULT FALSE
|
||||||
, validation_type zitadel.domain_validation_type
|
, validation_type zitadel.domain_validation_type
|
||||||
|
|
||||||
, created_at TIMESTAMPTZ DEFAULT NOW()
|
, created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
|
||||||
, updated_at TIMESTAMPTZ DEFAULT NOW()
|
, updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
|
||||||
|
|
||||||
, PRIMARY KEY (instance_id, org_id, domain)
|
, PRIMARY KEY (instance_id, org_id, domain)
|
||||||
|
|
||||||
|
@@ -3,7 +3,6 @@
|
|||||||
package events_test
|
package events_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -11,6 +10,7 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||||
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
||||||
"github.com/zitadel/zitadel/internal/integration"
|
"github.com/zitadel/zitadel/internal/integration"
|
||||||
@@ -42,7 +42,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
assert.NoError(ttt, err)
|
assert.NoError(ttt, err)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
|
|
||||||
t.Run("test instance domain add reduces", func(t *testing.T) {
|
t.Run("test instance custom domain add reduces", func(t *testing.T) {
|
||||||
// Add a domain to the instance
|
// Add a domain to the instance
|
||||||
domainName := gofakeit.DomainName()
|
domainName := gofakeit.DomainName()
|
||||||
beforeAdd := time.Now()
|
beforeAdd := time.Now()
|
||||||
@@ -71,6 +71,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -78,15 +79,13 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
// event instance.domain.added
|
// event instance.domain.added
|
||||||
assert.Equal(ttt, domainName, domain.Domain)
|
assert.Equal(ttt, domainName, domain.Domain)
|
||||||
assert.Equal(ttt, instance.Instance.Id, domain.InstanceID)
|
assert.Equal(ttt, instance.Instance.Id, domain.InstanceID)
|
||||||
assert.False(ttt, domain.IsPrimary)
|
assert.False(ttt, *domain.IsPrimary)
|
||||||
log.Printf("created at %v\n", domain.CreatedAt)
|
|
||||||
log.Printf("after %v\n", afterAdd)
|
|
||||||
log.Printf("before %v\n", beforeAdd)
|
|
||||||
assert.WithinRange(ttt, domain.CreatedAt, beforeAdd, afterAdd)
|
assert.WithinRange(ttt, domain.CreatedAt, beforeAdd, afterAdd)
|
||||||
assert.WithinRange(ttt, domain.UpdatedAt, beforeAdd, afterAdd)
|
assert.WithinRange(ttt, domain.UpdatedAt, beforeAdd, afterAdd)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
})
|
})
|
||||||
t.Run("test instance domain set primary reduces", func(t *testing.T) {
|
|
||||||
|
t.Run("test instance custom domain set primary reduces", func(t *testing.T) {
|
||||||
// Add a domain to the instance
|
// Add a domain to the instance
|
||||||
domainName := gofakeit.DomainName()
|
domainName := gofakeit.DomainName()
|
||||||
_, err := instance.Client.InstanceV2Beta.AddCustomDomain(CTX, &v2beta.AddCustomDomainRequest{
|
_, err := instance.Client.InstanceV2Beta.AddCustomDomain(CTX, &v2beta.AddCustomDomainRequest{
|
||||||
@@ -101,6 +100,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.WithCondition(
|
database.WithCondition(
|
||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
instanceDomainRepo.IsPrimaryCondition(false),
|
instanceDomainRepo.IsPrimaryCondition(false),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -129,11 +129,13 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.WithCondition(
|
database.WithCondition(
|
||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName)),
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
require.NoError(ttt, err)
|
require.NoError(ttt, err)
|
||||||
require.False(ttt, domain.IsPrimary)
|
require.False(ttt, *domain.IsPrimary)
|
||||||
assert.Equal(ttt, domainName, domain.Domain)
|
assert.Equal(ttt, domainName, domain.Domain)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
|
|
||||||
@@ -154,18 +156,19 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
instanceDomainRepo.IsPrimaryCondition(true),
|
instanceDomainRepo.IsPrimaryCondition(true),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
require.NoError(ttt, err)
|
require.NoError(ttt, err)
|
||||||
// event instance.domain.primary.set
|
// event instance.domain.primary.set
|
||||||
assert.Equal(ttt, domainName, domain.Domain)
|
assert.Equal(ttt, domainName, domain.Domain)
|
||||||
assert.True(ttt, domain.IsPrimary)
|
assert.True(ttt, *domain.IsPrimary)
|
||||||
assert.WithinRange(ttt, domain.UpdatedAt, beforeSetPrimary, afterSetPrimary)
|
assert.WithinRange(ttt, domain.UpdatedAt, beforeSetPrimary, afterSetPrimary)
|
||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("test instance domain remove reduces", func(t *testing.T) {
|
t.Run("test instance custom domain remove reduces", func(t *testing.T) {
|
||||||
// Add a domain to the instance
|
// Add a domain to the instance
|
||||||
domainName := gofakeit.DomainName()
|
domainName := gofakeit.DomainName()
|
||||||
_, err := instance.Client.InstanceV2Beta.AddCustomDomain(CTX, &v2beta.AddCustomDomainRequest{
|
_, err := instance.Client.InstanceV2Beta.AddCustomDomain(CTX, &v2beta.AddCustomDomainRequest{
|
||||||
@@ -174,16 +177,6 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_, err := instance.Client.InstanceV2Beta.RemoveCustomDomain(CTX, &v2beta.RemoveCustomDomainRequest{
|
|
||||||
InstanceId: instance.Instance.Id,
|
|
||||||
Domain: domainName,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Logf("Failed to delete instance domain on cleanup: %v", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Wait for domain to be created and verify it exists
|
// Wait for domain to be created and verify it exists
|
||||||
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
@@ -192,6 +185,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -199,7 +193,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
}, retryDuration, tick)
|
}, retryDuration, tick)
|
||||||
|
|
||||||
// Remove the domain
|
// Remove the domain
|
||||||
_, err = SystemClient.RemoveDomain(CTX, &system.RemoveDomainRequest{
|
_, err = instance.Client.InstanceV2Beta.RemoveCustomDomain(CTX, &v2beta.RemoveCustomDomainRequest{
|
||||||
InstanceId: instance.Instance.Id,
|
InstanceId: instance.Instance.Id,
|
||||||
Domain: domainName,
|
Domain: domainName,
|
||||||
})
|
})
|
||||||
@@ -213,6 +207,98 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
|
|||||||
database.And(
|
database.And(
|
||||||
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
// event instance.domain.removed
|
||||||
|
assert.Nil(ttt, domain)
|
||||||
|
require.ErrorIs(ttt, err, new(database.NoRowFoundError))
|
||||||
|
}, retryDuration, tick)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("test instance trusted domain add reduces", func(t *testing.T) {
|
||||||
|
// Add a domain to the instance
|
||||||
|
domainName := gofakeit.DomainName()
|
||||||
|
beforeAdd := time.Now()
|
||||||
|
_, err := instance.Client.InstanceV2Beta.AddTrustedDomain(CTX, &v2beta.AddTrustedDomainRequest{
|
||||||
|
InstanceId: instance.Instance.Id,
|
||||||
|
Domain: domainName,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
afterAdd := time.Now()
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_, err := instance.Client.InstanceV2Beta.RemoveTrustedDomain(CTX, &v2beta.RemoveTrustedDomainRequest{
|
||||||
|
InstanceId: instance.Instance.Id,
|
||||||
|
Domain: domainName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Logf("Failed to delete instance domain on cleanup: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test that domain add reduces
|
||||||
|
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
|
domain, err := instanceDomainRepo.Get(CTX,
|
||||||
|
database.WithCondition(
|
||||||
|
database.And(
|
||||||
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeTrusted),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
require.NoError(ttt, err)
|
||||||
|
// event instance.domain.added
|
||||||
|
assert.Equal(ttt, domainName, domain.Domain)
|
||||||
|
assert.Equal(ttt, instance.Instance.Id, domain.InstanceID)
|
||||||
|
assert.WithinRange(ttt, domain.CreatedAt, beforeAdd, afterAdd)
|
||||||
|
assert.WithinRange(ttt, domain.UpdatedAt, beforeAdd, afterAdd)
|
||||||
|
}, retryDuration, tick)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("test instance trusted domain remove reduces", func(t *testing.T) {
|
||||||
|
// Add a domain to the instance
|
||||||
|
domainName := gofakeit.DomainName()
|
||||||
|
_, err := instance.Client.InstanceV2Beta.AddTrustedDomain(CTX, &v2beta.AddTrustedDomainRequest{
|
||||||
|
InstanceId: instance.Instance.Id,
|
||||||
|
Domain: domainName,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Wait for domain to be created and verify it exists
|
||||||
|
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
|
_, err := instanceDomainRepo.Get(CTX,
|
||||||
|
database.WithCondition(
|
||||||
|
database.And(
|
||||||
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeTrusted),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
require.NoError(ttt, err)
|
||||||
|
}, retryDuration, tick)
|
||||||
|
|
||||||
|
// Remove the domain
|
||||||
|
_, err = instance.Client.InstanceV2Beta.RemoveTrustedDomain(CTX, &v2beta.RemoveTrustedDomainRequest{
|
||||||
|
InstanceId: instance.Instance.Id,
|
||||||
|
Domain: domainName,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Test that domain remove reduces
|
||||||
|
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||||
|
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||||
|
domain, err := instanceDomainRepo.Get(CTX,
|
||||||
|
database.WithCondition(
|
||||||
|
database.And(
|
||||||
|
instanceDomainRepo.InstanceIDCondition(instance.Instance.Id),
|
||||||
|
instanceDomainRepo.DomainCondition(database.TextOperationEqual, domainName),
|
||||||
|
instanceDomainRepo.TypeCondition(domain.DomainTypeTrusted),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@@ -54,13 +54,22 @@ func (i *instanceDomain) List(ctx context.Context, opts ...database.QueryOption)
|
|||||||
|
|
||||||
// Add implements [domain.InstanceDomainRepository].
|
// Add implements [domain.InstanceDomainRepository].
|
||||||
func (i *instanceDomain) Add(ctx context.Context, domain *domain.AddInstanceDomain) error {
|
func (i *instanceDomain) Add(ctx context.Context, domain *domain.AddInstanceDomain) error {
|
||||||
var builder database.StatementBuilder
|
var (
|
||||||
|
builder database.StatementBuilder
|
||||||
|
createdAt, updatedAt any = database.DefaultInstruction, database.DefaultInstruction
|
||||||
|
)
|
||||||
|
if !domain.CreatedAt.IsZero() {
|
||||||
|
createdAt = domain.CreatedAt
|
||||||
|
}
|
||||||
|
if !domain.UpdatedAt.IsZero() {
|
||||||
|
updatedAt = domain.UpdatedAt
|
||||||
|
}
|
||||||
|
|
||||||
builder.WriteString(`INSERT INTO zitadel.instance_domains (instance_id, domain, is_primary, is_generated, type, created_at, updated_at) VALUES (`)
|
builder.WriteString(`INSERT INTO zitadel.instance_domains (instance_id, domain, is_primary, is_generated, type, created_at, updated_at) VALUES (`)
|
||||||
builder.WriteArgs(domain.InstanceID, domain.Domain, domain.IsPrimary, domain.IsGenerated, domain.Type, domain.CreatedAt, domain.UpdatedAt)
|
builder.WriteArgs(domain.InstanceID, domain.Domain, domain.IsPrimary, domain.IsGenerated, domain.Type, createdAt, updatedAt)
|
||||||
builder.WriteString(`) RETURNING created_at, updated_at`)
|
builder.WriteString(`) RETURNING created_at, updated_at`)
|
||||||
|
|
||||||
return i.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(domain.CreatedAt, domain.UpdatedAt)
|
return i.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&domain.CreatedAt, &domain.UpdatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements [domain.InstanceDomainRepository].
|
// Remove implements [domain.InstanceDomainRepository].
|
||||||
|
@@ -3,8 +3,10 @@ package repository_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/brianvoe/gofakeit/v6"
|
"github.com/brianvoe/gofakeit/v6"
|
||||||
|
"github.com/muhlemmer/gu"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@@ -40,7 +42,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain: domain.AddInstanceDomain{
|
instanceDomain: domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -48,7 +52,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain: domain.AddInstanceDomain{
|
instanceDomain: domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: true,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(true),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -56,7 +62,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain: domain.AddInstanceDomain{
|
instanceDomain: domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: "",
|
Domain: "",
|
||||||
IsPrimary: false,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
err: new(database.CheckError),
|
err: new(database.CheckError),
|
||||||
},
|
},
|
||||||
@@ -68,7 +76,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain := &domain.AddInstanceDomain{
|
instanceDomain := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName,
|
Domain: domainName,
|
||||||
IsPrimary: false,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := domainRepo.Add(ctx, instanceDomain)
|
err := domainRepo.Add(ctx, instanceDomain)
|
||||||
@@ -78,7 +88,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
return &domain.AddInstanceDomain{
|
return &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName,
|
Domain: domainName,
|
||||||
IsPrimary: true,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(true),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
err: new(database.UniqueError),
|
err: new(database.UniqueError),
|
||||||
@@ -88,7 +100,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain: domain.AddInstanceDomain{
|
instanceDomain: domain.AddInstanceDomain{
|
||||||
InstanceID: "non-existent-instance",
|
InstanceID: "non-existent-instance",
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
err: new(database.ForeignKeyError),
|
err: new(database.ForeignKeyError),
|
||||||
},
|
},
|
||||||
@@ -96,7 +110,9 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
name: "add domain without instance id",
|
name: "add domain without instance id",
|
||||||
instanceDomain: domain.AddInstanceDomain{
|
instanceDomain: domain.AddInstanceDomain{
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
Type: domain.DomainTypeCustom,
|
||||||
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
err: new(database.ForeignKeyError),
|
err: new(database.ForeignKeyError),
|
||||||
},
|
},
|
||||||
@@ -106,6 +122,8 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
ctx := t.Context()
|
ctx := t.Context()
|
||||||
|
|
||||||
|
// we take now here because the timestamp of the transaction is used to set the createdAt and updatedAt fields
|
||||||
|
beforeAdd := time.Now()
|
||||||
tx, err := pool.Begin(t.Context(), nil)
|
tx, err := pool.Begin(t.Context(), nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -122,6 +140,7 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = domainRepo.Add(ctx, instanceDomain)
|
err = domainRepo.Add(ctx, instanceDomain)
|
||||||
|
afterAdd := time.Now()
|
||||||
if test.err != nil {
|
if test.err != nil {
|
||||||
assert.ErrorIs(t, err, test.err)
|
assert.ErrorIs(t, err, test.err)
|
||||||
return
|
return
|
||||||
@@ -130,6 +149,8 @@ func TestAddInstanceDomain(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotZero(t, instanceDomain.CreatedAt)
|
assert.NotZero(t, instanceDomain.CreatedAt)
|
||||||
assert.NotZero(t, instanceDomain.UpdatedAt)
|
assert.NotZero(t, instanceDomain.UpdatedAt)
|
||||||
|
assert.WithinRange(t, instanceDomain.CreatedAt, beforeAdd, afterAdd)
|
||||||
|
assert.WithinRange(t, instanceDomain.UpdatedAt, beforeAdd, afterAdd)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,12 +184,16 @@ func TestGetInstanceDomain(t *testing.T) {
|
|||||||
domain1 := &domain.AddInstanceDomain{
|
domain1 := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName1,
|
Domain: domainName1,
|
||||||
IsPrimary: true,
|
IsPrimary: gu.Ptr(true),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
}
|
}
|
||||||
domain2 := &domain.AddInstanceDomain{
|
domain2 := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName2,
|
Domain: domainName2,
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = domainRepo.Add(t.Context(), domain1)
|
err = domainRepo.Add(t.Context(), domain1)
|
||||||
@@ -190,7 +215,7 @@ func TestGetInstanceDomain(t *testing.T) {
|
|||||||
expected: &domain.InstanceDomain{
|
expected: &domain.InstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName1,
|
Domain: domainName1,
|
||||||
IsPrimary: true,
|
IsPrimary: gu.Ptr(true),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -201,7 +226,7 @@ func TestGetInstanceDomain(t *testing.T) {
|
|||||||
expected: &domain.InstanceDomain{
|
expected: &domain.InstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName2,
|
Domain: domainName2,
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -261,17 +286,23 @@ func TestListInstanceDomains(t *testing.T) {
|
|||||||
{
|
{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: true,
|
IsPrimary: gu.Ptr(true),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: gofakeit.DomainName(),
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +391,9 @@ func TestUpdateInstanceDomain(t *testing.T) {
|
|||||||
instanceDomain := &domain.AddInstanceDomain{
|
instanceDomain := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName,
|
Domain: domainName,
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = domainRepo.Add(t.Context(), instanceDomain)
|
err = domainRepo.Add(t.Context(), instanceDomain)
|
||||||
@@ -445,17 +478,20 @@ func TestRemoveInstanceDomain(t *testing.T) {
|
|||||||
// add domains
|
// add domains
|
||||||
domainRepo := instanceRepo.Domains(false)
|
domainRepo := instanceRepo.Domains(false)
|
||||||
domainName1 := gofakeit.DomainName()
|
domainName1 := gofakeit.DomainName()
|
||||||
domainName2 := gofakeit.DomainName()
|
|
||||||
|
|
||||||
domain1 := &domain.AddInstanceDomain{
|
domain1 := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName1,
|
Domain: domainName1,
|
||||||
IsPrimary: true,
|
IsPrimary: gu.Ptr(true),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
}
|
}
|
||||||
domain2 := &domain.AddInstanceDomain{
|
domain2 := &domain.AddInstanceDomain{
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
Domain: domainName2,
|
Domain: gofakeit.DomainName(),
|
||||||
IsPrimary: false,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: gu.Ptr(false),
|
||||||
|
Type: domain.DomainTypeCustom,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = domainRepo.Add(t.Context(), domain1)
|
err = domainRepo.Add(t.Context(), domain1)
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
type Instruction string
|
type Instruction string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
DefaultInstruction Instruction = "DEFAULT"
|
||||||
NowInstruction Instruction = "NOW()"
|
NowInstruction Instruction = "NOW()"
|
||||||
NullInstruction Instruction = "NULL"
|
NullInstruction Instruction = "NULL"
|
||||||
)
|
)
|
||||||
|
@@ -69,10 +69,11 @@ func (p *instanceDomainRelationalProjection) reduceCustomDomainAdded(event event
|
|||||||
return repository.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &domain.AddInstanceDomain{
|
return repository.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &domain.AddInstanceDomain{
|
||||||
InstanceID: e.Aggregate().InstanceID,
|
InstanceID: e.Aggregate().InstanceID,
|
||||||
Domain: e.Domain,
|
Domain: e.Domain,
|
||||||
IsGenerated: e.Generated,
|
IsPrimary: gu.Ptr(false),
|
||||||
|
IsGenerated: &e.Generated,
|
||||||
Type: domain.DomainTypeCustom,
|
Type: domain.DomainTypeCustom,
|
||||||
CreatedAt: gu.Ptr(e.CreationDate()),
|
CreatedAt: e.CreationDate(),
|
||||||
UpdatedAt: gu.Ptr(e.CreationDate()),
|
UpdatedAt: e.CreationDate(),
|
||||||
})
|
})
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
@@ -136,9 +137,9 @@ func (p *instanceDomainRelationalProjection) reduceTrustedDomainAdded(event even
|
|||||||
return repository.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &domain.AddInstanceDomain{
|
return repository.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &domain.AddInstanceDomain{
|
||||||
InstanceID: e.Aggregate().InstanceID,
|
InstanceID: e.Aggregate().InstanceID,
|
||||||
Domain: e.Domain,
|
Domain: e.Domain,
|
||||||
Type: domain.DomainTypeCustom,
|
Type: domain.DomainTypeTrusted,
|
||||||
CreatedAt: gu.Ptr(e.CreationDate()),
|
CreatedAt: e.CreationDate(),
|
||||||
UpdatedAt: gu.Ptr(e.CreationDate()),
|
UpdatedAt: e.CreationDate(),
|
||||||
})
|
})
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user