2016-03-09 18:25:39 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-04-21 20:32:02 +00:00
|
|
|
describe API::ProjectSnippets do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, :public) }
|
2017-02-01 18:15:59 +00:00
|
|
|
let(:user) { create(:user) }
|
2016-11-21 14:06:32 +00:00
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
|
2017-07-07 15:02:54 +00:00
|
|
|
describe "GET /projects/:project_id/snippets/:id/user_agent_detail" do
|
|
|
|
let(:snippet) { create(:project_snippet, :public, project: project) }
|
|
|
|
let!(:user_agent_detail) { create(:user_agent_detail, subject: snippet) }
|
|
|
|
|
|
|
|
it 'exposes known attributes' do
|
|
|
|
get api("/projects/#{project.id}/snippets/#{snippet.id}/user_agent_detail", admin)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(json_response['user_agent']).to eq(user_agent_detail.user_agent)
|
|
|
|
expect(json_response['ip_address']).to eq(user_agent_detail.ip_address)
|
|
|
|
expect(json_response['akismet_submitted']).to eq(user_agent_detail.submitted)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns unautorized for non-admin users" do
|
|
|
|
get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/user_agent_detail", user)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-25 04:45:26 +00:00
|
|
|
describe 'GET /projects/:project_id/snippets/' do
|
2016-11-21 14:06:32 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2016-08-01 15:00:44 +00:00
|
|
|
it 'returns all snippets available to team member' do
|
2017-02-02 13:10:42 +00:00
|
|
|
project.add_developer(user)
|
2016-04-25 04:45:26 +00:00
|
|
|
public_snippet = create(:project_snippet, :public, project: project)
|
|
|
|
internal_snippet = create(:project_snippet, :internal, project: project)
|
|
|
|
private_snippet = create(:project_snippet, :private, project: project)
|
|
|
|
|
2017-01-24 20:49:10 +00:00
|
|
|
get api("/projects/#{project.id}/snippets", user)
|
2016-04-25 04:45:26 +00:00
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2017-01-24 20:49:10 +00:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
2016-04-25 04:45:26 +00:00
|
|
|
expect(json_response.size).to eq(3)
|
2017-07-07 15:02:54 +00:00
|
|
|
expect(json_response.map { |snippet| snippet['id'] }).to include(public_snippet.id, internal_snippet.id, private_snippet.id)
|
2016-08-02 17:37:04 +00:00
|
|
|
expect(json_response.last).to have_key('web_url')
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'hides private snippets from regular user' do
|
|
|
|
create(:project_snippet, :private, project: project)
|
|
|
|
|
|
|
|
get api("/projects/#{project.id}/snippets/", user)
|
2017-01-24 20:49:10 +00:00
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2017-01-24 20:49:10 +00:00
|
|
|
expect(response).to include_pagination_headers
|
|
|
|
expect(json_response).to be_an Array
|
2016-04-25 04:45:26 +00:00
|
|
|
expect(json_response.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-03 15:26:49 +00:00
|
|
|
describe 'GET /projects/:project_id/snippets/:id' do
|
|
|
|
let(:user) { create(:user) }
|
2017-07-07 15:02:54 +00:00
|
|
|
let(:snippet) { create(:project_snippet, :public, project: project) }
|
2017-05-03 15:26:49 +00:00
|
|
|
|
|
|
|
it 'returns snippet json' do
|
|
|
|
get api("/projects/#{project.id}/snippets/#{snippet.id}", user)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
|
|
|
|
expect(json_response['title']).to eq(snippet.title)
|
|
|
|
expect(json_response['description']).to eq(snippet.description)
|
|
|
|
expect(json_response['file_name']).to eq(snippet.file_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
|
|
|
get api("/projects/#{project.id}/snippets/1234", user)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Not found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-25 04:45:26 +00:00
|
|
|
describe 'POST /projects/:project_id/snippets/' do
|
2016-11-21 14:06:32 +00:00
|
|
|
let(:params) do
|
|
|
|
{
|
2016-04-25 04:45:26 +00:00
|
|
|
title: 'Test Title',
|
|
|
|
file_name: 'test.rb',
|
2017-05-03 15:26:49 +00:00
|
|
|
description: 'test description',
|
2016-04-25 04:45:26 +00:00
|
|
|
code: 'puts "hello world"',
|
2017-03-01 15:16:29 +00:00
|
|
|
visibility: 'public'
|
2016-04-25 04:45:26 +00:00
|
|
|
}
|
2016-11-21 14:06:32 +00:00
|
|
|
end
|
2016-04-25 04:45:26 +00:00
|
|
|
|
2016-11-21 14:06:32 +00:00
|
|
|
it 'creates a new snippet' do
|
2016-04-25 04:45:26 +00:00
|
|
|
post api("/projects/#{project.id}/snippets/", admin), params
|
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(201)
|
2016-04-25 04:45:26 +00:00
|
|
|
snippet = ProjectSnippet.find(json_response['id'])
|
|
|
|
expect(snippet.content).to eq(params[:code])
|
2017-05-03 15:26:49 +00:00
|
|
|
expect(snippet.description).to eq(params[:description])
|
2016-04-25 04:45:26 +00:00
|
|
|
expect(snippet.title).to eq(params[:title])
|
|
|
|
expect(snippet.file_name).to eq(params[:file_name])
|
2017-03-01 15:16:29 +00:00
|
|
|
expect(snippet.visibility_level).to eq(Snippet::PUBLIC)
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
2016-11-21 14:06:32 +00:00
|
|
|
|
|
|
|
it 'returns 400 for missing parameters' do
|
|
|
|
params.delete(:title)
|
|
|
|
|
|
|
|
post api("/projects/#{project.id}/snippets/", admin), params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
2017-02-01 18:15:59 +00:00
|
|
|
|
|
|
|
context 'when the snippet is spam' do
|
|
|
|
def create_snippet(project, snippet_params = {})
|
2017-02-02 13:10:42 +00:00
|
|
|
project.add_developer(user)
|
2017-02-01 18:15:59 +00:00
|
|
|
|
|
|
|
post api("/projects/#{project.id}/snippets", user), params.merge(snippet_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-08-24 17:05:02 +00:00
|
|
|
allow_any_instance_of(AkismetService).to receive(:spam?).and_return(true)
|
2017-02-01 18:15:59 +00:00
|
|
|
end
|
|
|
|
|
2017-02-14 19:07:11 +00:00
|
|
|
context 'when the snippet is private' do
|
|
|
|
it 'creates the snippet' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { create_snippet(project, visibility: 'private') }
|
|
|
|
.to change { Snippet.count }.by(1)
|
2017-02-01 18:15:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-14 19:07:11 +00:00
|
|
|
context 'when the snippet is public' do
|
2017-03-01 15:16:29 +00:00
|
|
|
it 'rejects the snippet' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { create_snippet(project, visibility: 'public') }
|
|
|
|
.not_to change { Snippet.count }
|
2017-02-14 19:07:11 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
expect(json_response['message']).to eq({ "error" => "Spam detected" })
|
2017-02-01 18:15:59 +00:00
|
|
|
end
|
|
|
|
|
2017-02-14 19:07:11 +00:00
|
|
|
it 'creates a spam log' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { create_snippet(project, visibility: 'public') }
|
|
|
|
.to change { SpamLog.count }.by(1)
|
2017-02-01 18:15:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT /projects/:project_id/snippets/:id/' do
|
2017-02-14 19:07:11 +00:00
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
let(:snippet) { create(:project_snippet, author: admin, visibility_level: visibility_level) }
|
2016-11-21 14:06:32 +00:00
|
|
|
|
2016-04-25 04:45:26 +00:00
|
|
|
it 'updates snippet' do
|
|
|
|
new_content = 'New content'
|
2017-05-03 15:26:49 +00:00
|
|
|
new_description = 'New description'
|
2016-04-25 04:45:26 +00:00
|
|
|
|
2017-05-03 15:26:49 +00:00
|
|
|
put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin), code: new_content, description: new_description
|
2016-04-25 04:45:26 +00:00
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2016-04-25 04:45:26 +00:00
|
|
|
snippet.reload
|
|
|
|
expect(snippet.content).to eq(new_content)
|
2017-05-03 15:26:49 +00:00
|
|
|
expect(snippet.description).to eq(new_description)
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
2016-11-21 14:06:32 +00:00
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
|
|
|
put api("/projects/#{snippet.project.id}/snippets/1234", admin), title: 'foo'
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Snippet Not Found')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 400 for missing parameters' do
|
|
|
|
put api("/projects/#{project.id}/snippets/1234", admin)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
end
|
2017-02-14 19:07:11 +00:00
|
|
|
|
|
|
|
context 'when the snippet is spam' do
|
|
|
|
def update_snippet(snippet_params = {})
|
|
|
|
put api("/projects/#{snippet.project.id}/snippets/#{snippet.id}", admin), snippet_params
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-08-24 17:05:02 +00:00
|
|
|
allow_any_instance_of(AkismetService).to receive(:spam?).and_return(true)
|
2017-02-14 19:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is private' do
|
|
|
|
let(:visibility_level) { Snippet::PRIVATE }
|
|
|
|
|
|
|
|
it 'creates the snippet' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { update_snippet(title: 'Foo') }
|
|
|
|
.to change { snippet.reload.title }.to('Foo')
|
2017-02-14 19:07:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is public' do
|
|
|
|
let(:visibility_level) { Snippet::PUBLIC }
|
|
|
|
|
|
|
|
it 'rejects the snippet' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { update_snippet(title: 'Foo') }
|
|
|
|
.not_to change { snippet.reload.title }
|
2017-02-14 19:07:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a spam log' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { update_snippet(title: 'Foo') }
|
|
|
|
.to change { SpamLog.count }.by(1)
|
2017-02-14 19:07:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the private snippet is made public' do
|
|
|
|
let(:visibility_level) { Snippet::PRIVATE }
|
|
|
|
|
|
|
|
it 'rejects the snippet' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { update_snippet(title: 'Foo', visibility: 'public') }
|
|
|
|
.not_to change { snippet.reload.title }
|
2017-02-14 19:07:11 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
expect(json_response['message']).to eq({ "error" => "Spam detected" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a spam log' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect { update_snippet(title: 'Foo', visibility: 'public') }
|
|
|
|
.to change { SpamLog.count }.by(1)
|
2017-02-14 19:07:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /projects/:project_id/snippets/:id/' do
|
2016-11-21 14:06:32 +00:00
|
|
|
let(:snippet) { create(:project_snippet, author: admin) }
|
|
|
|
|
2016-04-25 04:45:26 +00:00
|
|
|
it 'deletes snippet' do
|
|
|
|
delete api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin)
|
|
|
|
|
2017-02-20 18:18:12 +00:00
|
|
|
expect(response).to have_http_status(204)
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
2016-11-21 14:06:32 +00:00
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
|
|
|
delete api("/projects/#{snippet.project.id}/snippets/1234", admin)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Snippet Not Found')
|
|
|
|
end
|
2017-08-24 16:03:39 +00:00
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/", admin) }
|
|
|
|
end
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:project_id/snippets/:id/raw' do
|
2016-11-21 14:06:32 +00:00
|
|
|
let(:snippet) { create(:project_snippet, author: admin) }
|
2016-04-25 04:45:26 +00:00
|
|
|
|
2016-11-21 14:06:32 +00:00
|
|
|
it 'returns raw text' do
|
2016-04-25 04:45:26 +00:00
|
|
|
get api("/projects/#{snippet.project.id}/snippets/#{snippet.id}/raw", admin)
|
|
|
|
|
2016-06-27 18:10:42 +00:00
|
|
|
expect(response).to have_http_status(200)
|
2016-04-25 04:45:26 +00:00
|
|
|
expect(response.content_type).to eq 'text/plain'
|
|
|
|
expect(response.body).to eq(snippet.content)
|
|
|
|
end
|
2016-11-21 14:06:32 +00:00
|
|
|
|
|
|
|
it 'returns 404 for invalid snippet id' do
|
2017-02-20 19:32:44 +00:00
|
|
|
get api("/projects/#{snippet.project.id}/snippets/1234/raw", admin)
|
2016-11-21 14:06:32 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Snippet Not Found')
|
|
|
|
end
|
2016-04-25 04:45:26 +00:00
|
|
|
end
|
2016-03-09 18:25:39 +00:00
|
|
|
end
|