test_comments.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # -*- coding: utf-8 -*-
  2. from tracim.tests import FunctionalTest
  3. from tracim.fixtures.content import Content as ContentFixtures
  4. from tracim.fixtures.users_and_groups import Base as BaseFixture
  5. class TestCommentsEndpoint(FunctionalTest):
  6. """
  7. Tests for /api/v2/workspaces/{workspace_id}/contents/{content_id}/comments
  8. endpoint
  9. """
  10. fixtures = [BaseFixture, ContentFixtures]
  11. def test_api__get_contents_comments__ok_200__nominal_case(self) -> None:
  12. """
  13. Get alls comments of a content
  14. """
  15. self.testapp.authorization = (
  16. 'Basic',
  17. (
  18. 'admin@admin.admin',
  19. 'admin@admin.admin'
  20. )
  21. )
  22. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200) # nopep8
  23. assert len(res.json_body) == 3
  24. comment = res.json_body[0]
  25. assert comment['content_id'] == 18
  26. assert comment['parent_id'] == 7
  27. assert comment['raw_content'] == '<p> What is for you the best cake ever ? </br> I personnally vote for Chocolate cupcake !</p>' # nopep8
  28. assert comment['author']
  29. assert comment['author']['user_id'] == 1
  30. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  31. assert comment['author']['avatar_url'] == None
  32. assert comment['author']['public_name'] == 'Global manager'
  33. comment = res.json_body[1]
  34. assert comment['content_id'] == 19
  35. assert comment['parent_id'] == 7
  36. assert comment['raw_content'] == '<p>What about Apple Pie ? There are Awesome !</p>' # nopep8
  37. assert comment['author']
  38. assert comment['author']['user_id'] == 3
  39. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  40. assert comment['author']['avatar_url'] == None
  41. assert comment['author']['public_name'] == 'Bob i.'
  42. comment = res.json_body[2]
  43. assert comment['content_id'] == 20
  44. assert comment['parent_id'] == 7
  45. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  46. assert comment['author']
  47. assert comment['author']['user_id'] == 4
  48. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  49. assert comment['author']['avatar_url'] == None
  50. assert comment['author']['public_name'] == 'John Reader'
  51. def test_api__post_content_comment__ok_200__nominal_case(self) -> None:
  52. """
  53. Get alls comments of a content
  54. """
  55. self.testapp.authorization = (
  56. 'Basic',
  57. (
  58. 'admin@admin.admin',
  59. 'admin@admin.admin'
  60. )
  61. )
  62. params = {
  63. 'raw_content': 'I strongly disagree, Tiramisu win !'
  64. }
  65. res = self.testapp.post_json(
  66. '/api/v2/workspaces/2/contents/7/comments',
  67. params=params,
  68. status=200
  69. )
  70. comment = res.json_body
  71. assert comment['content_id']
  72. assert comment['parent_id'] == 7
  73. assert comment['raw_content'] == 'I strongly disagree, Tiramisu win !'
  74. assert comment['author']
  75. assert comment['author']['user_id'] == 1
  76. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  77. assert comment['author']['avatar_url'] is None
  78. assert comment['author']['public_name'] == 'Global manager'
  79. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200) # nopep8
  80. assert len(res.json_body) == 4
  81. assert comment == res.json_body[3]
  82. def test_api__delete_content_comment__ok_200__workspace_manager_owner(self) -> None:
  83. """
  84. delete comment (user is workspace_manager and owner)
  85. """
  86. self.testapp.authorization = (
  87. 'Basic',
  88. (
  89. 'admin@admin.admin',
  90. 'admin@admin.admin'
  91. )
  92. )
  93. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  94. assert len(res.json_body) == 3
  95. comment = res.json_body[0]
  96. assert comment['content_id'] == 18
  97. assert comment['parent_id'] == 7
  98. assert comment['raw_content'] == '<p> What is for you the best cake ever ? </br> I personnally vote for Chocolate cupcake !</p>' # nopep8
  99. assert comment['author']
  100. assert comment['author']['user_id'] == 1
  101. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  102. assert comment['author']['avatar_url'] is None
  103. assert comment['author']['public_name'] == 'Global manager'
  104. res = self.testapp.delete(
  105. '/api/v2/workspaces/2/contents/7/comments/18',
  106. status=204
  107. )
  108. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  109. assert len(res.json_body) == 2
  110. assert not [content for content in res.json_body if content['content_id'] == 18] # nopep8
  111. def test_api__delete_content_comment__ok_200__workspace_manager(self) -> None:
  112. """
  113. delete comment (user is workspace_manager)
  114. """
  115. self.testapp.authorization = (
  116. 'Basic',
  117. (
  118. 'admin@admin.admin',
  119. 'admin@admin.admin'
  120. )
  121. )
  122. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  123. assert len(res.json_body) == 3
  124. comment = res.json_body[1]
  125. assert comment['content_id'] == 19
  126. assert comment['parent_id'] == 7
  127. assert comment['raw_content'] == '<p>What about Apple Pie ? There are Awesome !</p>' # nopep8
  128. assert comment['author']
  129. assert comment['author']['user_id'] == 3
  130. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  131. assert comment['author']['avatar_url'] is None
  132. assert comment['author']['public_name'] == 'Bob i.'
  133. res = self.testapp.delete(
  134. '/api/v2/workspaces/2/contents/7/comments/19',
  135. status=204
  136. )
  137. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  138. assert len(res.json_body) == 2
  139. assert not [content for content in res.json_body if content['content_id'] == 19] # nopep8
  140. def test_api__delete_content_comment__ok_200__content_manager_owner(self) -> None:
  141. """
  142. delete comment (user is content-manager and owner)
  143. """
  144. self.testapp.authorization = (
  145. 'Basic',
  146. (
  147. 'admin@admin.admin',
  148. 'admin@admin.admin'
  149. )
  150. )
  151. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  152. assert len(res.json_body) == 3
  153. comment = res.json_body[1]
  154. assert comment['content_id'] == 19
  155. assert comment['parent_id'] == 7
  156. assert comment['raw_content'] == '<p>What about Apple Pie ? There are Awesome !</p>' # nopep8
  157. assert comment['author']
  158. assert comment['author']['user_id'] == 3
  159. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  160. assert comment['author']['avatar_url'] is None
  161. assert comment['author']['public_name'] == 'Bob i.'
  162. res = self.testapp.delete(
  163. '/api/v2/workspaces/2/contents/7/comments/19',
  164. status=204
  165. )
  166. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  167. assert len(res.json_body) == 2
  168. assert not [content for content in res.json_body if content['content_id'] == 19] # nopep8
  169. def test_api__delete_content_comment__err_403__content_manager(self) -> None:
  170. """
  171. delete comment (user is content-manager)
  172. """
  173. self.testapp.authorization = (
  174. 'Basic',
  175. (
  176. 'bob@fsf.local',
  177. 'foobarbaz'
  178. )
  179. )
  180. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  181. assert len(res.json_body) == 3
  182. comment = res.json_body[2]
  183. assert comment['content_id'] == 20
  184. assert comment['parent_id'] == 7
  185. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  186. assert comment['author']
  187. assert comment['author']['user_id'] == 4
  188. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  189. assert comment['author']['avatar_url'] is None
  190. assert comment['author']['public_name'] == 'John Reader'
  191. res = self.testapp.delete(
  192. '/api/v2/workspaces/2/contents/7/comments/20',
  193. status=403
  194. )
  195. def test_api__delete_content_comment__err_403__reader_owner(self) -> None:
  196. """
  197. delete comment (user is reader and owner)
  198. """
  199. self.testapp.authorization = (
  200. 'Basic',
  201. (
  202. 'bob@fsf.local',
  203. 'foobarbaz'
  204. )
  205. )
  206. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  207. assert len(res.json_body) == 3
  208. comment = res.json_body[2]
  209. assert comment['content_id'] == 20
  210. assert comment['parent_id'] == 7
  211. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  212. assert comment['author']
  213. assert comment['author']['user_id'] == 4
  214. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  215. assert comment['author']['avatar_url'] is None
  216. assert comment['author']['public_name'] == 'John Reader'
  217. res = self.testapp.delete(
  218. '/api/v2/workspaces/2/contents/7/comments/20',
  219. status=403
  220. )
  221. def test_api__delete_content_comment__err_403__reader(self) -> None:
  222. """
  223. delete comment (user is reader)
  224. """
  225. self.testapp.authorization = (
  226. 'Basic',
  227. (
  228. 'bob@fsf.local',
  229. 'foobarbaz'
  230. )
  231. )
  232. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  233. assert len(res.json_body) == 3
  234. comment = res.json_body[2]
  235. assert comment['content_id'] == 20
  236. assert comment['parent_id'] == 7
  237. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  238. assert comment['author']
  239. assert comment['author']['user_id'] == 4
  240. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  241. assert comment['author']['avatar_url'] is None
  242. assert comment['author']['public_name'] == 'John Reader'
  243. res = self.testapp.delete(
  244. '/api/v2/workspaces/2/contents/7/comments/20',
  245. status=403
  246. )