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
Merge branch 'main' into nexucis/project-middleware
  • Loading branch information
Nexucis committed Oct 24, 2022
commit b38fd768cbf928cbf421d1b066f55e435905c41d
201 changes: 1 addition & 200 deletions internal/api/e2e/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,10 @@ func TestMainScenarioDatasource(t *testing.T) {
e2eframework.MainTestScenarioWithProject(t, shared.PathDatasource, func(projectName string, name string) (api.Entity, api.Entity) {
return e2eframework.NewProject(projectName), e2eframework.NewDatasource(t, projectName, name)
})
// perform the POST request, no error should occur at this point
e.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(entity).
Expect().
Status(http.StatusOK)

// check the document exists in the db
_, err := persistenceManager.GetDatasource().Get(entity.Metadata.Project, entity.Metadata.Name)
assert.NoError(t, err)
utils.ClearAllKeys(t, persistenceManager.GetPersesDAO(), entity.GenerateID())
}

func TestCreateDatasourceWithConflict(t *testing.T) {
entity := utils.NewDatasource(t)

server, persistenceManager := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})
utils.CreateAndWaitUntilEntityExists(t, persistenceManager, entity)

// recall the same endpoint, it should now return a conflict error
e.POST(fmt.Sprintf("%s/%s", shared.APIV1Prefix, shared.PathDatasource)).
WithJSON(entity).
Expect().
Status(http.StatusConflict)

utils.ClearAllKeys(t, persistenceManager.GetPersesDAO(), entity.GenerateID())
}

func TestCreateDatasourceBadRequest(t *testing.T) {
dts := &v1.Datasource{Kind: v1.KindDatasource}

server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

// 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 TestCreateDatasourceWithEmptyProjectName(t *testing.T) {

dts := utils.NewDatasource(t)
dts.Metadata.Project = ""
server, _ := utils.CreateServer(t)
Expand Down Expand Up @@ -109,156 +63,3 @@ func TestCreateDatasourceWithNonExistingProject(t *testing.T) {
Expect().
Status(http.StatusBadRequest)
}

func TestUpdateDatasource(t *testing.T) {
entity := utils.NewDatasource(t)

server, persistenceManager := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})
utils.CreateAndWaitUntilEntityExists(t, persistenceManager, entity)

// call now the update endpoint, shouldn't return an error

