user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 log in the user after creating an account', (done) => {
  155. let agent = chai.request.agent(server)
  156. agent
  157. .post('/api/v1/user')
  158. .set('content-type', 'application/x-www-form-urlencoded')
  159. .send({
  160. username: 'username1',
  161. password: 'password'
  162. })
  163. .end((err, res) => {
  164. agent
  165. .get('/api/v1/user/username1')
  166. .then((res) => {
  167. res.should.have.status(200)
  168. done()
  169. })
  170. .catch(done)
  171. })
  172. })
  173. it('should throw an error if account already created', (done) => {
  174. chai.request(server)
  175. .post('/api/v1/user')
  176. .set('content-type', 'application/x-www-form-urlencoded')
  177. .send({
  178. username: 'username',
  179. password: 'password'
  180. })
  181. .end((err, res) => {
  182. res.should.have.status(400)
  183. res.should.be.json
  184. res.body.should.have.property('errors')
  185. res.body.errors.should.include.something.that.deep.equals(Errors.accountAlreadyCreated)
  186. done()
  187. })
  188. })
  189. it('should throw an error if no username or password', (done) => {
  190. chai.request(server)
  191. .post('/api/v1/user')
  192. .set('content-type', 'application/x-www-form-urlencoded')
  193. .send({})
  194. .end((err, res) => {
  195. res.should.have.status(400)
  196. res.should.be.json
  197. res.body.should.have.property('errors')
  198. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('username'))
  199. res.body.errors.should.include.something.that.deep.equals(Errors.missingParameter('password'))
  200. done()
  201. })
  202. })
  203. it('should throw an error if username or password are not a string', (done) => {
  204. chai.request(server)
  205. .post('/api/v1/user')
  206. .set('content-type', 'application/json')
  207. .send({
  208. username: 123,
  209. password: 123
  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.should.have.property('errors')
  216. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('username', 'string'))
  217. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameterType('password', 'string'))
  218. done()
  219. })
  220. })
  221. it('should throw an error if username or password less than 6 characters', (done) => {
  222. chai.request(server)
  223. .post('/api/v1/user')
  224. .set('content-type', 'application/x-www-form-urlencoded')
  225. .send({
  226. username: 'test',
  227. password: 'pass'
  228. })
  229. .end((err, res) => {
  230. res.should.have.status(400)
  231. res.should.be.json
  232. res.body.should.have.property('errors')
  233. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('username', '6'))
  234. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooSmall('password', '6'))
  235. done()
  236. })
  237. })
  238. it('should throw an error if username greater than 50 characters or password is greater than 100 characters', (done) => {
  239. chai.request(server)
  240. .post('/api/v1/user')
  241. .set('content-type', 'application/x-www-form-urlencoded')
  242. .send({
  243. username: '123456789012345678901234567890123456789012345678901',
  244. password: '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
  245. })
  246. .end((err, res) => {
  247. res.should.have.status(400)
  248. res.should.be.json
  249. res.body.should.have.property('errors')
  250. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('username', '50'))
  251. res.body.errors.should.contain.something.that.deep.equals(Errors.parameterLengthTooLarge('password', '100'))
  252. done()
  253. })
  254. })
  255. })
  256. describe('/:username/login POST user', () => {
  257. let agent = chai.request.agent(server)
  258. it('should throw an error if invalid username is provided', (done) => {
  259. chai.request(server)
  260. .post('/api/v1/user/invalid_username/login')
  261. .set('content-type', 'application/x-www-form-urlencoded')
  262. .send({
  263. password: 'password'
  264. })
  265. .end((err, res) => {
  266. res.should.have.status(401)
  267. res.body.should.have.property('errors')
  268. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  269. done()
  270. })
  271. })
  272. it('should throw an error if invalid password is provided', (done) => {
  273. chai.request(server)
  274. .post('/api/v1/user/username/login')
  275. .set('content-type', 'application/x-www-form-urlencoded')
  276. .send({
  277. password: 'invalid_password'
  278. })
  279. .end((err, res) => {
  280. res.should.have.status(401)
  281. res.body.should.have.property('errors')
  282. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
  283. done()
  284. })
  285. })
  286. it('should log in the user', (done) => {
  287. agent
  288. .post('/api/v1/user/username/login')
  289. .set('content-type', 'application/x-www-form-urlencoded')
  290. .send({
  291. password: 'password'
  292. })
  293. .end((err, res) => {
  294. res.should.have.status(200)
  295. res.should.be.json
  296. res.should.have.cookie('connect.sid')
  297. agent
  298. .get('/api/v1/user/username')
  299. .then((res) => {
  300. res.should.have.status(200)
  301. done()
  302. })
  303. .catch(done)
  304. })
  305. })
  306. })
  307. describe('/:username/logout POST user', () => {
  308. let agent = chai.request.agent(server)
  309. it('should log out the user', (done) => {
  310. agent
  311. .post('/api/v1/user/login')
  312. .set('content-type', 'application/x-www-form-urlencoded')
  313. .send({
  314. username: 'username',
  315. password: 'password'
  316. })
  317. .end((err, res) => {
  318. agent
  319. .post('/api/v1/user/username/logout')
  320. .end((err, res) => {
  321. res.should.have.status(200)
  322. agent
  323. .get('/api/v1/user/username')
  324. .then((res) => {
  325. res.should.have.status(403)
  326. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  327. done()
  328. })
  329. .catch((res) => {
  330. res.should.have.status(403)
  331. JSON
  332. .parse(res.response.text)
  333. .errors.should.contain.something
  334. .that.deep.equals(Errors.requestNotAuthorized)
  335. done()
  336. })
  337. })
  338. })
  339. })
  340. })
  341. })