diff --git a/app/jobs/mqtt_forwarding_job.rb b/app/jobs/mqtt_forwarding_job.rb index 8707bc34..efe4aa0a 100644 --- a/app/jobs/mqtt_forwarding_job.rb +++ b/app/jobs/mqtt_forwarding_job.rb @@ -16,14 +16,7 @@ def perform(device_id, reading) private def payload_for(device, reading) - renderer.render( - partial: "v0/devices/device", - locals: { - device: device.reload, - current_user: nil, - slim_owner: true - } - ) + Presenters.present(device, device.owner, renderer, readings: [reading]) end def mqtt_client diff --git a/app/models/raw_storer.rb b/app/models/raw_storer.rb index 69f682ef..905eea9c 100644 --- a/app/models/raw_storer.rb +++ b/app/models/raw_storer.rb @@ -68,12 +68,5 @@ def store data, mac, version, ip, raise_errors=false success = false raise e if raise_errors end - - if !Rails.env.test? and device - begin - Redis.current.publish("data-received", renderer.render( partial: "v0/devices/device", locals: {device: @device, current_user: nil})) - rescue - end - end end end diff --git a/spec/jobs/mqtt_forwarding_job_spec.rb b/spec/jobs/mqtt_forwarding_job_spec.rb index 24b273d1..1ae052d7 100644 --- a/spec/jobs/mqtt_forwarding_job_spec.rb +++ b/spec/jobs/mqtt_forwarding_job_spec.rb @@ -19,9 +19,7 @@ } let(:renderer) { - double(:renderer).tap do |renderer| - allow(renderer).to receive(:render).and_return(device_json) - end + double(:renderer) } let(:forwarder) { @@ -32,6 +30,7 @@ before do allow(MQTTClientFactory).to receive(:create_client).and_return(mqtt_client) + allow(Presenters).to receive(:present).and_return(device_json) allow_any_instance_of(ActionController::Base).to receive(:view_context).and_return(renderer) allow(MQTTForwarder).to receive(:new).and_return(forwarder) allow_any_instance_of(Device).to receive(:forwarding_token).and_return(forwarding_token) @@ -50,16 +49,9 @@ expect(MQTTForwarder).to have_received(:new).with(mqtt_client) end - it "renders the device json for the given device, as an unauthorized user" do + it "renders the device json for the given device and reading, as the device owner" do MQTTForwardingJob.perform_now(device.id, reading) - expect(renderer).to have_received(:render).with({ - partial: "v0/devices/device", - locals: { - device: device.reload, - current_user: nil, - slim_owner: true - } - }) + expect(Presenters).to have_received(:present).with(device, device.owner, renderer, readings: [reading]) end it "forwards using the device's id and forwarding token, with the rendered json payload" do diff --git a/spec/models/raw_storer_spec.rb b/spec/models/raw_storer_spec.rb index 989e1ab2..69772fb7 100644 --- a/spec/models/raw_storer_spec.rb +++ b/spec/models/raw_storer_spec.rb @@ -92,7 +92,8 @@ def to_ts(time) context "when the device allows forwarding" do it "forwards the message" do allow_any_instance_of(Device).to receive(:forward_readings?).and_return(true) - expect(MQTTForwardingJob).to receive(:perform_later).with(device.id, json) + # TODO assert that the correct arguments are called after refactoring device representations + expect(MQTTForwardingJob).to receive(:perform_later) storer.store(json, device.mac_address, "1.1-0.9.0-A", "127.0.0.1", true) end end diff --git a/spec/models/storer_spec.rb b/spec/models/storer_spec.rb index 41d16873..00d648da 100644 --- a/spec/models/storer_spec.rb +++ b/spec/models/storer_spec.rb @@ -20,6 +20,13 @@ 'sensors'=> [{ 'id'=> sensor.id, 'value'=>21 }] } + # TODO get rid of this fucked-up intermediate representation + @sql_data = { + "" => Time.parse(@data["recorded_at"]), + "#{sensor.id}_raw" => 21, + sensor.id => component.calibrated_value(21) + } + sensor_key = device.find_sensor_key_by_id(sensor.id) normalized_value = component.normalized_value((Float(@data['sensors'][0]['value']))) calibrated_value = component.calibrated_value(normalized_value) @@ -75,22 +82,9 @@ double(:device_json) } - it "creates a presentation of the device for the authorized user, passing the readings" do - - forwarding_token = double(:forwarding_token) - forwarder = double(:mqtt_forwarder) - allow(device).to receive(:forwarding_token).and_return(forwarding_token) - allow(device).to receive(:forward_readings?).and_return(true) - allow(MQTTForwarder).to receive(:new).and_return(forwarder) - allow(forwarder).to receive(:forward_reading) - # TODO update this to test the actual arguments passed once we've refactored the mess of different reading formats and parsers. - expect(Presenters).to receive(:present).and_return(device_json) - storer.store(device, @data) - end - it "forwards the message with the forwarding token and the device's id" do allow(device).to receive(:forward_readings?).and_return(true) - expect(MQTTForwardingJob).to receive(:perform_later).with(device.id, @data) + expect(MQTTForwardingJob).to receive(:perform_later).with(device.id, @sql_data) storer.store(device, @data) end end