From 00511c5b64dd4cb14c191101e6d9036a2f159240 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sun, 27 Apr 2014 14:39:11 -0400 Subject: [PATCH] Finish Api/TimeEntries spec, fix bike_id handling --- .../api/v1/time_entries_controller.rb | 5 +- .../api/time_entries_controller_spec.rb | 51 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/time_entries_controller.rb b/app/controllers/api/v1/time_entries_controller.rb index dc07b03..2120c9d 100644 --- a/app/controllers/api/v1/time_entries_controller.rb +++ b/app/controllers/api/v1/time_entries_controller.rb @@ -5,9 +5,10 @@ class Api::V1::TimeEntriesController < Api::V1::BaseController if params[:time_entries] && time_entry = params[:time_entries].first time_entry.merge!({ loggable_id: current_user.id, logger_id: current_user.id }) + bike_id = time_entry[:bike_id].to_i - if time_entry[:bike_id] >= 0 - time_entry.copy_to_bike_history(time_entry[:bike_id]) + if bike_id > 0 + time_entry.copy_to_bike_history(bike_id) end @time_entry = TimeEntry.new(time_entry.except(:bike_id)) diff --git a/spec/controllers/api/time_entries_controller_spec.rb b/spec/controllers/api/time_entries_controller_spec.rb index 47fa8c0..7afddd0 100644 --- a/spec/controllers/api/time_entries_controller_spec.rb +++ b/spec/controllers/api/time_entries_controller_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe Api::V1::TimeEntriesController do + render_views describe "#create" do @@ -24,6 +25,56 @@ describe Api::V1::TimeEntriesController do expect(json["errors"].first).to eql Api::V1::TimeEntriesController::EXPECTED_TIME_ENTRY end end + + context "with valid time entry in json data" do + + before(:each) do + time_data = { time_entries: [{ + start_date: Time.zone.now, + end_date: Time.zone.now + 60, + log_action_id: 1, + bike_id: -1, + description: "My description"}] + } + + #this is necessary because render_views does not work with sign_in devise helper + @submit_json = api_submit_json(user, time_data) + #not sure why format: :json not working + request.accept = 'application/json' + end + + it "returns 200" do + post :create, @submit_json + expect(@response.code.to_i).to eql 200 + end + + it "returns the created time entry json" do + post :create, @submit_json + json = JSON.parse(@response.body) + expect(json).to have_key("time_entries") + expect(json.to_s).to include(@submit_json[:time_entries].first[:description]) + end + end + + context "with invalid time entry in json data" do + before(:each) do + @submit_json = { time_entries: [{ + description: "My description", + }]} + end + + it "returns 422" do + post :create, @submit_json + expect(@response.code.to_i).to eql 422 + end + + it "returns the fields with errors" do + post :create, @submit_json + json = JSON.parse(@response.body) + expect(json).to have_key("errors") + expect(json.to_s).to include("can't be blank") + end + end end end end