admin_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package seo
  2. import (
  3. "bytes"
  4. "errors"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "reflect"
  9. "testing"
  10. "github.com/qor5/admin/l10n"
  11. l10n_view "github.com/qor5/admin/l10n/views"
  12. "github.com/qor5/admin/presets"
  13. "gorm.io/gorm"
  14. )
  15. func TestAdmin(t *testing.T) {
  16. var (
  17. admin = presets.New().URIPrefix("/admin")
  18. server = httptest.NewServer(admin)
  19. )
  20. collection := NewCollection().SetSettingModel(&TestQorSEOSetting{}).RegisterSEOByNames("Product Detail", "Product")
  21. collection.Configure(admin, GlobalDB)
  22. l10n_view.Configure(admin, GlobalDB, l10n.New().RegisterLocales("en", "en", "English"), nil)
  23. // should create all seo setting in the first time
  24. resetDB()
  25. if req, err := http.Get(server.URL + "/admin/test-qor-seo-settings?__execute_event__=__reload__&locale=en"); err == nil {
  26. if req.StatusCode != 200 {
  27. t.Errorf("Setting page should be exist, status code is %v", req.StatusCode)
  28. }
  29. var seoSetting = collection.NewSettingModelSlice()
  30. err := GlobalDB.Find(seoSetting, "name in (?)", []string{"Product Detail", "Product", collection.globalName}).Error
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. t.Errorf("SEO Setting should be created successfully")
  33. }
  34. if reflect.Indirect(reflect.ValueOf(seoSetting)).Len() != 3 {
  35. t.Errorf("SEO Setting should be created successfully")
  36. }
  37. } else {
  38. t.Errorf(err.Error())
  39. }
  40. // save seo setting
  41. var (
  42. title = "title test"
  43. description = "description test"
  44. keyword = "keyword test"
  45. )
  46. var form = &bytes.Buffer{}
  47. mwriter := multipart.NewWriter(form)
  48. mwriter.WriteField("Product.Title", title)
  49. mwriter.WriteField("Product.Description", description)
  50. mwriter.WriteField("Product.Keywords", keyword)
  51. mwriter.Close()
  52. req, err := http.DefaultClient.Post(server.URL+"/admin/test-qor-seo-settings?__execute_event__=seo_save_collection&name=Product&locale=en", mwriter.FormDataContentType(), form)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if req.StatusCode != 200 {
  57. t.Errorf("Save should be processed successfully, status code is %v", req.StatusCode)
  58. }
  59. var seoSetting = collection.NewSettingModelInstance().(QorSEOSettingInterface)
  60. err = GlobalDB.First(&seoSetting, "name = ?", "Product").Error
  61. if errors.Is(err, gorm.ErrRecordNotFound) {
  62. t.Errorf("SEO Setting should be created successfully")
  63. }
  64. if seoSetting.GetSEOSetting().Title != title || seoSetting.GetSEOSetting().Description != description || seoSetting.GetSEOSetting().Keywords != keyword {
  65. t.Errorf("SEOSetting should be Save correctly, its value %#v", seoSetting)
  66. }
  67. }