user.js 11 KB

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