PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/requests/api/award_emoji_spec.rb

https://gitlab.com/certik/gitlab-ce
Ruby | 315 lines | 240 code | 75 blank | 0 comment | 4 complexity | 9a527842fe50b8ed94d4d051da61343e MD5 | raw file
  1. require 'spec_helper'
  2. describe API::AwardEmoji do
  3. set(:user) { create(:user) }
  4. set(:project) { create(:project) }
  5. set(:issue) { create(:issue, project: project) }
  6. set(:award_emoji) { create(:award_emoji, awardable: issue, user: user) }
  7. let!(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
  8. let!(:downvote) { create(:award_emoji, :downvote, awardable: merge_request, user: user) }
  9. set(:note) { create(:note, project: project, noteable: issue) }
  10. before do
  11. project.team << [user, :master]
  12. end
  13. describe "GET /projects/:id/awardable/:awardable_id/award_emoji" do
  14. context 'on an issue' do
  15. it "returns an array of award_emoji" do
  16. get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user)
  17. expect(response).to have_http_status(200)
  18. expect(json_response).to be_an Array
  19. expect(json_response.first['name']).to eq(award_emoji.name)
  20. end
  21. it "returns a 404 error when issue id not found" do
  22. get api("/projects/#{project.id}/issues/12345/award_emoji", user)
  23. expect(response).to have_http_status(404)
  24. end
  25. end
  26. context 'on a merge request' do
  27. it "returns an array of award_emoji" do
  28. get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji", user)
  29. expect(response).to have_http_status(200)
  30. expect(response).to include_pagination_headers
  31. expect(json_response).to be_an Array
  32. expect(json_response.first['name']).to eq(downvote.name)
  33. end
  34. end
  35. context 'on a snippet' do
  36. let(:snippet) { create(:project_snippet, :public, project: project) }
  37. let!(:award) { create(:award_emoji, awardable: snippet) }
  38. it 'returns the awarded emoji' do
  39. get api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji", user)
  40. expect(response).to have_http_status(200)
  41. expect(json_response).to be_an Array
  42. expect(json_response.first['name']).to eq(award.name)
  43. end
  44. end
  45. context 'when the user has no access' do
  46. it 'returns a status code 404' do
  47. user1 = create(:user)
  48. get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji", user1)
  49. expect(response).to have_http_status(404)
  50. end
  51. end
  52. end
  53. describe 'GET /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji' do
  54. let!(:rocket) { create(:award_emoji, awardable: note, name: 'rocket') }
  55. it 'returns an array of award emoji' do
  56. get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user)
  57. expect(response).to have_http_status(200)
  58. expect(json_response).to be_an Array
  59. expect(json_response.first['name']).to eq(rocket.name)
  60. end
  61. end
  62. describe "GET /projects/:id/awardable/:awardable_id/award_emoji/:award_id" do
  63. context 'on an issue' do
  64. it "returns the award emoji" do
  65. get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user)
  66. expect(response).to have_http_status(200)
  67. expect(json_response['name']).to eq(award_emoji.name)
  68. expect(json_response['awardable_id']).to eq(issue.id)
  69. expect(json_response['awardable_type']).to eq("Issue")
  70. end
  71. it "returns a 404 error if the award is not found" do
  72. get api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/12345", user)
  73. expect(response).to have_http_status(404)
  74. end
  75. end
  76. context 'on a merge request' do
  77. it 'returns the award emoji' do
  78. get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user)
  79. expect(response).to have_http_status(200)
  80. expect(json_response['name']).to eq(downvote.name)
  81. expect(json_response['awardable_id']).to eq(merge_request.id)
  82. expect(json_response['awardable_type']).to eq("MergeRequest")
  83. end
  84. end
  85. context 'on a snippet' do
  86. let(:snippet) { create(:project_snippet, :public, project: project) }
  87. let!(:award) { create(:award_emoji, awardable: snippet) }
  88. it 'returns the awarded emoji' do
  89. get api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user)
  90. expect(response).to have_http_status(200)
  91. expect(json_response['name']).to eq(award.name)
  92. expect(json_response['awardable_id']).to eq(snippet.id)
  93. expect(json_response['awardable_type']).to eq("Snippet")
  94. end
  95. end
  96. context 'when the user has no access' do
  97. it 'returns a status code 404' do
  98. user1 = create(:user)
  99. get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user1)
  100. expect(response).to have_http_status(404)
  101. end
  102. end
  103. end
  104. describe 'GET /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji/:award_id' do
  105. let!(:rocket) { create(:award_emoji, awardable: note, name: 'rocket') }
  106. it 'returns an award emoji' do
  107. get api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
  108. expect(response).to have_http_status(200)
  109. expect(json_response).not_to be_an Array
  110. expect(json_response['name']).to eq(rocket.name)
  111. end
  112. end
  113. describe "POST /projects/:id/awardable/:awardable_id/award_emoji" do
  114. let(:issue2) { create(:issue, project: project, author: user) }
  115. context "on an issue" do
  116. it "creates a new award emoji" do
  117. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'blowfish'
  118. expect(response).to have_http_status(201)
  119. expect(json_response['name']).to eq('blowfish')
  120. expect(json_response['user']['username']).to eq(user.username)
  121. end
  122. it "returns a 400 bad request error if the name is not given" do
  123. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user)
  124. expect(response).to have_http_status(400)
  125. end
  126. it "returns a 401 unauthorized error if the user is not authenticated" do
  127. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji"), name: 'thumbsup'
  128. expect(response).to have_http_status(401)
  129. end
  130. it "returns a 404 error if the user authored issue" do
  131. post api("/projects/#{project.id}/issues/#{issue2.id}/award_emoji", user), name: 'thumbsup'
  132. expect(response).to have_http_status(404)
  133. end
  134. it "normalizes +1 as thumbsup award" do
  135. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: '+1'
  136. expect(issue.award_emoji.last.name).to eq("thumbsup")
  137. end
  138. context 'when the emoji already has been awarded' do
  139. it 'returns a 404 status code' do
  140. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'thumbsup'
  141. post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: 'thumbsup'
  142. expect(response).to have_http_status(404)
  143. expect(json_response["message"]).to match("has already been taken")
  144. end
  145. end
  146. end
  147. context 'on a snippet' do
  148. it 'creates a new award emoji' do
  149. snippet = create(:project_snippet, :public, project: project)
  150. post api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji", user), name: 'blowfish'
  151. expect(response).to have_http_status(201)
  152. expect(json_response['name']).to eq('blowfish')
  153. expect(json_response['user']['username']).to eq(user.username)
  154. end
  155. end
  156. end
  157. describe "POST /projects/:id/awardable/:awardable_id/notes/:note_id/award_emoji" do
  158. let(:note2) { create(:note, project: project, noteable: issue, author: user) }
  159. it 'creates a new award emoji' do
  160. expect do
  161. post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
  162. end.to change { note.award_emoji.count }.from(0).to(1)
  163. expect(response).to have_http_status(201)
  164. expect(json_response['user']['username']).to eq(user.username)
  165. end
  166. it "it returns 404 error when user authored note" do
  167. post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note2.id}/award_emoji", user), name: 'thumbsup'
  168. expect(response).to have_http_status(404)
  169. end
  170. it "normalizes +1 as thumbsup award" do
  171. post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: '+1'
  172. expect(note.award_emoji.last.name).to eq("thumbsup")
  173. end
  174. context 'when the emoji already has been awarded' do
  175. it 'returns a 404 status code' do
  176. post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
  177. post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: 'rocket'
  178. expect(response).to have_http_status(404)
  179. expect(json_response["message"]).to match("has already been taken")
  180. end
  181. end
  182. end
  183. describe 'DELETE /projects/:id/awardable/:awardable_id/award_emoji/:award_id' do
  184. context 'when the awardable is an Issue' do
  185. it 'deletes the award' do
  186. expect do
  187. delete api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user)
  188. expect(response).to have_http_status(204)
  189. end.to change { issue.award_emoji.count }.from(1).to(0)
  190. end
  191. it 'returns a 404 error when the award emoji can not be found' do
  192. delete api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/12345", user)
  193. expect(response).to have_http_status(404)
  194. end
  195. it_behaves_like '412 response' do
  196. let(:request) { api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji/#{award_emoji.id}", user) }
  197. end
  198. end
  199. context 'when the awardable is a Merge Request' do
  200. it 'deletes the award' do
  201. expect do
  202. delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user)
  203. expect(response).to have_http_status(204)
  204. end.to change { merge_request.award_emoji.count }.from(1).to(0)
  205. end
  206. it 'returns a 404 error when note id not found' do
  207. delete api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/notes/12345", user)
  208. expect(response).to have_http_status(404)
  209. end
  210. it_behaves_like '412 response' do
  211. let(:request) { api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/award_emoji/#{downvote.id}", user) }
  212. end
  213. end
  214. context 'when the awardable is a Snippet' do
  215. let(:snippet) { create(:project_snippet, :public, project: project) }
  216. let!(:award) { create(:award_emoji, awardable: snippet, user: user) }
  217. it 'deletes the award' do
  218. expect do
  219. delete api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user)
  220. expect(response).to have_http_status(204)
  221. end.to change { snippet.award_emoji.count }.from(1).to(0)
  222. end
  223. it_behaves_like '412 response' do
  224. let(:request) { api("/projects/#{project.id}/snippets/#{snippet.id}/award_emoji/#{award.id}", user) }
  225. end
  226. end
  227. end
  228. describe 'DELETE /projects/:id/awardable/:awardable_id/award_emoji/:award_emoji_id' do
  229. let!(:rocket) { create(:award_emoji, awardable: note, name: 'rocket', user: user) }
  230. it 'deletes the award' do
  231. expect do
  232. delete api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user)
  233. expect(response).to have_http_status(204)
  234. end.to change { note.award_emoji.count }.from(1).to(0)
  235. end
  236. it_behaves_like '412 response' do
  237. let(:request) { api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji/#{rocket.id}", user) }
  238. end
  239. end
  240. end