PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/requests/api/commit_statuses_spec.rb

https://gitlab.com/matt.wilkinson/gitlab-ce
Ruby | 210 lines | 164 code | 46 blank | 0 comment | 0 complexity | 013131e5f6799ba6c341ae63c12a827f MD5 | raw file
  1. require 'spec_helper'
  2. describe API::CommitStatuses, api: true do
  3. include ApiHelpers
  4. let!(:project) { create(:project) }
  5. let(:commit) { project.repository.commit }
  6. let(:commit_status) { create(:commit_status, pipeline: pipeline) }
  7. let(:guest) { create_user(:guest) }
  8. let(:reporter) { create_user(:reporter) }
  9. let(:developer) { create_user(:developer) }
  10. let(:sha) { commit.id }
  11. describe "GET /projects/:id/repository/commits/:sha/statuses" do
  12. let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
  13. context 'ci commit exists' do
  14. let!(:master) { project.pipelines.create(sha: commit.id, ref: 'master') }
  15. let!(:develop) { project.pipelines.create(sha: commit.id, ref: 'develop') }
  16. it_behaves_like 'a paginated resources' do
  17. let(:request) { get api(get_url, reporter) }
  18. end
  19. context "reporter user" do
  20. let(:statuses_id) { json_response.map { |status| status['id'] } }
  21. def create_status(commit, opts = {})
  22. create(:commit_status, { pipeline: commit, ref: commit.ref }.merge(opts))
  23. end
  24. let!(:status1) { create_status(master, status: 'running') }
  25. let!(:status2) { create_status(master, name: 'coverage', status: 'pending') }
  26. let!(:status3) { create_status(develop, status: 'running', allow_failure: true) }
  27. let!(:status4) { create_status(master, name: 'coverage', status: 'success') }
  28. let!(:status5) { create_status(develop, name: 'coverage', status: 'success') }
  29. let!(:status6) { create_status(master, status: 'success') }
  30. context 'latest commit statuses' do
  31. before { get api(get_url, reporter) }
  32. it 'returns latest commit statuses' do
  33. expect(response.status).to eq(200)
  34. expect(json_response).to be_an Array
  35. expect(statuses_id).to contain_exactly(status3.id, status4.id, status5.id, status6.id)
  36. json_response.sort_by!{ |status| status['id'] }
  37. expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false])
  38. end
  39. end
  40. context 'all commit statuses' do
  41. before { get api(get_url, reporter), all: 1 }
  42. it 'returns all commit statuses' do
  43. expect(response.status).to eq(200)
  44. expect(json_response).to be_an Array
  45. expect(statuses_id).to contain_exactly(status1.id, status2.id,
  46. status3.id, status4.id,
  47. status5.id, status6.id)
  48. end
  49. end
  50. context 'latest commit statuses for specific ref' do
  51. before { get api(get_url, reporter), ref: 'develop' }
  52. it 'returns latest commit statuses for specific ref' do
  53. expect(response.status).to eq(200)
  54. expect(json_response).to be_an Array
  55. expect(statuses_id).to contain_exactly(status3.id, status5.id)
  56. end
  57. end
  58. context 'latest commit statues for specific name' do
  59. before { get api(get_url, reporter), name: 'coverage' }
  60. it 'return latest commit statuses for specific name' do
  61. expect(response.status).to eq(200)
  62. expect(json_response).to be_an Array
  63. expect(statuses_id).to contain_exactly(status4.id, status5.id)
  64. end
  65. end
  66. end
  67. end
  68. context 'ci commit does not exist' do
  69. before { get api(get_url, reporter) }
  70. it 'returns empty array' do
  71. expect(response.status).to eq 200
  72. expect(json_response).to be_an Array
  73. expect(json_response).to be_empty
  74. end
  75. end
  76. context "guest user" do
  77. before { get api(get_url, guest) }
  78. it "should not return project commits" do
  79. expect(response.status).to eq(403)
  80. end
  81. end
  82. context "unauthorized user" do
  83. before { get api(get_url) }
  84. it "should not return project commits" do
  85. expect(response.status).to eq(401)
  86. end
  87. end
  88. end
  89. describe 'POST /projects/:id/statuses/:sha' do
  90. let(:post_url) { "/projects/#{project.id}/statuses/#{sha}" }
  91. context 'developer user' do
  92. context 'only required parameters' do
  93. before { post api(post_url, developer), state: 'success' }
  94. it 'creates commit status' do
  95. expect(response.status).to eq(201)
  96. expect(json_response['sha']).to eq(commit.id)
  97. expect(json_response['status']).to eq('success')
  98. expect(json_response['name']).to eq('default')
  99. expect(json_response['ref']).to be_nil
  100. expect(json_response['target_url']).to be_nil
  101. expect(json_response['description']).to be_nil
  102. end
  103. end
  104. context 'with all optional parameters' do
  105. before do
  106. optional_params = { state: 'success', context: 'coverage',
  107. ref: 'develop', target_url: 'url', description: 'test' }
  108. post api(post_url, developer), optional_params
  109. end
  110. it 'creates commit status' do
  111. expect(response.status).to eq(201)
  112. expect(json_response['sha']).to eq(commit.id)
  113. expect(json_response['status']).to eq('success')
  114. expect(json_response['name']).to eq('coverage')
  115. expect(json_response['ref']).to eq('develop')
  116. expect(json_response['target_url']).to eq('url')
  117. expect(json_response['description']).to eq('test')
  118. end
  119. end
  120. context 'invalid status' do
  121. before { post api(post_url, developer), state: 'invalid' }
  122. it 'does not create commit status' do
  123. expect(response.status).to eq(400)
  124. end
  125. end
  126. context 'request without state' do
  127. before { post api(post_url, developer) }
  128. it 'does not create commit status' do
  129. expect(response.status).to eq(400)
  130. end
  131. end
  132. context 'invalid commit' do
  133. let(:sha) { 'invalid_sha' }
  134. before { post api(post_url, developer), state: 'running' }
  135. it 'returns not found error' do
  136. expect(response.status).to eq(404)
  137. end
  138. end
  139. end
  140. context 'reporter user' do
  141. before { post api(post_url, reporter) }
  142. it 'should not create commit status' do
  143. expect(response.status).to eq(403)
  144. end
  145. end
  146. context 'guest user' do
  147. before { post api(post_url, guest) }
  148. it 'should not create commit status' do
  149. expect(response.status).to eq(403)
  150. end
  151. end
  152. context 'unauthorized user' do
  153. before { post api(post_url) }
  154. it 'should not create commit status' do
  155. expect(response.status).to eq(401)
  156. end
  157. end
  158. end
  159. def create_user(access_level_trait)
  160. user = create(:user)
  161. create(:project_member, access_level_trait, user: user, project: project)
  162. user
  163. end
  164. end