user.js 24 KB

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