You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
3.4 KiB

  1. package handler_test
  2. import (
  3. "net/http/httptest"
  4. "net/url"
  5. "strconv"
  6. "testing"
  7. "git.hoogi.eu/snafu/go-blog/handler"
  8. "git.hoogi.eu/snafu/go-blog/models"
  9. )
  10. func TestCategoryWorkflow(t *testing.T) {
  11. setup(t)
  12. defer teardown()
  13. c := &models.Category{
  14. Name: "My Category",
  15. }
  16. id, err := doAdminCategoryNewRequest(rAdminUser, c)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. rcvCategory, err := doAdminGetCategoryRequest(rAdminUser, id)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if rcvCategory.Name != c.Name {
  25. t.Fatalf("the category name is wrong. expected: %s, actual: %s", c.Name, rcvCategory.Name)
  26. }
  27. c.ID = id
  28. c.Name = "Updated Category"
  29. err = doAdminCategoryEditRequest(rAdminUser, c)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. rcvCategory, err = doAdminGetCategoryRequest(rAdminUser, id)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if rcvCategory.Name != c.Name {
  38. t.Fatalf("the category name is wrong. expected: %s, actual: %s", c.Name, rcvCategory.Name)
  39. }
  40. err = doAdminDeleteCategoryRequest(rAdminUser, id)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. rcvCategory, err = doAdminGetCategoryRequest(rAdminUser, id)
  45. if err == nil {
  46. t.Fatalf("removed category, but got a category %v", rcvCategory)
  47. }
  48. }
  49. func doAdminGetCategoryRequest(user reqUser, categoryID int) (*models.Category, error) {
  50. r := request{
  51. url: "/admin/category/" + strconv.Itoa(categoryID),
  52. user: user,
  53. method: "GET",
  54. pathVar: []pathVar{
  55. pathVar{
  56. key: "categoryID",
  57. value: strconv.Itoa(categoryID),
  58. },
  59. },
  60. }
  61. rw := httptest.NewRecorder()
  62. tpl := handler.AdminGetCategoryHandler(ctx, rw, r.buildRequest())
  63. if tpl.Err != nil {
  64. return nil, tpl.Err
  65. }
  66. return tpl.Data["category"].(*models.Category), nil
  67. }
  68. func doAdminListCategoriesRequest(user reqUser) ([]models.Category, error) {
  69. r := request{
  70. url: "/admin/categories/",
  71. user: user,
  72. method: "GET",
  73. }
  74. rw := httptest.NewRecorder()
  75. tpl := handler.AdminListCategoriesHandler(ctx, rw, r.buildRequest())
  76. if tpl.Err != nil {
  77. return nil, tpl.Err
  78. }
  79. return tpl.Data["categories"].([]models.Category), nil
  80. }
  81. func doAdminCategoryNewRequest(user reqUser, c *models.Category) (int, error) {
  82. values := url.Values{}
  83. addValue(values, "name", c.Name)
  84. r := request{
  85. url: "/admin/category/new",
  86. user: user,
  87. method: "POST",
  88. values: values,
  89. }
  90. rw := httptest.NewRecorder()
  91. tpl := handler.AdminCategoryNewPostHandler(ctx, rw, r.buildRequest())
  92. if tpl.Err != nil {
  93. return -1, tpl.Err
  94. }
  95. return tpl.Data["categoryID"].(int), nil
  96. }
  97. func doAdminCategoryEditRequest(user reqUser, c *models.Category) error {
  98. values := url.Values{}
  99. addValue(values, "name", c.Name)
  100. r := request{
  101. url: "/admin/category/edit/" + strconv.Itoa(c.ID),
  102. user: user,
  103. method: "POST",
  104. values: values,
  105. pathVar: []pathVar{
  106. pathVar{
  107. key: "categoryID",
  108. value: strconv.Itoa(c.ID),
  109. },
  110. },
  111. }
  112. rw := httptest.NewRecorder()
  113. tpl := handler.AdminCategoryEditPostHandler(ctx, rw, r.buildRequest())
  114. if tpl.Err != nil {
  115. return tpl.Err
  116. }
  117. return nil
  118. }
  119. func doAdminDeleteCategoryRequest(user reqUser, categoryID int) error {
  120. r := request{
  121. url: "/admin/category/" + strconv.Itoa(categoryID),
  122. user: user,
  123. method: "GET",
  124. pathVar: []pathVar{
  125. pathVar{
  126. key: "categoryID",
  127. value: strconv.Itoa(categoryID),
  128. },
  129. },
  130. }
  131. rw := httptest.NewRecorder()
  132. tpl := handler.AdminCategoryDeletePostHandler(ctx, rw, r.buildRequest())
  133. if tpl.Err != nil {
  134. return tpl.Err
  135. }
  136. return nil
  137. }