Micro-service for file storage and processing written in Go
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.

144 lines
4.5 KiB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/geplauder/lithium/controllers"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/bxcodec/faker/v3"
  11. "github.com/geplauder/lithium/pipelines"
  12. "github.com/geplauder/lithium/settings"
  13. "github.com/geplauder/lithium/storage"
  14. "github.com/gorilla/mux"
  15. "github.com/spf13/afero"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestIndexRoute(t *testing.T) {
  19. data := pipelines.Pipeline{}
  20. err := faker.FakeData(&data)
  21. assert.Nil(t, err)
  22. t.Run("Index route returns valid response", func(t *testing.T) {
  23. request := httptest.NewRequest(http.MethodGet, "/", nil)
  24. responseRecorder := httptest.NewRecorder()
  25. controllers.IndexHandler([]pipelines.IPipeline{}, responseRecorder, request, "")
  26. assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200")
  27. assert.NotNil(t, responseRecorder.Body, "Response should contain body")
  28. })
  29. t.Run("Index route returns valid amount of pipelines", func(t *testing.T) {
  30. request := httptest.NewRequest(http.MethodGet, "/", nil)
  31. responseRecorder := httptest.NewRecorder()
  32. controllers.IndexHandler([]pipelines.IPipeline{data}, responseRecorder, request, "")
  33. assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200")
  34. var body = controllers.Metadata{}
  35. err = json.Unmarshal(responseRecorder.Body.Bytes(), &body)
  36. assert.Nil(t, err)
  37. assert.Equal(t, 1, len(body.Pipelines))
  38. assert.Equal(t, data.Slug, body.Pipelines[0])
  39. })
  40. }
  41. func TestEndpointRoute(t *testing.T) {
  42. data := pipelines.Pipeline{}
  43. err := faker.FakeData(&data)
  44. assert.Nil(t, err)
  45. t.Run("Registered pipelines are valid routes", func(t *testing.T) {
  46. router := mux.NewRouter()
  47. fs := storage.GetMemoryStorageProvider()
  48. appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
  49. RegisterRoutes(router, appSettings, []pipelines.IPipeline{data}, fs)
  50. request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
  51. responseRecorder := httptest.NewRecorder()
  52. router.ServeHTTP(responseRecorder, request)
  53. assert.Equal(t, 200, responseRecorder.Code)
  54. body, _ := json.Marshal(data)
  55. assert.JSONEq(t, string(body), responseRecorder.Body.String())
  56. })
  57. t.Run("Unregistered pipelines return 404", func(t *testing.T) {
  58. router := mux.NewRouter()
  59. request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil)
  60. responseRecorder := httptest.NewRecorder()
  61. router.ServeHTTP(responseRecorder, request)
  62. assert.Equal(t, 404, responseRecorder.Code)
  63. })
  64. }
  65. func TestUploadRoute(t *testing.T) {
  66. t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
  67. router := mux.NewRouter()
  68. fs := storage.GetMemoryStorageProvider()
  69. appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
  70. RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{
  71. Name: "",
  72. Slug: "",
  73. Type: 0,
  74. RemoveMetadata: false,
  75. Steps: []pipelines.Step{},
  76. Output: struct {
  77. Format string `json:"format"`
  78. Quality int `json:"quality"`
  79. }{"jpeg", 10},
  80. }}, fs)
  81. request, _ := http.NewRequest("POST", "/upload", nil)
  82. request.Header["Content-Type"] = []string{"multipart/form-data"}
  83. responseRecorder := httptest.NewRecorder()
  84. router.ServeHTTP(responseRecorder, request)
  85. assert.Equal(t, 0x1A6, responseRecorder.Code)
  86. str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5" +
  87. "vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==")
  88. assert.JSONEq(t, string(str), responseRecorder.Body.String())
  89. })
  90. t.Run("Test uploads missing multipart boundary", func(t *testing.T) {
  91. router := mux.NewRouter()
  92. fs := storage.GetMemoryStorageProvider()
  93. appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
  94. RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{
  95. Name: "",
  96. Slug: "",
  97. Type: 0,
  98. RemoveMetadata: false,
  99. Steps: []pipelines.Step{},
  100. Output: struct {
  101. Format string `json:"format"`
  102. Quality int `json:"quality"`
  103. }{"jpeg", 10},
  104. }}, fs)
  105. request, _ := http.NewRequest("POST", "/upload", nil)
  106. request.Header["Content-Type"] = []string{"multipart/form-data", "boundary=X-INSOMNIA-BOUNDARY"}
  107. responseRecorder := httptest.NewRecorder()
  108. router.ServeHTTP(responseRecorder, request)
  109. assert.Equal(t, 0x1A6, responseRecorder.Code)
  110. str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==")
  111. assert.JSONEq(t, string(str), responseRecorder.Body.String())
  112. fmt.Println(responseRecorder.Body.String())
  113. })
  114. }