adminUser.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let expect = chai.expect
  6. let { sequelize, User, Category, Thread, Post} = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. chai.use(require('chai-http'))
  9. chai.use(require('chai-things'))
  10. describe('GET /user', () => {
  11. let adminAgent = chai.request.agent(server)
  12. //Wait for app to start before commencing
  13. before((done) => {
  14. const NumUsers = 65;
  15. const NumThreads = 5;
  16. const NumPosts = 30;
  17. async function createUsers () {
  18. let userPromises = [];
  19. userPromises.push(
  20. adminAgent
  21. .post('/api/v1/user')
  22. .set('content-type', 'application/json')
  23. .send({
  24. username: 'adminuser',
  25. password: 'password',
  26. admin: true
  27. })
  28. )
  29. for(let i = 0; i < NumUsers; i++) {
  30. let promise = chai.request(server)
  31. .post('/api/v1/user')
  32. .set('content-type', 'application/json')
  33. .send({
  34. username: 'username' + i,
  35. password: 'password',
  36. admin: false
  37. })
  38. userPromises.push(promise)
  39. }
  40. let users = await Promise.all(userPromises);
  41. return users;
  42. }
  43. function createCategory () {
  44. return adminAgent
  45. .post('/api/v1/category')
  46. .set('content-type', 'application/json')
  47. .send({ name: 'category-name' })
  48. }
  49. async function getUserAgents () {
  50. let agents = [];
  51. for(let i = 0; i < 5; i++) {
  52. agents.push(chai.request.agent(server))
  53. }
  54. let agentPromises = agents.map((agent, i) => {
  55. let username = 'username' + i;
  56. return agent
  57. .post('/api/v1/user/' + username + '/login')
  58. .set('content-type', 'application/json')
  59. .send({
  60. username: username,
  61. password: 'password',
  62. });
  63. });
  64. await Promise.all(agentPromises);
  65. return agents;
  66. }
  67. async function createThreads (agents) {
  68. let threadPromises = [];
  69. for(let i = 0; i < NumThreads; i++) {
  70. let agent = agents[i % agents.length];
  71. threadPromises.push(
  72. agent
  73. .post('/api/v1/thread')
  74. .set('content-type', 'application/json')
  75. .send({ name: 'thread' + i, category: 'category-name' })
  76. );
  77. }
  78. await Promise.all(threadPromises);
  79. return agents;
  80. }
  81. async function createPosts (agents) {
  82. let postPromises = [];
  83. for(let i = 0; i < NumPosts; i++) {
  84. postPromises.push(
  85. agents[i%agents.length]
  86. .post('/api/v1/post')
  87. .set('content-type', 'application/json')
  88. .send({ content: 'post', threadId: i%NumThreads+1 })
  89. );
  90. }
  91. await Promise.all(postPromises);
  92. return agents;
  93. }
  94. function createMockData () {
  95. createUsers()
  96. .then(createCategory)
  97. .then(getUserAgents)
  98. .then(createThreads)
  99. .then(createPosts)
  100. .then(_ => {
  101. console.log('Completed');
  102. done();
  103. })
  104. .catch(err => {
  105. console.log(err);
  106. done(err);
  107. })
  108. }
  109. if(server.locals.appStarted) createMockData()
  110. server.on('appStarted', () => {
  111. createMockData()
  112. })
  113. })
  114. //Delete all rows in table after
  115. //tests completed
  116. after(() => sequelize.sync({ force: true }) )
  117. it('should get first 30 users, by default ordered by username descending', (done) => {
  118. adminAgent.get('/api/v1/user?role=admin').end((err, res) => {
  119. if(err) done(err);
  120. console.log(res.body)
  121. res.body.should.have.length(30);
  122. res.body[0].id.should.equal(65);
  123. //res.body[0].postsCount.should.equal(2);
  124. //res.body[0].threadsCount.should.equal(1);
  125. res.body[29].id.should.equal(36);
  126. done();
  127. });
  128. })
  129. /*it('should paginate correctly', (done) => {
  130. adminAgent.get('/api/v1/user?offset=60').end((err, res) => {
  131. if(err) done(err);
  132. res.body.should.have.length(30);
  133. res.body[0].username.should.equal('username60');
  134. res.body[0].postsCount.should.equal(0);
  135. res.body[0].threadsCount.should.equal(0);
  136. res.body[29].username.should.equal('username64');
  137. done();
  138. });
  139. })
  140. it('should enable sorting by username ascending', (done) => {
  141. adminAgent.get('/api/v1/user?sort=username&order=asc').end((err, res) => {
  142. if(err) done(err);
  143. res.body.should.have.length(30);
  144. res.body[0].username.should.equal('username64');
  145. res.body[29].username.should.equal('username44');
  146. done();
  147. });
  148. })
  149. it('should enable filtering by username', (done) => {
  150. adminAgent.get('/api/v1/user', (err, res) => {
  151. res.body.should.contain
  152. });
  153. })
  154. it('should enable filtering by role (admin or user)', (done) => {
  155. adminAgent.get('/api/v1/user?role=admin').end((err, res) => {
  156. if(err) done(err);
  157. res.body.should.have.length(1);
  158. res.body[0].username.should.equal('admin123');
  159. done();
  160. });
  161. })
  162. it('should enable sorting by date joined', (done) => {
  163. adminAgent.get('/api/v1/user', (err, res) => {
  164. res.body.should.contain
  165. });
  166. })
  167. it('should enable sorting by number of posts', (done) => {
  168. adminAgent.get('/api/v1/user?sort=posts&order=asc').end((err, res) => {
  169. if(err) done(err);
  170. res.body.should.have.length(30);
  171. res.body[0].username.should.equal('username0');
  172. res.body[29].username.should.equal('username28');
  173. done();
  174. });
  175. })
  176. it('should enable sorting by number of threads', (done) => {
  177. adminAgent.get('/api/v1/user?sort=threads&order=asc').end((err, res) => {
  178. if(err) done(err);
  179. res.body.should.have.length(30);
  180. res.body[0].username.should.equal('username0');
  181. res.body[29].username.should.equal('username28');
  182. done();
  183. });
  184. })
  185. it('should throw an error if not logged in', (done) => {
  186. adminAgent.get('/api/v1/user').end((err, res) => {
  187. expect(err).to.exist;
  188. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  189. done();
  190. });
  191. })*/
  192. })