Farmbot-Web-App/spec/controllers/api/points/index_spec.rb

81 lines
2.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe Api::PointsController do
include Devise::Test::ControllerHelpers
describe '#index' do
2017-10-22 07:19:50 -06:00
let(:device) { FactoryBot.create(:device) }
2017-08-08 07:01:32 -06:00
let(:user) do
2017-10-22 07:19:50 -06:00
FactoryBot.create(:user, device: device, password: "password123")
2017-08-08 07:01:32 -06:00
end
let(:auth_token) do
params = {email: user.email,
password: "password123",
fbos_version: Gem::Version.new("999.9.9")}
Auth::CreateToken.run!(params)[:token].encoded
2017-08-08 07:01:32 -06:00
end
it 'lists points' do
sign_in user
2017-10-22 07:19:50 -06:00
FactoryBot.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
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
2018-04-07 14:09:31 -06:00
ts = ToolSlot.create(x: 0,
y: 0,
z: 0,
radius: 50,
name: "My TS",
device: user.device)
get :index
expect(json.first[:id]).to eq(ts.id)
expect(json.first[:name]).to eq(ts.name)
end
2017-08-07 17:10:25 -06:00
it "handles outdated FBOS" do
old_last_saw_api = user.device.last_saw_api
2017-08-07 17:10:25 -06:00
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
2017-10-22 07:19:50 -06:00
FactoryBot.create_list(:point, 1, device: device)
2017-08-07 17:10:25 -06:00
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_saw_api = user.device.last_saw_api
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}"
2017-10-22 07:19:50 -06:00
FactoryBot.create_list(:point, 1, device: device)
2017-08-08 07:01:32 -06:00
get :index
new_last_saw_api = user.device.reload.last_saw_api
2017-08-08 07:01:32 -06:00
expect(response.status).to eq(200)
expect(new_last_saw_api).not_to eq(old_last_saw_api)
2017-08-08 07:01:32 -06:00
end
end
end