Skip to content

Commit

Permalink
Do not use deprecated request helpers in controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Aug 14, 2017
1 parent fce9cfb commit eec30eb
Show file tree
Hide file tree
Showing 22 changed files with 310 additions and 246 deletions.
26 changes: 14 additions & 12 deletions spec/controllers/alchemy/admin/attachments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Alchemy
describe Admin::AttachmentsController do
routes { Alchemy::Engine.routes }

let(:attachment) { build_stubbed(:alchemy_attachment) }

let(:file) do
Expand All @@ -18,13 +20,13 @@ module Alchemy
describe "#index" do
it "should always paginate the records" do
expect_any_instance_of(ActiveRecord::Relation).to receive(:page).and_call_original
alchemy_get :index
get :index
end

context "when params[:tagged_with] is set" do
it "should filter the records by tags" do
expect(Attachment).to receive(:tagged_with).and_return(Attachment.all)
alchemy_get :index, tagged_with: "pdf"
get :index, params: {tagged_with: "pdf"}
end
end

Expand All @@ -34,15 +36,15 @@ module Alchemy
context "is set" do
it "it renders the archive_overlay partial" do
expect(Content).to receive(:find_by).and_return(content)
alchemy_get :index, {content_id: content.id}
get :index, params: {content_id: content.id}
expect(response).to render_template(partial: '_archive_overlay')
expect(assigns(:content)).to eq(content)
end
end

context "is not set" do
it "should render the default index view" do
alchemy_get :index
get :index
expect(response).to render_template(:index)
end
end
Expand All @@ -58,7 +60,7 @@ module Alchemy

context 'with params[:file_type]' do
it 'loads only attachments with matching content type' do
alchemy_get :index, file_type: 'image/jpeg'
get :index, params: {file_type: 'image/jpeg'}
expect(assigns(:attachments).to_a).to eq([jpg])
expect(assigns(:attachments).to_a).to_not eq([png])
end
Expand All @@ -72,13 +74,13 @@ module Alchemy
end

it "renders the show template" do
alchemy_get :show, id: attachment.id
get :show, params: {id: attachment.id}
expect(response).to render_template(:show)
end
end

describe '#create' do
subject { alchemy_post :create, params }
subject { post :create, params: params }

context 'with passing validations' do
let(:params) { {attachment: {file: file}} }
Expand Down Expand Up @@ -110,7 +112,7 @@ module Alchemy
end

subject do
alchemy_put :update, params
put :update, params: params
end

let!(:attachment) { create(:alchemy_attachment) }
Expand Down Expand Up @@ -161,7 +163,7 @@ module Alchemy
end

