integration_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package example_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "strings"
  10. "testing"
  11. "github.com/qor5/admin/seo"
  12. "github.com/qor5/admin/media/oss"
  13. "github.com/qor5/admin/pagebuilder"
  14. "github.com/qor5/admin/pagebuilder/example"
  15. "github.com/qor5/admin/presets"
  16. "github.com/qor5/admin/presets/gorm2op"
  17. "github.com/qor5/admin/publish"
  18. publish_view "github.com/qor5/admin/publish/views"
  19. "github.com/qor5/x/perm"
  20. "github.com/theplant/gofixtures"
  21. "gorm.io/driver/postgres"
  22. "gorm.io/gorm"
  23. )
  24. func ConnectDB() *gorm.DB {
  25. db, err := gorm.Open(postgres.Open(os.Getenv("DBURL")), &gorm.Config{})
  26. if err != nil {
  27. panic(err)
  28. }
  29. return db.Debug()
  30. }
  31. func TestEditor(t *testing.T) {
  32. db := ConnectDB()
  33. b := presets.New().DataOperator(gorm2op.DataOperator(db)).URIPrefix("/admin")
  34. pb := example.ConfigPageBuilder(db, "/page_builder", "", b.I18n())
  35. sdb, _ := db.DB()
  36. gofixtures.Data(
  37. gofixtures.Sql(`
  38. INSERT INTO page_builder_pages (id, version, locale_code, title, slug) VALUES (1, 'v1','International', '123', '123');
  39. INSERT INTO page_builder_containers ( page_id, page_version,locale_code, model_name, model_id, display_order) VALUES ( 1, 'v1','International', 'Header', 1, 1);
  40. INSERT INTO container_headers (color) VALUES ('black');
  41. `, []string{"page_builder_pages", "page_builder_containers", "container_headers"}),
  42. ).TruncatePut(sdb)
  43. r := httptest.NewRequest("GET", "/page_builder/editors/1?version=v1&locale=International", nil)
  44. w := httptest.NewRecorder()
  45. pb.ServeHTTP(w, r)
  46. if strings.Index(w.Body.String(), "headers") < 0 {
  47. t.Error(w.Body.String())
  48. }
  49. _, err := pb.AddContainerToPage(1, "v1", "International", "Header")
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. }
  54. func TestUpdatePage(t *testing.T) {
  55. db := ConnectDB()
  56. pb := presets.New().DataOperator(gorm2op.DataOperator(db)).URIPrefix("/admin").
  57. Permission(
  58. perm.New().Policies(
  59. perm.PolicyFor("root").WhoAre(perm.Allowed).ToDo(presets.PermCreate, presets.PermUpdate, presets.PermDelete, presets.PermGet, presets.PermList).On("*"),
  60. ).SubjectsFunc(func(r *http.Request) []string {
  61. return []string{"root"}
  62. }),
  63. )
  64. pageBuilder := example.ConfigPageBuilder(db, "", "", pb.I18n())
  65. publisher := publish.New(db, oss.Storage)
  66. mb := pageBuilder.Configure(pb, db, nil, nil, publisher, seo.NewCollection())
  67. publish_view.Configure(pb, db, nil, publisher, mb)
  68. // _ = publisher
  69. pb.Model(&pagebuilder.Page{})
  70. sdb, _ := db.DB()
  71. gofixtures.Data(
  72. gofixtures.Sql(`
  73. INSERT INTO page_builder_pages (id,version, title, slug) VALUES (1,'v1', '123', '123');
  74. `, []string{"page_builder_pages"}),
  75. ).TruncatePut(sdb)
  76. body := bytes.NewBuffer(nil)
  77. mw := multipart.NewWriter(body)
  78. _ = mw.WriteField("__event_data__", `{"eventFuncId":{"id":"presets_Update","params":["1"],"pushState":null},"event":{}}`)
  79. _ = mw.Close()
  80. r := httptest.NewRequest("POST", "/admin/pages?__execute_event__=presets_Update", body)
  81. r.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", mw.Boundary()))
  82. w := httptest.NewRecorder()
  83. pb.ServeHTTP(w, r)
  84. }