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