user.js 10 KB

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