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.

143 lines
4.4 KiB

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