user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. process.env.NODE_ENV = 'test'
  2. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
  3. let chai = require('chai')
  4. let server = require('../server')
  5. let should = chai.should()
  6. let { sequelize } = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. chai.use(require('chai-http'))
  9. chai.use(require('chai-things'))
  10. describe('User', () => {
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) done()
  14. server.on('appStarted', () => {
  15. done()
  16. })
  17. })
  18. //Delete all rows in table after
  19. //tests completed
  20. after(() => sequelize.sync({ force: true }) )
  21. describe('/ POST user', () => {
  22. it('should create an account', (done) => {
  23. chai.request(server)
  24. .post('/api/v1/user')
  25. .set('content-type', 'application/x-www-form-urlencoded')
  26. .send({
  27. username: 'username',
  28. password: 'password'
  29. })
  30. .end((err, res) => {
  31. res.should.have.status(200)
  32. res.should.be.json
  33. res.body.should.have.property('username', 'username')
  34. res.body.should.have.property('hash')
  35. res.body.should.have.property('color')
  36. res.body.color.should.not.be.null
  37. done()
  38. })
  39. })
  40. it('should create an admin account if no is already created', (done) => {
  41. chai.request(server)
  42. .post('/api/v1/user')
  43. .set('content-type', 'application/json')
  44. .send({
  45. username: 'adminaccount',
  46. password: 'password',
  47. admin: true
  48. })
  49. .end((err, res) => {
  50. res.should.have.status(200)
  51. res.body.should.have.property('username', 'adminaccount')
  52. res.body.should.have.property('hash')
  53. res.body.should.have.property('admin', true)
  54. done()
  55. })
  56. })
  57. it('should give an error if an admin account is already created and no token is provided', (done) => {
  58. chai.request(server)
  59. .post('/api/v1/user')
  60. .set('content-type', 'application/json')
  61. .send({
  62. username: 'adminaccount1',
  63. password: 'password',
  64. admin: true
  65. })
  66. .end((err, res) => {
  67. res.should.have.status(400)
  68. res.should.be.json
  69. res.body.should.have.property('errors')
  70. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('token'))
  71. done()
  72. })
  73. })
  74. it('should give an error if admin and token fields are not of the correct type ', (done) => {
  75. chai.request(server)
  76. .post('/api/v1/user')
  77. .set('content-type', 'application/json')
  78. .send({
  79. username: 'adminaccount1',
  80. password: 'password',
  81. admin: 'not a boolean',
  82. token: 123
  83. })
  84. .end((err, res) => {
  85. res.should.have.status(400)
  86. res.should.be.json
  87. res.body.should.have.property('errors')
  88. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('admin', 'boolean'))
  89. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('token', 'string'))
  90. done()
  91. })
  92. })
  93. it('should give an error if an admin account is already created and token is invalid', (done) => {
  94. chai.request(server)
  95. .post('/api/v1/user')
  96. .set('content-type', 'application/json')
  97. .send({
  98. username: 'adminaccount1',
  99. password: 'password',
  100. admin: true,
  101. token: 'invalid_token'
  102. })
  103. .end((err, res) => {
  104. res.should.have.status(401)
  105. res.should.be.json
  106. res.body.should.have.property('errors')
  107. res.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  108. done()
  109. })
  110. })
  111. it('should create an admin account provided with a token', async () => {
  112. let agent = chai.request.agent(server)
  113. await agent
  114. .post('/api/v1/user/adminaccount/login')
  115. .set('content-type', 'application/json')
  116. .send({
  117. password: 'password'
  118. })
  119. let tokenRes = await agent.post('/api/v1/admin_token')
  120. let token = tokenRes.body.token
  121. let accountRes = await chai.request(server)
  122. .post('/api/v1/user')
  123. .set('content-type', 'application/json')
  124. .send({
  125. username: 'adminaccount1',
  126. password: 'password',
  127. admin: true,
  128. token: token
  129. })
  130. accountRes.should.have.status(200)
  131. accountRes.should.be.json
  132. accountRes.body.should.have.property('admin', true)
  133. accountRes.body.should.have.property('username', 'adminaccount1')
  134. accountRes.body.should.have.property('hash')
  135. try {
  136. let invalidAccountRes = await chai.request(server)
  137. .post('/api/v1/user')
  138. .set('content-type', 'application/json')
  139. .send({
  140. username: 'adminaccount2',
  141. password: 'password',
  142. admin: true,
  143. token: token
  144. })
  145. invalidAccountRes.should.have.status(401)
  146. invalidAccountRes.should.be.json
  147. invalidAccountRes.body.should.have.property('errors')
  148. invalidAccountRes.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  149. } catch (res) {
  150. res.should.have.status(401)
  151. JSON.parse(res.response.text).errors.should.include.something.that.deep.equals(Errors.invalidToken)
  152. }
  153. })
  154. it('should throw an error if account already created', (done) => {
  155. chai.request(server)
  156. .post('/api/v1/user')
  157. .set('content-type', 'application/x-www-form-urlencoded')
  158. .send({
  159. username: 'username',
  160. password: 'password'
  161. })
  162. .end((err, res) => {
  163. res.should.have.status(400)
  164. res.should.be.json
  165. res.body.should.have.property('errors')
  166. res.body.errors.should.include.something.that.deep.equals(Errors.accountAlreadyCreated)
  167. done()
  168. })
  169. })
  170. it('should throw an error if no username or password', (done) => {
  171. chai.request(server)
  172. .post('/api/v1/user')
  173. .set('content-type', 'application/x-www-form-urlencoded')
  174. .send({})
  175. .end((err, res) => {
  176. res.should.have.status(400)
  177. res.should.be.json
  178. res.body.should.have.property('errors')
  179. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('username'))
  180. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('password'))
  181. done()
  182. })
  183. })
  184. it('should throw an error if username or password are not a string', (done) => {
  185. chai.request(server)
  186. .post('/api/v1/user')
  187. .set('content-type', 'application/json')
  188. .send({
  189. username: 123,
  190. password: 123
  191. })
  192. .end((err, res) => {
  193. res.should.have.status(400)
  194. res.should.be.json
  195. res.body.should.have.property('errors')
  196. res.body.should.have.property('errors')
  197. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('username', 'string'))
  198. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('password', 'string'))
  199. done()
  200. })
  201. })
  202. it('should throw an error if username or password less than 6 characters', (done) => {
  203. chai.request(server)
  204. .post('/api/v1/user')
  205. .set('content-type', 'application/x-www-form-urlencoded')
  206. .send({
  207. username: 'test',
  208. password: 'pass'
  209. })
  210. .end((err, res) => {
  211. res.should.have.status(400)
  212. res.should.be.json
  213. res.body.should.have.property('errors')
  214. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('username', '6'))
  215. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('password', '6'))
  216. done()
  217. })
  218. })
  219. it('should throw an error if username greater than 50 characters or password is greater than 100 characters', (done) => {
  220. chai.request(server)
  221. .post('/api/v1/user')
  222. .set('content-type', 'application/x-www-form-urlencoded')
  223. .send({
  224. username: '123456789012345678901234567890123456789012345678901',
  225. password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
  226. })
  227. .end((err, res) => {
  228. res.should.have.status(400)
  229. res.should.be.json
  230. res.body.should.have.property('errors')
  231. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('username', '50'))
  232. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('password', '100'))
  233. done()
  234. })
  235. })
  236. })
  237. describe('/:username GET user', () => {
  238. it('should return the user', async () => {
  239. let res = await chai.request(server)
  240. .get('/api/v1/user/username')
  241. res.should.have.status(200)
  242. res.should.be.json
  243. res.body.should.have.property('username', 'username')
  244. res.body.should.have.property('color')
  245. res.body.should.not.have.property('hash')
  246. })
  247. it('should return an error if username invalid', async () => {
  248. try {
  249. let res = await chai.request(server)
  250. .get('/api/v1/user/not_a_user')
  251. res.should.have.status(400)
  252. res.body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  253. } catch(res) {
  254. let body = JSON.parse(res.response.text)
  255. res.should.have.status(400)
  256. body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  257. }
  258. })
  259. })
  260. describe('/:username/login POST user', () => {
  261. let agent = chai.request.agent(server)
  262. it('should throw an error if invalid username is provided', (done) => {
  263. chai.request(server)
  264. .post('/api/v1/user/invalid_username/login')
  265. .set('content-type', 'application/x-www-form-urlencoded')
  266. .send({
  267. password: 'password'
  268. })
  269. .end((err, res) => {
  270. res.should.have.status(401)
  271. res.body.should.have.property('errors')
  272. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  273. done()
  274. })
  275. })
  276. it('should throw an error if invalid password is provided', (done) => {
  277. chai.request(server)
  278. .post('/api/v1/user/username/login')
  279. .set('content-type', 'application/x-www-form-urlencoded')
  280. .send({
  281. password: 'invalid_password'
  282. })
  283. .end((err, res) => {
  284. res.should.have.status(401)
  285. res.body.should.have.property('errors')
  286. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  287. res.should.not.have.cookie('username')
  288. done()
  289. })
  290. })
  291. it('should log in the user', (done) => {
  292. agent
  293. .post('/api/v1/user/username/login')
  294. .set('content-type', 'application/x-www-form-urlencoded')
  295. .send({
  296. password: 'password'
  297. })
  298. .end((err, res) => {
  299. res.should.have.status(200)
  300. res.should.be.json
  301. res.should.have.cookie('username', 'username')
  302. if(err) {
  303. done(err)
  304. } else {
  305. done()
  306. }
  307. })
  308. })
  309. })
  310. describe('/:username/logout POST user', () => {
  311. let agent = chai.request.agent(server)
  312. it('should log out the user', (done) => {
  313. agent
  314. .post('/api/v1/user/login')
  315. .set('content-type', 'application/x-www-form-urlencoded')
  316. .send({
  317. username: 'username',
  318. password: 'password'
  319. })
  320. .end((err, res) => {
  321. agent
  322. .post('/api/v1/user/username/logout')
  323. .end((err, res) => {
  324. res.should.have.status(200)
  325. res.should.not.have.cookie('username')
  326. if(err) {
  327. done(err)
  328. } else {
  329. done()
  330. }
  331. })
  332. })
  333. })
  334. })
  335. })