user.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 { sequelize } = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. chai.use(require('chai-http'))
  9. chai.use(require('chai-things'))
  10. describe('User', () => {
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) done()
  14. server.on('appStarted', () => {
  15. done()
  16. })
  17. })
  18. //Delete all rows in table after
  19. //tests completed
  20. after(() => sequelize.sync({ force: true }) )
  21. describe('/ POST user', () => {
  22. it('should create an account', (done) => {
  23. chai.request(server)
  24. .post('/api/v1/user')
  25. .set('content-type', 'application/x-www-form-urlencoded')
  26. .send({
  27. username: 'username',
  28. password: 'password'
  29. })
  30. .end((err, res) => {
  31. res.should.have.status(200)
  32. res.should.be.json
  33. res.body.should.have.property('username', 'username')
  34. res.body.should.have.property('hash')
  35. res.body.should.have.property('color')
  36. res.body.color.should.not.be.null
  37. done()
  38. })
  39. })
  40. it('should create an admin account if no is already created', (done) => {
  41. chai.request(server)
  42. .post('/api/v1/user')
  43. .set('content-type', 'application/json')
  44. .send({
  45. username: 'adminaccount',
  46. password: 'password',
  47. admin: true
  48. })
  49. .end((err, res) => {
  50. res.should.have.status(200)
  51. res.body.should.have.property('username', 'adminaccount')
  52. res.body.should.have.property('hash')
  53. res.body.should.have.property('admin', true)
  54. done()
  55. })
  56. })
  57. it('should give an error if an admin account is already created and no token is provided', (done) => {
  58. chai.request(server)
  59. .post('/api/v1/user')
  60. .set('content-type', 'application/json')
  61. .send({
  62. username: 'adminaccount1',
  63. password: 'password',
  64. admin: true
  65. })
  66. .end((err, res) => {
  67. res.should.have.status(400)
  68. res.should.be.json
  69. res.body.should.have.property('errors')
  70. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('token'))
  71. done()
  72. })
  73. })
  74. it('should give an error if admin and token fields are not of the correct type ', (done) => {
  75. chai.request(server)
  76. .post('/api/v1/user')
  77. .set('content-type', 'application/json')
  78. .send({
  79. username: 'adminaccount1',
  80. password: 'password',
  81. admin: 'not a boolean',
  82. token: 123
  83. })
  84. .end((err, res) => {
  85. res.should.have.status(400)
  86. res.should.be.json
  87. res.body.should.have.property('errors')
  88. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('admin', 'boolean'))
  89. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('token', 'string'))
  90. done()
  91. })
  92. })
  93. it('should give an error if an admin account is already created and token is invalid', (done) => {
  94. chai.request(server)
  95. .post('/api/v1/user')
  96. .set('content-type', 'application/json')
  97. .send({
  98. username: 'adminaccount1',
  99. password: 'password',
  100. admin: true,
  101. token: 'invalid_token'
  102. })
  103. .end((err, res) => {
  104. res.should.have.status(401)
  105. res.should.be.json
  106. res.body.should.have.property('errors')
  107. res.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  108. done()
  109. })
  110. })
  111. it('should create an admin account provided with a token', async () => {
  112. let agent = chai.request.agent(server)
  113. await agent
  114. .post('/api/v1/user/adminaccount/login')
  115. .set('content-type', 'application/json')
  116. .send({
  117. password: 'password'
  118. })
  119. let tokenRes = await agent.post('/api/v1/admin_token')
  120. let token = tokenRes.body.token
  121. let accountRes = await chai.request(server)
  122. .post('/api/v1/user')
  123. .set('content-type', 'application/json')
  124. .send({
  125. username: 'adminaccount1',
  126. password: 'password',
  127. admin: true,
  128. token: token
  129. })
  130. accountRes.should.have.status(200)
  131. accountRes.should.be.json
  132. accountRes.body.should.have.property('admin', true)
  133. accountRes.body.should.have.property('username', 'adminaccount1')
  134. accountRes.body.should.have.property('hash')
  135. try {
  136. let invalidAccountRes = await chai.request(server)
  137. .post('/api/v1/user')
  138. .set('content-type', 'application/json')
  139. .send({
  140. username: 'adminaccount2',
  141. password: 'password',
  142. admin: true,
  143. token: token
  144. })
  145. invalidAccountRes.should.have.status(401)
  146. invalidAccountRes.should.be.json
  147. invalidAccountRes.body.should.have.property('errors')
  148. invalidAccountRes.body.errors.should.include.something.that.deep.equals(Errors.invalidToken)
  149. } catch (res) {
  150. res.should.have.status(401)
  151. JSON.parse(res.response.text).errors.should.include.something.that.deep.equals(Errors.invalidToken)
  152. }
  153. })
  154. it('should throw an error if account already created', (done) => {
  155. chai.request(server)
  156. .post('/api/v1/user')
  157. .set('content-type', 'application/x-www-form-urlencoded')
  158. .send({
  159. username: 'username',
  160. password: 'password'
  161. })
  162. .end((err, res) => {
  163. res.should.have.status(400)
  164. res.should.be.json
  165. res.body.should.have.property('errors')
  166. res.body.errors.should.include.something.that.deep.equals(Errors.accountAlreadyCreated)
  167. done()
  168. })
  169. })
  170. it('should throw an error if no username or password', (done) => {
  171. chai.request(server)
  172. .post('/api/v1/user')
  173. .set('content-type', 'application/x-www-form-urlencoded')
  174. .send({})
  175. .end((err, res) => {
  176. res.should.have.status(400)
  177. res.should.be.json
  178. res.body.should.have.property('errors')
  179. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('username'))
  180. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('password'))
  181. done()
  182. })
  183. })
  184. it('should throw an error if username or password are not a string', (done) => {
  185. chai.request(server)
  186. .post('/api/v1/user')
  187. .set('content-type', 'application/json')
  188. .send({
  189. username: 123,
  190. password: 123
  191. })
  192. .end((err, res) => {
  193. res.should.have.status(400)
  194. res.should.be.json
  195. res.body.should.have.property('errors')
  196. res.body.should.have.property('errors')
  197. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('username', 'string'))
  198. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('password', 'string'))
  199. done()
  200. })
  201. })
  202. it('should throw an error if username or password less than 6 characters', (done) => {
  203. chai.request(server)
  204. .post('/api/v1/user')
  205. .set('content-type', 'application/x-www-form-urlencoded')
  206. .send({
  207. username: 'test',
  208. password: 'pass'
  209. })
  210. .end((err, res) => {
  211. res.should.have.status(400)
  212. res.should.be.json
  213. res.body.should.have.property('errors')
  214. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('username', '6'))
  215. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('password', '6'))
  216. done()
  217. })
  218. })
  219. it('should throw an error if username greater than 50 characters or password is greater than 100 characters', (done) => {
  220. chai.request(server)
  221. .post('/api/v1/user')
  222. .set('content-type', 'application/x-www-form-urlencoded')
  223. .send({
  224. username: '123456789012345678901234567890123456789012345678901',
  225. password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
  226. })
  227. .end((err, res) => {
  228. res.should.have.status(400)
  229. res.should.be.json
  230. res.body.should.have.property('errors')
  231. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('username', '50'))
  232. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('password', '100'))
  233. done()
  234. })
  235. })
  236. })
  237. describe('/:username GET user', () => {
  238. it('should return the user', async () => {
  239. let res = await chai.request(server)
  240. .get('/api/v1/user/username')
  241. res.should.have.status(200)
  242. res.should.be.json
  243. res.body.should.have.property('username', 'username')
  244. res.body.should.have.property('color')
  245. res.body.should.not.have.property('hash')
  246. })
  247. it('should return an error if username invalid', async () => {
  248. try {
  249. let res = await chai.request(server)
  250. .get('/api/v1/user/not_a_user')
  251. res.should.have.status(400)
  252. res.body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  253. } catch(res) {
  254. let body = JSON.parse(res.response.text)
  255. res.should.have.status(400)
  256. body.errors.should.contain.something.that.deep.equals(Errors.accountDoesNotExist)
  257. }
  258. })
  259. it('should get posts as well if posts query is appended', async () => {
  260. let agent = chai.request.agent(server)
  261. await agent
  262. .post('/api/v1/user/adminaccount/login')
  263. .set('content-type', 'application/x-www-form-urlencoded')
  264. .send({ password: 'password' })
  265. await agent
  266. .post('/api/v1/category')
  267. .set('content-type', 'application/x-www-form-urlencoded')
  268. .send({ name: 'categorynamehere' })
  269. await agent
  270. .post('/api/v1/thread')
  271. .set('content-type', 'application/x-www-form-urlencoded')
  272. .send({ name: 'a thread name', category: 'categorynamehere' })
  273. await agent
  274. .post('/api/v1/post')
  275. .set('content-type', 'application/json')
  276. .send({ threadId: 1, content: 'content for post' })
  277. await agent
  278. .post('/api/v1/post')
  279. .set('content-type', 'application/json')
  280. .send({ threadId: 1, content: 'content for another post' })
  281. let res = await agent
  282. .get('/api/v1/user/adminaccount?posts=true')
  283. res.should.be.json
  284. res.should.have.status(200)
  285. res.body.should.have.property('username', 'adminaccount')
  286. res.body.should.have.property('Posts')
  287. res.body.Posts.should.have.property('length', 2)
  288. })
  289. })
  290. describe('/:username/login POST user', () => {
  291. let agent = chai.request.agent(server)
  292. it('should throw an error if invalid username is provided', (done) => {
  293. chai.request(server)
  294. .post('/api/v1/user/invalid_username/login')
  295. .set('content-type', 'application/x-www-form-urlencoded')
  296. .send({
  297. password: 'password'
  298. })
  299. .end((err, res) => {
  300. res.should.have.status(401)
  301. res.body.should.have.property('errors')
  302. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  303. done()
  304. })
  305. })
  306. it('should throw an error if invalid password is provided', (done) => {
  307. chai.request(server)
  308. .post('/api/v1/user/username/login')
  309. .set('content-type', 'application/x-www-form-urlencoded')
  310. .send({
  311. password: 'invalid_password'
  312. })
  313. .end((err, res) => {
  314. res.should.have.status(401)
  315. res.body.should.have.property('errors')
  316. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  317. res.should.not.have.cookie('username')
  318. done()
  319. })
  320. })
  321. it('should log in the user', (done) => {
  322. agent
  323. .post('/api/v1/user/username/login')
  324. .set('content-type', 'application/x-www-form-urlencoded')
  325. .send({
  326. password: 'password'
  327. })
  328. .end((err, res) => {
  329. res.should.have.status(200)
  330. res.should.be.json
  331. res.should.have.cookie('username', 'username')
  332. if(err) {
  333. done(err)
  334. } else {
  335. done()
  336. }
  337. })
  338. })
  339. })
  340. describe('/:username/logout POST user', () => {
  341. let agent = chai.request.agent(server)
  342. it('should log out the user', (done) => {
  343. agent
  344. .post('/api/v1/user/login')
  345. .set('content-type', 'application/x-www-form-urlencoded')
  346. .send({
  347. username: 'username',
  348. password: 'password'
  349. })
  350. .end((err, res) => {
  351. agent
  352. .post('/api/v1/user/username/logout')
  353. .end((err, res) => {
  354. res.should.have.status(200)
  355. res.should.not.have.cookie('username')
  356. if(err) {
  357. done(err)
  358. } else {
  359. done()
  360. }
  361. })
  362. })
  363. })
  364. })
  365. })