test_comments.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. # TODO - G.M - 2018-06-179 - better check for datetime
  43. assert comment['created']
  44. comment = res.json_body[2]
  45. assert comment['content_id'] == 20
  46. assert comment['parent_id'] == 7
  47. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  48. assert comment['author']
  49. assert comment['author']['user_id'] == 4
  50. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  51. assert comment['author']['avatar_url'] == None
  52. assert comment['author']['public_name'] == 'John Reader'
  53. # TODO - G.M - 2018-06-179 - better check for datetime
  54. assert comment['created']
  55. def test_api__post_content_comment__ok_200__nominal_case(self) -> None:
  56. """
  57. Get alls comments of a content
  58. """
  59. self.testapp.authorization = (
  60. 'Basic',
  61. (
  62. 'admin@admin.admin',
  63. 'admin@admin.admin'
  64. )
  65. )
  66. params = {
  67. 'raw_content': 'I strongly disagree, Tiramisu win!'
  68. }
  69. res = self.testapp.post_json(
  70. '/api/v2/workspaces/2/contents/7/comments',
  71. params=params,
  72. status=200
  73. )
  74. comment = res.json_body
  75. assert comment['content_id']
  76. assert comment['parent_id'] == 7
  77. assert comment['raw_content'] == 'I strongly disagree, Tiramisu win!'
  78. assert comment['author']
  79. assert comment['author']['user_id'] == 1
  80. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  81. assert comment['author']['avatar_url'] is None
  82. assert comment['author']['public_name'] == 'Global manager'
  83. # TODO - G.M - 2018-06-179 - better check for datetime
  84. assert comment['created']
  85. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200) # nopep8
  86. assert len(res.json_body) == 4
  87. assert comment == res.json_body[3]
  88. def test_api__delete_content_comment__ok_200__user_is_owner_and_workspace_manager(self) -> None: # nopep8
  89. """
  90. delete comment (user is workspace_manager and owner)
  91. """
  92. self.testapp.authorization = (
  93. 'Basic',
  94. (
  95. 'admin@admin.admin',
  96. 'admin@admin.admin'
  97. )
  98. )
  99. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  100. assert len(res.json_body) == 3
  101. comment = res.json_body[0]
  102. assert comment['content_id'] == 18
  103. assert comment['parent_id'] == 7
  104. assert comment['raw_content'] == '<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>' # nopep8
  105. assert comment['author']
  106. assert comment['author']['user_id'] == 1
  107. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  108. assert comment['author']['avatar_url'] is None
  109. assert comment['author']['public_name'] == 'Global manager'
  110. # TODO - G.M - 2018-06-179 - better check for datetime
  111. assert comment['created']
  112. res = self.testapp.delete(
  113. '/api/v2/workspaces/2/contents/7/comments/18',
  114. status=204
  115. )
  116. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  117. assert len(res.json_body) == 2
  118. assert not [content for content in res.json_body if content['content_id'] == 18] # nopep8
  119. def test_api__delete_content_comment__ok_200__user_is_workspace_manager(self) -> None: # nopep8
  120. """
  121. delete comment (user is workspace_manager)
  122. """
  123. self.testapp.authorization = (
  124. 'Basic',
  125. (
  126. 'admin@admin.admin',
  127. 'admin@admin.admin'
  128. )
  129. )
  130. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  131. assert len(res.json_body) == 3
  132. comment = res.json_body[1]
  133. assert comment['content_id'] == 19
  134. assert comment['parent_id'] == 7
  135. assert comment['raw_content'] == '<p>What about Apple Pie? There are Awesome!</p>' # nopep8
  136. assert comment['author']
  137. assert comment['author']['user_id'] == 3
  138. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  139. assert comment['author']['avatar_url'] is None
  140. assert comment['author']['public_name'] == 'Bob i.'
  141. # TODO - G.M - 2018-06-179 - better check for datetime
  142. assert comment['created']
  143. res = self.testapp.delete(
  144. '/api/v2/workspaces/2/contents/7/comments/19',
  145. status=204
  146. )
  147. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  148. assert len(res.json_body) == 2
  149. assert not [content for content in res.json_body if content['content_id'] == 19] # nopep8
  150. def test_api__delete_content_comment__ok_200__user_is_owner_and_content_manager(self) -> None: # nopep8
  151. """
  152. delete comment (user is content-manager and owner)
  153. """
  154. self.testapp.authorization = (
  155. 'Basic',
  156. (
  157. 'admin@admin.admin',
  158. 'admin@admin.admin'
  159. )
  160. )
  161. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  162. assert len(res.json_body) == 3
  163. comment = res.json_body[1]
  164. assert comment['content_id'] == 19
  165. assert comment['parent_id'] == 7
  166. assert comment['raw_content'] == '<p>What about Apple Pie? There are Awesome!</p>' # nopep8
  167. assert comment['author']
  168. assert comment['author']['user_id'] == 3
  169. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  170. assert comment['author']['avatar_url'] is None
  171. assert comment['author']['public_name'] == 'Bob i.'
  172. # TODO - G.M - 2018-06-179 - better check for datetime
  173. assert comment['created']
  174. res = self.testapp.delete(
  175. '/api/v2/workspaces/2/contents/7/comments/19',
  176. status=204
  177. )
  178. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  179. assert len(res.json_body) == 2
  180. assert not [content for content in res.json_body if content['content_id'] == 19] # nopep8
  181. def test_api__delete_content_comment__err_403__user_is_content_manager(self) -> None: # nopep8
  182. """
  183. delete comment (user is content-manager)
  184. """
  185. self.testapp.authorization = (
  186. 'Basic',
  187. (
  188. 'bob@fsf.local',
  189. 'foobarbaz'
  190. )
  191. )
  192. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200) # nopep8
  193. assert len(res.json_body) == 3
  194. comment = res.json_body[2]
  195. assert comment['content_id'] == 20
  196. assert comment['parent_id'] == 7
  197. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  198. assert comment['author']
  199. assert comment['author']['user_id'] == 4
  200. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  201. assert comment['author']['avatar_url'] is None
  202. assert comment['author']['public_name'] == 'John Reader'
  203. # TODO - G.M - 2018-06-179 - better check for datetime
  204. assert comment['created']
  205. res = self.testapp.delete(
  206. '/api/v2/workspaces/2/contents/7/comments/20',
  207. status=403
  208. )
  209. def test_api__delete_content_comment__err_403__user_is_owner_and_reader(self) -> None:
  210. """
  211. delete comment (user is reader and owner)
  212. """
  213. self.testapp.authorization = (
  214. 'Basic',
  215. (
  216. 'bob@fsf.local',
  217. 'foobarbaz'
  218. )
  219. )
  220. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  221. assert len(res.json_body) == 3
  222. comment = res.json_body[2]
  223. assert comment['content_id'] == 20
  224. assert comment['parent_id'] == 7
  225. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  226. assert comment['author']
  227. assert comment['author']['user_id'] == 4
  228. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  229. assert comment['author']['avatar_url'] is None
  230. assert comment['author']['public_name'] == 'John Reader'
  231. # TODO - G.M - 2018-06-179 - better check for datetime
  232. assert comment['created']
  233. res = self.testapp.delete(
  234. '/api/v2/workspaces/2/contents/7/comments/20',
  235. status=403
  236. )
  237. def test_api__delete_content_comment__err_403__user_is_reader(self) -> None:
  238. """
  239. delete comment (user is reader)
  240. """
  241. self.testapp.authorization = (
  242. 'Basic',
  243. (
  244. 'bob@fsf.local',
  245. 'foobarbaz'
  246. )
  247. )
  248. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  249. assert len(res.json_body) == 3
  250. comment = res.json_body[2]
  251. assert comment['content_id'] == 20
  252. assert comment['parent_id'] == 7
  253. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  254. assert comment['author']
  255. assert comment['author']['user_id'] == 4
  256. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  257. assert comment['author']['avatar_url'] is None
  258. assert comment['author']['public_name'] == 'John Reader'
  259. # TODO - G.M - 2018-06-179 - better check for datetime
  260. assert comment['created']
  261. res = self.testapp.delete(
  262. '/api/v2/workspaces/2/contents/7/comments/20',
  263. status=403
  264. )