diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 423bc8b505..0dc66dc7e1 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -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 diff --git a/spec/controllers/api/farm_events/farm_events_update_spec.rb b/spec/controllers/api/farm_events/farm_events_update_spec.rb index aa5acb749c..7134a630c9 100644 --- a/spec/controllers/api/farm_events/farm_events_update_spec.rb +++ b/spec/controllers/api/farm_events/farm_events_update_spec.rb @@ -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 @@ -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) @@ -41,7 +41,7 @@ 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") @@ -49,19 +49,20 @@ 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 @@ -69,35 +70,37 @@ 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 @@ -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) diff --git a/spec/controllers/api/regimens/regimens_update_spec.rb b/spec/controllers/api/regimens/regimens_update_spec.rb index 7c7ba6690a..51fe12cac6 100644 --- a/spec/controllers/api/regimens/regimens_update_spec.rb +++ b/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 @@ -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) @@ -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 = [ @@ -50,21 +49,21 @@ 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: { @@ -72,19 +71,20 @@ 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, @@ -92,22 +92,22 @@ 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