subject do
alchemy_put :update, {
put :update, params: {
id: attachment.id, attachment: {name: ''}
}.merge(search_params)
end
Expand All @@ -188,7 +190,7 @@ module Alchemy

it "destroys the attachment and sets a success message" do
expect(attachment).to receive(:destroy)
alchemy_xhr :delete, :destroy, id: 1
delete :destroy, params: {id: 1}, xhr: true
expect(assigns(:attachment)).to eq(attachment)
expect(assigns(:url)).not_to be_blank
expect(flash[:notice]).not_to be_blank
Expand All @@ -206,7 +208,7 @@ module Alchemy

it "passes them along" do
expect(attachment).to receive(:destroy) { true }
alchemy_xhr :delete, :destroy, {id: 1}.merge(search_params)
delete :destroy, params: {id: 1}.merge(search_params), xhr: true
expect(assigns(:url)).to eq admin_attachments_url(search_params.merge(host: 'test.host'))
end
end
Expand All @@ -218,7 +220,7 @@ module Alchemy
end

it "sends the file as download" do
alchemy_get :download, id: attachment.id
get :download, params: {id: attachment.id}
expect(response.headers['Content-Disposition']).to match(/attachment/)
end
end
Expand Down
16 changes: 9 additions & 7 deletions spec/controllers/alchemy/admin/clipboard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Alchemy
describe Admin::ClipboardController do
routes { Alchemy::Engine.routes }

let(:public_page) { build_stubbed(:alchemy_page, :public) }
let(:element) { build_stubbed(:alchemy_element, page: public_page) }
let(:another_element) { build_stubbed(:alchemy_element, page: public_page) }
Expand All @@ -14,15 +16,15 @@ module Alchemy
describe '#index' do
context 'with `remarkable_type` being an allowed type' do
it 'is successful' do
alchemy_get :index, {remarkable_type: 'elements'}
get :index, params: {remarkable_type: 'elements'}
expect(response).to be_success
end
end

context 'with `remarkable_type` not an allowed type' do
it 'raises 400 Bad Request' do
expect {
alchemy_get :index, {remarkable_type: 'evil'}
get :index, params: {remarkable_type: 'evil'}
}.to raise_error(ActionController::BadRequest)
end
end
Expand All @@ -35,13 +37,13 @@ module Alchemy

describe "#insert" do
it "should hold element ids" do
alchemy_xhr :post, :insert, {remarkable_type: 'elements', remarkable_id: element.id}
post :insert, params: {remarkable_type: 'elements', remarkable_id: element.id}, xhr: true
expect(session[:alchemy_clipboard]['elements']).to eq([{'id' => element.id.to_s, 'action' => 'copy'}])
end

it "should not have the same element twice" do
session[:alchemy_clipboard]['elements'] = [{'id' => element.id.to_s, 'action' => 'copy'}]
alchemy_xhr :post, :insert, {remarkable_type: 'elements', remarkable_id: element.id}
post :insert, params: {remarkable_type: 'elements', remarkable_id: element.id}, xhr: true
expect(session[:alchemy_clipboard]['elements'].collect { |e| e['id'] }).not_to eq([element.id, element.id])
end
end
Expand All @@ -50,7 +52,7 @@ module Alchemy
it "should remove element ids from clipboard" do
session[:alchemy_clipboard]['elements'] = [{'id' => element.id.to_s, 'action' => 'copy'}]
session[:alchemy_clipboard]['elements'] << {'id' => another_element.id.to_s, 'action' => 'copy'}
alchemy_xhr :delete, :remove, {remarkable_type: 'elements', remarkable_id: another_element.id}
delete :remove, params: {remarkable_type: 'elements', remarkable_id: another_element.id}, xhr: true
expect(session[:alchemy_clipboard]['elements']).to eq([{'id' => element.id.to_s, 'action' => 'copy'}])
end
end
Expand All @@ -60,15 +62,15 @@ module Alchemy
context "with elements as remarkable_type" do
it "should clear the elements clipboard" do
session[:alchemy_clipboard]['elements'] = [{'id' => element.id.to_s}]
alchemy_xhr :delete, :clear, {remarkable_type: 'elements'}
delete :clear, params: {remarkable_type: 'elements'}, xhr: true
expect(session[:alchemy_clipboard]['elements']).to be_empty
end
end

context "with pages as remarkable_type" do
it "should clear the pages clipboard" do
session[:alchemy_clipboard]['pages'] = [{'id' => public_page.id.to_s}]
alchemy_xhr :delete, :clear, {remarkable_type: 'pages'}
delete :clear, params: {remarkable_type: 'pages'}, xhr: true
expect(session[:alchemy_clipboard]['pages']).to be_empty
end
end
Expand Down
16 changes: 9 additions & 7 deletions spec/controllers/alchemy/admin/contents_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Alchemy
describe Admin::ContentsController do
routes { Alchemy::Engine.routes }

before do
authorize_user(:as_admin)
end
Expand All @@ -12,17 +14,17 @@ module Alchemy

it "creates a content from name" do
expect {
alchemy_xhr :post, :create, {content: {element_id: element.id, name: 'headline'}}
post :create, params: {content: {element_id: element.id, name: 'headline'}}, xhr: true
}.to change { Alchemy::Content.count }.by(1)
end

it "creates a content from essence_type" do
expect {
alchemy_xhr :post, :create, {
post :create, params: {
content: {
element_id: element.id, essence_type: 'EssencePicture'
}
}
}, xhr: true
}.to change { Alchemy::Content.count }.by(1)
end
end
Expand All @@ -43,13 +45,13 @@ module Alchemy
end

it "adds it into the gallery editor" do
alchemy_xhr :post, :create, attributes
post :create, params: attributes, xhr: true
expect(assigns(:content_dom_id)).to eq("#add_picture_#{element.id}")
end

context 'with picture_id given' do
it "assigns the picture to the essence" do
alchemy_xhr :post, :create, attributes.merge(picture_id: '1')
post :create, params: attributes.merge(picture_id: '1'), xhr: true
expect(Alchemy::Content.last.essence.picture_id).to eq(1)
end
end
Expand All @@ -65,7 +67,7 @@ module Alchemy

it "should update a content via ajax" do
expect {
alchemy_xhr :post, :update, {id: content.id, content: {ingredient: 'Peters Petshop'}}
post :update, params: {id: content.id, content: {ingredient: 'Peters Petshop'}}, xhr: true
}.to change { content.ingredient }.to 'Peters Petshop'
end
end
Expand All @@ -79,7 +81,7 @@ module Alchemy
let(:content_ids) { element.contents.pluck(:id).shuffle }

it "should reorder the contents" do
alchemy_xhr :post, :order, {content_ids: content_ids}
post :order, params: {content_ids: content_ids}, xhr: true

expect(response.status).to eq(200)
expect(element.contents(true).pluck(:id)).to eq(content_ids)
Expand Down
26 changes: 14 additions & 12 deletions spec/controllers/alchemy/admin/dashboard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Alchemy
describe Admin::DashboardController do
routes { Alchemy::Engine.routes }

let(:user) { build_stubbed(:alchemy_dummy_user, :as_admin) }

before { authorize_user(user) }
Expand All @@ -18,12 +20,12 @@ module Alchemy
end

it "assigns @last_edited_pages" do
alchemy_get :index
get :index
expect(assigns(:last_edited_pages)).to eq([])
end

it "assigns @all_locked_pages" do
alchemy_get :index
get :index
expect(assigns(:all_locked_pages)).to eq([])
end

Expand All @@ -36,14 +38,14 @@ module Alchemy
end

it "assigns @online_users" do
alchemy_get :index
get :index
expect(assigns(:online_users)).to eq([another_user])
end
end

context 'without other users online' do
it "does not assign @online_users" do
alchemy_get :index
get :index
expect(assigns(:online_users)).to eq([])
end
end
Expand All @@ -56,20 +58,20 @@ module Alchemy
end

it "assigns @first_time" do
alchemy_get :index
get :index
expect(assigns(:first_time)).to eq(false)
end
end

it "assigns @sites" do
alchemy_get :index
get :index
expect(assigns(:sites)).to eq(Site.all)
end
end

describe '#info' do
it "assigns @alchemy_version with the current Alchemy version" do
alchemy_get :info
get :info
expect(assigns(:alchemy_version)).to eq(Alchemy.version)
end
end
Expand All @@ -82,7 +84,7 @@ module Alchemy
}

it "should render 'false'" do
alchemy_get :update_check
get :update_check
expect(response.body).to eq('false')
end
end
Expand All @@ -94,7 +96,7 @@ module Alchemy
}

it "should render 'true'" do
alchemy_get :update_check
get :update_check
expect(response.body).to eq('true')
end
end
Expand All @@ -108,7 +110,7 @@ module Alchemy
}

it "should have response code of 200" do
alchemy_get :update_check
get :update_check
expect(response.code).to eq('200')
end
end
Expand All @@ -122,7 +124,7 @@ module Alchemy
}

it "should have response code of 200" do
alchemy_get :update_check
get :update_check
expect(response.code).to eq('200')
end
end
Expand All @@ -135,7 +137,7 @@ module Alchemy
}

it "should have status code 503" do
alchemy_get :update_check
get :update_check
expect(response.code).to eq('503')
end
end
Expand Down
Loading

0 comments on commit eec30eb

Please sign in to comment.