user.js 22 KB

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