Files
growstuff/spec/requests/api/v1/plantings_request_spec.rb
T

150 lines
5.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Plantings' do
subject { JSON.parse response.body }
let(:headers) { { 'Accept' => 'application/vnd.api+json' } }
let!(:planting) { create(:planting) }
let(:planting_encoded_as_json_api) do
{ "id" => planting.id.to_s,
"type" => "plantings",
"attributes" => attributes,
"relationships" => {
"garden" => garden_as_json_api,
"crop" => crop_as_json_api,
"owner" => owner_as_json_api,
"photos" => photos_as_json_api,
"harvests" => harvests_as_json_api
} }
end
let(:harvests_as_json_api) { related_link("harvests?filter[planting_id]=#{planting.id}") }
let(:photos_as_json_api) { related_link("photos?filter[planting_id]=#{planting.id}") }
let(:owner_as_json_api) { related_link("members/#{planting.owner_id}") }
let(:crop_as_json_api) { related_link("crops/#{planting.crop_id}") }
let(:garden_as_json_api) { related_link("gardens/#{planting.garden_id}") }
let(:attributes) do
{
"slug" => planting.slug,
"planted-at" => "2014-07-30",
"failed" => false,
"finished-at" => nil,
"finished" => false,
"quantity" => 33,
"description" => planting.description,
"crop-name" => planting.crop.name,
"crop-slug" => planting.crop.slug,
"sunniness" => nil,
"planted-from" => nil,
"expected-lifespan" => nil,
"finish-predicted-at" => nil,
"percentage-grown" => nil,
"first-harvest-date" => nil,
"last-harvest-date" => nil,
"thumbnail" => nil,
"location" => planting.garden.location,
"longitude" => planting.garden.longitude,
"latitude" => planting.garden.latitude
}
end
it '#index' do
get('/api/v1/plantings', params: {}, headers:)
expect(subject['data'][0].keys).to eq(planting_encoded_as_json_api.keys)
expect(subject['data'][0]['attributes'].keys.sort!).to eq(planting_encoded_as_json_api['attributes'].keys.sort!)
expect(subject['data']).to include(planting_encoded_as_json_api)
end
describe 'the thumbnail' do
let(:photo) { create(:photo, owner: planting.owner) }
before do
planting.photos << photo
get "/api/v1/plantings/#{planting.id}", headers:
end
it 'is the thumbnail of its photo' do
expect(subject['data']['attributes']['thumbnail']).to eq(photo.thumbnail_url)
end
end
it '#show' do
get("/api/v1/plantings/#{planting.id}", params: {}, headers:)
expect(subject['data']['relationships']).to include("garden" => garden_as_json_api)
expect(subject['data']['relationships']).to include("crop" => crop_as_json_api)
expect(subject['data']['relationships']).to include("owner" => owner_as_json_api)
expect(subject['data']['relationships']).to include("harvests" => harvests_as_json_api)
expect(subject['data']['relationships']).to include("photos" => photos_as_json_api)
expect(subject['data']).to eq(planting_encoded_as_json_api)
end
describe "by member/owner" do
before :each do
@member1 = planting.owner
@planting2 = create(:planting, owner: create(:owner))
@member2 = @planting2.owner
end
describe "#show" do
it "locates the correct member" do
get "/api/v1/plantings?filter[owner-id]=#{@member1.id}"
expect(JSON.parse(response.body)['data'][0]['id']).to eq(planting.id.to_s)
get "/api/v1/plantings?filter[owner-id]=#{@member2.id}"
expect(JSON.parse(response.body)['data'][0]['id']).to eq(@planting2.id.to_s)
end
end
end
context 'filtering' do
let!(:planting2) { create(:planting, failed: true, sunniness: 'shade') }
let!(:perennial_planting) { create(:planting, crop: create(:crop, perennial: true)) }
it 'filters by failed' do
get('/api/v1/plantings?filter[failed]=true', params: {}, headers:)
expect(subject['data'].size).to eq(1)
expect(subject['data'][0]['id']).to eq(planting2.id.to_s)
end
it 'filters by sunniness' do
get('/api/v1/plantings?filter[sunniness]=shade', params: {}, headers:)
expect(subject['data'].size).to eq(1)
expect(subject['data'][0]['id']).to eq(planting2.id.to_s)
end
it 'filters by perennial' do
get('/api/v1/plantings?filter[perennial]=true', params: {}, headers:)
expect(response).to have_http_status(:ok)
expect(subject['data'].size).to eq(1)
expect(subject['data'][0]['id']).to eq(perennial_planting.id.to_s)
end
it 'filters by active' do
get('/api/v1/plantings?filter[active]=true', params: {}, headers:)
expect(response).to have_http_status(:ok)
expect(subject['data'].size).to eq(2)
expect(subject['data'][0]['id']).to eq(planting.id.to_s)
end
end
it_behaves_like 'a resource members can write', 'plantings' do
let(:garden) { create(:garden, owner: member) }
let(:create_params) do
{ attributes: { description: 'My API planting' },
relationships: { crop: link_to('crops', create(:crop).id),
garden: link_to('gardens', garden.id) } }
end
let(:own_record) { create(:planting, owner: member, garden: garden) }
let(:other_record) { create(:planting) }
let(:changes) { { description: 'An updated planting' } }
end
end