getImage.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict'
  2. import test from 'ava'
  3. import request from 'supertest'
  4. import { apiPrefix, imageServedUrl } from '../../config'
  5. import { json, getHttpServer } from './_test_functions'
  6. // ROUTE /getImage
  7. // Before each tests, start a server
  8. test.beforeEach(async t => (t.context.server = await getHttpServer()))
  9. test('GET /getImage', async t => {
  10. const res = await request(t.context.server)
  11. .get(`${apiPrefix}/getImage`)
  12. t.is(res.status, 400, json(res))
  13. t.true(res.body.message.includes('Missing parameter'), json(res.body))
  14. t.true(res.body.message.includes('sceneName'), json(res.body))
  15. t.true(res.body.message.includes('imageQuality'), json(res.body))
  16. })
  17. test('GET /getImage?sceneName=invalid/../scene&imageQuality=aaaa', async t => {
  18. const res = await request(t.context.server)
  19. .get(`${apiPrefix}/getImage?sceneName=invalid/../scene&imageQuality=aaaa`)
  20. t.is(res.status, 400, json(res))
  21. t.true(res.body.message.includes('Invalid query parameter'), json(res.body))
  22. t.truthy(res.body.data.find(x => x.match(/The requested scene name.*is not valid/)), json(res.body))
  23. t.truthy(res.body.data.find(x => x.includes('The specified quality is not an integer')), json(res.body))
  24. })
  25. test('GET /getImage?sceneName=bathroom&imageQuality=max&nearestQuality=true', async t => {
  26. const res = await request(t.context.server)
  27. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=max&nearestQuality=true`)
  28. t.is(res.status, 400, json(res))
  29. t.true(res.body.message.includes('Invalid query parameter'), json(res.body))
  30. t.truthy(res.body.data.find(x => x.match(/Impossible to use.*min.*max.*median.*with.*nearestQuality/)), json(res.body))
  31. })
  32. test('GET /getImage?sceneName=unknown-scene-name&imageQuality=10', async t => {
  33. const res = await request(t.context.server)
  34. .get(`${apiPrefix}/getImage?sceneName=unknown-scene-name&imageQuality=10`)
  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 /getImage?sceneName=bathroom&imageQuality=999999', async t => {
  39. const res = await request(t.context.server)
  40. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=999999`)
  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 /getImage?sceneName=bathroom&imageQuality=min', async t => {
  45. const res = await request(t.context.server)
  46. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=min`)
  47. t.is(res.status, 200, json(res))
  48. t.is(res.body.data.link, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  49. })
  50. test('GET /getImage?sceneName=bathroom&imageQuality=median', async t => {
  51. const res = await request(t.context.server)
  52. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=median`)
  53. t.is(res.status, 200, json(res))
  54. t.is(res.body.data.link, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  55. })
  56. test('GET /getImage?sceneName=bathroom&imageQuality=max', async t => {
  57. const res = await request(t.context.server)
  58. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=max`)
  59. t.is(res.status, 200, json(res))
  60. t.is(res.body.data.link, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  61. })
  62. test('GET /getImage?sceneName=bathroom&imageQuality=99999&nearestQuality=true', async t => {
  63. const res = await request(t.context.server)
  64. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=99999&nearestQuality=true`)
  65. t.is(res.status, 200, json(res))
  66. t.is(res.body.data.link, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  67. })
  68. test('GET /getImage?sceneName=bathroom&imageQuality=10', async t => {
  69. const res = await request(t.context.server)
  70. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=10`)
  71. t.is(res.status, 200, json(res))
  72. t.is(res.body.data.link, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  73. t.deepEqual(res.body.data, {
  74. link: `${imageServedUrl}/bathroom/bathroom_00010.png`,
  75. fileName: 'bathroom_00010.png',
  76. sceneName: 'bathroom',
  77. quality: 10,
  78. ext: 'png',
  79. metadata: {
  80. format: 'png',
  81. width: 800,
  82. height: 800,
  83. space: 'srgb',
  84. channels: 3,
  85. depth: 'uchar',
  86. density: 72,
  87. isProgressive: false,
  88. hasProfile: false,
  89. hasAlpha: false
  90. }
  91. }, json(res.body))
  92. // Check link is accessible and is an image
  93. const res2 = await request(t.context.server)
  94. .get(`${imageServedUrl}/bathroom/bathroom_00010.png`)
  95. t.is(res2.status, 200, json(res2))
  96. t.is(res2.header['content-type'], 'image/png', json(res2))
  97. })