Skip to content

Commit

Permalink
Include user tags and location in user device responses
Browse files Browse the repository at this point in the history
  • Loading branch information
timcowlishaw committed Dec 1, 2023
1 parent a616344 commit d36d957
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
7 changes: 5 additions & 2 deletions app/views/v0/devices/_device.jbuilder
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
with_owner = true unless local_assigns.has_key?(:with_owner)
with_data = true unless local_assigns.has_key?(:with_data)

json.(
device,
:id,
Expand All @@ -23,7 +26,7 @@ else
json.merge! mac_address: '[FILTERED]'
end

if device.owner
if with_owner && device.owner
json.owner do
json.id device.owner.id
json.uuid device.owner.uuid
Expand All @@ -41,7 +44,7 @@ else
json.merge! owner: nil
end

json.data device.formatted_data
json.data device.formatted_data if with_data

if device.kit
json.kit device.kit, :id, :uuid, :slug, :name, :description, :created_at, :updated_at
Expand Down
34 changes: 9 additions & 25 deletions app/views/v0/users/_user.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ json.(user,
:profile_picture,
:url,
:location,
:joined_at,
:updated_at
)

Expand All @@ -23,29 +22,14 @@ end

json.devices user.devices.filter { |d|
!d.is_private? || current_user == user || current_user&.is_admin?
} do |device|
json.id device.id
json.uuid device.uuid
json.is_private device.is_private

if current_user and (current_user.is_admin? or (device.owner_id and current_user.id == device.owner_id))
json.mac_address device.mac_address
else
json.mac_address '[FILTERED]'
if device.is_private?
next
end
}.map do |device|
json.partial! "devices/device", device: device, with_data: false, with_owner: false
json.merge!(kit_id: device.kit_id)
if current_user == user || current_user&.is_admin?
json.merge!(
location: device.location,
latitude: device.latitude,
longitude: device.longitude,
)
end

json.name device.name.present? ? device.name : nil
json.description device.description.present? ? device.description : nil
json.location device.location
json.latitude device.latitude
json.longitude device.longitude
json.kit_id device.kit_id
json.state device.state
json.system_tags device.system_tags
json.last_reading_at device.last_reading_at
json.added_at device.added_at.utc.iso8601
json.updated_at device.updated_at.utc.iso8601
end
56 changes: 50 additions & 6 deletions spec/requests/v0/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
expect(j["devices"].map { |d| d["id"] }).to include(@public_device.id)
expect(j["devices"].map { |d| d["id"] }).not_to include(@private_device.id)
end

it "does not include the device locations" do
j = api_get "users/testguy"
expect(j["devices"].map { |d| d["location"]}.compact).to be_empty
expect(j["devices"].map { |d| d["latitude"]}.compact).to be_empty
expect(j["devices"].map { |d| d["longitude"]}.compact).to be_empty
end
end

context "when the request is authenticated as the user being requested" do
Expand All @@ -62,42 +69,79 @@
expect(j["devices"].map { |d| d["id"] }).to include(@public_device.id)
expect(j["devices"].map { |d| d["id"] }).to include(@private_device.id)
end

it "includes the device locations" do
j = api_get "users/testguy?access_token=#{token.token}"
expect(j["devices"].map { |d| d["location"]}.compact).not_to be_empty
expect(j["devices"].map { |d| d["latitude"]}.compact).not_to be_empty
expect(j["devices"].map { |d| d["longitude"]}.compact).not_to be_empty
end
end

context "when the request is authenticated as a different citizen user" do
it "only returns public devices" do
let(:requesting_token) {
requesting_user = create :user
requesting_token = create :access_token,
create :access_token,
application: application,
resource_owner_id: requesting_user.id
}

it "only returns public devices" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["id"] }).to include(@public_device.id)
expect(j["devices"].map { |d| d["id"] }).not_to include(@private_device.id)
end

it "does not include the device locations" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["location"]}.compact).to be_empty
expect(j["devices"].map { |d| d["latitude"]}.compact).to be_empty
expect(j["devices"].map { |d| d["longitude"]}.compact).to be_empty
end
end

context "when the request is authenticated as a different researcher user" do
it "only returns public devices" do
let(:requesting_token) {
requesting_user = create :user, role_mask: 3
requesting_token = create :access_token,
create :access_token,
application: application,
resource_owner_id: requesting_user.id
}

it "only returns public devices" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["id"] }).to include(@public_device.id)
expect(j["devices"].map { |d| d["id"] }).not_to include(@private_device.id)
end

it "does not include the device locations" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["location"]}.compact).to be_empty
expect(j["devices"].map { |d| d["latitude"]}.compact).to be_empty
expect(j["devices"].map { |d| d["longitude"]}.compact).to be_empty
end
end

context "when the request is authenticated as a different admin user" do
it "returns all devices" do
let(:requesting_token) {
requesting_user = create :user, role_mask: 5
requesting_token = create :access_token,
create :access_token,
application: application,
resource_owner_id: requesting_user.id
}

it "returns all devices" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["id"] }).to include(@public_device.id)
expect(j["devices"].map { |d| d["id"] }).to include(@private_device.id)
end

it "includes the device locations" do
j = api_get "users/testguy?access_token=#{requesting_token.token}"
expect(j["devices"].map { |d| d["location"]}.compact).not_to be_empty
expect(j["devices"].map { |d| d["latitude"]}.compact).not_to be_empty
expect(j["devices"].map { |d| d["longitude"]}.compact).not_to be_empty
end
end
end
end
Expand Down

0 comments on commit d36d957

Please sign in to comment.