3
0

Add compose integ. tests for module, ns, page & chart

This commit is contained in:
Denis Arh
2019-09-10 20:44:34 +02:00
parent 230f2a4c4a
commit 6027caedbc
8 changed files with 642 additions and 117 deletions
-23
View File
@@ -1,23 +0,0 @@
// +build integration
package repository
import (
"os"
"testing"
"github.com/titpetric/factory"
dbLogger "github.com/titpetric/factory/logger"
"github.com/cortezaproject/corteza-server/pkg/logger"
)
func TestMain(m *testing.M) {
logger.SetDefault(logger.MakeDebugLogger())
factory.Database.Add("compose", os.Getenv("COMPOSE_DB_DSN"))
db := factory.Database.MustGet("compose")
db.SetLogger(dbLogger.Default{})
os.Exit(m.Run())
}
-55
View File
@@ -1,55 +0,0 @@
// +build integration
package repository
import (
"context"
"testing"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/internal/test"
"github.com/titpetric/factory"
)
func TestModule_updateFields(t *testing.T) {
tx(t, func(ctx context.Context, db *factory.DB, ns *types.Namespace) (err error) {
var (
m *types.Module
repo = Module(ctx, db)
)
m, err = repo.Create(&types.Module{
NamespaceID: ns.ID,
Name: "test-module",
})
test.NoError(t, err, "unexpected error on module creation")
test.Assert(t, len(m.Fields) == 0, "unexpected fields found in the fresh module")
m, err = repo.Create(&types.Module{
NamespaceID: ns.ID,
Name: "test-module",
Fields: types.ModuleFieldSet{
&types.ModuleField{Name: "one"},
&types.ModuleField{Name: "two"},
},
})
test.NoError(t, err, "unexpected error on module creation")
test.Assert(t, len(m.Fields) == 2, "expecting to find two fields in the new module")
m.Fields[0].Name = "one-should-not-be-renamed"
m.Fields[1] = &types.ModuleField{Name: "three"}
m, err = repo.Update(m)
test.NoError(t, err, "unexpected error on module update")
test.Assert(t, len(m.Fields) == 2, "expecting to find two fields in the new module")
test.Assert(t, m.Fields[0].Name == "one", "expecting to find field 'one', got %q", m.Fields[0].Name)
test.Assert(t, m.Fields[0].Place == 0, "expecting Place=0")
test.Assert(t, m.Fields[1].Name == "three", "expecting to find field 'three'")
test.Assert(t, m.Fields[1].Place == 1, "expecting Place=1")
return
})
}
-39
View File
@@ -1,39 +0,0 @@
// +build integration
package repository
import (
"context"
"testing"
"github.com/titpetric/factory"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/internal/test"
)
func TestRepository(t *testing.T) {
repo := &repository{}
repo.With(context.Background(), nil)
}
func tx(t *testing.T, f func(context.Context, *factory.DB, *types.Namespace) error) {
var (
err error
ctx = context.Background()
db = DB(ctx)
ns *types.Namespace
)
err = db.Begin()
test.Assert(t, err == nil, "Could not begin transaction: %+v", err)
ns, err = Namespace(ctx, db).Create(&types.Namespace{})
test.Assert(t, err == nil, "Test transaction setup (namespace creation) resulted in an error: %+v", err)
err = f(ctx, db, ns)
test.Assert(t, err == nil, "Test transaction resulted in an error: %+v", err)
err = db.Quiet().Rollback()
test.Assert(t, err == nil, "Could not rollback transaction: %+v", err)
}
+165
View File
@@ -0,0 +1,165 @@
package compose
import (
"context"
"fmt"
"net/http"
"testing"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
"github.com/cortezaproject/corteza-server/compose/repository"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/tests/helpers"
)
func (h helper) repoChart() repository.ChartRepository {
return repository.Chart(context.Background(), db())
}
func (h helper) repoMakeChart(ns *types.Namespace, name string) *types.Chart {
m, err := h.
repoChart().
Create(&types.Chart{Name: name, NamespaceID: ns.ID})
h.a.NoError(err)
return m
}
func TestChartRead(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ChartPermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeChart(ns, "some-chart")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/chart/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
Assert(jsonpath.Equal(`$.response.name`, m.Name)).
Assert(jsonpath.Equal(`$.response.chartID`, fmt.Sprintf("%d", m.ID))).
End()
}
func TestChartList(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
h.repoMakeChart(ns, "app")
h.repoMakeChart(ns, "app")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/chart/", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestChartCreateForbidden(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/chart/", ns.ID)).
FormData("name", "some-chart").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoCreatePermissions")).
End()
}
func TestChartCreate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.NamespacePermissionResource.AppendWildcard(), "chart.create")
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/chart/", ns.ID)).
FormData("name", "some-chart").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestChartUpdateForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeChart(ns, "some-chart")
h.apiInit().
Debug().
Post(fmt.Sprintf("/namespace/%d/chart/%d", ns.ID, m.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoUpdatePermissions")).
End()
}
func TestChartUpdate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeChart(ns, "some-chart")
h.allow(types.ChartPermissionResource.AppendWildcard(), "update")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/chart/%d", ns.ID, m.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoChart().FindByID(ns.ID, m.ID)
h.a.NoError(err)
h.a.NotNil(m)
h.a.Equal(m.Name, "changed-name")
}
func TestChartDeleteForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ChartPermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeChart(ns, "some-chart")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/chart/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoDeletePermissions")).
End()
}
func TestChartDelete(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ChartPermissionResource.AppendWildcard(), "read")
h.allow(types.ChartPermissionResource.AppendWildcard(), "delete")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeChart(ns, "some-chart")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/chart/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoChart().FindByID(ns.ID, m.ID)
h.a.Error(err, "compose.repository.ChartNotFound")
}
+4
View File
@@ -46,6 +46,10 @@ var (
p = permissions.NewTestService()
)
func db() *factory.DB {
return factory.Database.MustGet("compose").With(context.Background())
}
func InitConfig() {
var err error
+165
View File
@@ -0,0 +1,165 @@
package compose
import (
"context"
"fmt"
"net/http"
"testing"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
"github.com/cortezaproject/corteza-server/compose/repository"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/tests/helpers"
)
func (h helper) repoModule() repository.ModuleRepository {
return repository.Module(context.Background(), db())
}
func (h helper) repoMakeModule(ns *types.Namespace, name string) *types.Module {
m, err := h.
repoModule().
Create(&types.Module{Name: name, NamespaceID: ns.ID})
h.a.NoError(err)
return m
}
func TestModuleRead(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ModulePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeModule(ns, "some-module")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
Assert(jsonpath.Equal(`$.response.name`, m.Name)).
Assert(jsonpath.Equal(`$.response.moduleID`, fmt.Sprintf("%d", m.ID))).
End()
}
func TestModuleList(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
h.repoMakeModule(ns, "app")
h.repoMakeModule(ns, "app")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/module/", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestModuleCreateForbidden(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/module/", ns.ID)).
FormData("name", "some-module").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoCreatePermissions")).
End()
}
func TestModuleCreate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.NamespacePermissionResource.AppendWildcard(), "module.create")
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/module/", ns.ID)).
FormData("name", "some-module").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestModuleUpdateForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeModule(ns, "some-module")
h.apiInit().
Debug().
Post(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoUpdatePermissions")).
End()
}
func TestModuleUpdate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeModule(ns, "some-module")
h.allow(types.ModulePermissionResource.AppendWildcard(), "update")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoModule().FindByID(ns.ID, m.ID)
h.a.NoError(err)
h.a.NotNil(m)
h.a.Equal("changed-name", m.Name)
}
func TestModuleDeleteForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ModulePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeModule(ns, "some-module")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoDeletePermissions")).
End()
}
func TestModuleDelete(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.ModulePermissionResource.AppendWildcard(), "read")
h.allow(types.ModulePermissionResource.AppendWildcard(), "delete")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakeModule(ns, "some-module")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoModule().FindByID(ns.ID, m.ID)
h.a.Error(err, "compose.repository.ModuleNotFound")
}
+143
View File
@@ -0,0 +1,143 @@
package compose
import (
"context"
"fmt"
"net/http"
"testing"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
"github.com/cortezaproject/corteza-server/compose/repository"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/tests/helpers"
)
func (h helper) repoNamespace() repository.NamespaceRepository {
return repository.Namespace(context.Background(), db())
}
func (h helper) repoMakeNamespace(name string) *types.Namespace {
ns, err := h.
repoNamespace().
Create(&types.Namespace{Name: name})
h.a.NoError(err)
return ns
}
func TestNamespaceRead(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
Assert(jsonpath.Equal(`$.response.name`, ns.Name)).
Assert(jsonpath.Equal(`$.response.namespaceID`, fmt.Sprintf("%d", ns.ID))).
End()
}
func TestNamespaceList(t *testing.T) {
h := newHelper(t)
h.repoMakeNamespace("app")
h.repoMakeNamespace("app")
h.apiInit().
Get("/namespace/").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestNamespaceCreateForbidden(t *testing.T) {
h := newHelper(t)
h.apiInit().
Post("/namespace/").
FormData("name", "some-namespace").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoCreatePermissions")).
End()
}
func TestNamespaceCreate(t *testing.T) {
h := newHelper(t)
h.allow(types.ComposePermissionResource, "namespace.create")
h.apiInit().
Post("/namespace/").
FormData("name", "some-namespace").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestNamespaceUpdateForbidden(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Debug().
Post(fmt.Sprintf("/namespace/%d", ns.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoUpdatePermissions")).
End()
}
func TestNamespaceUpdate(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.allow(types.NamespacePermissionResource.AppendWildcard(), "update")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d", ns.ID)).
FormData("name", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
ns, err := h.repoNamespace().FindByID(ns.ID)
h.a.NoError(err)
h.a.NotNil(ns)
h.a.Equal("changed-name", ns.Name)
}
func TestNamespaceDeleteForbidden(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoDeletePermissions")).
End()
}
func TestNamespaceDelete(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "delete")
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
ns, err := h.repoNamespace().FindByID(ns.ID)
h.a.Error(err, "compose.repository.NamespaceNotFound")
}
+165
View File
@@ -0,0 +1,165 @@
package compose
import (
"context"
"fmt"
"net/http"
"testing"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
"github.com/cortezaproject/corteza-server/compose/repository"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/tests/helpers"
)
func (h helper) repoPage() repository.PageRepository {
return repository.Page(context.Background(), db())
}
func (h helper) repoMakePage(ns *types.Namespace, name string) *types.Page {
m, err := h.
repoPage().
Create(&types.Page{Title: name, NamespaceID: ns.ID})
h.a.NoError(err)
return m
}
func TestPageRead(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.PagePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakePage(ns, "some-page")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/page/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
Assert(jsonpath.Equal(`$.response.title`, m.Title)).
Assert(jsonpath.Equal(`$.response.pageID`, fmt.Sprintf("%d", m.ID))).
End()
}
func TestPageList(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
h.repoMakePage(ns, "app")
h.repoMakePage(ns, "app")
h.apiInit().
Get(fmt.Sprintf("/namespace/%d/page/", ns.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestPageCreateForbidden(t *testing.T) {
h := newHelper(t)
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/page/", ns.ID)).
FormData("title", "some-page").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoCreatePermissions")).
End()
}
func TestPageCreate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.NamespacePermissionResource.AppendWildcard(), "page.create")
ns := h.repoMakeNamespace("some-namespace")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/page/", ns.ID)).
FormData("title", "some-page").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
}
func TestPageUpdateForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakePage(ns, "some-page")
h.apiInit().
Debug().
Post(fmt.Sprintf("/namespace/%d/page/%d", ns.ID, m.ID)).
FormData("title", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoUpdatePermissions")).
End()
}
func TestPageUpdate(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakePage(ns, "some-page")
h.allow(types.PagePermissionResource.AppendWildcard(), "update")
h.apiInit().
Post(fmt.Sprintf("/namespace/%d/page/%d", ns.ID, m.ID)).
FormData("title", "changed-name").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoPage().FindByID(ns.ID, m.ID)
h.a.NoError(err)
h.a.NotNil(m)
h.a.Equal("changed-name", m.Title)
}
func TestPageDeleteForbidden(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.PagePermissionResource.AppendWildcard(), "read")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakePage(ns, "some-page")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/page/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("compose.service.NoDeletePermissions")).
End()
}
func TestPageDelete(t *testing.T) {
h := newHelper(t)
h.allow(types.NamespacePermissionResource.AppendWildcard(), "read")
h.allow(types.PagePermissionResource.AppendWildcard(), "read")
h.allow(types.PagePermissionResource.AppendWildcard(), "delete")
ns := h.repoMakeNamespace("some-namespace")
m := h.repoMakePage(ns, "some-page")
h.apiInit().
Delete(fmt.Sprintf("/namespace/%d/page/%d", ns.ID, m.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
m, err := h.repoPage().FindByID(ns.ID, m.ID)
h.a.Error(err, "compose.repository.PageNotFound")
}