From 073a83daea5cb9ccd0d2ebbaf28a4000389884f7 Mon Sep 17 00:00:00 2001 From: Renan Fichberg Date: Tue, 14 Jan 2014 17:52:05 -0200 Subject: [PATCH] Reading update --- app/controllers/readings_controller.rb | 14 ++++++++++++++ config/routes.rb | 2 +- features/reading/edit.feature | 36 ++++++++++++++++++------------------ features/step_definitions/reading_steps.rb | 4 ++++ spec/controllers/readings_controller_spec.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ spec/routing/readings_routing_spec.rb | 2 +- 6 files changed, 97 insertions(+), 20 deletions(-) diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index b343905..cd50c18 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -23,6 +23,20 @@ class ReadingsController < ApplicationController @reading_group_id = params[:reading_group_id] end + # PUT /reading_groups/1/readings/1 + # PUT /reading_groups/1/readings/1.json + def update + @reading.group_id = params[:reading_group_id].to_i + respond_to do |format| + if @reading.update(reading_params) + format.html { redirect_to(reading_group_reading_path(params[:reading_group_id].to_i, @reading.id), notice: 'Reading was successfully updated.') } + format.json { head :no_content } + else + failed_action(format, 'edit') + end + end + end + private # Never trust parameters from the scary internet, only allow the white list through. diff --git a/config/routes.rb b/config/routes.rb index baed952..78cf35c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,7 +13,7 @@ Mezuro::Application.routes.draw do end resources :reading_groups do - resources :readings, except: [:index, :show, :update] + resources :readings, except: [:index, :update] put '/readings/:id' => 'readings#update', as: :reading_update end diff --git a/features/reading/edit.feature b/features/reading/edit.feature index 7cbc13d..71edc49 100644 --- a/features/reading/edit.feature +++ b/features/reading/edit.feature @@ -3,7 +3,7 @@ Feature: Reading Edit As a regular user I should be able to edit my readings -@kalibro_restart @wip +@kalibro_restart @wip Scenario: editing a reading successfully Given I am a regular user And I am signed in @@ -19,29 +19,29 @@ Feature: Reading Edit And I should see "10.5" And I should see "33dd33" - @kalibro_restart @wip + @kalibro_restart Scenario: editing a reading with blank fields Given I am a regular user And I am signed in - And I own a sample project - And I have a sample configuration with native metrics - And I have a sample repository within the sample project named "QtCalculator" - And I am at repository edit page - When I fill the Name field with " " - And I fill the Address field with " " + And I own a sample reading group + And I have a sample reading within the sample reading group + And I am at the Edit Reading page + When I fill the Label field with " " + And I fill the Grade field with " " + And I fill the Color field with " " And I press the Save button - Then I should see "Name can't be blank" - And I should see "Address can't be blank" + Then I should see "Label can't be blank" + And I should see "Grade can't be blank" + And I should see "Color can't be blank" - @kalibro_restart @wip + @kalibro_restart Scenario: editing a reading with already taken name Given I am a regular user And I am signed in - And I own a sample project - And I have a sample configuration with native metrics - And I have a sample repository within the sample project named "MedSquare" - And I have a sample repository within the sample project named "QtCalculator" - And I am at repository edit page - When I fill the Name field with "MedSquare" + And I own a sample reading group + And I have a sample reading within the sample reading group labeled "Average" + And I have a sample reading within the sample reading group labeled "Good" + And I am at the Edit Reading page + When I fill the Label field with "Average" And I press the Save button - Then I should see "There's already" \ No newline at end of file + Then I should see "There's already" diff --git a/features/step_definitions/reading_steps.rb b/features/step_definitions/reading_steps.rb index bec87f9..eda4a1f 100644 --- a/features/step_definitions/reading_steps.rb +++ b/features/step_definitions/reading_steps.rb @@ -9,3 +9,7 @@ end Given(/^I am at the Edit Reading page$/) do visit edit_reading_group_reading_url(@reading_group.id, @reading.id) end + +Given(/^I have a sample reading within the sample reading group labeled "(.*?)"$/) do |label| + @reading = FactoryGirl.create(:reading, {label: label, group_id: @reading_group.id, id: nil}) +end diff --git a/spec/controllers/readings_controller_spec.rb b/spec/controllers/readings_controller_spec.rb index ed3e284..63c30b6 100644 --- a/spec/controllers/readings_controller_spec.rb +++ b/spec/controllers/readings_controller_spec.rb @@ -102,4 +102,63 @@ describe ReadingsController do it { should redirect_to new_user_session_path } end end + + describe 'update' do + let(:reading) { FactoryGirl.build(:reading) } + let(:reading_params) { Hash[FactoryGirl.attributes_for(:reading).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers + + context 'when the user is logged in' do + before do + sign_in FactoryGirl.create(:user) + end + + context 'when user owns the reading' do + before :each do + subject.expects(:reading_owner?).returns true + end + + context 'with valid fields' do + before :each do + Reading.expects(:find).at_least_once.with(reading.id).returns(reading) + Reading.any_instance.expects(:update).with(reading_params).returns(true) + + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params + end + + it { should redirect_to(reading_group_reading_path(reading_group.id, reading.id)) } + it { should respond_with(:redirect) } + end + + context 'with an invalid field' do + before :each do + Reading.expects(:find).at_least_once.with(reading.id).returns(reading) + Reading.any_instance.expects(:update).with(reading_params).returns(false) + + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params + end + + it { should render_template(:edit) } + end + end + + context 'when the user does not own the reading' do + before :each do + Reading.expects(:find).at_least_once.with(reading.id).returns(reading) + + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params + end + + it { should redirect_to reading_group_path(reading_group.id) } + end + end + + context 'with no user logged in' do + before :each do + post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params + end + + it { should redirect_to new_user_session_path } + end + end + end \ No newline at end of file diff --git a/spec/routing/readings_routing_spec.rb b/spec/routing/readings_routing_spec.rb index ce643ac..066ccbc 100644 --- a/spec/routing/readings_routing_spec.rb +++ b/spec/routing/readings_routing_spec.rb @@ -8,7 +8,7 @@ describe ReadingsController do to(controller: :readings, action: :new, reading_group_id: 1) } it { should route(:get, '/reading_groups/1/readings/1/edit'). to(controller: :readings, action: :edit, reading_group_id: 1, id: 1) } - it { should_not route(:get, '/reading_groups/1/readings/1'). + it { should route(:get, '/reading_groups/1/readings/1'). to(controller: :readings, action: :show, reading_group_id: 1, id: 1) } it { should route(:delete, '/reading_groups/1/readings/1'). to(controller: :readings, action: :destroy, reading_group_id: 1, id: 1) } -- libgit2 0.21.2