profile_picture.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize, Thread, Post, User } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. let expect = chai.expect
  10. describe('User', () => {
  11. let admin = chai.request.agent(server)
  12. let user = chai.request.agent(server)
  13. let picture = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAFCAIAAADzBuo/AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAANSURBVBhXYxisgIEBAACbAAEriBitAAAAAElFTkSuQmCC";
  14. //Wait for app to start before commencing
  15. before((done) => {
  16. if(server.locals.appStarted) done()
  17. server.on('appStarted', () => {
  18. done()
  19. })
  20. })
  21. describe('POST /:user/picture', () => {
  22. before(async () => {
  23. try {
  24. let accounts = []
  25. accounts.push(
  26. admin
  27. .post('/api/v1/user')
  28. .set('content-type', 'application/json')
  29. .send({
  30. username: 'adminaccount',
  31. password: 'password',
  32. admin: true
  33. })
  34. )
  35. accounts.push(
  36. user
  37. .post('/api/v1/user')
  38. .set('content-type', 'application/json')
  39. .send({
  40. username: 'useraccount1',
  41. password: 'password'
  42. })
  43. )
  44. await Promise.all(accounts)
  45. return true
  46. } catch (e) {
  47. return e
  48. }
  49. })
  50. it('should add a picture', async () => {
  51. let res = await user
  52. .post('/api/v1/user/useraccount1/picture')
  53. .set('content-type', 'application/json')
  54. .send({ picture })
  55. res.should.be.json
  56. res.should.have.status(200)
  57. let foundUser = await User.findById(1)
  58. foundUser.should.have.property('picture', picture)
  59. })
  60. it('should not add a picture if not logged in', done => {
  61. chai.request(server)
  62. .post('/api/v1/user/useraccount1/picture')
  63. .set('content-type', 'application/json')
  64. .send({ picture })
  65. .end((err, res) => {
  66. res.should.be.json
  67. res.should.have.status(401)
  68. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  69. done()
  70. })
  71. })
  72. it('should not add a picture if not same user', done => {
  73. user
  74. .post('/api/v1/user/adminaccount/picture')
  75. .set('content-type', 'application/json')
  76. .send({ picture })
  77. .end((err, res) => {
  78. res.should.be.json
  79. res.should.have.status(401)
  80. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  81. done()
  82. })
  83. })
  84. it('should not add a picture if user does not exist', done => {
  85. user
  86. .post('/api/v1/user/notanaccount/picture')
  87. .set('content-type', 'application/json')
  88. .send({ picture })
  89. .end((err, res) => {
  90. res.should.be.json
  91. res.should.have.status(401)
  92. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  93. done()
  94. })
  95. })
  96. it('should not add a picture if not validated base64', done => {
  97. user
  98. .post('/api/v1/user/useraccount1/picture')
  99. .set('content-type', 'application/json')
  100. .send({ picture: 'not base64' })
  101. .end((err, res) => {
  102. res.should.be.json
  103. res.should.have.status(400)
  104. res.body.errors.should.contain.something.that.has.property('message', 'image must be valid base64')
  105. done()
  106. })
  107. })
  108. it('should not add a picture if not an image mime type', done => {
  109. user
  110. .post('/api/v1/user/useraccount1/picture')
  111. .set('content-type', 'application/json')
  112. .send({ picture: 'data:text/html;base64,iVBORw0KGgoAAAANSUhEUgAAAAoA' })
  113. .end((err, res) => {
  114. res.should.be.json
  115. res.should.have.status(400)
  116. res.body.errors.should.contain.something.that.has.property('message', 'image must be valid base64')
  117. done()
  118. })
  119. })
  120. it('should remove a picture if picture is null', async () => {
  121. let res = await user
  122. .post('/api/v1/user/useraccount1/picture')
  123. .set('content-type', 'application/json')
  124. .send({ picture: null })
  125. res.should.be.json
  126. res.should.have.status(200)
  127. let foundUser = await User.findById(1)
  128. foundUser.should.have.property('picture', null)
  129. })
  130. //it('should not add a picture if too large file size')
  131. })
  132. after(() => {
  133. sequelize.sync({ force: true })
  134. })
  135. })