user.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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(401)
  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 must be unique')
  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. })
  248. describe('/:username GET user', () => {
  249. it('should return the user', async () => {
  250. let res = await chai.request(server)
  251. .get('/api/v1/user/username')
  252. res.should.have.status(200)
  253. res.should.be.json
  254. res.body.should.have.property('username', 'username')
  255. res.body.should.have.property('color')
  256. res.body.should.not.have.property('hash')
  257. })
  258. it('should return an error if username invalid', async () => {
  259. try {
  260. let res = await chai.request(server)
  261. .get('/api/v1/user/not_a_user')
  262. res.should.have.status(400)
  263. res.body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  264. } catch(res) {
  265. let body = JSON.parse(res.response.text)
  266. res.should.have.status(400)
  267. body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  268. }
  269. })
  270. it('should get posts as well if posts query is appended', async () => {
  271. let agent = chai.request.agent(server)
  272. await agent
  273. .post('/api/v1/user/adminaccount/login')
  274. .set('content-type', 'application/x-www-form-urlencoded')
  275. .send({ password: 'password' })
  276. await agent
  277. .post('/api/v1/category')
  278. .set('content-type', 'application/x-www-form-urlencoded')
  279. .send({ name: 'categorynamehere' })
  280. await agent
  281. .post('/api/v1/thread')
  282. .set('content-type', 'application/x-www-form-urlencoded')
  283. .send({ name: 'a thread name', category: 'categorynamehere' })
  284. await agent
  285. .post('/api/v1/post')
  286. .set('content-type', 'application/json')
  287. .send({ threadId: 1, content: 'content for post' })
  288. await agent
  289. .post('/api/v1/post')
  290. .set('content-type', 'application/json')
  291. .send({ threadId: 1, content: 'content for another post' })
  292. let res = await agent
  293. .get('/api/v1/user/adminaccount?posts=true')
  294. res.should.be.json
  295. res.should.have.status(200)
  296. res.body.should.have.property('username', 'adminaccount')
  297. res.body.should.have.property('Posts')
  298. res.body.Posts.should.have.property('length', 2)
  299. res.body.Posts[0].should.have.deep.property('Thread.id', 1)
  300. res.body.Posts[0].should.have.deep.property('User.username', 'adminaccount')
  301. })
  302. it('should allow pagination', async () => {
  303. let agent = chai.request.agent(server)
  304. await agent
  305. .post('/api/v1/user')
  306. .set('content-type', 'application/x-www-form-urlencoded')
  307. .send({ username: 'paginationaccount', password: 'password' })
  308. for(var i = 0; i < 30; i++) {
  309. let thread = await agent
  310. .post('/api/v1/thread')
  311. .set('content-type', 'application/json')
  312. .send({ category: 'categorynamehere', name: `THREAD ${i}` })
  313. await agent
  314. .post('/api/v1/post')
  315. .set('content-type', 'application/json')
  316. .send({ threadId: thread.body.id, content: `POST ${i}` })
  317. }
  318. let pageOne = await agent.get('/api/v1/user/paginationaccount?posts=true')
  319. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  320. let pageThree = await agent.get(pageTwo.body.meta.nextURL)
  321. pageOne.body.Posts.should.have.length(10)
  322. pageOne.body.meta.should.have.property('nextPostsCount', 10)
  323. pageOne.body.Posts[0].should.have.property('content', '<p>POST 29</p>\n')
  324. pageTwo.body.Posts.should.have.length(10)
  325. pageTwo.body.meta.should.have.property('nextPostsCount', 10)
  326. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 19</p>\n')
  327. pageThree.body.Posts.should.have.length(10)
  328. pageThree.body.meta.should.have.property('nextPostsCount', 0)
  329. pageThree.body.Posts[0].should.have.property('content', '<p>POST 9</p>\n')
  330. pageThree.body.Posts[9].should.have.property('content', '<p>POST 0</p>\n')
  331. expect(pageThree.body.meta.nextURL).to.be.null
  332. })
  333. it('should get threads as well if threads query is appended', async () => {
  334. let agent = chai.request.agent(server)
  335. await agent
  336. .post('/api/v1/user')
  337. .set('content-type', 'application/x-www-form-urlencoded')
  338. .send({ username: 'threadaccount', password: 'password' })
  339. for(var i = 0; i < 20; i++) {
  340. let thread = await agent
  341. .post('/api/v1/thread')
  342. .set('content-type', 'application/json')
  343. .send({ category: 'categorynamehere', name: 'THREAD ' + i })
  344. await agent
  345. .post('/api/v1/post')
  346. .set('content-type', 'application/json')
  347. .send({ threadId: thread.body.id, content: `POST ${i}` })
  348. }
  349. let pageOne = await agent.get('/api/v1/user/threadaccount?threads=true')
  350. //Add another newer thread
  351. //This should not affect other tests
  352. let thread = await agent
  353. .post('/api/v1/thread')
  354. .set('content-type', 'application/json')
  355. .send({ category: 'categorynamehere', name: 'THREAD 20'})
  356. await agent
  357. .post('/api/v1/post')
  358. .set('content-type', 'application/json')
  359. .send({ threadId: thread.body.id, content: `POST 20` })
  360. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  361. pageOne.body.Threads.should.have.length(10)
  362. pageOne.body.Threads[0].should.have.property('name', 'THREAD 19')
  363. pageOne.body.meta.should.have.property('nextThreadsCount', 10)
  364. pageTwo.body.Threads.should.have.length(10)
  365. pageTwo.body.Threads[0].should.have.property('name', 'THREAD 9')
  366. pageTwo.body.meta.should.have.property('nextThreadsCount', 0)
  367. expect(pageTwo.body.meta.nextURL).to.be.null
  368. })
  369. })
  370. describe('/:username/login POST user', () => {
  371. let agent = chai.request.agent(server)
  372. it('should throw an error if invalid username is provided', (done) => {
  373. chai.request(server)
  374. .post('/api/v1/user/invalid_username/login')
  375. .set('content-type', 'application/x-www-form-urlencoded')
  376. .send({
  377. password: 'password'
  378. })
  379. .end((err, res) => {
  380. res.should.have.status(401)
  381. res.body.should.have.property('errors')
  382. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  383. done()
  384. })
  385. })
  386. it('should throw an error if invalid password is provided', (done) => {
  387. chai.request(server)
  388. .post('/api/v1/user/username/login')
  389. .set('content-type', 'application/x-www-form-urlencoded')
  390. .send({
  391. password: 'invalid_password'
  392. })
  393. .end((err, res) => {
  394. res.should.have.status(401)
  395. res.body.should.have.property('errors')
  396. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  397. res.should.not.have.cookie('username')
  398. done()
  399. })
  400. })
  401. it('should log in the user', (done) => {
  402. agent
  403. .post('/api/v1/user/username/login')
  404. .set('content-type', 'application/x-www-form-urlencoded')
  405. .send({
  406. password: 'password'
  407. })
  408. .end((err, res) => {
  409. res.should.have.status(200)
  410. res.should.be.json
  411. res.should.have.cookie('username', 'username')
  412. if(err) {
  413. done(err)
  414. } else {
  415. done()
  416. }
  417. })
  418. })
  419. })
  420. describe('/:username/logout POST user', () => {
  421. let agent = chai.request.agent(server)
  422. it('should log out the user', (done) => {
  423. agent
  424. .post('/api/v1/user/login')
  425. .set('content-type', 'application/x-www-form-urlencoded')
  426. .send({
  427. username: 'username',
  428. password: 'password'
  429. })
  430. .end((err, res) => {
  431. agent
  432. .post('/api/v1/user/username/logout')
  433. .end((err, res) => {
  434. res.should.have.status(200)
  435. res.should.not.have.cookie('username')
  436. if(err) {
  437. done(err)
  438. } else {
  439. done()
  440. }
  441. })
  442. })
  443. })
  444. })
  445. describe('/:username PUT user', () => {
  446. let agent = chai.request.agent(server)
  447. before(async () => {
  448. await agent
  449. .post('/api/v1/user/adminaccount/login')
  450. .set('content-type', 'application/json')
  451. .send({
  452. password: 'password'
  453. })
  454. })
  455. it('should add user description if it doesn\'t already exist', async () => {
  456. let putRes = await agent
  457. .put('/api/v1/user/adminaccount')
  458. .set('content-type', 'application/json')
  459. .send({
  460. description: 'description here'
  461. })
  462. putRes.should.be.json
  463. putRes.body.should.have.property('success', true)
  464. let getRes = await agent.get('/api/v1/user/adminaccount')
  465. getRes.should.be.json
  466. getRes.body.should.have.property('description', 'description here')
  467. getRes.body.should.have.property('username', 'adminaccount')
  468. getRes.body.should.have.property('color')
  469. })
  470. it('should update user description if it already exists', async () => {
  471. let putRes = await agent
  472. .put('/api/v1/user/adminaccount')
  473. .set('content-type', 'application/json')
  474. .send({
  475. description: 'new description here'
  476. })
  477. putRes.should.be.json
  478. putRes.body.should.have.property('success', true)
  479. let getRes = await agent.get('/api/v1/user/adminaccount')
  480. getRes.should.be.json
  481. getRes.body.should.have.property('description', 'new description here')
  482. })
  483. it('should return an error if username is not logged in', done => {
  484. agent
  485. .put('/api/v1/user/notloggedin')
  486. .set('content-type', 'application/json')
  487. .send({
  488. description: 'new description here'
  489. })
  490. .end((err, res) => {
  491. res.should.be.json
  492. res.should.have.status(400)
  493. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  494. done()
  495. })
  496. })
  497. it('should return an error if description is not a string', done => {
  498. agent
  499. .put('/api/v1/user/adminaccount')
  500. .set('content-type', 'application/json')
  501. .send({
  502. description: 123
  503. })
  504. .end((err, res) => {
  505. res.should.be.json
  506. res.should.have.status(400)
  507. res.body.errors.should.contain.something.that.has.property('message', 'description must be a string')
  508. done()
  509. })
  510. })
  511. it('should return an error if description is too long', done => {
  512. let str = []
  513. for(var i = 0; i < 2000; i++) { str.push('a') }
  514. agent
  515. .put('/api/v1/user/adminaccount')
  516. .set('content-type', 'application/json')
  517. .send({
  518. description: str.join('')
  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 less than 1024 characters')
  524. done()
  525. })
  526. })
  527. it('should update user password', async () => {
  528. let passwordAgent = chai.request.agent(server)
  529. await passwordAgent
  530. .post('/api/v1/user/adminaccount/login')
  531. .set('content-type', 'application/json')
  532. .send({
  533. password: 'password'
  534. })
  535. let putRes = await passwordAgent
  536. .put('/api/v1/user/adminaccount')
  537. .set('content-type', 'application/json')
  538. .send({
  539. currentPassword: 'password',
  540. newPassword: 'qwertyuiop'
  541. })
  542. putRes.should.be.json
  543. putRes.body.should.have.property('success', true)
  544. await passwordAgent.post('/api/v1/user/adminaccount/logout')
  545. let loginRes = await passwordAgent
  546. .post('/api/v1/user/adminaccount/login')
  547. .set('content-type', 'application/json')
  548. .send({
  549. password: 'qwertyuiop'
  550. })
  551. loginRes.should.have.status(200)
  552. loginRes.should.be.json
  553. loginRes.should.have.cookie('username', 'adminaccount')
  554. })
  555. it('should return an error if username is not logged in', done => {
  556. agent
  557. .put('/api/v1/user/notloggedin')
  558. .set('content-type', 'application/json')
  559. .send({
  560. currentPassword: 'qwertyuiop',
  561. newPassword: 'azertyuiop'
  562. })
  563. .end((err, res) => {
  564. res.should.have.status(400)
  565. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  566. done()
  567. })
  568. })
  569. it('should return an error if current password is incorrect', done => {
  570. agent
  571. .put('/api/v1/user/adminaccount')
  572. .set('content-type', 'application/json')
  573. .send({
  574. currentPassword: 'nottheirpassword',
  575. newPassword: 'azertyuiop'
  576. })
  577. .end((err, res) => {
  578. res.should.have.status(400)
  579. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  580. done()
  581. })
  582. })
  583. it('should return an error if password is the same', done => {
  584. agent
  585. .put('/api/v1/user/adminaccount')
  586. .set('content-type', 'application/json')
  587. .send({
  588. currentPassword: 'qwertyuiop',
  589. newPassword: 'qwertyuiop'
  590. })
  591. .end((err, res) => {
  592. res.should.have.status(400)
  593. res.body.errors.should.contain.something.that.deep.equals(Errors.passwordSame)
  594. done()
  595. })
  596. })
  597. it('should return an error if password is too short', done => {
  598. agent
  599. .put('/api/v1/user/adminaccount')
  600. .set('content-type', 'application/json')
  601. .send({
  602. currentPassword: 'qwertyuiop',
  603. newPassword: ''
  604. })
  605. .end((err, res) => {
  606. res.should.have.status(400)
  607. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  608. done()
  609. })
  610. })
  611. it('should return an error if password is too long', done => {
  612. let str = []
  613. for(var i = 0; i < 2000; i++) { str.push('a') }
  614. agent
  615. .put('/api/v1/user/adminaccount')
  616. .set('content-type', 'application/json')
  617. .send({
  618. currentPassword: 'qwertyuiop',
  619. newPassword: str.join('')
  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 missing currentPassword', done => {
  628. agent
  629. .put('/api/v1/user/adminaccount')
  630. .set('content-type', 'application/json')
  631. .send({
  632. newPassword: 'qwertyujkjnbgfdswazxcvbhytr'
  633. })
  634. .end((err, res) => {
  635. res.should.have.status(200)
  636. res.body.should.deep.equal({ success: false })
  637. done()
  638. })
  639. })
  640. })
  641. describe('/:username DELETE', () => {
  642. let agent = chai.request.agent(server)
  643. before(async () => {
  644. await agent.post('/api/v1/user/adminaccount/login')
  645. .set('content-type', 'application/json')
  646. .send({
  647. password: 'qwertyuiop'
  648. })
  649. })
  650. it('should delete the user', done => {
  651. agent
  652. .delete('/api/v1/user/adminaccount')
  653. .then(res => {
  654. res.should.be.json
  655. res.body.should.have.property('success', true)
  656. res.should.not.have.cookie('username')
  657. agent
  658. .post('/api/v1/user/adminaccount/login')
  659. .set('content-type', 'application/json')
  660. .send({
  661. password: 'qwertyuiop'
  662. })
  663. .end((err, res) => {
  664. res.should.have.status(401)
  665. res.body.should.have.property('errors')
  666. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  667. done()
  668. })
  669. })
  670. .catch(err => {
  671. console.log(err)
  672. done()
  673. })
  674. })
  675. it('should return an error if username is not logged in', done => {
  676. agent
  677. .delete('/api/v1/user/notloggedin')
  678. .end((err, res) => {
  679. res.should.have.status(401)
  680. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  681. done()
  682. })
  683. })
  684. })
  685. })