2015-10-29 16:42:29 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe SnippetsController do
|
2016-06-03 05:44:04 -04:00
|
|
|
let(:user) { create(:user) }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
describe 'GET #index' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
context 'when username parameter is present' do
|
|
|
|
it 'renders snippets of a user when username is present' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :index, params: { username: user.username }
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-11-28 07:03:31 -05:00
|
|
|
describe 'GET #new' do
|
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with status 200' do
|
|
|
|
get :new
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2016-11-28 07:03:31 -05:00
|
|
|
end
|
|
|
|
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-06-03 05:44:04 -04:00
|
|
|
describe 'GET #show' do
|
2015-10-29 16:42:29 -04:00
|
|
|
context 'when the personal snippet is private' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :private, author: user) }
|
|
|
|
|
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: other_personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when signed in user is the author' do
|
|
|
|
it 'renders the snippet' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
it 'responds with status 404 when embeddable content is requested' do
|
2019-01-02 15:47:45 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }, format: :js
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the personal snippet is internal' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :internal, author: user) }
|
|
|
|
|
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders the snippet' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
it 'responds with status 404 when embeddable content is requested' do
|
2019-01-02 15:47:45 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }, format: :js
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
end
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the personal snippet is public' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
|
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders the snippet' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
it 'responds with status 200 when embeddable content is requested' do
|
2019-01-02 15:47:45 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }, format: :js
|
2018-12-11 01:32:25 -05:00
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
end
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'renders the snippet' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: personal_snippet.to_param }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
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
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: 'doesntexist' }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'responds with status 404' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { id: 'doesntexist' }
|
2015-10-29 16:42:29 -04:00
|
|
|
|
2017-04-25 10:41:26 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-02-01 13:15:59 -05:00
|
|
|
describe 'POST #create' do
|
2017-02-14 14:07:11 -05:00
|
|
|
def create_snippet(snippet_params = {}, additional_params = {})
|
2017-02-01 13:15:59 -05:00
|
|
|
sign_in(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: {
|
2017-05-03 11:26:49 -04:00
|
|
|
personal_snippet: { title: 'Title', content: 'Content', description: 'Description' }.merge(snippet_params)
|
2017-02-14 14:07:11 -05:00
|
|
|
}.merge(additional_params)
|
|
|
|
|
|
|
|
Snippet.last
|
2017-02-01 13:15:59 -05:00
|
|
|
end
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
context 'when the snippet description contains a file' do
|
2017-08-07 13:43:11 -04:00
|
|
|
let(:picture_file) { '/-/system/temp/secret56/picture.jpg' }
|
|
|
|
let(:text_file) { '/-/system/temp/secret78/text.txt' }
|
2017-05-03 11:26:49 -04:00
|
|
|
let(:description) do
|
|
|
|
"Description with picture: ![picture](/uploads#{picture_file}) and "\
|
|
|
|
"text: [text.txt](/uploads#{text_file})"
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(FileUtils).to receive(:mkdir_p)
|
|
|
|
allow(FileUtils).to receive(:move)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { create_snippet({ description: description }, { files: [picture_file, text_file] }) }
|
|
|
|
|
|
|
|
it 'creates the snippet' do
|
|
|
|
expect { subject }.to change { Snippet.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores the snippet description correctly' do
|
|
|
|
snippet = subject
|
|
|
|
|
|
|
|
expected_description = "Description with picture: "\
|
2017-08-07 13:43:11 -04:00
|
|
|
"![picture](/uploads/-/system/personal_snippet/#{snippet.id}/secret56/picture.jpg) and "\
|
|
|
|
"text: [text.txt](/uploads/-/system/personal_snippet/#{snippet.id}/secret78/text.txt)"
|
2017-05-03 11:26:49 -04:00
|
|
|
|
|
|
|
expect(snippet.description).to eq(expected_description)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-01 13:15:59 -05:00
|
|
|
context 'when the snippet is spam' do
|
|
|
|
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_level: Snippet::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_level: Snippet::PUBLIC) }
|
|
|
|
.not_to change { Snippet.count }
|
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_level: Snippet::PUBLIC) }
|
|
|
|
.to change { SpamLog.count }.by(1)
|
2017-02-01 13:15:59 -05:00
|
|
|
end
|
2017-02-14 14:07:11 -05:00
|
|
|
|
|
|
|
it 'renders :new with recaptcha disabled' do
|
|
|
|
stub_application_setting(recaptcha_enabled: false)
|
|
|
|
|
|
|
|
create_snippet(visibility_level: Snippet::PUBLIC)
|
|
|
|
|
|
|
|
expect(response).to render_template(:new)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'recaptcha enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders :verify with recaptcha enabled' do
|
|
|
|
create_snippet(visibility_level: Snippet::PUBLIC)
|
|
|
|
|
|
|
|
expect(response).to render_template(:verify)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders snippet page when recaptcha verified' do
|
|
|
|
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
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create :project }
|
2017-02-14 14:07:11 -05:00
|
|
|
let(:snippet) { create :personal_snippet, author: user, project: project, visibility_level: visibility_level }
|
|
|
|
|
|
|
|
def update_snippet(snippet_params = {}, additional_params = {})
|
|
|
|
sign_in(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
put :update, params: {
|
2017-02-14 14:07:11 -05:00
|
|
|
id: snippet.id,
|
|
|
|
personal_snippet: { title: 'Title', content: 'Content' }.merge(snippet_params)
|
|
|
|
}.merge(additional_params)
|
|
|
|
|
|
|
|
snippet.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the snippet is spam' do
|
|
|
|
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 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_level: Snippet::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_level: Snippet::PUBLIC) }
|
|
|
|
.to change { SpamLog.count }.by(1)
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders :edit with recaptcha disabled' do
|
|
|
|
stub_application_setting(recaptcha_enabled: false)
|
|
|
|
|
|
|
|
update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC)
|
|
|
|
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'recaptcha enabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(recaptcha_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders :verify with recaptcha enabled' do
|
|
|
|
update_snippet(title: 'Foo', visibility_level: Snippet::PUBLIC)
|
|
|
|
|
|
|
|
expect(response).to render_template(:verify)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders snippet page when recaptcha verified' do
|
|
|
|
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-06-29 13:06:35 -04:00
|
|
|
expect(response).to redirect_to(snippet_path(snippet))
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
|
it 'renders :edit with recaptcha disabled' do
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'renders :verify with recaptcha enabled' do
|
|
|
|
update_snippet(title: 'Foo')
|
|
|
|
|
|
|
|
expect(response).to render_template(:verify)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders snippet page when recaptcha verified' do
|
|
|
|
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
|
2017-02-01 13:15:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #mark_as_spam' do
|
|
|
|
let(:snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(AkismetService).to receive_messages(submit_spam: true)
|
|
|
|
stub_application_setting(akismet_enabled: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_spam
|
|
|
|
admin = create(:admin)
|
|
|
|
create(:user_agent_detail, subject: snippet)
|
|
|
|
sign_in(admin)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :mark_as_spam, params: { id: snippet.id }
|
2017-02-01 13:15:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the snippet' do
|
|
|
|
mark_as_spam
|
|
|
|
|
|
|
|
expect(snippet.reload).not_to be_submittable_as_spam
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
describe "GET #raw" do
|
|
|
|
context 'when the personal snippet is private' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :private, author: user) }
|
2016-10-13 09:08:15 -04:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
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) }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'responds with status 404' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: other_personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2016-10-13 09:08:15 -04:00
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when signed in user is the author' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'responds with status 200' do
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'has expected headers' do
|
|
|
|
expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(response.header['Content-Disposition']).to match(/inline/)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
2018-12-06 16:22:39 -05:00
|
|
|
|
2018-12-19 06:51:07 -05:00
|
|
|
it "sets #{Gitlab::Workhorse::DETECT_HEADER} header" do
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
|
|
|
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2018-12-06 16:22:39 -05:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when the personal snippet is internal' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :internal, author: user) }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'responds with status 200' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when the personal snippet is public' do
|
|
|
|
let(:personal_snippet) { create(:personal_snippet, :public, author: user) }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'responds with status 200' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'CRLF line ending' do
|
|
|
|
let(:personal_snippet) do
|
|
|
|
create(:personal_snippet, :public, author: user, content: "first line\r\nsecond line\r\nthird line")
|
|
|
|
end
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'returns LF line endings by default' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(response.body).to eq("first line\nsecond line\nthird line")
|
|
|
|
end
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'does not convert line endings when parameter present' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param, line_ending: :raw }
|
2017-02-06 10:04:00 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(response.body).to eq("first line\r\nsecond line\r\nthird line")
|
2017-02-06 10:04:00 -05:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'responds with status 200' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when the personal snippet does not exist' do
|
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
it 'responds with status 404' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: 'doesntexist' }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
2017-04-30 13:15:20 -04:00
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-30 13:15:20 -04:00
|
|
|
context 'when not signed in' do
|
2017-04-25 10:41:26 -04:00
|
|
|
it 'redirects to the sign in path' do
|
2018-12-17 17:52:17 -05:00
|
|
|
get :raw, params: { id: 'doesntexist' }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2017-04-25 10:41:26 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-03 05:44:04 -04:00
|
|
|
|
|
|
|
context 'award emoji on snippets' do
|
2016-09-07 08:48:43 -04:00
|
|
|
let(:personal_snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
let(:another_user) { create(:user) }
|
2016-06-03 05:44:04 -04:00
|
|
|
|
|
|
|
before do
|
2016-09-07 08:48:43 -04:00
|
|
|
sign_in(another_user)
|
2016-06-03 05:44:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #toggle_award_emoji' do
|
|
|
|
it "toggles the award emoji" do
|
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
|
2016-09-07 08:48:43 -04:00
|
|
|
end.to change { personal_snippet.award_emoji.count }.from(0).to(1)
|
2016-06-03 05:44:04 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the already awarded emoji" do
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
|
2016-09-07 08:48:43 -04:00
|
|
|
|
2016-06-03 05:44:04 -04:00
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post(:toggle_award_emoji, params: { id: personal_snippet.to_param, name: "thumbsup" })
|
2016-09-07 08:48:43 -04:00
|
|
|
end.to change { personal_snippet.award_emoji.count }.from(1).to(0)
|
2016-06-03 05:44:04 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-20 04:56:41 -04:00
|
|
|
|
|
|
|
describe 'POST #preview_markdown' do
|
|
|
|
let(:snippet) { create(:personal_snippet, :public) }
|
|
|
|
|
|
|
|
it 'renders json in a correct format' do
|
|
|
|
sign_in(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :preview_markdown, params: { id: snippet, text: '*Markdown* text' }
|
2017-04-20 04:56:41 -04:00
|
|
|
|
|
|
|
expect(JSON.parse(response.body).keys).to match_array(%w(body references))
|
|
|
|
end
|
|
|
|
end
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|