user.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 } = 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/permissions PUT', () => {
  446. let admin = chai.request.agent(server)
  447. let user = chai.request.agent(server)
  448. before(async () => {
  449. await admin.post('/api/v1/user/adminaccount/login')
  450. .set('content-type', 'application/json')
  451. .send({
  452. password: 'password'
  453. })
  454. await admin.post('/api/v1/category')
  455. .set('content-type', 'application/json')
  456. .send({
  457. name: 'category'
  458. })
  459. await admin.post('/api/v1/thread')
  460. .set('content-type', 'application/json')
  461. .send({
  462. category: 'category',
  463. name: 'thread'
  464. })
  465. await user.post('/api/v1/user')
  466. .set('content-type', 'application/json')
  467. .send({
  468. username: 'user123',
  469. password: 'password'
  470. })
  471. })
  472. it('should update the permissions for the user', async () => {
  473. let res = await admin
  474. .put('/api/v1/user/user123/permissions')
  475. .set('content-type', 'application/json')
  476. .send({
  477. canCreatePosts: false
  478. })
  479. res.should.be.json
  480. res.should.have.status(200)
  481. let user = await User.findOne({
  482. where: { username: 'user123' }
  483. })
  484. user.should.have.property('canCreatePosts', false)
  485. })
  486. it('should return an error if not an administrator', done => {
  487. user
  488. .put('/api/v1/user/user123/permissions')
  489. .set('content-type', 'application/json')
  490. .send({
  491. canCreatePosts: true
  492. })
  493. .end((err, res) => {
  494. res.should.be.json
  495. res.should.have.status(400)
  496. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  497. done()
  498. })
  499. })
  500. it('should return an error if trying to post replies if permissions so set', done => {
  501. user
  502. .post('/api/v1/post')
  503. .set('content-type', 'application/json')
  504. .send({
  505. threadId: 1,
  506. content: 'post'
  507. })
  508. .end((err, res) => {
  509. res.should.be.json
  510. res.should.have.status(400)
  511. res.body.errors.should.contain.something.with.property('message', 'You have been banned from posting')
  512. done()
  513. })
  514. })
  515. it('should return an error if trying to create thread if permissions so set', done => {
  516. user
  517. .put('/api/v1/user/user123/permissions')
  518. .set('content-type', 'application/json')
  519. .send({
  520. canCreateThreads: false
  521. })
  522. .end((err, res) => {
  523. user
  524. .post('/api/v1/thread')
  525. .set('content-type', 'application/json')
  526. .send({
  527. category: 'category',
  528. name: 'thread name'
  529. })
  530. .end((err, res) => {
  531. res.should.be.json
  532. res.should.have.status(400)
  533. res.body.errors.should.contain.something.with.property('message', 'You have been banned from creating threads')
  534. done()
  535. })
  536. })
  537. })
  538. })
  539. describe('/:username PUT user', () => {
  540. let agent = chai.request.agent(server)
  541. before(async () => {
  542. await agent
  543. .post('/api/v1/user/adminaccount/login')
  544. .set('content-type', 'application/json')
  545. .send({
  546. password: 'password'
  547. })
  548. })
  549. it('should add user description if it doesn\'t already exist', async () => {
  550. let putRes = await agent
  551. .put('/api/v1/user/adminaccount')
  552. .set('content-type', 'application/json')
  553. .send({
  554. description: 'description here'
  555. })
  556. putRes.should.be.json
  557. putRes.body.should.have.property('success', true)
  558. let getRes = await agent.get('/api/v1/user/adminaccount')
  559. getRes.should.be.json
  560. getRes.body.should.have.property('description', 'description here')
  561. getRes.body.should.have.property('username', 'adminaccount')
  562. getRes.body.should.have.property('color')
  563. })
  564. it('should update user description if it already exists', async () => {
  565. let putRes = await agent
  566. .put('/api/v1/user/adminaccount')
  567. .set('content-type', 'application/json')
  568. .send({
  569. description: 'new description here'
  570. })
  571. putRes.should.be.json
  572. putRes.body.should.have.property('success', true)
  573. let getRes = await agent.get('/api/v1/user/adminaccount')
  574. getRes.should.be.json
  575. getRes.body.should.have.property('description', 'new description here')
  576. })
  577. it('should return an error if username is not logged in', done => {
  578. agent
  579. .put('/api/v1/user/notloggedin')
  580. .set('content-type', 'application/json')
  581. .send({
  582. description: 'new description here'
  583. })
  584. .end((err, res) => {
  585. res.should.be.json
  586. res.should.have.status(400)
  587. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  588. done()
  589. })
  590. })
  591. it('should return an error if description is not a string', done => {
  592. agent
  593. .put('/api/v1/user/adminaccount')
  594. .set('content-type', 'application/json')
  595. .send({
  596. description: 123
  597. })
  598. .end((err, res) => {
  599. res.should.be.json
  600. res.should.have.status(400)
  601. res.body.errors.should.contain.something.that.has.property('message', 'description must be a string')
  602. done()
  603. })
  604. })
  605. it('should return an error if description is too long', done => {
  606. let str = []
  607. for(var i = 0; i < 2000; i++) { str.push('a') }
  608. agent
  609. .put('/api/v1/user/adminaccount')
  610. .set('content-type', 'application/json')
  611. .send({
  612. description: str.join('')
  613. })
  614. .end((err, res) => {
  615. res.should.be.json
  616. res.should.have.status(400)
  617. res.body.errors.should.contain.something.that.has.property('message', 'description must be less than 1024 characters')
  618. done()
  619. })
  620. })
  621. it('should update user password', async () => {
  622. let passwordAgent = chai.request.agent(server)
  623. await passwordAgent
  624. .post('/api/v1/user/adminaccount/login')
  625. .set('content-type', 'application/json')
  626. .send({
  627. password: 'password'
  628. })
  629. let putRes = await passwordAgent
  630. .put('/api/v1/user/adminaccount')
  631. .set('content-type', 'application/json')
  632. .send({
  633. currentPassword: 'password',
  634. newPassword: 'qwertyuiop'
  635. })
  636. putRes.should.be.json
  637. putRes.body.should.have.property('success', true)
  638. await passwordAgent.post('/api/v1/user/adminaccount/logout')
  639. let loginRes = await passwordAgent
  640. .post('/api/v1/user/adminaccount/login')
  641. .set('content-type', 'application/json')
  642. .send({
  643. password: 'qwertyuiop'
  644. })
  645. loginRes.should.have.status(200)
  646. loginRes.should.be.json
  647. loginRes.should.have.cookie('username', 'adminaccount')
  648. })
  649. it('should return an error if username is not logged in', done => {
  650. agent
  651. .put('/api/v1/user/notloggedin')
  652. .set('content-type', 'application/json')
  653. .send({
  654. currentPassword: 'qwertyuiop',
  655. newPassword: 'azertyuiop'
  656. })
  657. .end((err, res) => {
  658. res.should.have.status(400)
  659. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  660. done()
  661. })
  662. })
  663. it('should return an error if current password is incorrect', done => {
  664. agent
  665. .put('/api/v1/user/adminaccount')
  666. .set('content-type', 'application/json')
  667. .send({
  668. currentPassword: 'nottheirpassword',
  669. newPassword: 'azertyuiop'
  670. })
  671. .end((err, res) => {
  672. res.should.have.status(400)
  673. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  674. done()
  675. })
  676. })
  677. it('should return an error if password is the same', done => {
  678. agent
  679. .put('/api/v1/user/adminaccount')
  680. .set('content-type', 'application/json')
  681. .send({
  682. currentPassword: 'qwertyuiop',
  683. newPassword: 'qwertyuiop'
  684. })
  685. .end((err, res) => {
  686. res.should.have.status(400)
  687. res.body.errors.should.contain.something.that.deep.equals(Errors.passwordSame)
  688. done()
  689. })
  690. })
  691. it('should return an error if password is too short', done => {
  692. agent
  693. .put('/api/v1/user/adminaccount')
  694. .set('content-type', 'application/json')
  695. .send({
  696. currentPassword: 'qwertyuiop',
  697. newPassword: ''
  698. })
  699. .end((err, res) => {
  700. res.should.have.status(400)
  701. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  702. done()
  703. })
  704. })
  705. it('should return an error if password is too long', done => {
  706. let str = []
  707. for(var i = 0; i < 2000; i++) { str.push('a') }
  708. agent
  709. .put('/api/v1/user/adminaccount')
  710. .set('content-type', 'application/json')
  711. .send({
  712. currentPassword: 'qwertyuiop',
  713. newPassword: str.join('')
  714. })
  715. .end((err, res) => {
  716. res.should.have.status(400)
  717. res.body.errors.should.contain.something.that.has.property('message', 'password must be between 6 and 100 characters')
  718. done()
  719. })
  720. })
  721. it('should return an error if missing currentPassword', done => {
  722. agent
  723. .put('/api/v1/user/adminaccount')
  724. .set('content-type', 'application/json')
  725. .send({
  726. newPassword: 'qwertyujkjnbgfdswazxcvbhytr'
  727. })
  728. .end((err, res) => {
  729. res.should.have.status(200)
  730. res.body.should.deep.equal({ success: false })
  731. done()
  732. })
  733. })
  734. })
  735. describe('/:username DELETE', () => {
  736. let agent = chai.request.agent(server)
  737. before(async () => {
  738. await agent.post('/api/v1/user/adminaccount/login')
  739. .set('content-type', 'application/json')
  740. .send({
  741. password: 'qwertyuiop'
  742. })
  743. })
  744. it('should delete the user', done => {
  745. agent
  746. .delete('/api/v1/user/adminaccount')
  747. .then(res => {
  748. res.should.be.json
  749. res.body.should.have.property('success', true)
  750. res.should.not.have.cookie('username')
  751. agent
  752. .post('/api/v1/user/adminaccount/login')
  753. .set('content-type', 'application/json')
  754. .send({
  755. password: 'qwertyuiop'
  756. })
  757. .end((err, res) => {
  758. res.should.have.status(401)
  759. res.body.should.have.property('errors')
  760. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  761. done()
  762. })
  763. })
  764. .catch(err => {
  765. console.log(err)
  766. done()
  767. })
  768. })
  769. it('should return an error if username is not logged in', done => {
  770. agent
  771. .delete('/api/v1/user/notloggedin')
  772. .end((err, res) => {
  773. res.should.have.status(401)
  774. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  775. done()
  776. })
  777. })
  778. })
  779. })