commit
992b6f9e1c
|
@ -37,6 +37,7 @@ class ApplicationRecord < ActiveRecord::Base
|
|||
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
|
||||
|
|
|
@ -5,20 +5,20 @@ describe Api::FarmEventsController do
|
|||
|
||||
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 @@ describe Api::FarmEventsController do
|
|||
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 @@ describe Api::FarmEventsController do
|
|||
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 @@ describe Api::FarmEventsController do
|
|||
|
||||
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 @@ describe Api::FarmEventsController do
|
|||
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 @@ describe Api::FarmEventsController do
|
|||
args: {
|
||||
x: 1,
|
||||
y: 2,
|
||||
z: 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
z: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
update_body(fe, body)
|
||||
expect(response.status).to eq(200)
|
||||
|
|
|
@ -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 @@ describe Api::RegimensController do
|
|||
"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 @@ describe Api::RegimensController do
|
|||
|
||||
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 @@ describe Api::RegimensController do
|
|||
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 @@ describe Api::RegimensController do
|
|||
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 @@ describe Api::RegimensController do
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue