mirror of
https://github.com/documize/community.git
synced 2025-07-18 20:59:43 +02:00
Move over to Go embed directive
This commit is contained in:
parent
cddba799f8
commit
470e2d3ecf
18 changed files with 148 additions and 25205 deletions
|
@ -13,15 +13,17 @@ package mail
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/mail"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/setting"
|
||||
ds "github.com/documize/community/domain/smtp"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Mailer provides emailing facilities
|
||||
|
@ -43,15 +45,15 @@ func (m *Mailer) Initialize() {
|
|||
func (m *Mailer) ParseTemplate(filename string, params interface{}) (html string, err error) {
|
||||
html = ""
|
||||
|
||||
file, err := web.ReadFile(filename)
|
||||
content, _, err := asset.FetchStatic(m.Runtime.Assets, filename)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("missing %s", filename))
|
||||
m.Runtime.Log.Error("failed to load mail template", err)
|
||||
return
|
||||
}
|
||||
|
||||
emailTemplate := string(file)
|
||||
buffer := new(bytes.Buffer)
|
||||
|
||||
t := template.Must(template.New("emailTemplate").Parse(emailTemplate))
|
||||
t := template.Must(template.New("emailTemplate").Parse(content))
|
||||
t.Execute(buffer, ¶ms)
|
||||
|
||||
html = buffer.String()
|
||||
|
|
|
@ -20,6 +20,7 @@ import (
|
|||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/documize/community/core/asset"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/response"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
|
@ -28,7 +29,6 @@ import (
|
|||
"github.com/documize/community/domain/store"
|
||||
om "github.com/documize/community/model/onboard"
|
||||
"github.com/documize/community/model/permission"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
|
@ -112,14 +112,14 @@ func (h *Handler) loadFile(data om.SampleData, filename string, v interface{}) {
|
|||
|
||||
// Reads file and unmarshals content as JSON.
|
||||
func (h *Handler) unpackFile(filename string, v interface{}) (err error) {
|
||||
data, err := web.Embed.Asset("bindata/onboard/" + filename)
|
||||
content, _, err := asset.FetchStatic(h.Runtime.Assets, "onboard/"+filename)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("missing %s", filename))
|
||||
h.Runtime.Log.Error("failed to load file", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &v)
|
||||
err = json.Unmarshal([]byte(content), &v)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("failed to read %s as JSON", filename))
|
||||
h.Runtime.Log.Error("failed to load file", err)
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
package organization
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/model/org"
|
||||
|
||||
"github.com/documize/community/domain/test"
|
||||
)
|
||||
|
||||
// TestSpace tests all space database operations.
|
||||
|
||||
func TestOrganization(t *testing.T) {
|
||||
rt, s, ctx := test.SetupTest()
|
||||
//Create a new organization
|
||||
var err error
|
||||
org := org.Organization{}
|
||||
orgID := uniqueid.Generate()
|
||||
|
||||
t.Run("AddOrginization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
org.RefID = orgID
|
||||
org.Company = "test"
|
||||
org.Title = "test"
|
||||
org.Message = "test"
|
||||
org.Domain = "testDomain"
|
||||
org.Active = true
|
||||
|
||||
err = s.Organization.AddOrganization(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get org organization")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetOrganizationByDomain", func(t *testing.T) {
|
||||
orgGot, err := s.Organization.GetOrganizationByDomain("testDomain")
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get org organization by domain")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpdateOrginization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
org.Title = "testUpdate"
|
||||
|
||||
err = s.Organization.UpdateOrganization(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || org.Title != orgGot.Title {
|
||||
t.Error("failed to get updated org organization")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("CheckDomain", func(t *testing.T) {
|
||||
Domain := s.Organization.CheckDomain(ctx, "")
|
||||
if Domain != Domain {
|
||||
t.Error("failed to CheckDomain")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("UpdateAuthConfig", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = s.Organization.UpdateAuthConfig(ctx, org)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update organization AuthConfig")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
//
|
||||
//Run after everything except delete as this makes an org inactive
|
||||
//
|
||||
|
||||
t.Run("RemoveOrganization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = s.Organization.RemoveOrganization(ctx, org.RefID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to remove organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
orgGot, err := s.Organization.GetOrganization(ctx, org.RefID)
|
||||
if err != nil || orgGot.Active != false {
|
||||
t.Error("failed to get removed organization activity")
|
||||
}
|
||||
})
|
||||
|
||||
//
|
||||
// teardown code goes here
|
||||
//
|
||||
|
||||
t.Run("DeleteOrganization", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_,
|
||||
err = s.Organization.DeleteOrganization(ctx, orgID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete org organization")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
}
|
|
@ -1,234 +0,0 @@
|
|||
package space
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/domain/test"
|
||||
"github.com/documize/community/model/space"
|
||||
)
|
||||
|
||||
// TestSpace tests all space database operations.
|
||||
func TestSpace(t *testing.T) {
|
||||
rt, s, ctx := test.SetupTest()
|
||||
spaceID := uniqueid.Generate()
|
||||
spaceID2 := uniqueid.Generate()
|
||||
sp := space.Space{}
|
||||
sp2 := space.Space{}
|
||||
r := space.Role{}
|
||||
r2 := space.Role{}
|
||||
r3 := space.Role{}
|
||||
var err error
|
||||
|
||||
t.Run("Add Space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sp.RefID = spaceID
|
||||
sp.OrgID = ctx.OrgID
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Type = space.ScopePublic
|
||||
sp.Name = "PublicTestSpace"
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Created = time.Now().UTC()
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
err = s.Space.Add(ctx, sp)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add sp space")
|
||||
}
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.SpaceOwner, space.SpaceManage, space.SpaceView)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
spGet, err := s.Space.Get(ctx, sp.RefID)
|
||||
if err != nil || sp.Name != spGet.Name {
|
||||
t.Error("failed to get sp space")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Update Space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
sp, err := s.Space.Get(ctx, spaceID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to get space prior to update")
|
||||
return
|
||||
}
|
||||
|
||||
sp.Name = "test update"
|
||||
err = s.Space.Update(ctx, sp)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to update space")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
sp, err = s.Space.Get(ctx, spaceID)
|
||||
if err != nil || sp.Name != "test update" {
|
||||
t.Error("failed to get the space after update")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Get All", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
sp2.UserID = ctx.UserID
|
||||
sp2.RefID = spaceID2
|
||||
sp2.OrgID = ctx.OrgID
|
||||
sp2.Type = space.ScopePrivate
|
||||
sp2.Name = "PrivateTestSpace"
|
||||
sp.UserID = ctx.UserID
|
||||
sp.Created = time.Now().UTC()
|
||||
sp.Revised = time.Now().UTC()
|
||||
|
||||
err = s.Space.Add(ctx, sp2)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add sp2")
|
||||
}
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID2
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.SpaceOwner, space.SpaceManage, space.SpaceView)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
spSlice, err := s.Space.GetAll(ctx)
|
||||
if err != nil || spSlice == nil {
|
||||
t.Error("failed to get all spaces")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("PublicSpaces", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
spSlice, err := s.Space.PublicSpaces(ctx, sp.OrgID)
|
||||
if err != nil || spSlice == nil {
|
||||
t.Error("failed to get public spaces")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
t.Run("Change Owner", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
newUserID := "Updated Owner"
|
||||
|
||||
err := s.Space.ChangeOwner(ctx, sp.UserID, newUserID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to change Owner")
|
||||
return
|
||||
}
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
sp, err = s.Space.Get(ctx, spaceID)
|
||||
if err != nil || sp.UserID != newUserID {
|
||||
t.Error("failed to get space w/ new owner")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Add Role", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
perm := space.Permission{}
|
||||
perm.OrgID = ctx.OrgID
|
||||
perm.Who = "user"
|
||||
perm.WhoID = ctx.UserID
|
||||
perm.Scope = permission.ScopeRow
|
||||
perm.Location = permission.LocationSpace
|
||||
perm.RefID = spaceID
|
||||
perm.Action = "" // we send array for actions below
|
||||
|
||||
err = s.Space.AddPermissions(ctx, perm, space.DocumentAdd, space.DocumentDelete, space.DocumentMove)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to add permission")
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
roles, err := s.Space.GetUserPermissions(ctx, spaceID)
|
||||
if err != nil || roles == nil {
|
||||
t.Error("Could not get any roles")
|
||||
return
|
||||
}
|
||||
// TODO: could we Verify the role was added with the if r3.UserID == Returned.UserID?
|
||||
})
|
||||
|
||||
t.Run("Get User Permissions", func(t *testing.T) {
|
||||
userRoles, err := s.Space.GetUserPermissions(ctx, spaceID)
|
||||
if err != nil || userRoles == nil {
|
||||
t.Error("failed to get user roles")
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
// teardown
|
||||
t.Run("Delete space", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
_, err = s.Space.Delete(ctx, spaceID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete space")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
|
||||
t.Run("Delete space 2", func(t *testing.T) {
|
||||
ctx.Transaction, err = rt.Db.Beginx()
|
||||
|
||||
_, err = s.Space.Delete(ctx, spaceID2)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
t.Error("failed to delete space in teardown")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
})
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/documize/community/domain/store"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/edition/boot"
|
||||
"github.com/documize/community/edition/logging"
|
||||
"github.com/documize/community/embed"
|
||||
"github.com/documize/community/server/web"
|
||||
_ "github.com/go-sql-driver/mysql" // testing
|
||||
)
|
||||
|
||||
// SetupTest prepares test environment
|
||||
func SetupTest() (rt *env.Runtime, s *store
|
||||
.Store, ctx domain.RequestContext) {
|
||||
rt, s = startRuntime()
|
||||
ctx = setupContext()
|
||||
return rt, s, ctx
|
||||
}
|
||||
|
||||
func startRuntime() (rt *env.Runtime, s *store.Store) {
|
||||
rt = new(env.Runtime)
|
||||
s = new(store.Store)
|
||||
|
||||
rt.Log = logging.NewLogger(false)
|
||||
web.Embed = embed.NewEmbedder()
|
||||
|
||||
rt.Product = env.Product{}
|
||||
rt.Product.Major = "0"
|
||||
rt.Product.Minor = "0"
|
||||
rt.Product.Patch = "0"
|
||||
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
|
||||
rt.Product.Edition = "Test"
|
||||
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)
|
||||
|
||||
// parse settings from command line and environment
|
||||
rt.Flags = env.ParseFlags()
|
||||
boot.InitRuntime(rt, s)
|
||||
|
||||
// section.Register(rt, s)
|
||||
|
||||
return rt, s
|
||||
}
|
||||
|
||||
// setup testing context
|
||||
func setupContext() domain.RequestContext {
|
||||
ctx := domain.RequestContext{}
|
||||
ctx.AllowAnonymousAccess = true
|
||||
ctx.Authenticated = true
|
||||
ctx.Administrator = true
|
||||
ctx.Guest = false
|
||||
ctx.Editor = true
|
||||
ctx.GlobalAdmin = true
|
||||
ctx.UserID = "test"
|
||||
ctx.OrgID = "test"
|
||||
return ctx
|
||||
}
|
||||
|
||||
// For dummy user data https://www.mockaroo.com
|
Loading…
Add table
Add a link
Reference in a new issue