user.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 expect = chai.expect
  7. let { sequelize, User } = require('../models')
  8. const Errors = require('../lib/errors.js')
  9. chai.use(require('chai-http'))
  10. chai.use(require('chai-things'))
  11. describe('User', () => {
  12. //Wait for app to start before commencing
  13. before((done) => {
  14. if(server.locals.appStarted) done()
  15. server.on('appStarted', () => {
  16. done()
  17. })
  18. })
  19. //Delete all rows in table after
  20. //tests completed
  21. after(() => sequelize.sync({ force: true }) )
  22. describe('/ POST user', () => {
  23. it('should create an account', (done) => {
  24. chai.request(server)
  25. .post('/api/v1/user')
  26. .set('content-type', 'application/x-www-form-urlencoded')
  27. .send({
  28. username: 'username',
  29. password: 'password'
  30. })
  31. .end((err, res) => {
  32. res.should.have.status(200)
  33. res.should.be.json
  34. res.body.should.have.property('username', 'username')
  35. res.body.should.have.property('hash')
  36. res.body.should.have.property('color')
  37. res.body.color.should.not.be.null
  38. done()
  39. })
  40. })
  41. it('should create an admin account if no is already created', (done) => {
  42. chai.request(server)
  43. .post('/api/v1/user')
  44. .set('content-type', 'application/json')
  45. .send({
  46. username: 'adminaccount',
  47. password: 'password',
  48. admin: true
  49. })
  50. .end((err, res) => {
  51. res.should.have.status(200)
  52. res.body.should.have.property('username', 'adminaccount')
  53. res.body.should.have.property('hash')
  54. res.body.should.have.property('admin', true)
  55. done()
  56. })
  57. })
  58. it('should give an error if an admin account is already created and no token is provided', (done) => {
  59. chai.request(server)
  60. .post('/api/v1/user')
  61. .set('content-type', 'application/json')
  62. .send({
  63. username: 'adminaccount1',
  64. password: 'password',
  65. admin: true
  66. })
  67. .end((err, res) => {
  68. res.should.have.status(400)
  69. res.should.be.json
  70. res.body.should.have.property('errors')
  71. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('token'))
  72. done()
  73. })
  74. })
  75. it('should give an error if an admin account is already created and token is invalid', (done) => {
  76. chai.request(server)
  77. .post('/api/v1/user')
  78. .set('content-type', 'application/json')
  79. .send({
  80. username: 'adminaccount1',
  81. password: 'password',
  82. admin: true,
  83. token: 'invalid_token'
  84. })
  85. .end((err, res) => {
  86. res.should.have.status(401)
  87. res.should.be.json
  88. res.body.should.have.property('errors')
  89. res.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  90. done()
  91. })
  92. })
  93. it('should create an admin account provided with a token', async () => {
  94. let agent = chai.request.agent(server)
  95. await agent
  96. .post('/api/v1/user/adminaccount/login')
  97. .set('content-type', 'application/json')
  98. .send({
  99. password: 'password'
  100. })
  101. let tokenRes = await agent.post('/api/v1/admin_token')
  102. let token = tokenRes.body.token
  103. let accountRes = await chai.request(server)
  104. .post('/api/v1/user')
  105. .set('content-type', 'application/json')
  106. .send({
  107. username: 'adminaccount1',
  108. password: 'password',
  109. admin: true,
  110. token: token
  111. })
  112. accountRes.should.have.status(200)
  113. accountRes.should.be.json
  114. accountRes.body.should.have.property('admin', true)
  115. accountRes.body.should.have.property('username', 'adminaccount1')
  116. accountRes.body.should.have.property('hash')
  117. try {
  118. let invalidAccountRes = await chai.request(server)
  119. .post('/api/v1/user')
  120. .set('content-type', 'application/json')
  121. .send({
  122. username: 'adminaccount2',
  123. password: 'password',
  124. admin: true,
  125. token: token
  126. })
  127. invalidAccountRes.should.have.status(401)
  128. invalidAccountRes.should.be.json
  129. invalidAccountRes.body.should.have.property('errors')
  130. invalidAccountRes.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  131. } catch (res) {
  132. res.should.have.status(401)
  133. JSON.parse(res.response.text).errors.should.include.something.that.deep.equals(Errors.invalidToken)
  134. }
  135. })
  136. it('should throw an error if account already created', (done) => {
  137. chai.request(server)
  138. .post('/api/v1/user')
  139. .set('content-type', 'application/x-www-form-urlencoded')
  140. .send({
  141. username: 'username',
  142. password: 'password'
  143. })
  144. .end((err, res) => {
  145. res.should.have.status(400)
  146. res.should.be.json
  147. res.body.should.have.property('errors')
  148. res.body.errors.should.contain.something.that.has.property('message', 'username already taken - try another')
  149. done()
  150. })
  151. })
  152. it('should throw an error if no username or password', (done) => {
  153. chai.request(server)
  154. .post('/api/v1/user')
  155. .set('content-type', 'application/x-www-form-urlencoded')
  156. .send({})
  157. .end((err, res) => {
  158. res.should.have.status(400)
  159. res.should.be.json
  160. res.body.should.have.property('errors')
  161. res.body.errors.should.contain.something.that.has.property('message', 'hash cannot be null')
  162. done()
  163. })
  164. })
  165. it('should throw an error if username or password are not a string', (done) => {
  166. chai.request(server)
  167. .post('/api/v1/user')
  168. .set('content-type', 'application/json')
  169. .send({
  170. username: 123,
  171. password: 123
  172. })
  173. .end((err, res) => {
  174. res.should.have.status(400)
  175. res.should.be.json
  176. res.body.should.have.property('errors')
  177. res.body.should.have.property('errors')
  178. res.body.errors.should.contain.something.that.has.property('message', 'username must be a string')
  179. res.body.errors.should.contain.something.that.has.property('message', 'password must be a string')
  180. done()
  181. })
  182. })
  183. it('should throw an error if username less than 6 characters', (done) => {
  184. chai.request(server)
  185. .post('/api/v1/user')
  186. .set('content-type', 'application/x-www-form-urlencoded')
  187. .send({
  188. username: 'test',
  189. password: '12345678'
  190. })
  191. .end((err, res) => {
  192. res.should.have.status(400)
  193. res.should.be.json
  194. res.body.should.have.property('errors')
  195. res.body.errors.should.contain.something.that.has.property('message', 'username must be between 6 and 50 characters')
  196. done()
  197. })
  198. })
  199. it('should throw an error if password less than 6 characters', (done) => {
  200. chai.request(server)
  201. .post('/api/v1/user')
  202. .set('content-type', 'application/x-www-form-urlencoded')
  203. .send({
  204. username: 'test12567',
  205. password: '123'
  206. })
  207. .end((err, res) => {
  208. res.should.have.status(400)
  209. res.should.be.json
  210. res.body.should.have.property('errors')
  211. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  212. done()
  213. })
  214. })
  215. it('should throw an error if password is greater than 100 characters', (done) => {
  216. chai.request(server)
  217. .post('/api/v1/user')
  218. .set('content-type', 'application/x-www-form-urlencoded')
  219. .send({
  220. username: '12345678765432345676543',
  221. password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
  222. })
  223. .end((err, res) => {
  224. res.should.have.status(400)
  225. res.should.be.json
  226. res.body.should.have.property('errors')
  227. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  228. done()
  229. })
  230. })
  231. it('should throw an error if username greater than 50 characters', (done) => {
  232. chai.request(server)
  233. .post('/api/v1/user')
  234. .set('content-type', 'application/x-www-form-urlencoded')
  235. .send({
  236. username: '123456789012345678901234567890123456789012345678901',
  237. password: '123456789876'
  238. })
  239. .end((err, res) => {
  240. res.should.have.status(400)
  241. res.should.be.json
  242. res.body.should.have.property('errors')
  243. res.body.errors.should.contain.something.that.has.property('message', 'username must be between 6 and 50 characters')
  244. done()
  245. })
  246. })
  247. it('should give an error if an username has blank character', (done) => {
  248. chai.request(server)
  249. .post('/api/v1/user')
  250. .set('content-type', 'application/json')
  251. .send({
  252. username: 'username with space',
  253. password: 'password'
  254. })
  255. .end((err, res) => {
  256. res.should.have.status(400)
  257. res.should.be.json
  258. res.body.should.have.property('errors')
  259. res.body.errors.should.include.something.that.has.property('message', 'username can\'t contain blank characters')
  260. done()
  261. })
  262. })
  263. })
  264. describe('/:username GET user', () => {
  265. it('should return the user', async () => {
  266. let res = await chai.request(server)
  267. .get('/api/v1/user/username')
  268. res.should.have.status(200)
  269. res.should.be.json
  270. res.body.should.have.property('username', 'username')
  271. res.body.should.have.property('color')
  272. res.body.should.not.have.property('hash')
  273. })
  274. it('should return an error if username invalid', async () => {
  275. try {
  276. let res = await chai.request(server)
  277. .get('/api/v1/user/not_a_user')
  278. res.should.have.status(400)
  279. res.body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  280. } catch(res) {
  281. let body = JSON.parse(res.response.text)
  282. res.should.have.status(400)
  283. body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  284. }
  285. })
  286. it('should get posts as well if posts query is appended', async () => {
  287. let agent = chai.request.agent(server)
  288. await agent
  289. .post('/api/v1/user/adminaccount/login')
  290. .set('content-type', 'application/x-www-form-urlencoded')
  291. .send({ password: 'password' })
  292. await agent
  293. .post('/api/v1/category')
  294. .set('content-type', 'application/x-www-form-urlencoded')
  295. .send({ name: 'categorynamehere' })
  296. await agent
  297. .post('/api/v1/thread')
  298. .set('content-type', 'application/x-www-form-urlencoded')
  299. .send({ name: 'a thread name', category: 'categorynamehere' })
  300. await agent
  301. .post('/api/v1/post')
  302. .set('content-type', 'application/json')
  303. .send({ threadId: 1, content: 'content for post' })
  304. await agent
  305. .post('/api/v1/post')
  306. .set('content-type', 'application/json')
  307. .send({ threadId: 1, content: 'content for another post' })
  308. let res = await agent
  309. .get('/api/v1/user/adminaccount?posts=true')
  310. res.should.be.json
  311. res.should.have.status(200)
  312. res.body.should.have.property('username', 'adminaccount')
  313. res.body.should.have.property('Posts')
  314. res.body.Posts.should.have.property('length', 2)
  315. res.body.Posts[0].should.have.deep.property('Thread.id', 1)
  316. res.body.Posts[0].should.have.deep.property('User.username', 'adminaccount')
  317. })
  318. it('should allow pagination', async () => {
  319. let agent = chai.request.agent(server)
  320. await agent
  321. .post('/api/v1/user')
  322. .set('content-type', 'application/x-www-form-urlencoded')
  323. .send({ username: 'paginationaccount', password: 'password' })
  324. for(var i = 0; i < 30; i++) {
  325. let thread = await agent
  326. .post('/api/v1/thread')
  327. .set('content-type', 'application/json')
  328. .send({ category: 'categorynamehere', name: `THREAD ${i}` })
  329. await agent
  330. .post('/api/v1/post')
  331. .set('content-type', 'application/json')
  332. .send({ threadId: thread.body.id, content: `POST ${i}` })
  333. }
  334. let pageOne = await agent.get('/api/v1/user/paginationaccount?posts=true')
  335. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  336. let pageThree = await agent.get(pageTwo.body.meta.nextURL)
  337. pageOne.body.Posts.should.have.length(10)
  338. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  339. pageOne.body.Posts[0].should.have.property('content', '<p>POST 29</p>\n')
  340. pageTwo.body.Posts.should.have.length(10)
  341. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  342. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 19</p>\n')
  343. pageThree.body.Posts.should.have.length(10)
  344. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  345. pageThree.body.Posts[0].should.have.property('content', '<p>POST 9</p>\n')
  346. pageThree.body.Posts[9].should.have.property('content', '<p>POST 0</p>\n')
  347. expect(pageThree.body.meta.nextURL).to.be.null
  348. })
  349. it('should get threads as well if threads query is appended', async () => {
  350. let agent = chai.request.agent(server)
  351. await agent
  352. .post('/api/v1/user')
  353. .set('content-type', 'application/x-www-form-urlencoded')
  354. .send({ username: 'threadaccount', password: 'password' })
  355. for(var i = 0; i < 20; i++) {
  356. let thread = await agent
  357. .post('/api/v1/thread')
  358. .set('content-type', 'application/json')
  359. .send({ category: 'categorynamehere', name: 'THREAD ' + i })
  360. await agent
  361. .post('/api/v1/post')
  362. .set('content-type', 'application/json')
  363. .send({ threadId: thread.body.id, content: `POST ${i}` })
  364. }
  365. let pageOne = await agent.get('/api/v1/user/threadaccount?threads=true')
  366. //Add another newer thread
  367. //This should not affect other tests
  368. let thread = await agent
  369. .post('/api/v1/thread')
  370. .set('content-type', 'application/json')
  371. .send({ category: 'categorynamehere', name: 'THREAD 20'})
  372. await agent
  373. .post('/api/v1/post')
  374. .set('content-type', 'application/json')
  375. .send({ threadId: thread.body.id, content: `POST 20` })
  376. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  377. pageOne.body.Threads.should.have.length(10)
  378. pageOne.body.Threads[0].should.have.property('name', 'THREAD 19')
  379. pageOne.body.meta.should.have.property('nextThreadsCount', 10)
  380. pageTwo.body.Threads.should.have.length(10)
  381. pageTwo.body.Threads[0].should.have.property('name', 'THREAD 9')
  382. pageTwo.body.meta.should.have.property('nextThreadsCount', 0)
  383. expect(pageTwo.body.meta.nextURL).to.be.null
  384. })
  385. })
  386. describe('/?admin', () => {
  387. let admin1 = chai.request.agent(server)
  388. before(done => {
  389. admin1
  390. .post('/api/v1/user/adminaccount/login')
  391. .set('content-type', 'application/json')
  392. .send({
  393. password: 'password'
  394. })
  395. .then(_ => {
  396. done()
  397. })
  398. .catch(done)
  399. })
  400. it('should return an array of admins', async () => {
  401. let res = await admin1.get('/api/v1/user?admin=true')
  402. res.should.be.json
  403. res.should.have.status(200)
  404. res.body.should.contain.something.with.property('username', 'adminaccount')
  405. res.body.should.contain.something.with.property('username', 'adminaccount1')
  406. res.body.should.not.contain.something.with.property('hash')
  407. res.body.should.have.property('length', 2)
  408. })
  409. it('should return an error if not admin', done => {
  410. chai.request(server)
  411. .get('/api/v1/user?admin=true')
  412. .end((err, res) => {
  413. res.should.have.status(401)
  414. res.body.should.have.property('errors')
  415. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  416. done()
  417. })
  418. })
  419. })
  420. describe('/:username/login POST user', () => {
  421. let agent = chai.request.agent(server)
  422. it('should throw an error if invalid username is provided', (done) => {
  423. chai.request(server)
  424. .post('/api/v1/user/invalid_username/login')
  425. .set('content-type', 'application/x-www-form-urlencoded')
  426. .send({
  427. password: 'password'
  428. })
  429. .end((err, res) => {
  430. res.should.have.status(401)
  431. res.body.should.have.property('errors')
  432. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  433. done()
  434. })
  435. })
  436. it('should throw an error if invalid password is provided', (done) => {
  437. chai.request(server)
  438. .post('/api/v1/user/username/login')
  439. .set('content-type', 'application/x-www-form-urlencoded')
  440. .send({
  441. password: 'invalid_password'
  442. })
  443. .end((err, res) => {
  444. res.should.have.status(401)
  445. res.body.should.have.property('errors')
  446. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  447. res.should.not.have.cookie('username')
  448. done()
  449. })
  450. })
  451. it('should log in the user', (done) => {
  452. agent
  453. .post('/api/v1/user/username/login')
  454. .set('content-type', 'application/x-www-form-urlencoded')
  455. .send({
  456. password: 'password'
  457. })
  458. .end((err, res) => {
  459. res.should.have.status(200)
  460. res.should.be.json
  461. res.should.have.cookie('username', 'username')
  462. if(err) {
  463. done(err)
  464. } else {
  465. done()
  466. }
  467. })
  468. })
  469. })
  470. describe('/:username/logout POST user', () => {
  471. let agent = chai.request.agent(server)
  472. it('should log out the user', (done) => {
  473. agent
  474. .post('/api/v1/user/login')
  475. .set('content-type', 'application/x-www-form-urlencoded')
  476. .send({
  477. username: 'username',
  478. password: 'password'
  479. })
  480. .end((err, res) => {
  481. agent
  482. .post('/api/v1/user/username/logout')
  483. .end((err, res) => {
  484. res.should.have.status(200)
  485. res.should.not.have.cookie('username')
  486. if(err) {
  487. done(err)
  488. } else {
  489. done()
  490. }
  491. })
  492. })
  493. })
  494. })
  495. describe('/:username PUT user', () => {
  496. let agent = chai.request.agent(server)
  497. before(async () => {
  498. await agent
  499. .post('/api/v1/user/adminaccount/login')
  500. .set('content-type', 'application/json')
  501. .send({
  502. password: 'password'
  503. })
  504. })
  505. it('should add user description if it doesn\'t already exist', async () => {
  506. let putRes = await agent
  507. .put('/api/v1/user/adminaccount')
  508. .set('content-type', 'application/json')
  509. .send({
  510. description: 'description here'
  511. })
  512. putRes.should.be.json
  513. putRes.body.should.have.property('success', true)
  514. let getRes = await agent.get('/api/v1/user/adminaccount')
  515. getRes.should.be.json
  516. getRes.body.should.have.property('description', 'description here')
  517. getRes.body.should.have.property('username', 'adminaccount')
  518. getRes.body.should.have.property('color')
  519. })
  520. it('should update user description if it already exists', async () => {
  521. let putRes = await agent
  522. .put('/api/v1/user/adminaccount')
  523. .set('content-type', 'application/json')
  524. .send({
  525. description: 'new description here'
  526. })
  527. putRes.should.be.json
  528. putRes.body.should.have.property('success', true)
  529. let getRes = await agent.get('/api/v1/user/adminaccount')
  530. getRes.should.be.json
  531. getRes.body.should.have.property('description', 'new description here')
  532. })
  533. it('should return an error if username is not logged in', done => {
  534. agent
  535. .put('/api/v1/user/notloggedin')
  536. .set('content-type', 'application/json')
  537. .send({
  538. description: 'new description here'
  539. })
  540. .end((err, res) => {
  541. res.should.be.json
  542. res.should.have.status(401)
  543. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  544. done()
  545. })
  546. })
  547. it('should return an error if description is not a string', done => {
  548. agent
  549. .put('/api/v1/user/adminaccount')
  550. .set('content-type', 'application/json')
  551. .send({
  552. description: 123
  553. })
  554. .end((err, res) => {
  555. res.should.be.json
  556. res.should.have.status(400)
  557. res.body.errors.should.contain.something.that.has.property('message', 'description must be a string')
  558. done()
  559. })
  560. })
  561. it('should return an error if description is too long', done => {
  562. let str = []
  563. for(var i = 0; i < 2000; i++) { str.push('a') }
  564. agent
  565. .put('/api/v1/user/adminaccount')
  566. .set('content-type', 'application/json')
  567. .send({
  568. description: str.join('')
  569. })
  570. .end((err, res) => {
  571. res.should.be.json
  572. res.should.have.status(400)
  573. res.body.errors.should.contain.something.that.has.property('message', 'description must be less than 1024 characters')
  574. done()
  575. })
  576. })
  577. it('should update user password', async () => {
  578. let passwordAgent = chai.request.agent(server)
  579. await passwordAgent
  580. .post('/api/v1/user/adminaccount/login')
  581. .set('content-type', 'application/json')
  582. .send({
  583. password: 'password'
  584. })
  585. let putRes = await passwordAgent
  586. .put('/api/v1/user/adminaccount')
  587. .set('content-type', 'application/json')
  588. .send({
  589. currentPassword: 'password',
  590. newPassword: 'qwertyuiop'
  591. })
  592. putRes.should.be.json
  593. putRes.body.should.have.property('success', true)
  594. await passwordAgent.post('/api/v1/user/adminaccount/logout')
  595. let loginRes = await passwordAgent
  596. .post('/api/v1/user/adminaccount/login')
  597. .set('content-type', 'application/json')
  598. .send({
  599. password: 'qwertyuiop'
  600. })
  601. loginRes.should.have.status(200)
  602. loginRes.should.be.json
  603. loginRes.should.have.cookie('username', 'adminaccount')
  604. })
  605. it('should return an error if username is not logged in', done => {
  606. agent
  607. .put('/api/v1/user/notloggedin')
  608. .set('content-type', 'application/json')
  609. .send({
  610. currentPassword: 'qwertyuiop',
  611. newPassword: 'azertyuiop'
  612. })
  613. .end((err, res) => {
  614. res.should.have.status(401)
  615. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  616. done()
  617. })
  618. })
  619. it('should return an error if current password is incorrect', done => {
  620. agent
  621. .put('/api/v1/user/adminaccount')
  622. .set('content-type', 'application/json')
  623. .send({
  624. currentPassword: 'nottheirpassword',
  625. newPassword: 'azertyuiop'
  626. })
  627. .end((err, res) => {
  628. res.should.have.status(401)
  629. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  630. done()
  631. })
  632. })
  633. it('should return an error if password is the same', done => {
  634. agent
  635. .put('/api/v1/user/adminaccount')
  636. .set('content-type', 'application/json')
  637. .send({
  638. currentPassword: 'qwertyuiop',
  639. newPassword: 'qwertyuiop'
  640. })
  641. .end((err, res) => {
  642. res.should.have.status(400)
  643. res.body.errors.should.contain.something.that.deep.equals(Errors.passwordSame)
  644. done()
  645. })
  646. })
  647. it('should return an error if password is too short', done => {
  648. agent
  649. .put('/api/v1/user/adminaccount')
  650. .set('content-type', 'application/json')
  651. .send({
  652. currentPassword: 'qwertyuiop',
  653. newPassword: ''
  654. })
  655. .end((err, res) => {
  656. res.should.have.status(400)
  657. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  658. done()
  659. })
  660. })
  661. it('should return an error if password is too long', done => {
  662. let str = []
  663. for(var i = 0; i < 2000; i++) { str.push('a') }
  664. agent
  665. .put('/api/v1/user/adminaccount')
  666. .set('content-type', 'application/json')
  667. .send({
  668. currentPassword: 'qwertyuiop',
  669. newPassword: str.join('')
  670. })
  671. .end((err, res) => {
  672. res.should.have.status(400)
  673. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  674. done()
  675. })
  676. })
  677. it('should return an error if missing currentPassword', done => {
  678. agent
  679. .put('/api/v1/user/adminaccount')
  680. .set('content-type', 'application/json')
  681. .send({
  682. newPassword: 'qwertyujkjnbgfdswazxcvbhytr'
  683. })
  684. .end((err, res) => {
  685. res.should.have.status(200)
  686. res.body.should.deep.equal({ success: false })
  687. done()
  688. })
  689. })
  690. })
  691. describe('/:username DELETE', () => {
  692. let agent = chai.request.agent(server)
  693. before(async () => {
  694. await agent.post('/api/v1/user/adminaccount/login')
  695. .set('content-type', 'application/json')
  696. .send({
  697. password: 'qwertyuiop'
  698. })
  699. })
  700. it('should delete the user', done => {
  701. agent
  702. .delete('/api/v1/user/adminaccount')
  703. .then(res => {
  704. res.should.be.json
  705. res.body.should.have.property('success', true)
  706. res.should.not.have.cookie('username')
  707. agent
  708. .post('/api/v1/user/adminaccount/login')
  709. .set('content-type', 'application/json')
  710. .send({
  711. password: 'qwertyuiop'
  712. })
  713. .end((err, res) => {
  714. res.should.have.status(401)
  715. res.body.should.have.property('errors')
  716. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  717. done()
  718. })
  719. })
  720. .catch(err => {
  721. console.log(err)
  722. done()
  723. })
  724. })
  725. it('should return an error if username is not logged in', done => {
  726. agent
  727. .delete('/api/v1/user/notloggedin')
  728. .end((err, res) => {
  729. res.should.have.status(401)
  730. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  731. done()
  732. })
  733. })
  734. })
  735. })