user.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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.has.property('message', 'Missing 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('/:username/login POST user', () => {
  387. let agent = chai.request.agent(server)
  388. it('should throw an error if invalid username is provided', (done) => {
  389. chai.request(server)
  390. .post('/api/v1/user/invalid_username/login')
  391. .set('content-type', 'application/x-www-form-urlencoded')
  392. .send({
  393. password: 'password'
  394. })
  395. .end((err, res) => {
  396. res.should.have.status(401)
  397. res.body.should.have.property('errors')
  398. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  399. done()
  400. })
  401. })
  402. it('should throw an error if invalid password is provided', (done) => {
  403. chai.request(server)
  404. .post('/api/v1/user/username/login')
  405. .set('content-type', 'application/x-www-form-urlencoded')
  406. .send({
  407. password: 'invalid_password'
  408. })
  409. .end((err, res) => {
  410. res.should.have.status(401)
  411. res.body.should.have.property('errors')
  412. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  413. res.should.not.have.cookie('username')
  414. done()
  415. })
  416. })
  417. it('should log in the user', (done) => {
  418. agent
  419. .post('/api/v1/user/username/login')
  420. .set('content-type', 'application/x-www-form-urlencoded')
  421. .send({
  422. password: 'password'
  423. })
  424. .end((err, res) => {
  425. res.should.have.status(200)
  426. res.should.be.json
  427. res.should.have.cookie('username', 'username')
  428. if(err) {
  429. done(err)
  430. } else {
  431. done()
  432. }
  433. })
  434. })
  435. })
  436. describe('/:username/logout POST user', () => {
  437. let agent = chai.request.agent(server)
  438. it('should log out the user', (done) => {
  439. agent
  440. .post('/api/v1/user/login')
  441. .set('content-type', 'application/x-www-form-urlencoded')
  442. .send({
  443. username: 'username',
  444. password: 'password'
  445. })
  446. .end((err, res) => {
  447. agent
  448. .post('/api/v1/user/username/logout')
  449. .end((err, res) => {
  450. res.should.have.status(200)
  451. res.should.not.have.cookie('username')
  452. if(err) {
  453. done(err)
  454. } else {
  455. done()
  456. }
  457. })
  458. })
  459. })
  460. })
  461. describe('/:username PUT user', () => {
  462. let agent = chai.request.agent(server)
  463. before(async () => {
  464. await agent
  465. .post('/api/v1/user/adminaccount/login')
  466. .set('content-type', 'application/json')
  467. .send({
  468. password: 'password'
  469. })
  470. })
  471. it('should add user description if it doesn\'t already exist', async () => {
  472. let putRes = await agent
  473. .put('/api/v1/user/adminaccount')
  474. .set('content-type', 'application/json')
  475. .send({
  476. description: 'description here'
  477. })
  478. putRes.should.be.json
  479. putRes.body.should.have.property('success', true)
  480. let getRes = await agent.get('/api/v1/user/adminaccount')
  481. getRes.should.be.json
  482. getRes.body.should.have.property('description', 'description here')
  483. getRes.body.should.have.property('username', 'adminaccount')
  484. getRes.body.should.have.property('color')
  485. })
  486. it('should update user description if it already exists', async () => {
  487. let putRes = await agent
  488. .put('/api/v1/user/adminaccount')
  489. .set('content-type', 'application/json')
  490. .send({
  491. description: 'new description here'
  492. })
  493. putRes.should.be.json
  494. putRes.body.should.have.property('success', true)
  495. let getRes = await agent.get('/api/v1/user/adminaccount')
  496. getRes.should.be.json
  497. getRes.body.should.have.property('description', 'new description here')
  498. })
  499. it('should return an error if username is not logged in', done => {
  500. agent
  501. .put('/api/v1/user/notloggedin')
  502. .set('content-type', 'application/json')
  503. .send({
  504. description: 'new description here'
  505. })
  506. .end((err, res) => {
  507. res.should.be.json
  508. res.should.have.status(401)
  509. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  510. done()
  511. })
  512. })
  513. it('should return an error if description is not a string', done => {
  514. agent
  515. .put('/api/v1/user/adminaccount')
  516. .set('content-type', 'application/json')
  517. .send({
  518. description: 123
  519. })
  520. .end((err, res) => {
  521. res.should.be.json
  522. res.should.have.status(400)
  523. res.body.errors.should.contain.something.that.has.property('message', 'description must be a string')
  524. done()
  525. })
  526. })
  527. it('should return an error if description is too long', done => {
  528. let str = []
  529. for(var i = 0; i < 2000; i++) { str.push('a') }
  530. agent
  531. .put('/api/v1/user/adminaccount')
  532. .set('content-type', 'application/json')
  533. .send({
  534. description: str.join('')
  535. })
  536. .end((err, res) => {
  537. res.should.be.json
  538. res.should.have.status(400)
  539. res.body.errors.should.contain.something.that.has.property('message', 'description must be less than 1024 characters')
  540. done()
  541. })
  542. })
  543. it('should update user password', async () => {
  544. let passwordAgent = chai.request.agent(server)
  545. await passwordAgent
  546. .post('/api/v1/user/adminaccount/login')
  547. .set('content-type', 'application/json')
  548. .send({
  549. password: 'password'
  550. })
  551. let putRes = await passwordAgent
  552. .put('/api/v1/user/adminaccount')
  553. .set('content-type', 'application/json')
  554. .send({
  555. currentPassword: 'password',
  556. newPassword: 'qwertyuiop'
  557. })
  558. putRes.should.be.json
  559. putRes.body.should.have.property('success', true)
  560. await passwordAgent.post('/api/v1/user/adminaccount/logout')
  561. let loginRes = await passwordAgent
  562. .post('/api/v1/user/adminaccount/login')
  563. .set('content-type', 'application/json')
  564. .send({
  565. password: 'qwertyuiop'
  566. })
  567. loginRes.should.have.status(200)
  568. loginRes.should.be.json
  569. loginRes.should.have.cookie('username', 'adminaccount')
  570. })
  571. it('should return an error if username is not logged in', done => {
  572. agent
  573. .put('/api/v1/user/notloggedin')
  574. .set('content-type', 'application/json')
  575. .send({
  576. currentPassword: 'qwertyuiop',
  577. newPassword: 'azertyuiop'
  578. })
  579. .end((err, res) => {
  580. res.should.have.status(401)
  581. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  582. done()
  583. })
  584. })
  585. it('should return an error if current password is incorrect', done => {
  586. agent
  587. .put('/api/v1/user/adminaccount')
  588. .set('content-type', 'application/json')
  589. .send({
  590. currentPassword: 'nottheirpassword',
  591. newPassword: 'azertyuiop'
  592. })
  593. .end((err, res) => {
  594. res.should.have.status(401)
  595. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  596. done()
  597. })
  598. })
  599. it('should return an error if password is the same', done => {
  600. agent
  601. .put('/api/v1/user/adminaccount')
  602. .set('content-type', 'application/json')
  603. .send({
  604. currentPassword: 'qwertyuiop',
  605. newPassword: 'qwertyuiop'
  606. })
  607. .end((err, res) => {
  608. res.should.have.status(400)
  609. res.body.errors.should.contain.something.that.deep.equals(Errors.passwordSame)
  610. done()
  611. })
  612. })
  613. it('should return an error if password is too short', done => {
  614. agent
  615. .put('/api/v1/user/adminaccount')
  616. .set('content-type', 'application/json')
  617. .send({
  618. currentPassword: 'qwertyuiop',
  619. newPassword: ''
  620. })
  621. .end((err, res) => {
  622. res.should.have.status(400)
  623. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  624. done()
  625. })
  626. })
  627. it('should return an error if password is too long', done => {
  628. let str = []
  629. for(var i = 0; i < 2000; i++) { str.push('a') }
  630. agent
  631. .put('/api/v1/user/adminaccount')
  632. .set('content-type', 'application/json')
  633. .send({
  634. currentPassword: 'qwertyuiop',
  635. newPassword: str.join('')
  636. })
  637. .end((err, res) => {
  638. res.should.have.status(400)
  639. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  640. done()
  641. })
  642. })
  643. it('should return an error if missing currentPassword', done => {
  644. agent
  645. .put('/api/v1/user/adminaccount')
  646. .set('content-type', 'application/json')
  647. .send({
  648. newPassword: 'qwertyujkjnbgfdswazxcvbhytr'
  649. })
  650. .end((err, res) => {
  651. res.should.have.status(200)
  652. res.body.should.deep.equal({ success: false })
  653. done()
  654. })
  655. })
  656. })
  657. describe('/:username DELETE', () => {
  658. let agent = chai.request.agent(server)
  659. before(async () => {
  660. await agent.post('/api/v1/user/adminaccount/login')
  661. .set('content-type', 'application/json')
  662. .send({
  663. password: 'qwertyuiop'
  664. })
  665. })
  666. it('should delete the user', done => {
  667. agent
  668. .delete('/api/v1/user/adminaccount')
  669. .then(res => {
  670. res.should.be.json
  671. res.body.should.have.property('success', true)
  672. res.should.not.have.cookie('username')
  673. agent
  674. .post('/api/v1/user/adminaccount/login')
  675. .set('content-type', 'application/json')
  676. .send({
  677. password: 'qwertyuiop'
  678. })
  679. .end((err, res) => {
  680. res.should.have.status(401)
  681. res.body.should.have.property('errors')
  682. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  683. done()
  684. })
  685. })
  686. .catch(err => {
  687. console.log(err)
  688. done()
  689. })
  690. })
  691. it('should return an error if username is not logged in', done => {
  692. agent
  693. .delete('/api/v1/user/notloggedin')
  694. .end((err, res) => {
  695. res.should.have.status(401)
  696. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  697. done()
  698. })
  699. })
  700. })
  701. })