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
|
|
|
|
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
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
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
|
|
|
|
get :show, id: other_personal_snippet.to_param
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_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
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
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
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
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
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not signed in' do
|
|
|
|
it 'renders the snippet' do
|
|
|
|
get :show, id: personal_snippet.to_param
|
|
|
|
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_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
|
|
|
|
get :show, id: 'doesntexist'
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_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
|
|
|
|
get :show, id: 'doesntexist'
|
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
%w(raw download).each do |action|
|
|
|
|
describe "GET #{action}" do
|
|
|
|
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
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -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
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
it 'responds with status 404' do
|
|
|
|
get action, id: other_personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when signed in user is the author' do
|
|
|
|
before { get action, id: personal_snippet.to_param }
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
it 'responds with status 200' do
|
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -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
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
if action == :download
|
|
|
|
expect(response.header['Content-Disposition']).to match(/attachment/)
|
|
|
|
elsif action == :raw
|
|
|
|
expect(response.header['Content-Disposition']).to match(/inline/)
|
|
|
|
end
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
|
|
|
get action, id: personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -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
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
it 'responds with status 200' do
|
|
|
|
get action, id: personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'redirects to the sign in page' do
|
|
|
|
get action, id: personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_path)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -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
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when signed in' do
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
it 'responds with status 200' do
|
|
|
|
get action, id: personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'responds with status 200' do
|
|
|
|
get action, id: personal_snippet.to_param
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(assigns(:snippet)).to eq(personal_snippet)
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -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
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
it 'responds with status 404' do
|
|
|
|
get action, id: 'doesntexist'
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
2015-11-25 14:24:07 -05:00
|
|
|
end
|
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
context 'when not signed in' do
|
|
|
|
it 'responds with status 404' do
|
|
|
|
get action, id: 'doesntexist'
|
2015-11-25 14:24:07 -05:00
|
|
|
|
2016-10-13 09:08:15 -04:00
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
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
|
|
|
|
post(:toggle_award_emoji, 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
|
|
|
|
post(:toggle_award_emoji, 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
|
|
|
|
post(:toggle_award_emoji, 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
|
2015-10-29 16:42:29 -04:00
|
|
|
end
|