o := e.PUT(fmt.Sprintf("%s/%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource, entity.Metadata.Name)).
WithJSON(entity).
Expect().
Status(http.StatusOK).
JSON().Raw()

// To be able to compare the result, an easy is to convert the map returned by the test framework.
// So for that we have to first marshal again the data
raw, err := json.Marshal(o)
if err != nil {
t.Fatal(err)
}
result := &v1.Datasource{}
if err := json.Unmarshal(raw, result); err != nil {
t.Fatal(err)
}

// for the moment the only thing to test is that the dates are correctly updated
assert.True(t, result.Metadata.CreatedAt.UnixNano() < result.Metadata.UpdatedAt.UnixNano())
assert.True(t, result.Metadata.Version > 0)

// check the document exists in the db
_, err = persistenceManager.GetDatasource().Get(entity.Metadata.Project, entity.Metadata.Name)
assert.NoError(t, err)

utils.ClearAllKeys(t, persistenceManager.GetPersesDAO(), entity.GenerateID())
}

func TestUpdateDatasourceNotFound(t *testing.T) {
entity := utils.NewDatasource(t)
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

e.PUT(fmt.Sprintf("%s/%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource, entity.Metadata.Name)).
WithJSON(entity).
Expect().
Status(http.StatusNotFound)
}

func TestUpdateDatasourceBadRequest(t *testing.T) {
entity := utils.NewDatasource(t)
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

// the name in the metadata and the name in the path doesn't match, it should return a bad request
e.PUT(fmt.Sprintf("%s/%s/%s/%s/otherProject", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource)).
WithJSON(entity).
Expect().
Status(http.StatusBadRequest)
}

func TestGetDatasource(t *testing.T) {
entity := utils.NewDatasource(t)
server, persistenceManager := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})
utils.CreateAndWaitUntilEntityExists(t, persistenceManager, entity)

e.GET(fmt.Sprintf("%s/%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource, entity.Metadata.Name)).
Expect().
Status(http.StatusOK)

utils.ClearAllKeys(t, persistenceManager.GetPersesDAO(), entity.GenerateID())
}

func TestGetDatasourceNotFound(t *testing.T) {
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

e.GET(fmt.Sprintf("%s/%s/perses/%s/otherProject", shared.APIV1Prefix, shared.PathProject, shared.PathDatasource)).
Expect().
Status(http.StatusNotFound)
}

func TestDeleteDatasource(t *testing.T) {
entity := utils.NewDatasource(t)
server, persistenceManager := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})
utils.CreateAndWaitUntilEntityExists(t, persistenceManager, entity)

e.DELETE(fmt.Sprintf("%s/%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource, entity.Metadata.Name)).
Expect().
Status(http.StatusNoContent)

e.GET(fmt.Sprintf("%s/%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource, entity.Metadata.Name)).
Expect().
Status(http.StatusNotFound)
}

func TestDeleteDatasourceNotFound(t *testing.T) {
server, _ := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

e.DELETE(fmt.Sprintf("%s/%s/perses/%s/otherProject", shared.APIV1Prefix, shared.PathProject, shared.PathDatasource)).
Expect().
Status(http.StatusNotFound)
}

func TestListDatasource(t *testing.T) {
entity := utils.NewDatasource(t)
server, persistenceManager := utils.CreateServer(t)
defer server.Close()
e := httpexpect.WithConfig(httpexpect.Config{
BaseURL: server.URL,
Reporter: httpexpect.NewAssertReporter(t),
})

if err := persistenceManager.GetDatasource().Update(entity); err != nil {
t.Fatal(err)
}

e.GET(fmt.Sprintf("%s/%s/%s/%s", shared.APIV1Prefix, shared.PathProject, entity.Metadata.Project, shared.PathDatasource)).
Expect().
Status(http.StatusOK)
utils.ClearAllKeys(t, persistenceManager.GetPersesDAO(), entity.GenerateID())
}
47 changes: 0 additions & 47 deletions internal/api/e2e/framework/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"testing"
"time"

"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"
v1 "github.com/perses/perses/pkg/model/api/v1"
Expand Down Expand Up @@ -185,45 +180,3 @@ func NewGlobalDatasource(t *testing.T, name string) *v1.GlobalDatasource {
entity.Metadata.CreateNow()
return entity
}

func defaultFileConfig() *config.File {
return &config.File{
Folder: "./test",
FileExtension: config.JSONExtension,
}
}

func CreateServer(t *testing.T) (*httptest.Server, dependency.PersistenceManager) {
projectPathByte, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
t.Fatal(err)
}
projectPath := strings.TrimSpace(string(projectPathByte))
handler := echo.New()
conf := config.Config{
Database: config.Database{
File: defaultFileConfig(),
},
Schemas: config.Schemas{
PanelsPath: filepath.Join(projectPath, config.DefaultPanelsPath),
QueriesPath: filepath.Join(projectPath, config.DefaultQueriesPath),
DatasourcesPath: filepath.Join(projectPath, config.DefaultDatasourcesPath),
Interval: 0,
},
}
persistenceManager, err := dependency.NewPersistenceManager(conf.Database)
if err != nil {
t.Fatal(err)
}
serviceManager := dependency.NewServiceManager(persistenceManager, conf)
// Load every cue schemas
for _, loader := range serviceManager.GetSchemas().GetLoaders() {
if err := loader.Load(); err != nil {
t.Fatal(err)
}
}
handler.Use(middleware.CheckProject(serviceManager.GetProject()))
persesAPI := core.NewPersesAPI(serviceManager, false)
persesAPI.RegisterRoute(handler)
return httptest.NewServer(handler), persistenceManager
}
2 changes: 1 addition & 1 deletion internal/api/shared/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Dashboard(entity *modelV1.Dashboard, sch schemas.Schemas) error {
}

func Datasource[T modelV1.DatasourceInterface](entity T, list []T, sch schemas.Schemas) error {
plugin := entity.GetSpec().Plugin
plugin := entity.GetDTSSpec().Plugin
if _, err := http.ValidateAndExtract(plugin.Spec); err != nil {
return err
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.