Files
opencloud/services/web/pkg/apps/apps_test.go
Florian Schade 6814c61506 [full-ci] enhancement: allow ocis to provide custom web applications (#8523)
* enhancement: allow ocis to provide custom web applications

* enhancement: add an option to disable web apps

* test: add default logger tests

* test: add app loading tests

* test: add asset server tests

* enhancement: make use of dedicated app conf file and app asset paths

* enhancement: adjust asset locations and deprecate WEB_ASSET_PATH

* enhancement: get rid of default logger and use the service level logger instead

* Apply suggestions from code review

Co-authored-by: Benedikt Kulmann <benedikt@kulmann.biz>
Co-authored-by: kobergj <juliankoberg@googlemail.com>

* enhancement: use basename as app id

* Apply suggestions from code review

Co-authored-by: Martin <github@diemattels.at>

* enhancement: use afero as fs abstraction

* enhancement: simplify logo upload

* enhancement: make use of introductionVersion field annotations

---------

Co-authored-by: Benedikt Kulmann <benedikt@kulmann.biz>
Co-authored-by: kobergj <juliankoberg@googlemail.com>
Co-authored-by: Martin <github@diemattels.at>
2024-03-05 14:11:18 +01:00

182 lines
5.3 KiB
Go

package apps_test
import (
"io/fs"
"testing"
"testing/fstest"
"github.com/onsi/gomega"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/web/pkg/apps"
"github.com/owncloud/ocis/v2/services/web/pkg/config"
)
func TestApplication_ToExternal(t *testing.T) {
g := gomega.NewWithT(t)
app := apps.Application{
ID: "app",
Entrypoint: "entrypoint.js",
Config: map[string]interface{}{
"foo": "bar",
},
}
externalApp := app.ToExternal("path")
g.Expect(externalApp.ID).To(gomega.Equal("app"))
g.Expect(externalApp.Path).To(gomega.Equal("path/entrypoint.js"))
g.Expect(externalApp.Config).To(gomega.Equal(app.Config))
}
func TestBuild(t *testing.T) {
g := gomega.NewWithT(t)
dir := &fstest.MapFile{
Mode: fs.ModeDir,
}
_, err := apps.Build(fstest.MapFS{
"app": &fstest.MapFile{},
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrInvalidApp))
_, err = apps.Build(fstest.MapFS{
"app": dir,
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrMissingManifest))
_, err = apps.Build(fstest.MapFS{
"app": dir,
"app/manifest.json": dir,
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrInvalidManifest))
_, err = apps.Build(fstest.MapFS{
"app": dir,
"app/manifest.json": &fstest.MapFile{
Data: []byte("{}"),
},
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrInvalidManifest))
_, err = apps.Build(fstest.MapFS{
"app": dir,
"app/entrypoint.js": &fstest.MapFile{},
"app/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app", "entrypoint":"entrypoint.js"}`),
},
}, "app", map[string]any{})
g.Expect(err).ToNot(gomega.HaveOccurred())
_, err = apps.Build(fstest.MapFS{
"app": dir,
"app/entrypoint.js": dir,
"app/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app", "entrypoint":"entrypoint.js"}`),
},
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrEntrypointDoesNotExist))
_, err = apps.Build(fstest.MapFS{
"app": dir,
"app/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app", "entrypoint":"entrypoint.js"}`),
},
}, "app", map[string]any{})
g.Expect(err).To(gomega.MatchError(apps.ErrEntrypointDoesNotExist))
application, err := apps.Build(fstest.MapFS{
"app": dir,
"app/entrypoint.js": &fstest.MapFile{},
"app/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app", "entrypoint":"entrypoint.js", "config": {"foo": "1", "bar": "2"}}`),
},
}, "app", map[string]any{"foo": "overwritten-1", "baz": "injected-1"})
g.Expect(err).ToNot(gomega.HaveOccurred())
g.Expect(application.Entrypoint).To(gomega.Equal("app/entrypoint.js"))
g.Expect(application.Config).To(gomega.Equal(map[string]interface{}{
"foo": "overwritten-1", "baz": "injected-1", "bar": "2",
}))
}
func TestList(t *testing.T) {
g := gomega.NewWithT(t)
applications := apps.List(log.NopLogger(), map[string]config.App{})
g.Expect(applications).To(gomega.BeEmpty())
applications = apps.List(log.NopLogger(), map[string]config.App{}, nil)
g.Expect(applications).To(gomega.BeEmpty())
applications = apps.List(log.NopLogger(), map[string]config.App{}, fstest.MapFS{})
g.Expect(applications).To(gomega.BeEmpty())
dir := &fstest.MapFile{
Mode: fs.ModeDir,
}
applications = apps.List(log.NopLogger(), map[string]config.App{
"app": {
Disabled: true,
},
}, fstest.MapFS{
"app": dir,
})
g.Expect(applications).To(gomega.BeEmpty())
applications = apps.List(log.NopLogger(), map[string]config.App{
"app": {},
}, fstest.MapFS{
"app": dir,
})
g.Expect(applications).To(gomega.BeEmpty())
applications = apps.List(log.NopLogger(), map[string]config.App{
"app-3": {
Config: map[string]any{
"foo": "local conf 1",
"bar": "local conf 2",
},
},
}, fstest.MapFS{
"app-1": dir,
"app-1/entrypoint.js": &fstest.MapFile{},
"app-1/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app-1", "entrypoint":"entrypoint.js", "config": {"foo": "fs1"}}`),
},
"app-2": dir,
"app-2/entrypoint.js": &fstest.MapFile{},
"app-2/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app-2", "entrypoint":"entrypoint.js", "config": {"foo": "fs1"}}`),
},
}, fstest.MapFS{
"app-1": dir,
"app-1/entrypoint.js": &fstest.MapFile{},
"app-1/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app-1", "entrypoint":"entrypoint.js", "config": {"foo": "fs2"}}`),
},
"app-3": dir,
"app-3/entrypoint.js": &fstest.MapFile{},
"app-3/manifest.json": &fstest.MapFile{
Data: []byte(`{"id":"app-3", "entrypoint":"entrypoint.js", "config": {"foo": "fs2"}}`),
},
})
g.Expect(len(applications)).To(gomega.Equal(3))
for _, application := range applications {
switch {
case application.Entrypoint == "app-1/entrypoint.js":
g.Expect(application.Config["foo"]).To(gomega.Equal("fs2"))
case application.Entrypoint == "app-2/entrypoint.js":
g.Expect(application.Config["foo"]).To(gomega.Equal("fs1"))
case application.Entrypoint == "app-3/entrypoint.js":
g.Expect(application.Config["foo"]).To(gomega.Equal("local conf 1"))
g.Expect(application.Config["bar"]).To(gomega.Equal("local conf 2"))
default:
t.Fatalf("unexpected application %s", application.Entrypoint)
}
}
}