2013-11-07 11:53:35 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-23 15:14:08 -05:00
|
|
|
describe API::Files, api: true do
|
2013-11-07 11:53:35 -05:00
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
2014-01-22 14:03:52 -05:00
|
|
|
let!(:project) { create(:project, namespace: user.namespace ) }
|
2016-12-20 09:05:57 -05:00
|
|
|
let(:guest) { create(:user).tap { |u| create(:project_member, :guest, user: u, project: project) } }
|
2014-08-06 02:57:15 -04:00
|
|
|
let(:file_path) { 'files/ruby/popen.rb' }
|
2016-12-20 09:05:57 -05:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
ref: 'master'
|
|
|
|
}
|
|
|
|
end
|
2016-09-08 11:40:07 -04:00
|
|
|
let(:author_email) { FFaker::Internet.email }
|
|
|
|
|
|
|
|
# I have to remove periods from the end of the name
|
|
|
|
# This happened when the user's name had a suffix (i.e. "Sr.")
|
|
|
|
# This seems to be what git does under the hood. For example, this commit:
|
|
|
|
#
|
|
|
|
# $ git commit --author='Foo Sr. <foo@example.com>' -m 'Where's my trailing period?'
|
|
|
|
#
|
|
|
|
# results in this:
|
|
|
|
#
|
|
|
|
# $ git show --pretty
|
|
|
|
# ...
|
|
|
|
# Author: Foo Sr <foo@example.com>
|
|
|
|
# ...
|
|
|
|
let(:author_name) { FFaker::Name.name.chomp("\.") }
|
2014-08-06 02:57:15 -04:00
|
|
|
|
2013-11-07 11:53:35 -05:00
|
|
|
before { project.team << [user, :developer] }
|
2014-02-18 05:27:02 -05:00
|
|
|
|
|
|
|
describe "GET /projects/:id/repository/files" do
|
2016-12-20 09:05:57 -05:00
|
|
|
let(:route) { "/projects/#{project.id}/repository/files" }
|
|
|
|
|
2016-12-16 12:58:24 -05:00
|
|
|
shared_examples_for 'repository files' do
|
|
|
|
it "returns file info" do
|
2016-12-20 09:05:57 -05:00
|
|
|
get api(route, current_user), params
|
2016-12-16 12:58:24 -05:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(json_response['file_path']).to eq(file_path)
|
|
|
|
expect(json_response['file_name']).to eq('popen.rb')
|
|
|
|
expect(json_response['last_commit_id']).to eq('570e7b2abdd848b95f2f578043fc23bd6f6fd24d')
|
|
|
|
expect(Base64.decode64(json_response['content']).lines.first).to eq("require 'fileutils'\n")
|
|
|
|
end
|
2016-12-20 09:05:57 -05:00
|
|
|
|
|
|
|
context 'when no params are given' do
|
|
|
|
it_behaves_like '400 response' do
|
|
|
|
let(:request) { get api(route, current_user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file_path does not exist' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
file_path: 'app/models/application.rb',
|
|
|
|
ref: 'master',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route, current_user), params }
|
|
|
|
let(:message) { '404 File Not Found' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repository is disabled' do
|
|
|
|
include_context 'disabled repository'
|
|
|
|
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, current_user), params }
|
|
|
|
end
|
|
|
|
end
|
2016-12-16 12:58:24 -05:00
|
|
|
end
|
|
|
|
|
2016-12-20 09:05:57 -05:00
|
|
|
context 'when unauthenticated', 'and project is public' do
|
2016-12-16 12:58:24 -05:00
|
|
|
it_behaves_like 'repository files' do
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
end
|
|
|
|
end
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-12-20 09:05:57 -05:00
|
|
|
context 'when unauthenticated', 'and project is private' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(route), params }
|
|
|
|
let(:message) { '404 Project Not Found' }
|
2016-12-16 12:58:24 -05:00
|
|
|
end
|
2014-02-18 05:27:02 -05:00
|
|
|
end
|
|
|
|
|
2016-12-20 09:05:57 -05:00
|
|
|
context 'when authenticated', 'as a developer' do
|
|
|
|
it_behaves_like 'repository files' do
|
|
|
|
let(:current_user) { user }
|
|
|
|
end
|
2014-02-18 05:27:02 -05:00
|
|
|
end
|
|
|
|
|
2016-12-20 09:05:57 -05:00
|
|
|
context 'when authenticated', 'as a guest' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(route, guest), params }
|
|
|
|
end
|
2014-02-18 05:27:02 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-07 11:53:35 -05:00
|
|
|
|
|
|
|
describe "POST /projects/:id/repository/files" do
|
2015-06-22 16:00:54 -04:00
|
|
|
let(:valid_params) do
|
2013-11-11 07:58:06 -05:00
|
|
|
{
|
2013-11-20 03:03:50 -05:00
|
|
|
file_path: 'newfile.rb',
|
2013-11-11 07:58:06 -05:00
|
|
|
branch_name: 'master',
|
|
|
|
content: 'puts 8',
|
|
|
|
commit_message: 'Added newfile'
|
|
|
|
}
|
2015-06-22 16:00:54 -04:00
|
|
|
end
|
2013-11-11 07:58:06 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "creates a new file in project repo" do
|
2013-11-07 11:53:35 -05:00
|
|
|
post api("/projects/#{project.id}/repository/files", user), valid_params
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['file_path']).to eq('newfile.rb')
|
2016-09-08 11:40:07 -04:00
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(user.email)
|
|
|
|
expect(last_commit.author_name).to eq(user.name)
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 bad request if no params given" do
|
2013-11-07 11:53:35 -05:00
|
|
|
post api("/projects/#{project.id}/repository/files", user)
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 if editor fails to create file" do
|
2015-08-11 08:33:31 -04:00
|
|
|
allow_any_instance_of(Repository).to receive(:commit_file).
|
|
|
|
and_return(false)
|
2013-11-07 11:53:35 -05:00
|
|
|
|
|
|
|
post api("/projects/#{project.id}/repository/files", user), valid_params
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|
2016-09-08 11:40:07 -04:00
|
|
|
|
|
|
|
context "when specifying an author" do
|
|
|
|
it "creates a new file with the specified author" do
|
|
|
|
valid_params.merge!(author_email: author_email, author_name: author_name)
|
|
|
|
|
|
|
|
post api("/projects/#{project.id}/repository/files", user), valid_params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(author_email)
|
|
|
|
expect(last_commit.author_name).to eq(author_name)
|
|
|
|
end
|
|
|
|
end
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|
|
|
|
|
2013-11-11 07:58:06 -05:00
|
|
|
describe "PUT /projects/:id/repository/files" do
|
2015-06-22 16:00:54 -04:00
|
|
|
let(:valid_params) do
|
2013-11-11 07:58:06 -05:00
|
|
|
{
|
2014-08-06 02:57:15 -04:00
|
|
|
file_path: file_path,
|
2013-11-11 07:58:06 -05:00
|
|
|
branch_name: 'master',
|
|
|
|
content: 'puts 8',
|
|
|
|
commit_message: 'Changed file'
|
|
|
|
}
|
2015-06-22 16:00:54 -04:00
|
|
|
end
|
2013-11-11 07:58:06 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "updates existing file in project repo" do
|
2013-11-11 07:58:06 -05:00
|
|
|
put api("/projects/#{project.id}/repository/files", user), valid_params
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['file_path']).to eq(file_path)
|
2016-09-08 11:40:07 -04:00
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(user.email)
|
|
|
|
expect(last_commit.author_name).to eq(user.name)
|
2013-11-11 07:58:06 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 bad request if no params given" do
|
2013-11-11 07:58:06 -05:00
|
|
|
put api("/projects/#{project.id}/repository/files", user)
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-11-11 07:58:06 -05:00
|
|
|
end
|
2016-09-08 11:40:07 -04:00
|
|
|
|
|
|
|
context "when specifying an author" do
|
|
|
|
it "updates a file with the specified author" do
|
|
|
|
valid_params.merge!(author_email: author_email, author_name: author_name, content: "New content")
|
|
|
|
|
|
|
|
put api("/projects/#{project.id}/repository/files", user), valid_params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(author_email)
|
|
|
|
expect(last_commit.author_name).to eq(author_name)
|
|
|
|
end
|
|
|
|
end
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|
2013-11-19 09:36:11 -05:00
|
|
|
|
|
|
|
describe "DELETE /projects/:id/repository/files" do
|
2015-06-22 16:00:54 -04:00
|
|
|
let(:valid_params) do
|
2013-11-19 09:36:11 -05:00
|
|
|
{
|
2014-08-06 02:57:15 -04:00
|
|
|
file_path: file_path,
|
2013-11-19 09:36:11 -05:00
|
|
|
branch_name: 'master',
|
|
|
|
commit_message: 'Changed file'
|
|
|
|
}
|
2015-06-22 16:00:54 -04:00
|
|
|
end
|
2013-11-19 09:36:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "deletes existing file in project repo" do
|
2013-11-19 09:36:11 -05:00
|
|
|
delete api("/projects/#{project.id}/repository/files", user), valid_params
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['file_path']).to eq(file_path)
|
2016-09-08 11:40:07 -04:00
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(user.email)
|
|
|
|
expect(last_commit.author_name).to eq(user.name)
|
2013-11-19 09:36:11 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 bad request if no params given" do
|
2013-11-19 09:36:11 -05:00
|
|
|
delete api("/projects/#{project.id}/repository/files", user)
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-11-19 09:36:11 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 if fails to create file" do
|
2015-08-11 08:33:31 -04:00
|
|
|
allow_any_instance_of(Repository).to receive(:remove_file).and_return(false)
|
2013-11-19 09:36:11 -05:00
|
|
|
|
|
|
|
delete api("/projects/#{project.id}/repository/files", user), valid_params
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-11-19 09:36:11 -05:00
|
|
|
end
|
2016-09-08 11:40:07 -04:00
|
|
|
|
|
|
|
context "when specifying an author" do
|
|
|
|
it "removes a file with the specified author" do
|
|
|
|
valid_params.merge!(author_email: author_email, author_name: author_name)
|
|
|
|
|
|
|
|
delete api("/projects/#{project.id}/repository/files", user), valid_params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
last_commit = project.repository.commit.raw
|
|
|
|
expect(last_commit.author_email).to eq(author_email)
|
|
|
|
expect(last_commit.author_name).to eq(author_name)
|
|
|
|
end
|
|
|
|
end
|
2013-11-19 09:36:11 -05:00
|
|
|
end
|
2015-08-09 01:35:40 -04:00
|
|
|
|
|
|
|
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
|
2016-09-08 11:40:07 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-08-09 01:35:40 -04:00
|
|
|
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
|
2013-11-07 11:53:35 -05:00
|
|
|
end
|