Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a middleware to verify the project exists during a request #680

Merged
merged 6 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix test and server
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
  • Loading branch information
Nexucis committed Oct 24, 2022
commit 696b7ed34fd70f8d1016b09d53e08c64bef6b1f4
46 changes: 19 additions & 27 deletions internal/api/e2e/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package e2e

import (
"fmt"
"net/http"
"testing"

"github.com/gavv/httpexpect/v2"
e2eframework "github.com/perses/perses/internal/api/e2e/framework"
"github.com/perses/perses/internal/api/shared"
"github.com/perses/perses/internal/api/shared/dependency"
"github.com/perses/perses/pkg/model/api"
)

Expand All @@ -30,36 +34,24 @@ func TestMainScenarioDatasource(t *testing.T) {
}

func TestCreateDatasourceWithEmptyProjectName(t *testing.T) {

dts := utils.NewDatasource(t)
dts.Metadata.Project = ""
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
e2eframework.WithServer(t, func(expect *httpexpect.Expect, manager dependency.PersistenceManager) []api.Entity {
entity := e2eframework.NewDatasource(t, "", "myDTS")
expect.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(entity).
Expect().
Status(http.StatusBadRequest)
return []api.Entity{}
})

// metadata.name is not provided, it should return a bad request
e.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(dts).
Expect().
Status(http.StatusBadRequest)
}

func TestCreateDatasourceWithNonExistingProject(t *testing.T) {
dts := utils.NewDatasource(t)
dts.Metadata.Project = "404NotFound"
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
e2eframework.WithServer(t, func(expect *httpexpect.Expect, manager dependency.PersistenceManager) []api.Entity {
entity := e2eframework.NewDatasource(t, "awesomeProjectThatDoesntExist", "myDTS")
// metadata.name is not provided, it should return a bad request
expect.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(entity).
Expect().
Status(http.StatusBadRequest)
return []api.Entity{}
})

// metadata.name is not provided, it should return a bad request
e.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(dts).
Expect().
Status(http.StatusBadRequest)
}
2 changes: 2 additions & 0 deletions internal/api/e2e/framework/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/perses/perses/internal/api/config"
"github.com/perses/perses/internal/api/core"
"github.com/perses/perses/internal/api/core/middleware"
"github.com/perses/perses/internal/api/shared/database"
"github.com/perses/perses/internal/api/shared/dependency"
"github.com/perses/perses/pkg/model/api"
Expand Down Expand Up @@ -76,6 +77,7 @@ func CreateServer(t *testing.T) (*httptest.Server, *httpexpect.Expect, dependenc
t.Fatal(err)
}
}
handler.Use(middleware.CheckProject(serviceManager.GetProject()))
persesAPI := core.NewPersesAPI(serviceManager, false)
persesAPI.RegisterRoute(handler)
server := httptest.NewServer(handler)
Expand Down