Browse Source

Finish Api/TimeEntries spec, fix bike_id handling

denney-fix-saving-dates
Jason Denney 11 years ago
parent
commit
00511c5b64
  1. 5
      app/controllers/api/v1/time_entries_controller.rb
  2. 51
      spec/controllers/api/time_entries_controller_spec.rb

5
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))

51
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

Loading…
Cancel
Save