getImageExtracts.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict'
  2. import test from 'ava'
  3. import request from 'supertest'
  4. import sharp from 'sharp'
  5. import fs from 'fs-extra'
  6. import path from 'path'
  7. import { apiPrefix, imageServedUrl, imagesPath } from '../../config'
  8. import { json, beforeEachTests } from './_test_functions'
  9. // ROUTE /getImageExtracts
  10. // Before each tests, start a server
  11. test.beforeEach(beforeEachTests)
  12. test('GET /getImageExtracts', async t => {
  13. const res = await request(t.context.server)
  14. .get(`${apiPrefix}/getImageExtracts`)
  15. t.is(res.status, 400, json(res))
  16. t.true(res.body.message.includes('Missing parameter'), json(res.body))
  17. t.true(res.body.message.includes('sceneName'), json(res.body))
  18. t.true(res.body.message.includes('imageQuality'), json(res.body))
  19. t.true(res.body.message.includes('horizontalExtractCount'), json(res.body))
  20. t.true(res.body.message.includes('verticalExtractCount'), json(res.body))
  21. })
  22. test('GET /getImageExtracts?sceneName=/../&imageQuality=a&horizontalExtractCount=a&verticalExtractCount=a', async t => {
  23. const res = await request(t.context.server)
  24. .get(`${apiPrefix}/getImageExtracts?sceneName=/../&imageQuality=a&horizontalExtractCount=a&verticalExtractCount=a`)
  25. t.is(res.status, 400, json(res))
  26. t.true(res.body.message.includes('Invalid query parameter'), json(res.body))
  27. t.truthy(res.body.data.find(x => x.match(/The requested scene name.*is not valid/)), json(res.body))
  28. t.truthy(res.body.data.find(x => x.includes('The specified quality is not an integer')), json(res.body))
  29. t.truthy(res.body.data.find(x => x.includes('horizontal axis is not an integer')), json(res.body))
  30. t.truthy(res.body.data.find(x => x.includes('vertical axis is not an integer')), json(res.body))
  31. })
  32. test('GET /getImageExtracts?sceneName=unknown-scene-name&imageQuality=10&horizontalExtractCount=5&verticalExtractCount=2', async t => {
  33. const res = await request(t.context.server)
  34. .get(`${apiPrefix}/getImageExtracts?sceneName=unknown-scene-name&imageQuality=10&horizontalExtractCount=5&verticalExtractCount=2`)
  35. t.is(res.status, 500, json(res))
  36. t.truthy(res.body.message.match(/Can't access.*scene dir.*Check it exist.*and you have read permission/), json(res.body))
  37. })
  38. test('GET /getImageExtracts?sceneName=bathroom&imageQuality=99999&horizontalExtractCount=5&verticalExtractCount=2', async t => {
  39. const res = await request(t.context.server)
  40. .get(`${apiPrefix}/getImageExtracts?sceneName=bathroom&imageQuality=99999&horizontalExtractCount=5&verticalExtractCount=2`)
  41. t.is(res.status, 404, json(res))
  42. t.truthy(res.body.message.match(/requested quality.*not found for.*scene/), json(res.body))
  43. })
  44. test('GET /getImageExtracts?sceneName=bathroom&imageQuality=10&horizontalExtractCount=0&verticalExtractCount=9999', async t => {
  45. const res = await request(t.context.server)
  46. .get(`${apiPrefix}/getImageExtracts?sceneName=bathroom&imageQuality=10&horizontalExtractCount=0&verticalExtractCount=9999`)
  47. t.is(res.status, 400, json(res))
  48. t.true(res.body.message.includes('Invalid query parameter'), json(res.body))
  49. t.truthy(res.body.data.find(x => x.includes('Incompatible number of horizontal extracts')), json(res.body))
  50. t.truthy(res.body.data.find(x => x.includes('Incompatible number of vertical extracts')), json(res.body))
  51. })
  52. test.serial('GET /getImageExtracts?sceneName=bathroom&imageQuality=10&horizontalExtractCount=5&verticalExtractCount=2', async t => {
  53. const res = await request(t.context.server)
  54. .get(`${apiPrefix}/getImageExtracts?sceneName=bathroom&imageQuality=10&horizontalExtractCount=5&verticalExtractCount=2`)
  55. t.is(res.status, 200, json(res))
  56. t.true(Array.isArray(res.body.data), json(res.body))
  57. t.is(res.body.data[0], `${imageServedUrl}/bathroom/extracts/x5_y2/zone00001/bathroom_zone00001_10.png`, json(res.body))
  58. // Check link is accessible and is an image
  59. const res2 = await request(t.context.server)
  60. .get(`${imageServedUrl}/bathroom/extracts/x5_y2/zone00001/bathroom_zone00001_10.png`)
  61. t.is(res2.status, 200, json(res2))
  62. t.is(res2.header['content-type'], 'image/png', json(res2))
  63. })
  64. test.serial('Extracts were successfully generated', async t => {
  65. // Check the extract on the file system
  66. const extracts = path.resolve(imagesPath, 'bathroom', 'extracts')
  67. const aBathroomConfig = path.resolve(extracts, 'x5_y2')
  68. const aBathroomConfigZone = path.resolve(aBathroomConfig, 'zone00001')
  69. const aBathroomConfigZoneImg = path.resolve(aBathroomConfigZone, 'bathroom_zone00001_10.png')
  70. const fsp = fs.promises
  71. // Check `bathroom/extracts`
  72. t.true(await fs.pathExists(extracts))
  73. t.deepEqual(await fsp.readdir(extracts), ['x5_y2'])
  74. // Check `bathroom/extracts/x5_y2`
  75. t.true(await fs.pathExists(aBathroomConfig), aBathroomConfig)
  76. t.is((await fsp.readdir(aBathroomConfig)).length, 10)
  77. // Check `bathroom/extracts/x5_y2/zone00001`
  78. t.true(await fs.pathExists(aBathroomConfigZone))
  79. t.is((await fsp.readdir(aBathroomConfigZone)).length, 1)
  80. // Check `bathroom/extracts/x5_y2/zone00001/bathroom_zone00001_10.png`
  81. t.true(await fs.pathExists(aBathroomConfigZoneImg))
  82. // Check image's type, width and height
  83. const metadata = await sharp(aBathroomConfigZoneImg).metadata()
  84. t.is(metadata.width, 160, json(metadata))
  85. t.is(metadata.height, 400, json(metadata))
  86. t.is(metadata.format, 'png', json(metadata))
  87. })