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.

149 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
  1. package handler_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "strconv"
  7. "testing"
  8. "git.hoogi.eu/snafu/go-blog/handler"
  9. "git.hoogi.eu/snafu/go-blog/models"
  10. )
  11. func TestFileWorkflow(t *testing.T) {
  12. err := doAdminUploadFileRequest(rAdminUser, "testdata/color.png")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. files, err := doAdminListFilesRequest(rAdminUser)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. if len(files) != 1 {
  21. t.Fatalf("one file is uploaded; but list files returns %d file(s)", len(files))
  22. }
  23. rr, err := doAdminGetFileRequest(rGuest, files[0].UniqueName)
  24. if err != nil {
  25. t.Error(err)
  26. }
  27. if rr.Result().ContentLength != 1610 {
  28. t.Errorf("expected 1610 bytes, but got %d", rr.Result().ContentLength)
  29. }
  30. if rr.Result().Header.Get("Content-Type") != "image/png" {
  31. t.Errorf("expected image/png content type, but got %s", rr.Result().Header.Get("Content-Type"))
  32. }
  33. err = doAdminFileDeleteRequest(rAdminUser, files[0].ID)
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. _, err = doAdminGetFileRequest(rGuest, files[0].UniqueName)
  38. if err == nil {
  39. t.Errorf("file should be removed, but file is there %s", files[0].UniqueName)
  40. }
  41. }
  42. func doAdminListFilesRequest(user reqUser) ([]models.File, error) {
  43. r := request{
  44. url: "/admin/files",
  45. user: user,
  46. method: "GET",
  47. }
  48. rw := httptest.NewRecorder()
  49. tpl := handler.AdminListFilesHandler(ctx, rw, r.buildRequest())
  50. if tpl.Err != nil {
  51. return nil, tpl.Err
  52. }
  53. return tpl.Data["files"].([]models.File), nil
  54. }
  55. func doAdminGetFileRequest(user reqUser, uniquename string) (*httptest.ResponseRecorder, error) {
  56. r := request{
  57. url: "/file/" + uniquename,
  58. user: user,
  59. method: "GET",
  60. pathVar: []pathVar{
  61. pathVar{
  62. key: "uniquename",
  63. value: uniquename,
  64. },
  65. },
  66. }
  67. rw := httptest.NewRecorder()
  68. fh := handler.FileHandler{
  69. Context: ctx,
  70. }
  71. fh.FileGetHandler(rw, r.buildRequest())
  72. if rw.Result().StatusCode != http.StatusOK {
  73. return rw, fmt.Errorf("got an invalid status code during file request /file/%s , code: %d, message %s", uniquename, rw.Result().StatusCode, rw.Result().Status)
  74. }
  75. return rw, nil
  76. }
  77. func doAdminUploadFileRequest(user reqUser, file string) error {
  78. mp := []multipartRequest{
  79. multipartRequest{
  80. key: "file",
  81. file: file,
  82. },
  83. }
  84. r := request{
  85. url: "/admin/file/upload",
  86. user: user,
  87. method: "POST",
  88. multipartReq: mp,
  89. }
  90. rw := httptest.NewRecorder()
  91. tpl := handler.AdminUploadFilePostHandler(ctx, rw, r.buildRequest())
  92. if tpl.Err != nil {
  93. return tpl.Err
  94. }
  95. return nil
  96. }
  97. func doAdminFileDeleteRequest(user reqUser, fileID int) error {
  98. r := request{
  99. url: "/admin/file/delete/" + strconv.Itoa(fileID),
  100. user: user,
  101. method: "GET",
  102. pathVar: []pathVar{
  103. pathVar{
  104. key: "fileID",
  105. value: strconv.Itoa(fileID),
  106. },
  107. },
  108. }
  109. rw := httptest.NewRecorder()
  110. tpl := handler.AdminUploadDeletePostHandler(ctx, rw, r.buildRequest())
  111. if tpl.Err != nil {
  112. return tpl.Err
  113. }
  114. return nil
  115. }