example_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package integration_test
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "strings"
  11. "testing"
  12. "github.com/qor5/admin/presets"
  13. "github.com/qor5/admin/presets/actions"
  14. examples2 "github.com/qor5/admin/presets/examples"
  15. "github.com/qor5/web/multipartestutils"
  16. "github.com/theplant/gofixtures"
  17. "gorm.io/driver/postgres"
  18. "gorm.io/gorm"
  19. )
  20. var customerData = gofixtures.Data(gofixtures.Sql(`
  21. insert into customers (id, name) values (11, 'Felix1');
  22. `, []string{"customers"}))
  23. var productData = gofixtures.Data(gofixtures.Sql(`
  24. insert into preset_products (id, name) values (12, 'Product 1');
  25. `, []string{"preset_products"}))
  26. var emptyCustomerData = gofixtures.Data(gofixtures.Sql(``, []string{"customers"}))
  27. var creditCardData = gofixtures.Data(customerData, gofixtures.Sql(``, []string{"credit_cards"}))
  28. type reqCase struct {
  29. name string
  30. reqFunc func(db *sql.DB) *http.Request
  31. eventResponseMatch func(er *testEventResponse, db *gorm.DB, t *testing.T)
  32. pageMatch func(body *bytes.Buffer, db *gorm.DB, t *testing.T)
  33. }
  34. var cases = []reqCase{
  35. {
  36. name: "Update",
  37. reqFunc: func(db *sql.DB) *http.Request {
  38. customerData.TruncatePut(db)
  39. return multipartestutils.NewMultipartBuilder().
  40. PageURL("/admin/my_customers").
  41. EventFunc(actions.Update).
  42. Query(presets.ParamID, "11").
  43. AddField("Bool1", "true").
  44. AddField("ID", "11").
  45. AddField("Int1", "42").
  46. AddField("Name", "Felix11").
  47. BuildEventFuncRequest()
  48. },
  49. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  50. var u = &examples2.Customer{}
  51. err := db.Find(u, 11).Error
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. if u.Name != "Felix11" {
  56. t.Error(u)
  57. }
  58. return
  59. },
  60. },
  61. {
  62. name: "Create",
  63. reqFunc: func(db *sql.DB) *http.Request {
  64. emptyCustomerData.TruncatePut(db)
  65. return multipartestutils.NewMultipartBuilder().
  66. PageURL("/admin/my_customers").
  67. EventFunc(actions.Update).
  68. AddField("Bool1", "true").
  69. AddField("ID", "").
  70. AddField("Int1", "42").
  71. AddField("Name", "Felix").
  72. BuildEventFuncRequest()
  73. },
  74. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  75. var u = &examples2.Customer{}
  76. err := db.First(u).Error
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. if u.Name != "Felix" {
  81. t.Error(u)
  82. }
  83. return
  84. },
  85. },
  86. {
  87. name: "New Form For Creating",
  88. reqFunc: func(db *sql.DB) *http.Request {
  89. emptyCustomerData.TruncatePut(db)
  90. return multipartestutils.NewMultipartBuilder().
  91. PageURL("/admin/credit-cards").
  92. EventFunc(actions.New).
  93. BuildEventFuncRequest()
  94. },
  95. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  96. partial := er.UpdatePortals[0].Body
  97. if strings.Index(partial, `field-name='[plaidForm, "Number"]'`) < 0 {
  98. t.Error(`can't find field-name='[plaidForm, "Number"]'`, partial)
  99. }
  100. return
  101. },
  102. },
  103. {
  104. name: "Create CreditCard",
  105. reqFunc: func(db *sql.DB) *http.Request {
  106. creditCardData.TruncatePut(db)
  107. return multipartestutils.NewMultipartBuilder().
  108. PageURL("/admin/credit-cards").
  109. EventFunc(actions.Update).
  110. Query("customerID", "11").
  111. AddField("Number", "12345678").
  112. BuildEventFuncRequest()
  113. },
  114. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  115. var u = &examples2.CreditCard{}
  116. err := db.First(u).Error
  117. if err != nil {
  118. t.Error(err)
  119. }
  120. if u.Number != "12345678" {
  121. t.Error(u)
  122. }
  123. return
  124. },
  125. },
  126. {
  127. name: "Without Editing Config/Product Edit Form",
  128. reqFunc: func(db *sql.DB) *http.Request {
  129. productData.TruncatePut(db)
  130. return multipartestutils.NewMultipartBuilder().
  131. PageURL("/admin/products").
  132. EventFunc(actions.Edit).
  133. Query(presets.ParamID, "12").
  134. BuildEventFuncRequest()
  135. },
  136. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  137. partial := er.UpdatePortals[0].Body
  138. if strings.Index(partial, `field-name='[plaidForm, "OwnerName"]'`) < 0 {
  139. t.Error(`can't find field-name='[plaidForm, "OwnerName"]'`, partial)
  140. }
  141. return
  142. },
  143. },
  144. {
  145. name: "Without Editing Config/Create Product",
  146. reqFunc: func(db *sql.DB) *http.Request {
  147. productData.TruncatePut(db)
  148. return multipartestutils.NewMultipartBuilder().
  149. PageURL("/admin/products").
  150. EventFunc(actions.Update).
  151. Query(presets.ParamID, "12").
  152. AddField("OwnerName", "owner1").
  153. BuildEventFuncRequest()
  154. },
  155. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  156. var u = &examples2.Product{}
  157. err := db.First(u).Error
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. if u.OwnerName != "owner1" {
  162. t.Error(u)
  163. }
  164. return
  165. },
  166. },
  167. {
  168. name: "formDrawerAction AgreeTerms",
  169. reqFunc: func(db *sql.DB) *http.Request {
  170. customerData.TruncatePut(db)
  171. return multipartestutils.NewMultipartBuilder().
  172. PageURL("/admin/my_customers/11").
  173. EventFunc(actions.Action).
  174. Query(presets.ParamAction, "AgreeTerms").
  175. Query(presets.ParamID, "11").
  176. BuildEventFuncRequest()
  177. },
  178. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  179. partial := er.UpdatePortals[0].Body
  180. if strings.Index(partial, `v-field-name='[plaidForm, "Agree"]'`) < 0 {
  181. t.Error(`can't find v-field-name='[plaidForm, "Agree"]'`, partial)
  182. }
  183. return
  184. },
  185. },
  186. {
  187. name: "doAction AgreeTerms",
  188. reqFunc: func(db *sql.DB) *http.Request {
  189. customerData.TruncatePut(db)
  190. return multipartestutils.NewMultipartBuilder().
  191. PageURL("/admin/my_customers/11").
  192. EventFunc(actions.DoAction).
  193. Query(presets.ParamAction, "AgreeTerms").
  194. Query(presets.ParamID, "11").
  195. AddField("Agree", "true").
  196. BuildEventFuncRequest()
  197. },
  198. eventResponseMatch: func(er *testEventResponse, db *gorm.DB, t *testing.T) {
  199. var u = &examples2.Customer{}
  200. err := db.First(u).Error
  201. if err != nil {
  202. t.Error(err)
  203. }
  204. if u.TermAgreedAt == nil {
  205. t.Error(fmt.Sprintf("%#+v", u))
  206. }
  207. return
  208. },
  209. },
  210. }
  211. func ConnectDB() *gorm.DB {
  212. db, err := gorm.Open(postgres.Open(os.Getenv("DBURL")), &gorm.Config{})
  213. if err != nil {
  214. panic(err)
  215. }
  216. return db.Debug()
  217. }
  218. type testPortalUpdate struct {
  219. Name string `json:"name,omitempty"`
  220. Body string `json:"body,omitempty"`
  221. AfterLoaded string `json:"afterLoaded,omitempty"`
  222. }
  223. type testEventResponse struct {
  224. PageTitle string `json:"pageTitle,omitempty"`
  225. Body string `json:"body,omitempty"`
  226. Reload bool `json:"reload,omitempty"`
  227. ReloadPortals []string `json:"reloadPortals,omitempty"`
  228. UpdatePortals []*testPortalUpdate `json:"updatePortals,omitempty"`
  229. Data interface{} `json:"data,omitempty"`
  230. }
  231. func TestAll(t *testing.T) {
  232. db := ConnectDB()
  233. p := examples2.Preset1(db)
  234. for _, c := range cases {
  235. t.Run(c.name, func(t *testing.T) {
  236. w := httptest.NewRecorder()
  237. dbraw, _ := db.DB()
  238. r := c.reqFunc(dbraw)
  239. p.ServeHTTP(w, r)
  240. if c.eventResponseMatch != nil {
  241. var er testEventResponse
  242. err := json.NewDecoder(w.Body).Decode(&er)
  243. if err != nil {
  244. t.Fatalf("%s for: %#+v", err, w.Body.String())
  245. }
  246. c.eventResponseMatch(&er, db, t)
  247. }
  248. if c.pageMatch != nil {
  249. c.pageMatch(w.Body, db, t)
  250. }
  251. })
  252. }
  253. }