BigW Consortium Gitlab

internal_spec.rb 5.31 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::API, api: true  do
4 5 6 7
  include ApiHelpers
  let(:user) { create(:user) }
  let(:key) { create(:key, user: user) }
  let(:project) { create(:project) }
8
  let(:secret_token) { File.read Gitlab.config.gitlab_shell.secret_file }
9 10 11

  describe "GET /internal/check", no_db: true do
    it do
12
      get api("/internal/check"), secret_token: secret_token
13

14 15
      expect(response.status).to eq(200)
      expect(json_response['api_version']).to eq(API::API.version)
16 17 18
    end
  end

19 20 21 22 23 24 25
  describe "GET /internal/broadcast_message" do
    context "broadcast message exists" do
      let!(:broadcast_message) { create(:broadcast_message, starts_at: Time.now.yesterday, ends_at: Time.now.tomorrow ) }

      it do
        get api("/internal/broadcast_message"), secret_token: secret_token

26 27
        expect(response.status).to eq(200)
        expect(json_response["message"]).to eq(broadcast_message.message)
28 29 30 31 32 33 34
      end
    end

    context "broadcast message doesn't exist" do
      it do
        get api("/internal/broadcast_message"), secret_token: secret_token

35 36
        expect(response.status).to eq(200)
        expect(json_response).to be_empty
37 38 39 40
      end
    end
  end

41 42
  describe "GET /internal/discover" do
    it do
43
      get(api("/internal/discover"), key_id: key.id, secret_token: secret_token)
44

45
      expect(response.status).to eq(200)
46

47
      expect(json_response['name']).to eq(user.name)
48 49 50
    end
  end

51
  describe "POST /internal/allowed" do
52 53 54 55 56 57 58
    context "access granted" do
      before do
        project.team << [user, :developer]
      end

      context "git pull" do
        it do
59
          pull(key, project)
60

61 62
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_truthy
63 64 65 66 67
        end
      end

      context "git push" do
        it do
68
          push(key, project)
69

70 71
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_truthy
72 73 74 75 76 77 78 79 80 81 82
        end
      end
    end

    context "access denied" do
      before do
        project.team << [user, :guest]
      end

      context "git pull" do
        it do
83
          pull(key, project)
84

85 86
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
87 88 89 90 91
        end
      end

      context "git push" do
        it do
92
          push(key, project)
93

94 95
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
96 97 98 99
        end
      end
    end

100 101 102 103 104 105 106 107 108 109 110
    context "blocked user" do
      let(:personal_project) { create(:project, namespace: user.namespace) }

      before do
        user.block
      end

      context "git pull" do
        it do
          pull(key, personal_project)

111 112
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
113 114 115 116 117 118 119
        end
      end

      context "git push" do
        it do
          push(key, personal_project)

120 121
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
122 123 124
        end
      end
    end
125

126 127 128 129 130 131 132 133 134 135 136 137
    context "archived project" do
      let(:personal_project) { create(:project, namespace: user.namespace) }

      before do
        project.team << [user, :developer]
        project.archive!
      end

      context "git pull" do
        it do
          pull(key, project)

138 139
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_truthy
140 141 142 143 144 145 146
        end
      end

      context "git push" do
        it do
          push(key, project)

147 148
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
149 150 151 152
        end
      end
    end

153 154 155 156 157 158 159 160 161 162 163
    context "deploy key" do
      let(:key) { create(:deploy_key) }

      context "added to project" do
        before do
          key.projects << project
        end

        it do
          archive(key, project)

164 165
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_truthy
166 167 168 169 170 171 172
        end
      end

      context "not added to project" do
        it do
          archive(key, project)

173 174
          expect(response.status).to eq(200)
          expect(json_response["status"]).to be_falsey
175 176 177
        end
      end
    end
178 179 180 181 182

    context 'project does not exist' do
      it do
        pull(key, OpenStruct.new(path_with_namespace: 'gitlab/notexists'))

183 184
        expect(response.status).to eq(200)
        expect(json_response["status"]).to be_falsey
185 186 187 188 189 190 191
      end
    end

    context 'user does not exist' do
      it do
        pull(OpenStruct.new(id: 0), project)

192 193
        expect(response.status).to eq(200)
        expect(json_response["status"]).to be_falsey
194 195
      end
    end
196 197 198
  end

  def pull(key, project)
199
    post(
200 201 202
      api("/internal/allowed"),
      key_id: key.id,
      project: project.path_with_namespace,
203 204
      action: 'git-upload-pack',
      secret_token: secret_token
205 206 207 208
    )
  end

  def push(key, project)
209
    post(
210
      api("/internal/allowed"),
211
      changes: 'd14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/master',
212 213
      key_id: key.id,
      project: project.path_with_namespace,
214 215
      action: 'git-receive-pack',
      secret_token: secret_token
216
    )
217
  end
218 219

  def archive(key, project)
220
    post(
221 222 223 224
      api("/internal/allowed"),
      ref: 'master',
      key_id: key.id,
      project: project.path_with_namespace,
225 226
      action: 'git-upload-archive',
      secret_token: secret_token
227 228
    )
  end
229
end