add_image_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package image
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "errors"
  7. "mime/multipart"
  8. "net/http/httptest"
  9. "strconv"
  10. "testing"
  11. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  16. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  17. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  18. "github.com/brianvoe/gofakeit/v6"
  19. "github.com/gofiber/fiber/v2"
  20. "github.com/gojuno/minimock/v3"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. // nolint:errcheck
  24. func Test_AddImageHandler(t *testing.T) {
  25. type req struct {
  26. method string
  27. route string
  28. contentType string
  29. body []byte
  30. }
  31. var (
  32. mc = minimock.NewController(t)
  33. placeID = gofakeit.Number(1, 1000)
  34. thingID = gofakeit.Number(1, 1000)
  35. testError = errors.New(gofakeit.Phrase())
  36. )
  37. // Correct request for adding place image
  38. addPlaceCorrectBody := &bytes.Buffer{}
  39. addPlaceCorrectWriter := multipart.NewWriter(addPlaceCorrectBody)
  40. addPlaceCorrectWriter.WriteField("place_id", strconv.Itoa(placeID))
  41. addPlaceCorrectWriter.CreateFormFile("files", gofakeit.Word())
  42. addPlaceCorrectContentType := addPlaceCorrectWriter.FormDataContentType()
  43. addPlaceCorrectWriter.Close()
  44. // Correct request for adding thing image
  45. addThingCorrectBody := &bytes.Buffer{}
  46. addThingCorrectWriter := multipart.NewWriter(addThingCorrectBody)
  47. addThingCorrectWriter.WriteField("thing_id", strconv.Itoa(thingID))
  48. addThingCorrectWriter.CreateFormFile("files", gofakeit.Word())
  49. addThingCorrectContentType := addThingCorrectWriter.FormDataContentType()
  50. addThingCorrectWriter.Close()
  51. // Incorrect request for adding place image
  52. addPlaceIncorrectBody := &bytes.Buffer{}
  53. addPlaceIncorrectWriter := multipart.NewWriter(addPlaceIncorrectBody)
  54. addPlaceIncorrectWriter.WriteField("place_id", gofakeit.Word())
  55. addPlaceIncorrectContentType := addPlaceIncorrectWriter.FormDataContentType()
  56. addPlaceIncorrectWriter.Close()
  57. // Incorrect request for adding thing image
  58. addThingIncorrectBody := &bytes.Buffer{}
  59. addThingIncorrectWriter := multipart.NewWriter(addThingIncorrectBody)
  60. addThingIncorrectWriter.WriteField("thing_id", gofakeit.Word())
  61. addThingIncorrectContentType := addThingIncorrectWriter.FormDataContentType()
  62. addThingIncorrectWriter.Close()
  63. // Incorrect empty request
  64. emptyBody := &bytes.Buffer{}
  65. emptyWriter := multipart.NewWriter(emptyBody)
  66. emptyContentType := emptyWriter.FormDataContentType()
  67. emptyWriter.Close()
  68. tests := []struct {
  69. name string
  70. req req
  71. resCode int
  72. resBody interface{}
  73. fileRepoMock func(mc *minimock.Controller) interfaces.FileRepository
  74. placeImageRepoMock func(mc *minimock.Controller) interfaces.PlaceImageRepository
  75. thingImageRepoMock func(mc *minimock.Controller) interfaces.ThingImageRepository
  76. }{
  77. {
  78. name: "negative case - bad request",
  79. req: req{
  80. method: fiber.MethodPost,
  81. route: "/v1/images",
  82. },
  83. resCode: fiber.StatusBadRequest,
  84. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  85. return repoMocks.NewPlaceImageRepositoryMock(mc)
  86. },
  87. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  88. return repoMocks.NewThingImageRepositoryMock(mc)
  89. },
  90. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  91. return repoMocks.NewFileRepositoryMock(mc)
  92. },
  93. },
  94. {
  95. name: "negative case - incorrect request",
  96. req: req{
  97. method: fiber.MethodPost,
  98. route: "/v1/images",
  99. body: addPlaceIncorrectBody.Bytes(),
  100. contentType: addPlaceIncorrectContentType,
  101. },
  102. resCode: fiber.StatusBadRequest,
  103. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  104. return repoMocks.NewPlaceImageRepositoryMock(mc)
  105. },
  106. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  107. return repoMocks.NewThingImageRepositoryMock(mc)
  108. },
  109. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  110. return repoMocks.NewFileRepositoryMock(mc)
  111. },
  112. },
  113. {
  114. name: "negative case - incorrect request",
  115. req: req{
  116. method: fiber.MethodPost,
  117. route: "/v1/images",
  118. body: addThingIncorrectBody.Bytes(),
  119. contentType: addThingIncorrectContentType,
  120. },
  121. resCode: fiber.StatusBadRequest,
  122. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  123. return repoMocks.NewPlaceImageRepositoryMock(mc)
  124. },
  125. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  126. return repoMocks.NewThingImageRepositoryMock(mc)
  127. },
  128. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  129. return repoMocks.NewFileRepositoryMock(mc)
  130. },
  131. },
  132. {
  133. name: "negative case - empty request",
  134. req: req{
  135. method: fiber.MethodPost,
  136. route: "/v1/images",
  137. body: emptyBody.Bytes(),
  138. contentType: emptyContentType,
  139. },
  140. resCode: fiber.StatusBadRequest,
  141. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  142. return repoMocks.NewPlaceImageRepositoryMock(mc)
  143. },
  144. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  145. return repoMocks.NewThingImageRepositoryMock(mc)
  146. },
  147. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  148. return repoMocks.NewFileRepositoryMock(mc)
  149. },
  150. },
  151. {
  152. name: "negative case - repository error (begin tx)",
  153. req: req{
  154. method: fiber.MethodPost,
  155. route: "/v1/images",
  156. body: addPlaceCorrectBody.Bytes(),
  157. contentType: addPlaceCorrectContentType,
  158. },
  159. resCode: fiber.StatusInternalServerError,
  160. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  161. mock := repoMocks.NewPlaceImageRepositoryMock(mc)
  162. mock.BeginTxMock.Return(nil, testError)
  163. return mock
  164. },
  165. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  166. return repoMocks.NewThingImageRepositoryMock(mc)
  167. },
  168. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  169. mock := repoMocks.NewFileRepositoryMock(mc)
  170. mock.SaveMock.Return(nil)
  171. return mock
  172. },
  173. },
  174. {
  175. name: "negative case - repository error (commit tx)",
  176. req: req{
  177. method: fiber.MethodPost,
  178. route: "/v1/images",
  179. body: addPlaceCorrectBody.Bytes(),
  180. contentType: addPlaceCorrectContentType,
  181. },
  182. resCode: fiber.StatusInternalServerError,
  183. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  184. mock := repoMocks.NewPlaceImageRepositoryMock(mc)
  185. mock.BeginTxMock.Return(nil, nil)
  186. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) {
  187. assert.Equal(mc, placeID, req.PlaceID)
  188. }).Return(nil)
  189. mock.CommitTxMock.Return(testError)
  190. return mock
  191. },
  192. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  193. return repoMocks.NewThingImageRepositoryMock(mc)
  194. },
  195. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  196. mock := repoMocks.NewFileRepositoryMock(mc)
  197. mock.SaveMock.Return(nil)
  198. return mock
  199. },
  200. },
  201. {
  202. name: "negative case - repository error (add image)",
  203. req: req{
  204. method: fiber.MethodPost,
  205. route: "/v1/images",
  206. body: addPlaceCorrectBody.Bytes(),
  207. contentType: addPlaceCorrectContentType,
  208. },
  209. resCode: fiber.StatusInternalServerError,
  210. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  211. mock := repoMocks.NewPlaceImageRepositoryMock(mc)
  212. mock.BeginTxMock.Return(nil, nil)
  213. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) {
  214. assert.Equal(mc, placeID, req.PlaceID)
  215. }).Return(testError)
  216. return mock
  217. },
  218. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  219. return repoMocks.NewThingImageRepositoryMock(mc)
  220. },
  221. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  222. mock := repoMocks.NewFileRepositoryMock(mc)
  223. mock.SaveMock.Return(nil)
  224. return mock
  225. },
  226. },
  227. {
  228. name: "positive case - add place image",
  229. req: req{
  230. method: fiber.MethodPost,
  231. route: "/v1/images",
  232. body: addPlaceCorrectBody.Bytes(),
  233. contentType: addPlaceCorrectContentType,
  234. },
  235. resCode: fiber.StatusOK,
  236. resBody: dto.EmptyResponse{},
  237. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  238. mock := repoMocks.NewPlaceImageRepositoryMock(mc)
  239. mock.BeginTxMock.Return(nil, nil)
  240. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) {
  241. assert.Equal(mc, placeID, req.PlaceID)
  242. }).Return(nil)
  243. mock.CommitTxMock.Return(nil)
  244. return mock
  245. },
  246. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  247. return repoMocks.NewThingImageRepositoryMock(mc)
  248. },
  249. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  250. mock := repoMocks.NewFileRepositoryMock(mc)
  251. mock.SaveMock.Return(nil)
  252. return mock
  253. },
  254. },
  255. {
  256. name: "negative case - repository error (begin tx)",
  257. req: req{
  258. method: fiber.MethodPost,
  259. route: "/v1/images",
  260. body: addThingCorrectBody.Bytes(),
  261. contentType: addThingCorrectContentType,
  262. },
  263. resCode: fiber.StatusInternalServerError,
  264. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  265. return repoMocks.NewPlaceImageRepositoryMock(mc)
  266. },
  267. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  268. mock := repoMocks.NewThingImageRepositoryMock(mc)
  269. mock.BeginTxMock.Return(nil, testError)
  270. return mock
  271. },
  272. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  273. mock := repoMocks.NewFileRepositoryMock(mc)
  274. mock.SaveMock.Return(nil)
  275. return mock
  276. },
  277. },
  278. {
  279. name: "negative case - repository error (commit tx)",
  280. req: req{
  281. method: fiber.MethodPost,
  282. route: "/v1/images",
  283. body: addThingCorrectBody.Bytes(),
  284. contentType: addThingCorrectContentType,
  285. },
  286. resCode: fiber.StatusInternalServerError,
  287. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  288. return repoMocks.NewPlaceImageRepositoryMock(mc)
  289. },
  290. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  291. mock := repoMocks.NewThingImageRepositoryMock(mc)
  292. mock.BeginTxMock.Return(nil, nil)
  293. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingImageRequest, tx *sql.Tx) {
  294. assert.Equal(mc, thingID, req.ThingID)
  295. }).Return(nil)
  296. mock.CommitTxMock.Return(testError)
  297. return mock
  298. },
  299. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  300. mock := repoMocks.NewFileRepositoryMock(mc)
  301. mock.SaveMock.Return(nil)
  302. return mock
  303. },
  304. },
  305. {
  306. name: "negative case - repository error (add image)",
  307. req: req{
  308. method: fiber.MethodPost,
  309. route: "/v1/images",
  310. body: addThingCorrectBody.Bytes(),
  311. contentType: addThingCorrectContentType,
  312. },
  313. resCode: fiber.StatusInternalServerError,
  314. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  315. return repoMocks.NewPlaceImageRepositoryMock(mc)
  316. },
  317. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  318. mock := repoMocks.NewThingImageRepositoryMock(mc)
  319. mock.BeginTxMock.Return(nil, nil)
  320. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingImageRequest, tx *sql.Tx) {
  321. assert.Equal(mc, thingID, req.ThingID)
  322. }).Return(testError)
  323. return mock
  324. },
  325. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  326. mock := repoMocks.NewFileRepositoryMock(mc)
  327. mock.SaveMock.Return(nil)
  328. return mock
  329. },
  330. },
  331. {
  332. name: "positive case - add thing image",
  333. req: req{
  334. method: fiber.MethodPost,
  335. route: "/v1/images",
  336. body: addThingCorrectBody.Bytes(),
  337. contentType: addThingCorrectContentType,
  338. },
  339. resCode: fiber.StatusOK,
  340. resBody: dto.EmptyResponse{},
  341. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  342. return repoMocks.NewPlaceImageRepositoryMock(mc)
  343. },
  344. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  345. mock := repoMocks.NewThingImageRepositoryMock(mc)
  346. mock.BeginTxMock.Return(nil, nil)
  347. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingImageRequest, tx *sql.Tx) {
  348. assert.Equal(mc, thingID, req.ThingID)
  349. }).Return(nil)
  350. mock.CommitTxMock.Return(nil)
  351. return mock
  352. },
  353. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  354. mock := repoMocks.NewFileRepositoryMock(mc)
  355. mock.SaveMock.Return(nil)
  356. return mock
  357. },
  358. },
  359. {
  360. name: "negative case - save file error",
  361. req: req{
  362. method: fiber.MethodPost,
  363. route: "/v1/images",
  364. body: addThingCorrectBody.Bytes(),
  365. contentType: addThingCorrectContentType,
  366. },
  367. resCode: fiber.StatusInternalServerError,
  368. placeImageRepoMock: func(mc *minimock.Controller) interfaces.PlaceImageRepository {
  369. return repoMocks.NewPlaceImageRepositoryMock(mc)
  370. },
  371. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  372. return repoMocks.NewThingImageRepositoryMock(mc)
  373. },
  374. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  375. mock := repoMocks.NewFileRepositoryMock(mc)
  376. mock.SaveMock.Return(testError)
  377. return mock
  378. },
  379. },
  380. }
  381. for _, tt := range tests {
  382. t.Run(tt.name, func(t *testing.T) {
  383. fiberApp := fiber.New()
  384. serviceProvider := sp.InitMock(tt.placeImageRepoMock(mc), tt.thingImageRepoMock(mc), tt.fileRepoMock(mc))
  385. fiberApp.Post("/v1/images", AddImageHandler(serviceProvider))
  386. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, bytes.NewReader(tt.req.body))
  387. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  388. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  389. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  390. if tt.resBody != nil {
  391. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  392. }
  393. })
  394. }
  395. }