user.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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(400)
  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 admin and token fields are not of the correct type ', (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: 'not a boolean',
  83. token: 123
  84. })
  85. .end((err, res) => {
  86. res.should.have.status(400)
  87. res.should.be.json
  88. res.body.should.have.property('errors')
  89. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('admin', 'boolean'))
  90. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('token', 'string'))
  91. done()
  92. })
  93. })
  94. it('should give an error if an admin account is already created and token is invalid', (done) => {
  95. chai.request(server)
  96. .post('/api/v1/user')
  97. .set('content-type', 'application/json')
  98. .send({
  99. username: 'adminaccount1',
  100. password: 'password',
  101. admin: true,
  102. token: 'invalid_token'
  103. })
  104. .end((err, res) => {
  105. res.should.have.status(401)
  106. res.should.be.json
  107. res.body.should.have.property('errors')
  108. res.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  109. done()
  110. })
  111. })
  112. it('should create an admin account provided with a token', async () => {
  113. let agent = chai.request.agent(server)
  114. await agent
  115. .post('/api/v1/user/adminaccount/login')
  116. .set('content-type', 'application/json')
  117. .send({
  118. password: 'password'
  119. })
  120. let tokenRes = await agent.post('/api/v1/admin_token')
  121. let token = tokenRes.body.token
  122. let accountRes = await chai.request(server)
  123. .post('/api/v1/user')
  124. .set('content-type', 'application/json')
  125. .send({
  126. username: 'adminaccount1',
  127. password: 'password',
  128. admin: true,
  129. token: token
  130. })
  131. accountRes.should.have.status(200)
  132. accountRes.should.be.json
  133. accountRes.body.should.have.property('admin', true)
  134. accountRes.body.should.have.property('username', 'adminaccount1')
  135. accountRes.body.should.have.property('hash')
  136. try {
  137. let invalidAccountRes = await chai.request(server)
  138. .post('/api/v1/user')
  139. .set('content-type', 'application/json')
  140. .send({
  141. username: 'adminaccount2',
  142. password: 'password',
  143. admin: true,
  144. token: token
  145. })
  146. invalidAccountRes.should.have.status(401)
  147. invalidAccountRes.should.be.json
  148. invalidAccountRes.body.should.have.property('errors')
  149. invalidAccountRes.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  150. } catch (res) {
  151. res.should.have.status(401)
  152. JSON.parse(res.response.text).errors.should.include.something.that.deep.equals(Errors.invalidToken)
  153. }
  154. })
  155. it('should throw an error if account already created', (done) => {
  156. chai.request(server)
  157. .post('/api/v1/user')
  158. .set('content-type', 'application/x-www-form-urlencoded')
  159. .send({
  160. username: 'username',
  161. password: 'password'
  162. })
  163. .end((err, res) => {
  164. res.should.have.status(400)
  165. res.should.be.json
  166. res.body.should.have.property('errors')
  167. res.body.errors.should.include.something.that.deep.equals(Errors.accountAlreadyCreated)
  168. done()
  169. })
  170. })
  171. it('should throw an error if no username or password', (done) => {
  172. chai.request(server)
  173. .post('/api/v1/user')
  174. .set('content-type', 'application/x-www-form-urlencoded')
  175. .send({})
  176. .end((err, res) => {
  177. res.should.have.status(400)
  178. res.should.be.json
  179. res.body.should.have.property('errors')
  180. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('username'))
  181. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('password'))
  182. done()
  183. })
  184. })
  185. it('should throw an error if username or password are not a string', (done) => {
  186. chai.request(server)
  187. .post('/api/v1/user')
  188. .set('content-type', 'application/json')
  189. .send({
  190. username: 123,
  191. password: 123
  192. })
  193. .end((err, res) => {
  194. res.should.have.status(400)
  195. res.should.be.json
  196. res.body.should.have.property('errors')
  197. res.body.should.have.property('errors')
  198. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('username', 'string'))
  199. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('password', 'string'))
  200. done()
  201. })
  202. })
  203. it('should throw an error if username or password less than 6 characters', (done) => {
  204. chai.request(server)
  205. .post('/api/v1/user')
  206. .set('content-type', 'application/x-www-form-urlencoded')
  207. .send({
  208. username: 'test',
  209. password: 'pass'
  210. })
  211. .end((err, res) => {
  212. res.should.have.status(400)
  213. res.should.be.json
  214. res.body.should.have.property('errors')
  215. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('username', '6'))
  216. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('password', '6'))
  217. done()
  218. })
  219. })
  220. it('should throw an error if username greater than 50 characters or password is greater than 100 characters', (done) => {
  221. chai.request(server)
  222. .post('/api/v1/user')
  223. .set('content-type', 'application/x-www-form-urlencoded')
  224. .send({
  225. username: '123456789012345678901234567890123456789012345678901',
  226. password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
  227. })
  228. .end((err, res) => {
  229. res.should.have.status(400)
  230. res.should.be.json
  231. res.body.should.have.property('errors')
  232. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('username', '50'))
  233. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('password', '100'))
  234. done()
  235. })
  236. })
  237. })
  238. describe('/:username GET user', () => {
  239. it('should return the user', async () => {
  240. let res = await chai.request(server)
  241. .get('/api/v1/user/username')
  242. res.should.have.status(200)
  243. res.should.be.json
  244. res.body.should.have.property('username', 'username')
  245. res.body.should.have.property('color')
  246. res.body.should.not.have.property('hash')
  247. })
  248. it('should return an error if username invalid', async () => {
  249. try {
  250. let res = await chai.request(server)
  251. .get('/api/v1/user/not_a_user')
  252. res.should.have.status(400)
  253. res.body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  254. } catch(res) {
  255. let body = JSON.parse(res.response.text)
  256. res.should.have.status(400)
  257. body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  258. }
  259. })
  260. it('should get posts as well if posts query is appended', async () => {
  261. let agent = chai.request.agent(server)
  262. await agent
  263. .post('/api/v1/user/adminaccount/login')
  264. .set('content-type', 'application/x-www-form-urlencoded')
  265. .send({ password: 'password' })
  266. await agent
  267. .post('/api/v1/category')
  268. .set('content-type', 'application/x-www-form-urlencoded')
  269. .send({ name: 'categorynamehere' })
  270. await agent
  271. .post('/api/v1/thread')
  272. .set('content-type', 'application/x-www-form-urlencoded')
  273. .send({ name: 'a thread name', category: 'categorynamehere' })
  274. await agent
  275. .post('/api/v1/post')
  276. .set('content-type', 'application/json')
  277. .send({ threadId: 1, content: 'content for post' })
  278. await agent
  279. .post('/api/v1/post')
  280. .set('content-type', 'application/json')
  281. .send({ threadId: 1, content: 'content for another post' })
  282. let res = await agent
  283. .get('/api/v1/user/adminaccount?posts=true')
  284. res.should.be.json
  285. res.should.have.status(200)
  286. res.body.should.have.property('username', 'adminaccount')
  287. res.body.should.have.property('Posts')
  288. res.body.Posts.should.have.property('length', 2)
  289. res.body.Posts[0].should.have.deep.property('Thread.id', 1)
  290. res.body.Posts[0].should.have.deep.property('User.username', 'adminaccount')
  291. })
  292. it('should allow pagination', async () => {
  293. let agent = chai.request.agent(server)
  294. await agent
  295. .post('/api/v1/user')
  296. .set('content-type', 'application/x-www-form-urlencoded')
  297. .send({ username: 'paginationaccount', password: 'password' })
  298. let thread = await agent
  299. .post('/api/v1/thread')
  300. .set('content-type', 'application/json')
  301. .send({ category: 'categorynamehere', name: 'pagination' })
  302. for(var i = 0; i < 30; i++) {
  303. await agent
  304. .post('/api/v1/post')
  305. .set('content-type', 'application/json')
  306. .send({ threadId: thread.body.id, content: `POST ${i}` })
  307. }
  308. let pageOne = await agent.get('/api/v1/user/paginationaccount?posts=true')
  309. let pageTwo = await agent.get(pageOne.body.meta.nextURL)
  310. let pageThree = await agent.get(pageTwo.body.meta.nextURL)
  311. let pageInvalid = await agent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)
  312. pageOne.body.Posts.should.have.length(10)
  313. pageOne.body.Posts[0].should.have.property('content', '<p>POST 0</p>\n')
  314. pageTwo.body.Posts.should.have.length(10)
  315. pageTwo.body.Posts[0].should.have.property('content', '<p>POST 10</p>\n')
  316. pageThree.body.Posts.should.have.length(10)
  317. pageThree.body.Posts[0].should.have.property('content', '<p>POST 20</p>\n')
  318. pageThree.body.Posts[9].should.have.property('content', '<p>POST 29</p>\n')
  319. expect(pageThree.body.meta.nextURL).to.be.null
  320. pageInvalid.body.Posts.should.have.length(0)
  321. })
  322. })
  323. describe('/:username/login POST user', () => {
  324. let agent = chai.request.agent(server)
  325. it('should throw an error if invalid username is provided', (done) => {
  326. chai.request(server)
  327. .post('/api/v1/user/invalid_username/login')
  328. .set('content-type', 'application/x-www-form-urlencoded')
  329. .send({
  330. password: 'password'
  331. })
  332. .end((err, res) => {
  333. res.should.have.status(401)
  334. res.body.should.have.property('errors')
  335. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  336. done()
  337. })
  338. })
  339. it('should throw an error if invalid password is provided', (done) => {
  340. chai.request(server)
  341. .post('/api/v1/user/username/login')
  342. .set('content-type', 'application/x-www-form-urlencoded')
  343. .send({
  344. password: 'invalid_password'
  345. })
  346. .end((err, res) => {
  347. res.should.have.status(401)
  348. res.body.should.have.property('errors')
  349. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  350. res.should.not.have.cookie('username')
  351. done()
  352. })
  353. })
  354. it('should log in the user', (done) => {
  355. agent
  356. .post('/api/v1/user/username/login')
  357. .set('content-type', 'application/x-www-form-urlencoded')
  358. .send({
  359. password: 'password'
  360. })
  361. .end((err, res) => {
  362. res.should.have.status(200)
  363. res.should.be.json
  364. res.should.have.cookie('username', 'username')
  365. if(err) {
  366. done(err)
  367. } else {
  368. done()
  369. }
  370. })
  371. })
  372. })
  373. describe('/:username/logout POST user', () => {
  374. let agent = chai.request.agent(server)
  375. it('should log out the user', (done) => {
  376. agent
  377. .post('/api/v1/user/login')
  378. .set('content-type', 'application/x-www-form-urlencoded')
  379. .send({
  380. username: 'username',
  381. password: 'password'
  382. })
  383. .end((err, res) => {
  384. agent
  385. .post('/api/v1/user/username/logout')
  386. .end((err, res) => {
  387. res.should.have.status(200)
  388. res.should.not.have.cookie('username')
  389. if(err) {
  390. done(err)
  391. } else {
  392. done()
  393. }
  394. })
  395. })
  396. })
  397. })
  398. })