Skip to content

Commit

Permalink
test pubsub messages
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Feb 8, 2024
1 parent adaa562 commit e708b92
Showing 1 changed file with 73 additions and 20 deletions.
93 changes: 73 additions & 20 deletions internal/graph/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/nais/api/internal/logger"
"github.com/nais/api/internal/slug"
"github.com/nais/api/internal/usersync"
"github.com/nais/api/pkg/protoapi"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -109,11 +110,9 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
Return(nil).
Once()

_, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
psServer, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
defer closer()

// TODO: check message sent to the pubsub topic

auditLogger := auditlogger.NewAuditLoggerForTesting()
returnedTeam, err := graph.
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, psClient.Topic("topic-id"), log).
Expand All @@ -123,15 +122,46 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
Purpose: " some purpose ",
SlackChannel: slackChannel,
})
assert.NoError(t, err)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if createdTeam.Slug != returnedTeam.Slug {
t.Errorf("expected team slug %q, got %q", createdTeam.Slug, returnedTeam.Slug)
}

if len(auditLogger.Entries()) != 1 {
t.Fatalf("expected 1 audit log entry, got %d", len(auditLogger.Entries()))
}

assert.Equal(t, createdTeam.Slug, returnedTeam.Slug)
assert.Len(t, auditLogger.Entries(), 1)
entry := auditLogger.Entries()[0]
assert.Equal(t, ctx, entry.Context)
assert.Equal(t, string(createdTeam.Slug), entry.Targets[0].Identifier)
assert.Equal(t, user, entry.Fields.Actor.User)
assert.Equal(t, "Team created", entry.Message)

if ctx != entry.Context {
t.Errorf("incorrect context in audit log entry")
}

if string(createdTeam.Slug) != entry.Targets[0].Identifier {
t.Errorf("expected team slug %q, got %q", createdTeam.Slug, entry.Targets[0].Identifier)
}

if user != entry.Fields.Actor.User {
t.Errorf("incorrect actor in audit log entry")
}

if expected := "Team created"; entry.Message != expected {
t.Errorf("expected message %q, got %q", expected, entry.Message)
}

psMessages := psServer.Messages()
if len(psMessages) != 1 {
t.Fatalf("expected 1 message, got %d", len(psMessages))
}

msg := psMessages[0]
if msg.Attributes["EventType"] != protoapi.EventTypes_EVENT_TEAM_UPDATED.String() {
t.Errorf("expected event type %s, got %s", protoapi.EventTypes_EVENT_TEAM_UPDATED.String(), msg.Attributes["EventType"])

}
})

t.Run("calling with SA, adds sa as team owner", func(t *testing.T) {
Expand Down Expand Up @@ -160,11 +190,9 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
Return(nil).
Once()

_, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
psServer, psClient, closer := getPubsubServerAndClient(ctx, "some-id", "topic-id")
defer closer()

// TODO: check message sent to the pubsub topic

auditLogger := auditlogger.NewAuditLoggerForTesting()
returnedTeam, err := graph.
NewResolver(nil, nil, nil, nil, db, tenantDomain, userSync, auditLogger, nil, userSyncRuns, psClient.Topic("topic-id"), log).
Expand All @@ -174,14 +202,39 @@ func TestMutationResolver_CreateTeam(t *testing.T) {
SlackChannel: slackChannel,
})

assert.NoError(t, err)
assert.Equal(t, createdTeam.Slug, returnedTeam.Slug)
assert.Len(t, auditLogger.Entries(), 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if createdTeam.Slug != returnedTeam.Slug {
t.Errorf("expected team slug %q, got %q", createdTeam.Slug, returnedTeam.Slug)
}

if len(auditLogger.Entries()) != 1 {
t.Fatalf("expected 1 audit log entry, got %d", len(auditLogger.Entries()))
}

entry := auditLogger.Entries()[0]
assert.Equal(t, saCtx, entry.Context)
assert.Equal(t, string(createdTeam.Slug), entry.Targets[0].Identifier)
assert.Equal(t, serviceAccount, entry.Fields.Actor.User)
assert.Equal(t, "Team created", entry.Message)
if saCtx != entry.Context {
t.Errorf("incorrect context in audit log entry")
}

if string(createdTeam.Slug) != entry.Targets[0].Identifier {
t.Errorf("expected team slug %q, got %q", createdTeam.Slug, entry.Targets[0].Identifier)
}

if serviceAccount != entry.Fields.Actor.User {
t.Errorf("incorrect actor in audit log entry")
}

if expected := "Team created"; entry.Message != expected {
t.Errorf("expected message %q, got %q", expected, entry.Message)
}

psMessages := psServer.Messages()
if len(psMessages) != 1 {
t.Fatalf("expected 1 message, got %d", len(psMessages))
}
})
}

Expand Down

0 comments on commit e708b92

Please sign in to comment.