zitadel/internal/repository/feature/feature_v2/feature_test.go
Tim Möhlmann 26d1563643
feat(api): feature flags (#7356)
* feat(api): feature API proto definitions

* update proto based on discussion with @livio-a

* cleanup old feature flag stuff

* authz instance queries

* align defaults

* projection definitions

* define commands and event reducers

* implement system and instance setter APIs

* api getter implementation

* unit test repository package

* command unit tests

* unit test Get queries

* grpc converter unit tests

* migrate the V1 features

* migrate oidc to dynamic features

* projection unit test

* fix instance by host

* fix instance by id data type in sql

* fix linting errors

* add system projection test

* fix behavior inversion

* resolve proto file comments

* rename SystemDefaultLoginInstanceEventType to SystemLoginDefaultOrgEventType so it's consistent with the instance level event

* use write models and conditional set events

* system features integration tests

* instance features integration tests

* error on empty request

* documentation entry

* typo in feature.proto

* fix start unit tests

* solve linting error on key case switch

* remove system defaults after discussion with @eliobischof

* fix system feature projection

* resolve comments in defaults.yaml

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-02-28 10:55:54 +02:00

119 lines
2.8 KiB
Go

package feature_v2
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/feature"
"github.com/zitadel/zitadel/internal/zerrors"
)
func TestSetEvent_FeatureJSON(t *testing.T) {
tests := []struct {
name string
e *SetEvent[float64] // using float so it's easy to create marshal errors
want *FeatureJSON
wantErr error
}{
{
name: "invalid key error",
e: &SetEvent[float64]{
BaseEvent: &eventstore.BaseEvent{
EventType: "feature.system.foo_bar.some_feat",
},
},
wantErr: zerrors.ThrowInternalf(nil, "FEAT-eir0M", "reduce.wrong.event.type %s", "feature.system.foo_bar.some_feat"),
},
{
name: "marshal error",
e: &SetEvent[float64]{
BaseEvent: &eventstore.BaseEvent{
EventType: SystemLoginDefaultOrgEventType,
},
Value: math.NaN(),
},
wantErr: zerrors.ThrowInternalf(nil, "FEAT-go9Ji", "reduce.wrong.event.type %s", SystemLoginDefaultOrgEventType),
},
{
name: "success",
e: &SetEvent[float64]{
BaseEvent: &eventstore.BaseEvent{
EventType: SystemLoginDefaultOrgEventType,
},
Value: 555,
},
want: &FeatureJSON{
Key: feature.KeyLoginDefaultOrg,
Value: []byte(`555`),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.e.FeatureJSON()
require.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}
func TestSetEvent_FeatureInfo(t *testing.T) {
tests := []struct {
name string
e *SetEvent[bool]
want feature.Level
want1 feature.Key
wantErr error
}{
{
name: "format error",
e: &SetEvent[bool]{
BaseEvent: &eventstore.BaseEvent{
EventType: "foo.bar",
},
},
wantErr: zerrors.ThrowInternalf(nil, "FEAT-Ahs4m", "reduce.wrong.event.type %s", "foo.bar"),
},
{
name: "level error",
e: &SetEvent[bool]{
BaseEvent: &eventstore.BaseEvent{
EventType: "feature.foo.bar.something",
},
},
wantErr: zerrors.ThrowInternalf(nil, "FEAT-Boo2i", "reduce.wrong.event.type %s", "feature.foo.bar.something"),
},
{
name: "key error",
e: &SetEvent[bool]{
BaseEvent: &eventstore.BaseEvent{
EventType: "feature.system.bar.something",
},
},
wantErr: zerrors.ThrowInternalf(nil, "FEAT-eir0M", "reduce.wrong.event.type %s", "feature.system.bar.something"),
},
{
name: "success",
e: &SetEvent[bool]{
BaseEvent: &eventstore.BaseEvent{
EventType: SystemLoginDefaultOrgEventType,
},
},
want: feature.LevelSystem,
want1: feature.KeyLoginDefaultOrg,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := tt.e.FeatureInfo()
require.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.want1, got1)
})
}
}