getImage.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. import test from 'ava'
  3. import request from 'supertest'
  4. import { apiPrefix, imageServedUrl } from '../../config'
  5. import { json, beforeEachTests } from './_test_functions'
  6. // ROUTE /getImage
  7. // Before each tests, start a server
  8. test.beforeEach(beforeEachTests)
  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=unknown-scene-name&imageQuality=10', async t => {
  26. const res = await request(t.context.server)
  27. .get(`${apiPrefix}/getImage?sceneName=unknown-scene-name&imageQuality=10`)
  28. t.is(res.status, 500, json(res))
  29. t.truthy(res.body.message.match(/Can't access.*scene dir.*Check it exist.*and you have read permission/), json(res.body))
  30. })
  31. test('GET /getImage?sceneName=bathroom&imageQuality=999999', async t => {
  32. const res = await request(t.context.server)
  33. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=999999`)
  34. t.is(res.status, 404, json(res))
  35. t.truthy(res.body.message.match(/requested quality.*not found for.*scene/), json(res.body))
  36. })
  37. test('GET /getImage?sceneName=bathroom&imageQuality=10', async t => {
  38. const res = await request(t.context.server)
  39. .get(`${apiPrefix}/getImage?sceneName=bathroom&imageQuality=10`)
  40. t.is(res.status, 200, json(res))
  41. t.is(res.body.data, `${imageServedUrl}/bathroom/bathroom_00010.png`, json(res.body))
  42. // Check link is accessible and is an image
  43. const res2 = await request(t.context.server)
  44. .get(`${imageServedUrl}/bathroom/bathroom_00010.png`)
  45. t.is(res2.status, 200, json(res2))
  46. t.is(res2.header['content-type'], 'image/png', json(res2))
  47. })