test_comments.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # -*- coding: utf-8 -*-
  2. from tracim_backend.tests import FunctionalTest
  3. from tracim_backend.fixtures.content import Content as ContentFixtures
  4. from tracim_backend.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__post_content_comment__err_400__empty_raw_content(self) -> None:
  89. """
  90. Get alls comments of a content
  91. """
  92. self.testapp.authorization = (
  93. 'Basic',
  94. (
  95. 'admin@admin.admin',
  96. 'admin@admin.admin'
  97. )
  98. )
  99. params = {
  100. 'raw_content': ''
  101. }
  102. res = self.testapp.post_json(
  103. '/api/v2/workspaces/2/contents/7/comments',
  104. params=params,
  105. status=400
  106. )
  107. def test_api__delete_content_comment__ok_200__user_is_owner_and_workspace_manager(self) -> None: # nopep8
  108. """
  109. delete comment (user is workspace_manager and owner)
  110. """
  111. self.testapp.authorization = (
  112. 'Basic',
  113. (
  114. 'admin@admin.admin',
  115. 'admin@admin.admin'
  116. )
  117. )
  118. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  119. assert len(res.json_body) == 3
  120. comment = res.json_body[0]
  121. assert comment['content_id'] == 18
  122. assert comment['parent_id'] == 7
  123. assert comment['raw_content'] == '<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>' # nopep8
  124. assert comment['author']
  125. assert comment['author']['user_id'] == 1
  126. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  127. assert comment['author']['avatar_url'] is None
  128. assert comment['author']['public_name'] == 'Global manager'
  129. # TODO - G.M - 2018-06-179 - better check for datetime
  130. assert comment['created']
  131. res = self.testapp.delete(
  132. '/api/v2/workspaces/2/contents/7/comments/18',
  133. status=204
  134. )
  135. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  136. assert len(res.json_body) == 2
  137. assert not [content for content in res.json_body if content['content_id'] == 18] # nopep8
  138. def test_api__delete_content_comment__ok_200__user_is_workspace_manager(self) -> None: # nopep8
  139. """
  140. delete comment (user is workspace_manager)
  141. """
  142. self.testapp.authorization = (
  143. 'Basic',
  144. (
  145. 'admin@admin.admin',
  146. 'admin@admin.admin'
  147. )
  148. )
  149. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  150. assert len(res.json_body) == 3
  151. comment = res.json_body[1]
  152. assert comment['content_id'] == 19
  153. assert comment['parent_id'] == 7
  154. assert comment['raw_content'] == '<p>What about Apple Pie? There are Awesome!</p>' # nopep8
  155. assert comment['author']
  156. assert comment['author']['user_id'] == 3
  157. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  158. assert comment['author']['avatar_url'] is None
  159. assert comment['author']['public_name'] == 'Bob i.'
  160. # TODO - G.M - 2018-06-179 - better check for datetime
  161. assert comment['created']
  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__ok_200__user_is_owner_and_content_manager(self) -> None: # nopep8
  170. """
  171. delete comment (user is content-manager and owner)
  172. """
  173. self.testapp.authorization = (
  174. 'Basic',
  175. (
  176. 'admin@admin.admin',
  177. 'admin@admin.admin'
  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[1]
  183. assert comment['content_id'] == 19
  184. assert comment['parent_id'] == 7
  185. assert comment['raw_content'] == '<p>What about Apple Pie? There are Awesome!</p>' # nopep8
  186. assert comment['author']
  187. assert comment['author']['user_id'] == 3
  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'] == 'Bob i.'
  191. # TODO - G.M - 2018-06-179 - better check for datetime
  192. assert comment['created']
  193. res = self.testapp.delete(
  194. '/api/v2/workspaces/2/contents/7/comments/19',
  195. status=204
  196. )
  197. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  198. assert len(res.json_body) == 2
  199. assert not [content for content in res.json_body if content['content_id'] == 19] # nopep8
  200. def test_api__delete_content_comment__err_403__user_is_content_manager(self) -> None: # nopep8
  201. """
  202. delete comment (user is content-manager)
  203. """
  204. self.testapp.authorization = (
  205. 'Basic',
  206. (
  207. 'bob@fsf.local',
  208. 'foobarbaz'
  209. )
  210. )
  211. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200) # nopep8
  212. assert len(res.json_body) == 3
  213. comment = res.json_body[2]
  214. assert comment['content_id'] == 20
  215. assert comment['parent_id'] == 7
  216. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  217. assert comment['author']
  218. assert comment['author']['user_id'] == 4
  219. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  220. assert comment['author']['avatar_url'] is None
  221. assert comment['author']['public_name'] == 'John Reader'
  222. # TODO - G.M - 2018-06-179 - better check for datetime
  223. assert comment['created']
  224. res = self.testapp.delete(
  225. '/api/v2/workspaces/2/contents/7/comments/20',
  226. status=403
  227. )
  228. def test_api__delete_content_comment__err_403__user_is_owner_and_reader(self) -> None:
  229. """
  230. delete comment (user is reader and owner)
  231. """
  232. self.testapp.authorization = (
  233. 'Basic',
  234. (
  235. 'bob@fsf.local',
  236. 'foobarbaz'
  237. )
  238. )
  239. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  240. assert len(res.json_body) == 3
  241. comment = res.json_body[2]
  242. assert comment['content_id'] == 20
  243. assert comment['parent_id'] == 7
  244. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  245. assert comment['author']
  246. assert comment['author']['user_id'] == 4
  247. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  248. assert comment['author']['avatar_url'] is None
  249. assert comment['author']['public_name'] == 'John Reader'
  250. # TODO - G.M - 2018-06-179 - better check for datetime
  251. assert comment['created']
  252. res = self.testapp.delete(
  253. '/api/v2/workspaces/2/contents/7/comments/20',
  254. status=403
  255. )
  256. def test_api__delete_content_comment__err_403__user_is_reader(self) -> None:
  257. """
  258. delete comment (user is reader)
  259. """
  260. self.testapp.authorization = (
  261. 'Basic',
  262. (
  263. 'bob@fsf.local',
  264. 'foobarbaz'
  265. )
  266. )
  267. res = self.testapp.get('/api/v2/workspaces/2/contents/7/comments', status=200)
  268. assert len(res.json_body) == 3
  269. comment = res.json_body[2]
  270. assert comment['content_id'] == 20
  271. assert comment['parent_id'] == 7
  272. assert comment['raw_content'] == '<p>You are right, but Kouign-amann are clearly better.</p>' # nopep8
  273. assert comment['author']
  274. assert comment['author']['user_id'] == 4
  275. # TODO - G.M - 2018-06-172 - [avatar] setup avatar url
  276. assert comment['author']['avatar_url'] is None
  277. assert comment['author']['public_name'] == 'John Reader'
  278. # TODO - G.M - 2018-06-179 - better check for datetime
  279. assert comment['created']
  280. res = self.testapp.delete(
  281. '/api/v2/workspaces/2/contents/7/comments/20',
  282. status=403
  283. )