commit_status_spec.rb 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
require 'spec_helper'

describe API::API, api: true do
  include ApiHelpers
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let!(:project) { create(:project, creator_id: user.id) }
  let!(:reporter) { create(:project_member, user: user, project: project, access_level: ProjectMember::REPORTER) }
  let!(:guest) { create(:project_member, user: user2, project: project, access_level: ProjectMember::GUEST) }
  let(:commit) { project.repository.commit }
  let!(:ci_commit) { project.ensure_ci_commit(commit.id) }
  let(:commit_status) { create(:commit_status, commit: ci_commit) }

  describe "GET /projects/:id/repository/commits/:sha/statuses" do
    context "reporter user" do
      let(:statuses_id) { json_response.map { |status| status['id'] } }

      before do
        @status1 = create(:commit_status, commit: ci_commit, status: 'running')
        @status2 = create(:commit_status, commit: ci_commit, name: 'coverage', status: 'pending')
21
        @status3 = create(:commit_status, commit: ci_commit, name: 'coverage', ref: 'develop', status: 'running', allow_failure: true)
22 23 24 25 26 27 28 29 30 31 32
        @status4 = create(:commit_status, commit: ci_commit, name: 'coverage', status: 'success')
        @status5 = create(:commit_status, commit: ci_commit, ref: 'develop', status: 'success')
        @status6 = create(:commit_status, commit: ci_commit, status: 'success')
      end

      it "should return latest commit statuses" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", user)
        expect(response.status).to eq(200)

        expect(json_response).to be_an Array
        expect(statuses_id).to contain_exactly(@status3.id, @status4.id, @status5.id, @status6.id)
33 34
        json_response.sort_by!{ |status| status['id'] }
        expect(json_response.map{ |status| status['allow_failure'] }).to eq([true, false, false, false])
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      end

      it "should return all commit statuses" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?all=1", user)
        expect(response.status).to eq(200)

        expect(json_response).to be_an Array
        expect(statuses_id).to contain_exactly(@status1.id, @status2.id, @status3.id, @status4.id, @status5.id, @status6.id)
      end

      it "should return latest commit statuses for specific ref" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?ref=develop", user)
        expect(response.status).to eq(200)

        expect(json_response).to be_an Array
        expect(statuses_id).to contain_exactly(@status3.id, @status5.id)
      end

      it "should return latest commit statuses for specific name" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses?name=coverage", user)
        expect(response.status).to eq(200)

        expect(json_response).to be_an Array
        expect(statuses_id).to contain_exactly(@status3.id, @status4.id)
      end
    end

    context "guest user" do
      it "should not return project commits" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses", user2)
        expect(response.status).to eq(403)
      end
    end

    context "unauthorized user" do
      it "should not return project commits" do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/statuses")
        expect(response.status).to eq(401)
      end
    end
  end

77 78
  describe 'POST /projects/:id/statuses/:sha' do
    let(:post_url) { "/projects/#{project.id}/statuses/#{commit.id}" }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    context 'reporter user' do
      context 'should create commit status' do
        it 'with only required parameters' do
          post api(post_url, user), state: 'success'
          expect(response.status).to eq(201)
          expect(json_response['sha']).to eq(commit.id)
          expect(json_response['status']).to eq('success')
          expect(json_response['name']).to eq('default')
          expect(json_response['ref']).to be_nil
          expect(json_response['target_url']).to be_nil
          expect(json_response['description']).to be_nil
        end

        it 'with all optional parameters' do
          post api(post_url, user), state: 'success', context: 'coverage', ref: 'develop', target_url: 'url', description: 'test'
          expect(response.status).to eq(201)
          expect(json_response['sha']).to eq(commit.id)
          expect(json_response['status']).to eq('success')
          expect(json_response['name']).to eq('coverage')
          expect(json_response['ref']).to eq('develop')
          expect(json_response['target_url']).to eq('url')
          expect(json_response['description']).to eq('test')
        end
      end

      context 'should not create commit status' do
        it 'with invalid state' do
          post api(post_url, user), state: 'invalid'
          expect(response.status).to eq(400)
        end

        it 'without state' do
          post api(post_url, user)
          expect(response.status).to eq(400)
        end

        it 'invalid commit' do
117
          post api("/projects/#{project.id}/statuses/invalid_sha", user), state: 'running'
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
          expect(response.status).to eq(404)
        end
      end
    end

    context 'guest user' do
      it 'should not create commit status' do
        post api(post_url, user2)
        expect(response.status).to eq(403)
      end
    end

    context 'unauthorized user' do
      it 'should not create commit status' do
        post api(post_url)
        expect(response.status).to eq(401)
      end
    end
  end
end