2017-02-07 09:47:34 -07:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Api::PointsController do
|
|
|
|
include Devise::Test::ControllerHelpers
|
|
|
|
describe '#index' do
|
|
|
|
let(:device) { FactoryGirl.create(:device) }
|
2017-08-08 07:01:32 -06:00
|
|
|
let(:user) do
|
|
|
|
FactoryGirl.create(:user, device: device, password: "password123")
|
|
|
|
end
|
|
|
|
let(:auth_token) do
|
|
|
|
Auth::CreateToken
|
|
|
|
.run!(email: user.email, password: "password123")[:token].encoded
|
|
|
|
end
|
2017-02-07 09:47:34 -07:00
|
|
|
|
|
|
|
it 'lists points' do
|
|
|
|
sign_in user
|
|
|
|
FactoryGirl.create_list(:point, 3, device: device)
|
|
|
|
get :index
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json.length).to eq(3)
|
|
|
|
expect(json.first.keys).to include(:x)
|
|
|
|
end
|
2017-05-10 12:42:24 -06:00
|
|
|
it 'lists all plants' do
|
|
|
|
Point.destroy_all
|
|
|
|
plants = 3.times do |num|
|
|
|
|
Point.create!(x: num,
|
|
|
|
y: num,
|
|
|
|
z: num,
|
|
|
|
radius: 50,
|
|
|
|
name: "Cabbage #{num}",
|
|
|
|
device: user.device,
|
|
|
|
pointer: Plant.new(openfarm_slug: "cabbage"))
|
|
|
|
end
|
|
|
|
sign_in user
|
|
|
|
get :index
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json.length).to eq(3)
|
|
|
|
end
|
|
|
|
it 'lists all tool slots' do
|
|
|
|
Point.destroy_all
|
|
|
|
sign_in user
|
|
|
|
ts = Point.create(x: 0,
|
|
|
|
y: 0,
|
|
|
|
z: 0,
|
|
|
|
radius: 50,
|
|
|
|
name: "My TS",
|
|
|
|
device: user.device,
|
2017-05-10 13:37:35 -06:00
|
|
|
pointer: ToolSlot.new)
|
2017-05-10 12:42:24 -06:00
|
|
|
get :index
|
|
|
|
expect(json.first[:id]).to eq(ts.id)
|
2017-05-10 15:14:13 -06:00
|
|
|
expect(json.first[:name]).to eq(ts.name)
|
2017-05-10 12:42:24 -06:00
|
|
|
end
|
2017-08-07 17:10:25 -06:00
|
|
|
|
|
|
|
it "handles outdated FBOS" do
|
|
|
|
old_last_seen = user.device.last_seen
|
|
|
|
ua = "FARMBOTOS/1.1.1 (RPI3) RPI3 (1.1.1)"
|
|
|
|
allow(request).to receive(:user_agent).and_return(ua)
|
|
|
|
request.env["HTTP_USER_AGENT"] = ua
|
|
|
|
sign_in user
|
|
|
|
FactoryGirl.create_list(:point, 1, device: device)
|
|
|
|
get :index
|
|
|
|
expect(response.status).to eq(426)
|
|
|
|
expect(json[:error]).to include("Upgrade to latest FarmBot OS")
|
|
|
|
end
|
2017-08-08 07:01:32 -06:00
|
|
|
|
|
|
|
it "marks device as seen when they download points" do
|
|
|
|
old_last_seen = user.device.last_seen
|
2017-08-08 16:16:07 -06:00
|
|
|
ua = "FarmbotOS/5.0.2 (host) host ()"
|
2017-08-08 07:01:32 -06:00
|
|
|
allow(request).to receive(:user_agent).and_return(ua)
|
|
|
|
request.env["HTTP_USER_AGENT"] = ua
|
|
|
|
request.headers["Authorization"] = "bearer #{auth_token}"
|
|
|
|
FactoryGirl.create_list(:point, 1, device: device)
|
|
|
|
get :index
|
|
|
|
new_last_seen = user.device.reload.last_seen
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(new_last_seen).not_to eq(old_last_seen)
|
|
|
|
end
|
2017-02-07 09:47:34 -07:00
|
|
|
end
|
|
|
|
end
|