Skip to content

Commit

Permalink
Finish Api/TimeEntries spec, fix bike_id handling
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemunkay committed Apr 27, 2014
1 parent 1191014 commit 00511c5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/controllers/api/v1/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ def create
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))
Expand Down
51 changes: 51 additions & 0 deletions spec/controllers/api/time_entries_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'

describe Api::V1::TimeEntriesController do
render_views

describe "#create" do

Expand All @@ -24,6 +25,56 @@
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

0 comments on commit 00511c5

Please sign in to comment.