debian-mirror-gitlab/spec/controllers/snippets_controller_spec.rb

752 lines
22 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-11-26 14:37:03 +05:30
require 'spec_helper'
describe SnippetsController do
2020-04-08 14:13:33 +05:30
let_it_be(:user) { create(:user) }
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
describe 'GET #index' do
context 'when username parameter is present' do
2019-12-04 20:38:33 +05:30
it_behaves_like 'paginated collection' do
let(:collection) { Snippet.all }
let(:params) { { username: user.username } }
before do
create(:personal_snippet, :public, author: user)
end
end
2017-08-17 22:00:37 +05:30
it 'renders snippets of a user when username is present' do
2019-02-15 15:39:39 +05:30
get :index, params: { username: user.username }
2017-08-17 22:00:37 +05:30
expect(response).to render_template(:index)
end
end
context 'when username parameter is not present' do
it 'redirects to explore snippets page when user is not logged in' do
get :index
expect(response).to redirect_to(explore_snippets_path)
end
it 'redirects to snippets dashboard page when user is logged in' do
sign_in(user)
get :index
expect(response).to redirect_to(dashboard_snippets_path)
end
end
end
describe 'GET #new' do
context 'when signed in' do
before do
sign_in(user)
end
it 'responds with status 200' do
get :new
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
context 'when user is not allowed to create a personal snippet' do
let(:user) { create(:user, :external) }
it 'responds with status 404' do
get :new
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2020-01-01 13:55:28 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
context 'when not signed in' do
it 'redirects to the sign in page' do
get :new
expect(response).to redirect_to(new_user_session_path)
end
end
end
2016-09-29 09:46:39 +05:30
describe 'GET #show' do
2020-04-08 14:13:33 +05:30
shared_examples 'successful response' do
it 'renders the snippet' do
subject
expect(assigns(:snippet)).to eq(personal_snippet)
expect(response).to have_gitlab_http_status(:ok)
end
it 'renders the blob from the repository' do
subject
expect(assigns(:blob)).to eq(personal_snippet.blobs.first)
end
end
2015-11-26 14:37:03 +05:30
context 'when the personal snippet is private' do
2020-04-08 14:13:33 +05:30
let_it_be(:personal_snippet) { create(:personal_snippet, :private, :repository, author: user) }
2015-11-26 14:37:03 +05:30
context 'when signed in' do
before do
sign_in(user)
end
context 'when signed in user is not the author' do
let(:other_author) { create(:author) }
let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_author) }
it 'responds with status 404' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: other_personal_snippet.to_param }
2015-11-26 14:37:03 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-11-26 14:37:03 +05:30
end
end
context 'when signed in user is the author' do
2020-04-08 14:13:33 +05:30
it_behaves_like 'successful response' do
subject { get :show, params: { id: personal_snippet.to_param } }
2015-11-26 14:37:03 +05:30
end
2019-01-03 12:48:30 +05:30
it 'responds with status 404 when embeddable content is requested' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }, format: :js
2019-01-03 12:48:30 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-01-03 12:48:30 +05:30
end
2015-11-26 14:37:03 +05:30
end
end
context 'when not signed in' do
it 'redirects to the sign in page' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }
2015-11-26 14:37:03 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'when the personal snippet is internal' do
2020-04-08 14:13:33 +05:30
let_it_be(:personal_snippet) { create(:personal_snippet, :internal, :repository, author: user) }
2015-11-26 14:37:03 +05:30
context 'when signed in' do
before do
sign_in(user)
end
2020-04-08 14:13:33 +05:30
it_behaves_like 'successful response' do
subject { get :show, params: { id: personal_snippet.to_param } }
2015-11-26 14:37:03 +05:30
end
2019-01-03 12:48:30 +05:30
it 'responds with status 404 when embeddable content is requested' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }, format: :js
2019-01-03 12:48:30 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2019-01-03 12:48:30 +05:30
end
2015-11-26 14:37:03 +05:30
end
context 'when not signed in' do
it 'redirects to the sign in page' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }
2015-11-26 14:37:03 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'when the personal snippet is public' do
2020-04-08 14:13:33 +05:30
let_it_be(:personal_snippet) { create(:personal_snippet, :public, :repository, author: user) }
2015-11-26 14:37:03 +05:30
context 'when signed in' do
before do
sign_in(user)
end
2020-04-08 14:13:33 +05:30
it_behaves_like 'successful response' do
subject { get :show, params: { id: personal_snippet.to_param } }
2015-11-26 14:37:03 +05:30
end
2019-01-03 12:48:30 +05:30
it 'responds with status 200 when embeddable content is requested' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }, format: :js
2019-01-03 12:48:30 +05:30
expect(assigns(:snippet)).to eq(personal_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-01-03 12:48:30 +05:30
end
2015-11-26 14:37:03 +05:30
end
context 'when not signed in' do
it 'renders the snippet' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: personal_snippet.to_param }
2015-11-26 14:37:03 +05:30
expect(assigns(:snippet)).to eq(personal_snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2015-11-26 14:37:03 +05:30
end
end
end
context 'when the personal snippet does not exist' do
context 'when signed in' do
before do
sign_in(user)
end
it 'responds with status 404' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: 'doesntexist' }
2015-11-26 14:37:03 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-11-26 14:37:03 +05:30
end
end
context 'when not signed in' do
it 'responds with status 404' do
2019-02-15 15:39:39 +05:30
get :show, params: { id: 'doesntexist' }
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(new_user_session_path)
2015-11-26 14:37:03 +05:30
end
end
end
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
describe 'POST #create' do
def create_snippet(snippet_params = {}, additional_params = {})
sign_in(user)
2019-02-15 15:39:39 +05:30
post :create, params: {
2017-09-10 17:25:29 +05:30
personal_snippet: { title: 'Title', content: 'Content', description: 'Description' }.merge(snippet_params)
2017-08-17 22:00:37 +05:30
}.merge(additional_params)
Snippet.last
end
2017-09-10 17:25:29 +05:30
it 'creates the snippet correctly' do
snippet = create_snippet(visibility_level: Snippet::PRIVATE)
expect(snippet.title).to eq('Title')
expect(snippet.content).to eq('Content')
expect(snippet.description).to eq('Description')
end
2020-01-01 13:55:28 +05:30
context 'when user is not allowed to create a personal snippet' do
let(:user) { create(:user, :external) }
it 'responds with status 404' do
aggregate_failures do
expect do
create_snippet(visibility_level: Snippet::PUBLIC)
end.not_to change { Snippet.count }
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2020-01-01 13:55:28 +05:30
end
end
end
2020-05-24 23:13:21 +05:30
context 'when the controller receives the files param' do
let(:files) { %w(foo bar) }
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
it 'passes the files param to the snippet create service' do
expect(Snippets::CreateService).to receive(:new).with(nil, user, hash_including(files: files)).and_call_original
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
create_snippet({ title: nil }, { files: files })
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
context 'when the snippet is spam' do
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(Spam::AkismetService) do |instance|
2019-12-26 22:10:19 +05:30
allow(instance).to receive(:spam?).and_return(true)
end
2017-08-17 22:00:37 +05:30
end
context 'when the snippet is private' do
it 'creates the snippet' do
2017-09-10 17:25:29 +05:30
expect { create_snippet(visibility_level: Snippet::PRIVATE) }
.to change { Snippet.count }.by(1)
2017-08-17 22:00:37 +05:30
end
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
context 'when the snippet is public' do
2020-04-22 19:07:51 +05:30
it 'rejects the snippet' do
2017-09-10 17:25:29 +05:30
expect { create_snippet(visibility_level: Snippet::PUBLIC) }
.not_to change { Snippet.count }
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2017-09-10 17:25:29 +05:30
expect { create_snippet(visibility_level: Snippet::PUBLIC) }
2019-12-21 20:55:43 +05:30
.to log_spam(title: 'Title', user: user, noteable_type: 'PersonalSnippet')
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
it 'renders :new with reCAPTCHA disabled' do
2017-08-17 22:00:37 +05:30
stub_application_setting(recaptcha_enabled: false)
create_snippet(visibility_level: Snippet::PUBLIC)
expect(response).to render_template(:new)
end
2020-05-24 23:13:21 +05:30
context 'reCAPTCHA enabled' do
2017-08-17 22:00:37 +05:30
before do
stub_application_setting(recaptcha_enabled: true)
end
2020-05-24 23:13:21 +05:30
it 'renders :verify' do
2017-08-17 22:00:37 +05:30
create_snippet(visibility_level: Snippet::PUBLIC)
expect(response).to render_template(:verify)
end
2020-05-24 23:13:21 +05:30
it 'renders snippet page' do
2017-08-17 22:00:37 +05:30
spammy_title = 'Whatever'
spam_logs = create_list(:spam_log, 2, user: user, title: spammy_title)
snippet = create_snippet({ title: spammy_title },
{ spam_log_id: spam_logs.last.id,
recaptcha_verification: true })
expect(response).to redirect_to(snippet_path(snippet))
end
end
end
end
end
describe 'PUT #update' do
let(:project) { create :project }
2020-04-22 19:07:51 +05:30
let(:visibility_level) { Snippet::PUBLIC }
2017-08-17 22:00:37 +05:30
let(:snippet) { create :personal_snippet, author: user, project: project, visibility_level: visibility_level }
def update_snippet(snippet_params = {}, additional_params = {})
sign_in(user)
2019-02-15 15:39:39 +05:30
put :update, params: {
2017-08-17 22:00:37 +05:30
id: snippet.id,
personal_snippet: { title: 'Title', content: 'Content' }.merge(snippet_params)
}.merge(additional_params)
snippet.reload
end
2020-04-22 19:07:51 +05:30
it_behaves_like 'updating snippet checks blob is binary' do
let_it_be(:title) { 'Foo' }
subject { put :update, params: { id: snippet, personal_snippet: { title: title } } }
end
2017-08-17 22:00:37 +05:30
context 'when the snippet is spam' do
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(Spam::AkismetService) do |instance|
2019-12-26 22:10:19 +05:30
allow(instance).to receive(:spam?).and_return(true)
end
2017-08-17 22:00:37 +05:30
end
context 'when the snippet is private' do
let(:visibility_level) { Snippet::PRIVATE }
it 'updates the snippet' do
2017-09-10 17:25:29 +05:30
expect { update_snippet(title: 'Foo') }
.to change { snippet.reload.title }.to('Foo')
2017-08-17 22:00:37 +05:30
end
end
context 'when a private snippet is made public' do
let(:visibility_level) { Snippet::PRIVATE }
it 'rejects the snippet' do
2017-09-10 17:25:29 +05:30
expect { update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC) }
.not_to change { snippet.reload.title }
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2017-09-10 17:25:29 +05:30
expect { update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC) }
2019-12-21 20:55:43 +05:30
.to log_spam(title: 'Foo', user: user, noteable_type: 'PersonalSnippet')
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
it 'renders :edit with reCAPTCHA disabled' do
2017-08-17 22:00:37 +05:30
stub_application_setting(recaptcha_enabled: false)
update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC)
expect(response).to render_template(:edit)
end
2020-05-24 23:13:21 +05:30
context 'reCAPTCHA enabled' do
2017-08-17 22:00:37 +05:30
before do
stub_application_setting(recaptcha_enabled: true)
end
2020-05-24 23:13:21 +05:30
it 'renders :verify' do
2017-08-17 22:00:37 +05:30
update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC)
expect(response).to render_template(:verify)
end
2020-05-24 23:13:21 +05:30
it 'renders snippet page when reCAPTCHA verified' do
2017-08-17 22:00:37 +05:30
spammy_title = 'Whatever'
spam_logs = create_list(:spam_log, 2, user: user, title: spammy_title)
snippet = update_snippet({ title: spammy_title, visibility_level: Snippet::PUBLIC },
{ spam_log_id: spam_logs.last.id,
recaptcha_verification: true })
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(snippet_path(snippet))
2017-08-17 22:00:37 +05:30
end
end
end
context 'when the snippet is public' do
2020-04-22 19:07:51 +05:30
it 'rejects the snippet' do
2017-09-10 17:25:29 +05:30
expect { update_snippet(title: 'Foo') }
.not_to change { snippet.reload.title }
2017-08-17 22:00:37 +05:30
end
it 'creates a spam log' do
2019-12-21 20:55:43 +05:30
expect {update_snippet(title: 'Foo') }
.to log_spam(title: 'Foo', user: user, noteable_type: 'PersonalSnippet')
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
it 'renders :edit with reCAPTCHA disabled' do
2017-08-17 22:00:37 +05:30
stub_application_setting(recaptcha_enabled: false)
update_snippet(title: 'Foo')
expect(response).to render_template(:edit)
end
context 'recaptcha enabled' do
before do
stub_application_setting(recaptcha_enabled: true)
end
2020-05-24 23:13:21 +05:30
it 'renders :verify' do
2017-08-17 22:00:37 +05:30
update_snippet(title: 'Foo')
expect(response).to render_template(:verify)
end
2020-05-24 23:13:21 +05:30
it 'renders snippet page when reCAPTCHA verified' do
2017-08-17 22:00:37 +05:30
spammy_title = 'Whatever'
spam_logs = create_list(:spam_log, 2, user: user, title: spammy_title)
snippet = update_snippet({ title: spammy_title },
{ spam_log_id: spam_logs.last.id,
recaptcha_verification: true })
expect(response).to redirect_to(snippet_path(snippet))
end
end
end
end
end
describe 'POST #mark_as_spam' do
let(:snippet) { create(:personal_snippet, :public, author: user) }
before do
2020-03-13 15:44:24 +05:30
allow_next_instance_of(Spam::AkismetService) do |instance|
2019-12-26 22:10:19 +05:30
allow(instance).to receive_messages(submit_spam: true)
end
2017-08-17 22:00:37 +05:30
stub_application_setting(akismet_enabled: true)
end
def mark_as_spam
admin = create(:admin)
create(:user_agent_detail, subject: snippet)
sign_in(admin)
2019-02-15 15:39:39 +05:30
post :mark_as_spam, params: { id: snippet.id }
2017-08-17 22:00:37 +05:30
end
it 'updates the snippet' do
mark_as_spam
expect(snippet.reload).not_to be_submittable_as_spam
end
end
describe "GET #raw" do
2020-04-22 19:07:51 +05:30
let(:inline) { nil }
let(:params) { { id: snippet.to_param, inline: inline } }
subject { get :raw, params: params }
2020-04-08 14:13:33 +05:30
shared_examples '200 status' do
before do
subject
end
it 'responds with status 200' do
expect(assigns(:snippet)).to eq(snippet)
expect(response).to have_gitlab_http_status(:ok)
end
it "sets #{Gitlab::Workhorse::DETECT_HEADER} header" do
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
end
end
shared_examples 'CRLF line ending' do
let(:content) { "first line\r\nsecond line\r\nthird line" }
let(:formatted_content) { content.gsub(/\r\n/, "\n") }
let(:snippet) do
create(:personal_snippet, :public, :repository, author: user, content: content)
end
before do
allow_next_instance_of(Blob) do |instance|
allow(instance).to receive(:data).and_return(content)
end
subject
end
it 'returns LF line endings by default' do
expect(response.body).to eq(formatted_content)
end
context 'when parameter present' do
let(:params) { { id: snippet.to_param, line_ending: :raw } }
it 'does not convert line endings when parameter present' do
expect(response.body).to eq(content)
end
end
end
shared_examples 'successful response' do
it_behaves_like '200 status'
2020-04-22 19:07:51 +05:30
it 'has expected blob headers' do
2020-04-08 14:13:33 +05:30
subject
2020-04-22 19:07:51 +05:30
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
end
it_behaves_like 'content disposition headers'
it 'sets cache_control public header based on snippet visibility' do
subject
expect(response.cache_control[:public]).to eq snippet.public?
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
context 'when snippet repository is empty' do
before do
allow_any_instance_of(Repository).to receive(:empty?).and_return(true)
end
it_behaves_like '200 status'
it_behaves_like 'CRLF line ending'
it 'returns snippet database content' do
subject
expect(response.body).to eq snippet.content
expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
end
it_behaves_like 'content disposition headers'
2020-04-08 14:13:33 +05:30
end
end
2015-12-23 02:04:40 +05:30
context 'when the personal snippet is private' do
2020-04-22 19:07:51 +05:30
let_it_be(:snippet) { create(:personal_snippet, :private, :repository, author: user) }
2015-12-23 02:04:40 +05:30
context 'when signed in' do
before do
sign_in(user)
end
context 'when signed in user is not the author' do
let(:other_author) { create(:author) }
let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_author) }
it 'responds with status 404' do
2019-02-15 15:39:39 +05:30
get :raw, params: { id: other_personal_snippet.to_param }
2015-12-23 02:04:40 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-12-23 02:04:40 +05:30
end
end
context 'when signed in user is the author' do
2020-04-22 19:07:51 +05:30
it_behaves_like 'successful response'
2015-12-23 02:04:40 +05:30
end
end
context 'when not signed in' do
it 'redirects to the sign in page' do
2020-04-22 19:07:51 +05:30
subject
2015-12-23 02:04:40 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'when the personal snippet is internal' do
2020-04-22 19:07:51 +05:30
let_it_be(:snippet) { create(:personal_snippet, :internal, :repository, author: user) }
2015-12-23 02:04:40 +05:30
context 'when signed in' do
before do
sign_in(user)
end
2020-04-22 19:07:51 +05:30
it_behaves_like 'successful response'
2015-12-23 02:04:40 +05:30
end
context 'when not signed in' do
it 'redirects to the sign in page' do
2020-04-22 19:07:51 +05:30
subject
2015-12-23 02:04:40 +05:30
expect(response).to redirect_to(new_user_session_path)
end
end
end
context 'when the personal snippet is public' do
2020-04-22 19:07:51 +05:30
let_it_be(:snippet) { create(:personal_snippet, :public, :repository, author: user) }
2015-12-23 02:04:40 +05:30
context 'when signed in' do
before do
sign_in(user)
end
2020-04-22 19:07:51 +05:30
it_behaves_like 'successful response'
2015-12-23 02:04:40 +05:30
end
context 'when not signed in' do
2017-08-17 22:00:37 +05:30
it 'responds with status 200' do
2020-04-22 19:07:51 +05:30
subject
2015-12-23 02:04:40 +05:30
2020-04-22 19:07:51 +05:30
expect(assigns(:snippet)).to eq(snippet)
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:ok)
2015-12-23 02:04:40 +05:30
end
end
end
context 'when the personal snippet does not exist' do
context 'when signed in' do
before do
sign_in(user)
end
it 'responds with status 404' do
2019-02-15 15:39:39 +05:30
get :raw, params: { id: 'doesntexist' }
2015-12-23 02:04:40 +05:30
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2015-12-23 02:04:40 +05:30
end
end
context 'when not signed in' do
2017-08-17 22:00:37 +05:30
it 'redirects to the sign in path' do
2019-02-15 15:39:39 +05:30
get :raw, params: { id: 'doesntexist' }
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
expect(response).to redirect_to(new_user_session_path)
2015-12-23 02:04:40 +05:30
end
end
end
end
2016-09-29 09:46:39 +05:30
context 'award emoji on snippets' do
let(:personal_snippet) { create(:personal_snippet, :public, author: user) }
let(:another_user) { create(:user) }
before do
sign_in(another_user)
end
describe 'POST #toggle_award_emoji' do
it "toggles the award emoji" do
expect do
2019-02-15 15:39:39 +05:30
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
2016-09-29 09:46:39 +05:30
end.to change { personal_snippet.award_emoji.count }.from(0).to(1)
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-29 09:46:39 +05:30
end
it "removes the already awarded emoji" do
2019-02-15 15:39:39 +05:30
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
2016-09-29 09:46:39 +05:30
expect do
2019-02-15 15:39:39 +05:30
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
2016-09-29 09:46:39 +05:30
end.to change { personal_snippet.award_emoji.count }.from(1).to(0)
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2016-09-29 09:46:39 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST #preview_markdown' do
let(:snippet) { create(:personal_snippet, :public) }
it 'renders json in a correct format' do
sign_in(user)
2019-02-15 15:39:39 +05:30
post :preview_markdown, params: { id: snippet, text: '*Markdown* text' }
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
expect(json_response.keys).to match_array(%w(body references))
2017-08-17 22:00:37 +05:30
end
end
2020-03-13 15:44:24 +05:30
describe 'DELETE #destroy' do
let!(:snippet) { create :personal_snippet, author: user }
context 'when current user has ability to destroy the snippet' do
before do
sign_in(user)
end
it 'removes the snippet' do
delete :destroy, params: { id: snippet.to_param }
expect { snippet.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
context 'when snippet is succesfuly destroyed' do
it 'redirects to the project snippets page' do
delete :destroy, params: { id: snippet.to_param }
expect(response).to redirect_to(dashboard_snippets_path)
end
end
context 'when snippet is not destroyed' do
before do
allow(snippet).to receive(:destroy).and_return(false)
controller.instance_variable_set(:@snippet, snippet)
end
it 'renders the snippet page with errors' do
delete :destroy, params: { id: snippet.to_param }
expect(flash[:alert]).to eq('Failed to remove snippet.')
expect(response).to redirect_to(snippet_path(snippet))
end
end
end
context 'when current_user does not have ability to destroy the snippet' do
let(:another_user) { create(:user) }
before do
sign_in(another_user)
end
it 'responds with status 404' do
delete :destroy, params: { id: snippet.to_param }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
2020-04-22 19:07:51 +05:30
describe 'GET #edit' do
it_behaves_like 'editing snippet checks blob is binary' do
let_it_be(:snippet) { create(:personal_snippet, :public, :repository, author: user) }
subject { get :edit, params: { id: snippet } }
end
end
2015-11-26 14:37:03 +05:30
end