BigW Consortium Gitlab

files_spec.rb 4.82 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::API, api: true  do
4 5
  include ApiHelpers
  let(:user) { create(:user) }
6
  let!(:project) { create(:project, namespace: user.namespace ) }
7 8
  let(:file_path) { 'files/ruby/popen.rb' }

9
  before { project.team << [user, :developer] }
10 11 12 13

  describe "GET /projects/:id/repository/files" do
    it "should return file info" do
      params = {
14
        file_path: file_path,
15 16 17 18
        ref: 'master',
      }

      get api("/projects/#{project.id}/repository/files", user), params
19
      expect(response).to have_http_status(200)
20 21
      expect(json_response['file_path']).to eq(file_path)
      expect(json_response['file_name']).to eq('popen.rb')
22
      expect(json_response['last_commit_id']).to eq('570e7b2abdd848b95f2f578043fc23bd6f6fd24d')
23
      expect(Base64.decode64(json_response['content']).lines.first).to eq("require 'fileutils'\n")
24 25 26 27
    end

    it "should return a 400 bad request if no params given" do
      get api("/projects/#{project.id}/repository/files", user)
28
      expect(response).to have_http_status(400)
29 30 31 32 33 34 35 36 37
    end

    it "should return a 404 if such file does not exist" do
      params = {
        file_path: 'app/models/application.rb',
        ref: 'master',
      }

      get api("/projects/#{project.id}/repository/files", user), params
38
      expect(response).to have_http_status(404)
39 40
    end
  end
41 42

  describe "POST /projects/:id/repository/files" do
43
    let(:valid_params) do
44
      {
45
        file_path: 'newfile.rb',
46 47 48 49
        branch_name: 'master',
        content: 'puts 8',
        commit_message: 'Added newfile'
      }
50
    end
51

52 53
    it "should create a new file in project repo" do
      post api("/projects/#{project.id}/repository/files", user), valid_params
54
      expect(response).to have_http_status(201)
55
      expect(json_response['file_path']).to eq('newfile.rb')
56 57 58 59
    end

    it "should return a 400 bad request if no params given" do
      post api("/projects/#{project.id}/repository/files", user)
60
      expect(response).to have_http_status(400)
61 62
    end

63 64 65
    it "should return a 400 if editor fails to create file" do
      allow_any_instance_of(Repository).to receive(:commit_file).
        and_return(false)
66 67

      post api("/projects/#{project.id}/repository/files", user), valid_params
68
      expect(response).to have_http_status(400)
69 70 71
    end
  end

72
  describe "PUT /projects/:id/repository/files" do
73
    let(:valid_params) do
74
      {
75
        file_path: file_path,
76 77 78 79
        branch_name: 'master',
        content: 'puts 8',
        commit_message: 'Changed file'
      }
80
    end
81 82 83

    it "should update existing file in project repo" do
      put api("/projects/#{project.id}/repository/files", user), valid_params
84
      expect(response).to have_http_status(200)
85
      expect(json_response['file_path']).to eq(file_path)
86 87 88 89
    end

    it "should return a 400 bad request if no params given" do
      put api("/projects/#{project.id}/repository/files", user)
90
      expect(response).to have_http_status(400)
91
    end
92
  end
93 94

  describe "DELETE /projects/:id/repository/files" do
95
    let(:valid_params) do
96
      {
97
        file_path: file_path,
98 99 100
        branch_name: 'master',
        commit_message: 'Changed file'
      }
101
    end
102 103 104

    it "should delete existing file in project repo" do
      delete api("/projects/#{project.id}/repository/files", user), valid_params
105
      expect(response).to have_http_status(200)
106
      expect(json_response['file_path']).to eq(file_path)
107 108 109 110
    end

    it "should return a 400 bad request if no params given" do
      delete api("/projects/#{project.id}/repository/files", user)
111
      expect(response).to have_http_status(400)
112 113
    end

114 115
    it "should return a 400 if fails to create file" do
      allow_any_instance_of(Repository).to receive(:remove_file).and_return(false)
116 117

      delete api("/projects/#{project.id}/repository/files", user), valid_params
118
      expect(response).to have_http_status(400)
119 120
    end
  end
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  describe "POST /projects/:id/repository/files with binary file" do
    let(:file_path) { 'test.bin' }
    let(:put_params) do
      {
        file_path: file_path,
        branch_name: 'master',
        content: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=',
        commit_message: 'Binary file with a \n should not be touched',
        encoding: 'base64'
      }
    end
    let(:get_params) do
      {
        file_path: file_path,
        ref: 'master',
      }
    end

    before do
      post api("/projects/#{project.id}/repository/files", user), put_params
    end

    it "remains unchanged" do
      get api("/projects/#{project.id}/repository/files", user), get_params
146
      expect(response).to have_http_status(200)
147 148 149 150 151
      expect(json_response['file_path']).to eq(file_path)
      expect(json_response['file_name']).to eq(file_path)
      expect(json_response['content']).to eq(put_params[:content])
    end
  end
152
end