Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #405 from navikt/story-trim-naisteam
Browse files Browse the repository at this point in the history
Trim nais-team prefix in recreate story
  • Loading branch information
vebjorre authored Aug 13, 2024
2 parents 50416c2 + 597436c commit 29d8d74
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ define install-binary
fi
endef

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

STATICCHECK ?= $(shell command -v staticcheck || echo "$(GOBIN)/staticcheck")
STATICCHECK_VERSION := v0.4.6
SQLC ?= $(shell command -v sqlc || echo "$(GOBIN)/sqlc")
Expand Down
9 changes: 5 additions & 4 deletions pkg/service/core/service_story.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/navikt/nada-backend/pkg/auth"
"github.com/navikt/nada-backend/pkg/errs"
"github.com/navikt/nada-backend/pkg/service"
)
Expand Down Expand Up @@ -37,8 +38,8 @@ func (s *storyService) AppendStoryFiles(ctx context.Context, id uuid.UUID, creat
return errs.E(op, err)
}

if story.Group != creatorEmail {
return errs.E(errs.Unauthorized, op, errs.UserName(creatorEmail), fmt.Errorf("user not in the group of the data story: %s", story.Group))
if auth.TrimNaisTeamPrefix(story.Group) != creatorEmail {
return errs.E(errs.Unauthorized, op, errs.UserName(creatorEmail), fmt.Errorf("user %s not in the group of the data story: %s", creatorEmail, story.Group))
}

err = s.storyAPI.WriteFilesToBucket(ctx, id.String(), files, false)
Expand All @@ -57,8 +58,8 @@ func (s *storyService) RecreateStoryFiles(ctx context.Context, id uuid.UUID, cre
return errs.E(op, err)
}

if story.Group != creatorEmail {
return errs.E(errs.Unauthorized, op, errs.UserName(creatorEmail), fmt.Errorf("user not in the group of the data story: %s", story.Group))
if auth.TrimNaisTeamPrefix(story.Group) != creatorEmail {
return errs.E(errs.Unauthorized, op, errs.UserName(creatorEmail), fmt.Errorf("user %s not in the group of the data story: %s", creatorEmail, story.Group))
}

_, err = s.storyAPI.DeleteObjectsWithPrefix(ctx, id.String())
Expand Down
42 changes: 42 additions & 0 deletions test/integration/story_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,48 @@ func TestStory(t *testing.T) {
}
})

t.Run("Recreate story files with token and nais-team prefix", func(t *testing.T) {
storage := postgres.NewStoryStorage(repo)

updateStory, err := storage.CreateStory(context.Background(), "[email protected]", &service.NewStory{
Name: "My update story",
Description: strToStrPtr("This is my update story, and it is pretty bad"),
Keywords: []string{"story", "bad"},
Group: "[email protected]",
})
assert.NoError(t, err)

files := map[string]string{
"index.html": defaultHtml,
"subpage/index.html": "<html><h1>Subpage</h1></html>",
"subsubsubpage/something.html": "<html><h1>Subsubsubpage</h1></html>",
}

req := CreateMultipartFormRequest(
t,
http.MethodPut,
server.URL+"/quarto/update/"+updateStory.ID.String(),
files,
nil,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
},
)

NewTester(t, server).
Send(req).
HasStatusCode(http.StatusNoContent)

for path, content := range files {
got := NewTester(t, server).
Get("/quarto/" + updateStory.ID.String() + "/" + path).
HasStatusCode(http.StatusOK).
Body()

assert.Equal(t, content, got)
}
})

t.Run("Append story files with token", func(t *testing.T) {
files := map[string]string{
"newpage/test.html": "<html><h1>New page</h1></html>",
Expand Down

0 comments on commit 29d8d74

Please sign in to comment.