diff options
Diffstat (limited to 'spec/requests/api/snippets_spec.rb')
-rw-r--r-- | spec/requests/api/snippets_spec.rb | 196 |
1 files changed, 100 insertions, 96 deletions
diff --git a/spec/requests/api/snippets_spec.rb b/spec/requests/api/snippets_spec.rb index 7c8512f7589..b57a92b54eb 100644 --- a/spec/requests/api/snippets_spec.rb +++ b/spec/requests/api/snippets_spec.rb @@ -1,10 +1,10 @@ -require 'rails_helper' +require "rails_helper" describe API::Snippets do let!(:user) { create(:user) } - describe 'GET /snippets/' do - it 'returns snippets available' do + 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) @@ -14,16 +14,17 @@ describe API::Snippets do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers expect(json_response).to be_an Array - expect(json_response.map { |snippet| snippet['id']} ).to contain_exactly( + 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') - expect(json_response.last).to have_key('visibility') + private_snippet.id + ) + expect(json_response.last).to have_key("web_url") + expect(json_response.last).to have_key("raw_url") + expect(json_response.last).to have_key("visibility") end - it 'hides private snippets from regular user' do + it "hides private snippets from regular user" do create(:personal_snippet, :private) get api("/snippets/", user) @@ -34,7 +35,7 @@ describe API::Snippets do expect(json_response.size).to eq(0) end - it 'returns 404 for non-authenticated' do + it "returns 404 for non-authenticated" do create(:personal_snippet, :internal) get api("/snippets/") @@ -42,7 +43,7 @@ describe API::Snippets do expect(response).to have_gitlab_http_status(401) end - it 'does not return snippets related to a project with disable feature visibility' do + it "does not return snippets related to a project with disable feature visibility" do project = create(:project) create(:project_member, project: project, user: user) public_snippet = create(:personal_snippet, :public, author: user, project: project) @@ -56,7 +57,7 @@ describe API::Snippets do end end - describe 'GET /snippets/public' do + 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) } @@ -65,95 +66,98 @@ describe API::Snippets do 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 + it "returns all snippets with public visibility from all users" do get api("/snippets/public", user) expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers expect(json_response).to be_an Array - expect(json_response.map { |snippet| snippet['id']} ).to contain_exactly( + expect(json_response.map { |snippet| snippet["id"]}).to contain_exactly( public_snippet.id, - public_snippet_other.id) - expect(json_response.map { |snippet| snippet['web_url']} ).to include( + public_snippet_other.id + ) + expect(json_response.map { |snippet| snippet["web_url"]}).to include( "http://localhost/snippets/#{public_snippet.id}", - "http://localhost/snippets/#{public_snippet_other.id}") - expect(json_response.map { |snippet| snippet['raw_url']} ).to include( + "http://localhost/snippets/#{public_snippet_other.id}" + ) + expect(json_response.map { |snippet| snippet["raw_url"]}).to include( "http://localhost/snippets/#{public_snippet.id}/raw", - "http://localhost/snippets/#{public_snippet_other.id}/raw") + "http://localhost/snippets/#{public_snippet_other.id}/raw" + ) end end - describe 'GET /snippets/:id/raw' do + describe "GET /snippets/:id/raw" do let(:snippet) { create(:personal_snippet, author: user) } - it 'returns raw text' do + it "returns raw text" do get api("/snippets/#{snippet.id}/raw", user) expect(response).to have_gitlab_http_status(200) - expect(response.content_type).to eq 'text/plain' + expect(response.content_type).to eq "text/plain" expect(response.body).to eq(snippet.content) end - it 'forces attachment content disposition' do + it "forces attachment content disposition" do get api("/snippets/#{snippet.id}/raw", user) - expect(headers['Content-Disposition']).to match(/^attachment/) + expect(headers["Content-Disposition"]).to match(/^attachment/) end - it 'returns 404 for invalid snippet id' do + it "returns 404 for invalid snippet id" do get api("/snippets/1234/raw", user) expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Snippet Not Found') + expect(json_response["message"]).to eq("404 Snippet Not Found") end end - describe 'GET /snippets/:id' do + describe "GET /snippets/:id" do let(:snippet) { create(:personal_snippet, author: user) } - it 'returns snippet json' do + it "returns snippet json" do get api("/snippets/#{snippet.id}", user) expect(response).to have_gitlab_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) - expect(json_response['visibility']).to eq(snippet.visibility) + 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) + expect(json_response["visibility"]).to eq(snippet.visibility) end - it 'returns 404 for invalid snippet id' do + it "returns 404 for invalid snippet id" do get api("/snippets/1234", user) expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Not found') + expect(json_response["message"]).to eq("404 Not found") end end - describe 'POST /snippets/' do + describe "POST /snippets/" do let(:params) do { - title: 'Test Title', - file_name: 'test.rb', - description: 'test description', + title: "Test Title", + file_name: "test.rb", + description: "test description", content: 'puts "hello world"', - visibility: 'public' + visibility: "public", } end - it 'creates a new snippet' do - expect do + it "creates a new snippet" do + expect { post api("/snippets/", user), params: params - end.to change { PersonalSnippet.count }.by(1) + }.to change { PersonalSnippet.count }.by(1) expect(response).to have_gitlab_http_status(201) - expect(json_response['title']).to eq(params[:title]) - expect(json_response['description']).to eq(params[:description]) - expect(json_response['file_name']).to eq(params[:file_name]) - expect(json_response['visibility']).to eq(params[:visibility]) + expect(json_response["title"]).to eq(params[:title]) + expect(json_response["description"]).to eq(params[:description]) + expect(json_response["file_name"]).to eq(params[:file_name]) + expect(json_response["visibility"]).to eq(params[:visibility]) end - it 'returns 400 for missing parameters' do + it "returns 400 for missing parameters" do params.delete(:title) post api("/snippets/", user), params: params @@ -161,51 +165,51 @@ describe API::Snippets do expect(response).to have_gitlab_http_status(400) end - context 'when the snippet is spam' do + context "when the snippet is spam" do def create_snippet(snippet_params = {}) - post api('/snippets', user), params: params.merge(snippet_params) + post api("/snippets", user), params: params.merge(snippet_params) end before do allow_any_instance_of(AkismetService).to receive(:spam?).and_return(true) end - context 'when the snippet is private' do - it 'creates the snippet' do - expect { create_snippet(visibility: 'private') } + context "when the snippet is private" do + it "creates the snippet" do + expect { create_snippet(visibility: "private") } .to change { Snippet.count }.by(1) end end - context 'when the snippet is public' do - it 'rejects the shippet' do - expect { create_snippet(visibility: 'public') } + context "when the snippet is public" do + it "rejects the shippet" do + expect { create_snippet(visibility: "public") } .not_to change { Snippet.count } expect(response).to have_gitlab_http_status(400) - expect(json_response['message']).to eq({ "error" => "Spam detected" }) + expect(json_response["message"]).to eq({"error" => "Spam detected"}) end - it 'creates a spam log' do - expect { create_snippet(visibility: 'public') } + it "creates a spam log" do + expect { create_snippet(visibility: "public") } .to change { SpamLog.count }.by(1) end end end end - describe 'PUT /snippets/:id' do + describe "PUT /snippets/:id" do let(:visibility_level) { Snippet::PUBLIC } let(:other_user) { create(:user) } let(:snippet) do create(:personal_snippet, author: user, visibility_level: visibility_level) end - it 'updates snippet' do - new_content = 'New content' - new_description = 'New description' + it "updates snippet" do + new_content = "New content" + new_description = "New description" - put api("/snippets/#{snippet.id}", user), params: { content: new_content, description: new_description } + put api("/snippets/#{snippet.id}", user), params: {content: new_content, description: new_description} expect(response).to have_gitlab_http_status(200) snippet.reload @@ -213,27 +217,27 @@ describe API::Snippets do expect(snippet.description).to eq(new_description) end - it 'returns 404 for invalid snippet id' do - put api("/snippets/1234", user), params: { title: 'foo' } + it "returns 404 for invalid snippet id" do + put api("/snippets/1234", user), params: {title: "foo"} expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Snippet Not Found') + expect(json_response["message"]).to eq("404 Snippet Not Found") end it "returns 404 for another user's snippet" do - put api("/snippets/#{snippet.id}", other_user), params: { title: 'fubar' } + put api("/snippets/#{snippet.id}", other_user), params: {title: "fubar"} expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Snippet Not Found') + expect(json_response["message"]).to eq("404 Snippet Not Found") end - it 'returns 400 for missing parameters' do + it "returns 400 for missing parameters" do put api("/snippets/1234", user) expect(response).to have_gitlab_http_status(400) end - context 'when the snippet is spam' do + context "when the snippet is spam" do def update_snippet(snippet_params = {}) put api("/snippets/#{snippet.id}", user), params: snippet_params end @@ -242,66 +246,66 @@ describe API::Snippets do allow_any_instance_of(AkismetService).to receive(:spam?).and_return(true) end - context 'when the snippet is private' do + context "when the snippet is private" do let(:visibility_level) { Snippet::PRIVATE } - it 'updates the snippet' do - expect { update_snippet(title: 'Foo') } - .to change { snippet.reload.title }.to('Foo') + it "updates the snippet" do + expect { update_snippet(title: "Foo") } + .to change { snippet.reload.title }.to("Foo") end end - context 'when the snippet is public' do + context "when the snippet is public" do let(:visibility_level) { Snippet::PUBLIC } - it 'rejects the shippet' do - expect { update_snippet(title: 'Foo') } + it "rejects the shippet" do + expect { update_snippet(title: "Foo") } .not_to change { snippet.reload.title } expect(response).to have_gitlab_http_status(400) - expect(json_response['message']).to eq({ "error" => "Spam detected" }) + expect(json_response["message"]).to eq({"error" => "Spam detected"}) end - it 'creates a spam log' do - expect { update_snippet(title: 'Foo') } + it "creates a spam log" do + expect { update_snippet(title: "Foo") } .to change { SpamLog.count }.by(1) end end - context 'when a private snippet is made public' do + context "when a private snippet is made public" do let(:visibility_level) { Snippet::PRIVATE } - it 'rejects the snippet' do - expect { update_snippet(title: 'Foo', visibility: 'public') } + it "rejects the snippet" do + expect { update_snippet(title: "Foo", visibility: "public") } .not_to change { snippet.reload.title } end - it 'creates a spam log' do - expect { update_snippet(title: 'Foo', visibility: 'public') } + it "creates a spam log" do + expect { update_snippet(title: "Foo", visibility: "public") } .to change { SpamLog.count }.by(1) end end end end - describe 'DELETE /snippets/:id' do + describe "DELETE /snippets/:id" do let!(:public_snippet) { create(:personal_snippet, :public, author: user) } - it 'deletes snippet' do - expect do + it "deletes snippet" do + expect { delete api("/snippets/#{public_snippet.id}", user) expect(response).to have_gitlab_http_status(204) - end.to change { PersonalSnippet.count }.by(-1) + }.to change { PersonalSnippet.count }.by(-1) end - it 'returns 404 for invalid snippet id' do + it "returns 404 for invalid snippet id" do delete api("/snippets/1234", user) expect(response).to have_gitlab_http_status(404) - expect(json_response['message']).to eq('404 Snippet Not Found') + expect(json_response["message"]).to eq("404 Snippet Not Found") end - it_behaves_like '412 response' do + it_behaves_like "412 response" do let(:request) { api("/snippets/#{public_snippet.id}", user) } end end @@ -311,13 +315,13 @@ describe API::Snippets do let(:snippet) { create(:personal_snippet, :public, author: user) } let!(:user_agent_detail) { create(:user_agent_detail, subject: snippet) } - it 'exposes known attributes' do + it "exposes known attributes" do get api("/snippets/#{snippet.id}/user_agent_detail", admin) expect(response).to have_gitlab_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) + 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 unauthorized for non-admin users" do |