From 1b4c51effe48e35c29da01f74ba9ae3013f2a446 Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Thu, 15 Jan 2015 13:55:49 +0530 Subject: [PATCH 1/2] Fix protip controller specs, add routing specs --- spec/controllers/protips_controller_spec.rb | 65 +++++++++++---------- spec/routing/protips_routing_spec.rb | 11 ++++ 2 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 spec/routing/protips_routing_spec.rb diff --git a/spec/controllers/protips_controller_spec.rb b/spec/controllers/protips_controller_spec.rb index 712fc9f3..24b842e6 100644 --- a/spec/controllers/protips_controller_spec.rb +++ b/spec/controllers/protips_controller_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe ProtipsController, type: :controller, skip: true do +RSpec.describe ProtipsController, type: :controller do let(:current_user) { Fabricate(:user) } before { controller.send :sign_in, current_user } @@ -7,15 +7,11 @@ def valid_attributes { title: 'hello world', body: "somethings that's meaningful and nice", - topics: %w(java javascript), + topic_list: "java, javascript", user_id: current_user.id } end - def valid_session - {} - end - describe 'GET user' do describe 'banned' do it 'should assign user @protips for page, despite not being in search index' do @@ -23,7 +19,7 @@ def valid_session expect(current_user.banned?).to eq(true) Protip.rebuild_index protip = Protip.create! valid_attributes - get :user, { username: current_user.username }, valid_session + get :user, { username: current_user.username } expect(assigns(:protips).first.title).to eq(protip.title) end end @@ -32,28 +28,33 @@ def valid_session it 'should assign user @protips for page' do Protip.rebuild_index protip = Protip.create! valid_attributes - get :user, { username: current_user.username }, valid_session + get :user, { username: current_user.username } expect(assigns(:protips).results.first.title).to eq(protip.title) end - end - end # describe "GET topic" do # it "assigns all protips as @protips" do # Protip.rebuild_index # protip = Protip.create! valid_attributes - # get :topic, {tags: "java"}, valid_session + # get :topic, {tags: "java"} # expect(assigns(:protips).results.first.title).to eq(protip.title) # end # end describe 'GET show using public_id' do - it 'redirects to GET show using slug' do + it 'redirects to GET show if slug is empty' do + protip = Protip.create! valid_attributes + protip.save + get :show, { id: protip.to_param } + expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id) + end + + it 'redirects to GET show if slug is invalid' do protip = Protip.create! valid_attributes protip.save - get :show, { id: protip.to_param }, valid_session + get :show, { id: protip.to_param, slug: "an_invalid_slug" } expect(response).to redirect_to slug_protips_path(protip, protip.friendly_id) end end @@ -62,7 +63,7 @@ def valid_session it 'assigns the requested protip as @protip' do protip = Protip.create! valid_attributes protip.save - get :show, { id: protip.public_id, slug: protip.friendly_id }, valid_session + get :show, { id: protip.public_id, slug: protip.friendly_id } expect(assigns(:protip)).to eq(protip) end end @@ -71,18 +72,18 @@ def valid_session before { allow_any_instance_of(User).to receive(:skills).and_return(['skill']) } # User must have a skill to create protips it 'assigns a new protip as @protip' do - get :new, {}, valid_session + get :new, {} expect(assigns(:protip)).to be_a_new(Protip) end it 'allows viewing the page when you have a skill' do - get :new, {}, valid_session + get :new, {} expect(response).to render_template('new') end it "prevents viewing the page when you don't have a skill" do allow_any_instance_of(User).to receive(:skills).and_return([]) - get :new, {}, valid_session + get :new, {} expect(response).to redirect_to badge_path(username: current_user.username, anchor: 'add-skill') end end @@ -90,7 +91,7 @@ def valid_session describe 'GET edit' do it 'assigns the requested protip as @protip' do protip = Protip.create! valid_attributes - get :edit, { id: protip.to_param }, valid_session + get :edit, { id: protip.to_param } expect(assigns(:protip)).to eq(protip) end end @@ -101,18 +102,18 @@ def valid_session describe 'with valid params' do it 'creates a new Protip' do expect do - post :create, { protip: valid_attributes }, valid_session + post :create, { protip: valid_attributes } end.to change(Protip, :count).by(1) end it 'assigns a newly created protip as @protip' do - post :create, { protip: valid_attributes }, valid_session + post :create, { protip: valid_attributes } expect(assigns(:protip)).to be_a(Protip) expect(assigns(:protip)).to be_persisted end it 'redirects to the created protip' do - post :create, { protip: valid_attributes }, valid_session + post :create, { protip: valid_attributes } expect(response).to redirect_to(Protip.last) end end @@ -121,21 +122,21 @@ def valid_session it 'assigns a newly created but unsaved protip as @protip' do # Trigger the behavior that occurs when invalid params are submitted allow_any_instance_of(Protip).to receive(:save).and_return(false) - post :create, { protip: {} }, valid_session + post :create, { protip: {} } expect(assigns(:protip)).to be_a_new(Protip) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted allow_any_instance_of(Protip).to receive(:save).and_return(false) - post :create, { protip: {} }, valid_session + post :create, { protip: {} } expect(response).to render_template('new') end end it "prevents creating when you don't have a skill" do allow_any_instance_of(User).to receive(:skills).and_return([]) - post :create, { protip: valid_attributes }, valid_session + post :create, { protip: valid_attributes } expect(response).to redirect_to badge_path(username: current_user.username, anchor: 'add-skill') end end @@ -149,18 +150,18 @@ def valid_session # receives the :update_attributes message with whatever params are # submitted in the request. expect_any_instance_of(Protip).to receive(:update_attributes).with('body' => 'params') - put :update, { id: protip.to_param, protip: { 'body' => 'params' } }, valid_session + put :update, { id: protip.to_param, protip: { 'body' => 'params' } } end it 'assigns the requested protip as @protip' do protip = Protip.create! valid_attributes - put :update, { id: protip.to_param, protip: valid_attributes }, valid_session + put :update, { id: protip.to_param, protip: valid_attributes } expect(assigns(:protip)).to eq(protip) end it 'redirects to the protip' do protip = Protip.create! valid_attributes - put :update, { id: protip.to_param, protip: valid_attributes }, valid_session + put :update, { id: protip.to_param, protip: valid_attributes } expect(response).to redirect_to(protip) end end @@ -170,7 +171,7 @@ def valid_session protip = Protip.create! valid_attributes # Trigger the behavior that occurs when invalid params are submitted allow_any_instance_of(Protip).to receive(:save).and_return(false) - put :update, { id: protip.to_param, protip: {} }, valid_session + put :update, { id: protip.to_param, protip: {} } expect(assigns(:protip)).to eq(protip) end @@ -180,7 +181,7 @@ def valid_session # Trigger the behavior that occurs when invalid params are submitted allow_any_instance_of(Protip).to receive(:save).and_return(false) - put :update, { id: protip.to_param, protip: {} }, valid_session + put :update, { id: protip.to_param, protip: {} } expect(response).to render_template('edit') end end @@ -191,20 +192,20 @@ def valid_session attributes = valid_attributes attributes[:user_id] = Fabricate(:user).id protip = Protip.create! attributes - delete :destroy, { id: protip.to_param }, valid_session + delete :destroy, { id: protip.to_param } expect { protip.reload }.not_to raise_error end it 'destroys the requested protip' do protip = Protip.create! valid_attributes expect { - delete :destroy, { id: protip.to_param }, valid_session + delete :destroy, { id: protip.to_param } }.to change(Protip, :count).by(-1) end it 'redirects to the protips list' do protip = Protip.create!(valid_attributes) - delete :destroy, { id: protip.to_param }, valid_session + delete :destroy, { id: protip.to_param } expect(response).to redirect_to(protips_url) end end diff --git a/spec/routing/protips_routing_spec.rb b/spec/routing/protips_routing_spec.rb new file mode 100644 index 00000000..5710d404 --- /dev/null +++ b/spec/routing/protips_routing_spec.rb @@ -0,0 +1,11 @@ +RSpec.describe ProtipsController, type: :routing do + describe 'routing' do + it 'GET p/:id/:slug routes to #show' do + expect(get('/p/1234/abcd')).to route_to(controller: 'protips', action: 'show', id: '1234', slug: 'abcd') + end + + it 'POST p/:id/upvote routes to #upvote' do + expect(post('/p/abcd/upvote')).to route_to(controller: 'protips', action: 'upvote', id: 'abcd') + end + end +end From 0e15f8fecd893ae61bd83017ec5861cb89ccbada Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Thu, 15 Jan 2015 14:18:28 +0530 Subject: [PATCH 2/2] add validation for empty slugs --- app/models/protip.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/protip.rb b/app/models/protip.rb index b63614f3..23a84c69 100644 --- a/app/models/protip.rb +++ b/app/models/protip.rb @@ -97,6 +97,7 @@ class Protip < ActiveRecord::Base validates :body, presence: true validates :kind, presence: true, inclusion: { in: KINDS } validates :topic_list, length: { minimum: 1 } + validates :slug, presence: true after_validation :tag_user before_create :assign_random_id