mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:27:42 +00:00
Changes (#195)
* Changes added * Reading of events for applications changed. * Proto changed * Tests added * Added more tests. * Struct for Data expanded with additional fields. * refactoring * Changes from review. * Merge in to Master * Changes from review. * fix: generate proto Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
)
|
||||
|
||||
type Org struct {
|
||||
@@ -13,6 +14,18 @@ type Org struct {
|
||||
|
||||
Members []*OrgMember
|
||||
}
|
||||
type OrgChanges struct {
|
||||
Changes []*OrgChange
|
||||
LastSequence uint64
|
||||
}
|
||||
|
||||
type OrgChange struct {
|
||||
ChangeDate *timestamp.Timestamp `json:"changeDate,omitempty"`
|
||||
EventType string `json:"eventType,omitempty"`
|
||||
Sequence uint64 `json:"sequence,omitempty"`
|
||||
Modifier string `json:"modifierUser,omitempty"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type OrgState int32
|
||||
|
||||
|
@@ -2,13 +2,19 @@ package eventsourcing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||
"github.com/caos/zitadel/internal/id"
|
||||
org_model "github.com/caos/zitadel/internal/org/model"
|
||||
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
)
|
||||
|
||||
type OrgEventstore struct {
|
||||
@@ -132,6 +138,60 @@ func (es *OrgEventstore) ReactivateOrg(ctx context.Context, orgID string) (*org_
|
||||
return model.OrgToModel(org), nil
|
||||
}
|
||||
|
||||
func (es *OrgEventstore) OrgChanges(ctx context.Context, id string, lastSequence uint64, limit uint64) (*org_model.OrgChanges, error) {
|
||||
query := ChangesQuery(id, lastSequence)
|
||||
|
||||
events, err := es.Eventstore.FilterEvents(context.Background(), query)
|
||||
if err != nil {
|
||||
logging.Log("EVENT-ZRffs").WithError(err).Warn("eventstore unavailable")
|
||||
return nil, errors.ThrowInternal(err, "EVENT-328b1", "unable to get current user")
|
||||
}
|
||||
if len(events) == 0 {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "EVENT-FpQqK", "no objects found")
|
||||
}
|
||||
|
||||
result := make([]*org_model.OrgChange, 0)
|
||||
|
||||
for _, u := range events {
|
||||
creationDate, err := ptypes.TimestampProto(u.CreationDate)
|
||||
logging.Log("EVENT-qxIR7").OnError(err).Debug("unable to parse timestamp")
|
||||
change := &org_model.OrgChange{
|
||||
ChangeDate: creationDate,
|
||||
EventType: u.Type.String(),
|
||||
Modifier: u.EditorUser,
|
||||
Sequence: u.Sequence,
|
||||
}
|
||||
|
||||
orgDummy := model.Org{}
|
||||
if u.Data != nil {
|
||||
if err := json.Unmarshal(u.Data, &orgDummy); err != nil {
|
||||
log.Println("Error getting data!", err.Error())
|
||||
}
|
||||
}
|
||||
change.Data = orgDummy
|
||||
|
||||
result = append(result, change)
|
||||
if lastSequence < u.Sequence {
|
||||
lastSequence = u.Sequence
|
||||
}
|
||||
}
|
||||
|
||||
changes := &org_model.OrgChanges{
|
||||
Changes: result,
|
||||
LastSequence: lastSequence,
|
||||
}
|
||||
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
func ChangesQuery(orgID string, latestSequence uint64) *es_models.SearchQuery {
|
||||
query := es_models.NewSearchQuery().
|
||||
AggregateTypeFilter(model.OrgAggregate).
|
||||
LatestSequenceFilter(latestSequence).
|
||||
AggregateIDFilter(orgID)
|
||||
return query
|
||||
}
|
||||
|
||||
func (es *OrgEventstore) OrgMemberByIDs(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
||||
if member == nil || member.UserID == "" || member.AggregateID == "" {
|
||||
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ld93d", "member not set")
|
||||
|
@@ -0,0 +1,41 @@
|
||||
package eventsourcing
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caos/zitadel/internal/eventstore/mock"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||||
repo_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||||
"github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
func GetMockedEventstoreComplexity(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *OrgEventstore {
|
||||
return &OrgEventstore{
|
||||
Eventstore: mockEs,
|
||||
}
|
||||
}
|
||||
|
||||
func GetMockChangesOrgOK(ctrl *gomock.Controller) *OrgEventstore {
|
||||
org := model.Org{
|
||||
Name: "MusterOrg",
|
||||
Domain: "myDomain",
|
||||
}
|
||||
data, err := json.Marshal(org)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
events := []*es_models.Event{
|
||||
&es_models.Event{AggregateID: "AggregateIDApp", Sequence: 1, AggregateType: repo_model.OrgAggregate, Data: data},
|
||||
}
|
||||
mockEs := mock.NewMockEventstore(ctrl)
|
||||
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
||||
return GetMockedEventstoreComplexity(ctrl, mockEs)
|
||||
}
|
||||
|
||||
func GetMockChangesOrgNoEvents(ctrl *gomock.Controller) *OrgEventstore {
|
||||
events := []*es_models.Event{}
|
||||
mockEs := mock.NewMockEventstore(ctrl)
|
||||
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
||||
return GetMockedEventstoreComplexity(ctrl, mockEs)
|
||||
}
|
@@ -2,12 +2,15 @@ package eventsourcing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
||||
|
||||
"github.com/caos/zitadel/internal/api/auth"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
es_mock "github.com/caos/zitadel/internal/eventstore/mock"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
org_model "github.com/caos/zitadel/internal/org/model"
|
||||
@@ -1025,3 +1028,70 @@ func orgInactiveEvent() *es_models.Event {
|
||||
Type: model.OrgDeactivated,
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangesOrg(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *OrgEventstore
|
||||
id string
|
||||
lastSequence uint64
|
||||
limit uint64
|
||||
}
|
||||
type res struct {
|
||||
changes *org_model.OrgChanges
|
||||
org *model.Org
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "changes from events, ok",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
id: "1",
|
||||
lastSequence: 0,
|
||||
limit: 0,
|
||||
},
|
||||
res: res{
|
||||
changes: &org_model.OrgChanges{Changes: []*org_model.OrgChange{&org_model.OrgChange{EventType: "", Sequence: 1, Modifier: ""}}, LastSequence: 1},
|
||||
org: &model.Org{Name: "MusterOrg", Domain: "myDomain"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "changes from events, no events",
|
||||
args: args{
|
||||
es: GetMockChangesOrgNoEvents(ctrl),
|
||||
id: "2",
|
||||
lastSequence: 0,
|
||||
limit: 0,
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := tt.args.es.OrgChanges(nil, tt.args.id, tt.args.lastSequence, tt.args.limit)
|
||||
|
||||
org := &model.Org{}
|
||||
if result != nil && len(result.Changes) > 0 {
|
||||
b, err := json.Marshal(result.Changes[0].Data)
|
||||
json.Unmarshal(b, org)
|
||||
if err != nil {
|
||||
}
|
||||
}
|
||||
if !tt.res.wantErr && result.LastSequence != tt.res.changes.LastSequence && org.Name != tt.res.org.Name {
|
||||
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.res.changes.LastSequence, result.LastSequence)
|
||||
}
|
||||
if tt.res.wantErr && !tt.res.errFunc(err) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user