thread_post.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Thread and post', () => {
  10. let userAgent, replyAgent
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) mockData()
  14. server.on('appStarted', () => {
  15. mockData()
  16. })
  17. function mockData() {
  18. userAgent = chai.request.agent(server)
  19. replyAgent = chai.request.agent(server)
  20. userAgent
  21. .post('/api/v1/user')
  22. .set('content-type', 'application/json')
  23. .send({
  24. username: 'username',
  25. password: 'password',
  26. admin: true
  27. })
  28. .then(() => {
  29. userAgent
  30. .post('/api/v1/category')
  31. .set('content-type', 'application/json')
  32. .send({ name: 'category_name' })
  33. .then(() => { done() })
  34. .catch(done)
  35. })
  36. .catch(done)
  37. }
  38. })
  39. //Delete all rows in table after
  40. //tests completed
  41. after(() => {
  42. sequelize.sync({ force: true })
  43. })
  44. describe('POST /thread', () => {
  45. it('should create a thread if logged in', async () => {
  46. let res = await userAgent
  47. .post('/api/v1/thread')
  48. .set('content-type', 'application/json')
  49. .send({
  50. name: 'thread',
  51. category: 'category_name'
  52. })
  53. res.should.have.status(200)
  54. res.should.be.json
  55. res.body.should.have.property('name', 'thread')
  56. res.body.should.have.deep.property('User.username', 'username')
  57. res.body.should.have.deep.property('Category.name', 'category_name')
  58. })
  59. it('should return an error if not logged in', async () => {
  60. try {
  61. let res = await chai.request(server)
  62. .post('/api/v1/thread')
  63. .set('content-type', 'application/json')
  64. .send({
  65. name: 'thread',
  66. category: 'category_name'
  67. })
  68. res.should.be.json
  69. res.should.have.status(401)
  70. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  71. } catch (res) {
  72. res.should.have.status(401)
  73. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  74. }
  75. })
  76. it('should return an error if missing parameters', async () => {
  77. try {
  78. let res = await userAgent
  79. .post('/api/v1/thread')
  80. res.should.be.json
  81. res.should.have.status(400)
  82. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  83. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  84. } catch (res) {
  85. let body = JSON.parse(res.response.text)
  86. res.should.have.status(400)
  87. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('name'))
  88. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('category'))
  89. }
  90. })
  91. it('should return an error if invalid types', async () => {
  92. try {
  93. let res = await userAgent
  94. .post('/api/v1/thread')
  95. .set('content-type', 'application/json')
  96. .send({
  97. name: 123,
  98. category: 123
  99. })
  100. res.should.be.json
  101. res.should.have.status(400)
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  103. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  104. } catch (res) {
  105. let body = JSON.parse(res.response.text)
  106. res.should.have.status(400)
  107. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('name', 'string'))
  108. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('category', 'string'))
  109. }
  110. })
  111. it('should return an error if category does not exist', async () => {
  112. try {
  113. let res = await userAgent
  114. .post('/api/v1/thread')
  115. .set('content-type', 'application/json')
  116. .send({
  117. name: 'thread1',
  118. category: 'non-existent'
  119. })
  120. res.should.be.json
  121. res.should.have.status(400)
  122. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  123. } catch (res) {
  124. res.should.have.status(400)
  125. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.invalidCategory)
  126. }
  127. })
  128. })
  129. describe('POST /post', () => {
  130. it('should create a post if logged in', async () => {
  131. let res = await userAgent
  132. .post('/api/v1/post')
  133. .set('content-type', 'application/json')
  134. .send({
  135. content: 'content',
  136. threadId: 1
  137. })
  138. res.should.be.json
  139. res.should.have.status(200)
  140. res.body.should.have.property('content', '<p>content</p>\n')
  141. res.body.should.have.deep.property('User.username', 'username')
  142. res.body.should.have.deep.property('Thread.name', 'thread')
  143. })
  144. it('should return an error if not logged in', async () => {
  145. try {
  146. let res = await chai.request(server)
  147. .post('/api/v1/post')
  148. .set('content-type', 'application/json')
  149. .send({
  150. content: 'content',
  151. threadId: 1
  152. })
  153. res.should.be.json
  154. res.should.have.status(401)
  155. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  156. } catch (res) {
  157. res.should.have.status(401)
  158. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  159. }
  160. })
  161. it('should return an error if missing parameters', async () => {
  162. try {
  163. let res = await userAgent
  164. .post('/api/v1/post')
  165. res.should.be.json
  166. res.should.have.status(400)
  167. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  168. res.body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  169. } catch (res) {
  170. let body = JSON.parse(res.response.text)
  171. res.should.have.status(400)
  172. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('content'))
  173. body.errors.should.contain.something.that.deep.equals(Errors.missingParameter('threadId'))
  174. }
  175. })
  176. it('should return an error if invalid types', async () => {
  177. try {
  178. let res = await userAgent
  179. .post('/api/v1/post')
  180. .set('content-type', 'application/json')
  181. .send({
  182. content: 123,
  183. threadId: 'string',
  184. replyingToId: 'string'
  185. })
  186. res.should.be.json
  187. res.should.have.status(400)
  188. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  189. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  190. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  191. } catch (res) {
  192. let body = JSON.parse(res.response.text)
  193. res.should.have.status(400)
  194. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('content', 'string'))
  195. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('threadId', 'integer'))
  196. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('replyingToId', 'integer'))
  197. }
  198. })
  199. it('should return an error if thread id does not exist', async () => {
  200. try {
  201. let res = await userAgent
  202. .post('/api/v1/post')
  203. .set('content-type', 'application/json')
  204. .send({
  205. content: 'content',
  206. threadId: 10
  207. })
  208. res.should.be.json
  209. res.should.have.status(400)
  210. res.body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  211. } catch (res) {
  212. let body = JSON.parse(res.response.text)
  213. res.should.have.status(400)
  214. body.errors.should.include.something.that.deep.equals(Errors.invalidParameter('threadId', 'thread does not exist'))
  215. }
  216. })
  217. it('should be able to reply to a post', async () => {
  218. await replyAgent
  219. .post('/api/v1/user')
  220. .set('content-type', 'application/json')
  221. .send({
  222. username: 'username1',
  223. password: 'password'
  224. })
  225. let res = await replyAgent
  226. .post('/api/v1/post')
  227. .set('content-type', 'application/json')
  228. .send({
  229. content: 'another post',
  230. threadId: 1,
  231. replyingToId: 1
  232. })
  233. res.should.be.json
  234. res.should.have.status(200)
  235. res.body.should.have.property('content', '<p>another post</p>\n')
  236. res.body.should.have.deep.property('User.username', 'username1')
  237. res.body.should.have.deep.property('Thread.name', 'thread')
  238. res.body.should.have.deep.property('ReplyingTo.User.username', 'username')
  239. })
  240. it('should return an error if reply id does not exist', async () => {
  241. try {
  242. let res = await replyAgent
  243. .post('/api/v1/post')
  244. .set('content-type', 'application/json')
  245. .send({
  246. content: 'another post',
  247. threadId: 1,
  248. replyingToId: 10
  249. })
  250. res.should.have.status(400)
  251. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  252. } catch (res) {
  253. let body = JSON.parse(res.response.text)
  254. res.should.have.status(400)
  255. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'post does not exist'))
  256. }
  257. })
  258. it('should return an error if post reply not in same thread', async () => {
  259. try {
  260. let threadId = (await replyAgent
  261. .post('/api/v1/thread')
  262. .set('content-type', 'application/json')
  263. .send({
  264. name: 'another thread',
  265. category: 'category_name'
  266. })).body.id
  267. let res = await replyAgent
  268. .post('/api/v1/post')
  269. .set('content-type', 'application/json')
  270. .send({
  271. content: 'another post',
  272. threadId: threadId,
  273. replyingToId: 1
  274. })
  275. res.should.have.status(400)
  276. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  277. } catch (res) {
  278. let body = JSON.parse(res.response.text)
  279. res.should.have.status(400)
  280. body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('replyingToId', 'replies must be in same thread'))
  281. }
  282. })
  283. })
  284. describe('GET /thread/:id', () => {
  285. it('should return the thread and corresponding posts', async () => {})
  286. it('should return an error if :id is invalid', async () => {})
  287. })
  288. describe('GET /post/:id', () => {
  289. it('should return the post', async () => {})
  290. it('should return an error if invalid post id', async () => {})
  291. })
  292. })