Skip to content

Commit

Permalink
Force refresh updated_at of FarmEvent,Regimen. Fixes #1283
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Jul 17, 2019
1 parent 01eac40 commit 0284d90
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 68 deletions.
1 change: 1 addition & 0 deletions app/models/application_record.rb
Expand Up @@ -37,6 +37,7 @@ def notable_changes?
def self.auto_sync_debounce
@auto_sync_paused = true
result = yield
result.update_attributes!(updated_at: Time.now)
@auto_sync_paused = false
result.broadcast!
result
Expand Down
53 changes: 28 additions & 25 deletions spec/controllers/api/farm_events/farm_events_update_spec.rb
Expand Up @@ -5,20 +5,20 @@

describe "#update" do
let(:user) { FactoryBot.create(:user) }
let(:fe) { FactoryBot.create(:farm_event, device: user.device) }
let(:fe) { FactoryBot.create(:farm_event, device: user.device) }
it "allows authorized modification" do
sign_in user
id = FactoryBot.create(:farm_event, device: user.device).id
input = { id: id, farm_event: { repeat: 66 } }
patch :update, format: :json, body: input.to_json, params: {id: id}
patch :update, format: :json, body: input.to_json, params: { id: id }
expect(response.status).to eq(200)
end

it "prevents unauthorized modification" do
sign_in user
id = FactoryBot.create(:farm_event).id
id = FactoryBot.create(:farm_event).id
input = { id: id, repeat: 66 }
patch :update, format: :json, body: input.to_json, params: {id: id}
patch :update, format: :json, body: input.to_json, params: { id: id }
expect(response.status).to eq(403)
expect(json[:error]).to include("Not your farm_event")
end
Expand All @@ -27,8 +27,8 @@
sign_in user
id = FactoryBot.create(:farm_event, device: user.device).id
patch :update,
format: :json,
body: { id: id, repeat: 1, time_unit: FarmEvent::NEVER }.to_json,
format: :json,
body: { id: id, repeat: 1, time_unit: FarmEvent::NEVER }.to_json,
params: { id: id }
fe = FarmEvent.find(id)
expect(response.status).to eq(200)
Expand All @@ -41,63 +41,66 @@
id = FactoryBot.create(:farm_event, device: user.device).id
patch :update,
format: :json,
body: { id: id, end_time: "+045633-08-18T13:25:00.000Z" }.to_json,
body: { id: id, end_time: "+045633-08-18T13:25:00.000Z" }.to_json,
params: { id: id }
expect(response.status).to eq(422)
expect(json[:end_time]).to include("too far in the future")
end

def create_fe_with_fragment
fragment = Fragment.from_celery(
owner: fe,
owner: fe,
device: user.device,
kind: "internal_farm_event",
args: {},
body: [
kind: "internal_farm_event",
args: {},
body: [
{
kind: "parameter_application",
args: {
label: "foo",
data_value: { kind: "coordinate", args: { x: 0, y: 0, z: 0 } }
}
}
])
data_value: { kind: "coordinate", args: { x: 0, y: 0, z: 0 } },
},
},
],
)
return fe
end

def update_body(fe, body)
sign_in user
patch :update,
format: :json,
body: { body: body }.to_json,
body: { body: body }.to_json,
params: { id: fe.id }
end

it "ignores fragment when body is `nil`" do
fe = create_fe_with_fragment
fe = create_fe_with_fragment
fragment_b4 = fe.fragment.id
expect(fe.fragment).not_to be(nil)
update_body(fe, nil)
expect(response.status).to eq(200)
expect(fe.reload.fragment).not_to be(nil)
expect(fe.fragment.id).to eq(fragment_b4)
expect(fe.fragment.id).to eq(fragment_b4)
end

it "deletes old fragment when body is `[]`" do
fe = create_fe_with_fragment
expect(fe.fragment).not_to be(nil)
update_body(fe, [])
expect(response.status).to eq(200)
expect(response.status).to eq(200)
expect(fe.reload.fragment).to be(nil)
expect(json.fetch(:body)).to eq([])
expect(json.fetch(:body)).to eq([])
end

it "replaces old fragment when given a new one" do
fe = create_fe_with_fragment
expect(fe.fragment).not_to be(nil)
old_timestamp = fe.updated_at
update_body(fe, nil)
expect(response.status).to eq(200)
expect(fe.reload.fragment).not_to be(nil)
expect(fe.updated_at).to be > old_timestamp
end

it "inserts new fragment when there originally was none" do
Expand All @@ -113,11 +116,11 @@ def update_body(fe, body)
args: {
x: 1,
y: 2,
z: 3
}
}
}
}
z: 3,
},
},
},
},
]
update_body(fe, body)
expect(response.status).to eq(200)
Expand Down
86 changes: 43 additions & 43 deletions spec/controllers/api/regimens/regimens_update_spec.rb
@@ -1,12 +1,11 @@
require "spec_helper"

describe Api::RegimensController do

include Devise::Test::ControllerHelpers

describe "#update" do
let(:user) { FactoryBot.create(:user) }
let(:sequence) { FakeSequence.create( device: user.device) }
let(:sequence) { FakeSequence.create(device: user.device) }

it "updates an old regimen" do
sign_in user
Expand All @@ -17,20 +16,20 @@
"name" => "something new",
"color" => "blue",
"regimen_items" => [
{
"time_offset" => 1555500000,
"sequence_id" => sequence.id
},
{
"time_offset" => 864300000,
"sequence_id" => sequence.id
},
{
"time_offset" => 950700000,
"sequence_id" => sequence.id
}
]
}
{
"time_offset" => 1555500000,
"sequence_id" => sequence.id,
},
{
"time_offset" => 864300000,
"sequence_id" => sequence.id,
},
{
"time_offset" => 950700000,
"sequence_id" => sequence.id,
},
],
}
put :update, body: payload.to_json, params: { id: existing.id }
expect(response.status).to eq(200)
expect(existing.reload.regimen_items.count).to eq(payload["regimen_items"].length)
Expand All @@ -40,7 +39,7 @@

it "changes variable assignments in `body`" do
sequence = FakeSequence.with_parameters
user = FactoryBot.create(:user, device: sequence.device)
user = FactoryBot.create(:user, device: sequence.device)
sign_in user

var_declr = [
Expand All @@ -50,64 +49,65 @@
label: "parent",
data_value: {
kind: "coordinate",
args: { x: 0, y: 0, z: 0 }
}
}
}
args: { x: 0, y: 0, z: 0 },
},
},
},
]
existing = Regimens::Create.run!(device: user.device,
name: "x",
color: "red",
existing = Regimens::Create.run!(device: user.device,
name: "x",
color: "red",
regimen_items: [],
body: var_declr)
body: var_declr)
payload = {
id: existing.id,
name: "something new",
id: existing.id,
name: "something new",
color: "blue",
body: [
body: [
{
kind: "parameter_application",
args: {
label: "parent",
data_value: {
kind: "tool",
args: {
tool_id: FactoryBot.create(:tool, device: sequence.device).id
}
}
}
}
tool_id: FactoryBot.create(:tool, device: sequence.device).id,
},
},
},
},
],
regimen_items: [
{
time_offset: 950700000,
sequence_id: sequence.id
}
]
sequence_id: sequence.id,
},
],
}
old_timestamp = existing.created_at.as_json
put :update,
body: payload.to_json,
format: :json,
params: { id: existing.id }
expect(response.status).to eq(200)
path = [:body, 0, :args, :data_value, :kind]
expect(json.dig(*path)).to eq(payload.dig(*path))
expect(json.fetch(:updated_at)).to_not eq(old_timestamp)
end

it "catches bad regimen_items" do
sign_in user
existing = Regimens::Create.run!(device: user.device, name: "x", color: "red", regimen_items: [])
payload = {
"id" => existing.id,
"name" => "something new",
"color" => "blue",
"regimen_items" => [ { "time_offset" => 950700000, "sequence_id" => 0 } ]
}
"id" => existing.id,
"name" => "something new",
"color" => "blue",
"regimen_items" => [{ "time_offset" => 950700000, "sequence_id" => 0 }],
}
put :update, body: payload.to_json, params: { id: existing.id }, format: :json
expect(response.status).to eq(422)
expect(json[:regimen_items]).to be
expect(json[:regimen_items])
.to include("Failed to instantiate nested RegimenItem.")
expect(json[:regimen_items]).to include("Failed to instantiate nested RegimenItem.")
end
end
end

0 comments on commit 0284d90

Please sign in to comment.