Files
growstuff/spec/requests/api/v1/photos_request_spec.rb
T
Brenda Wallace 73f62d4983 Add API resources for photos, plantings, and seeds; update application configuration
- Introduced `PhotoResource`, `PlantingResource`, and `SeedResource` to manage respective models.
- Updated application configuration to include API base URL.
- Modified production and test environments to set API base URL dynamically.
- Removed outdated JSONAPI resources initializers and patches.
- Refactored routes to use resourceful routing for API endpoints.
- Updated request specs to reflect changes in API responses and relationships.
- Added helper methods for generating related links in request specs.
2026-10-24 18:19:11 +13:00

74 lines
2.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Photos' do
subject { JSON.parse response.body }
let(:headers) { { 'Accept' => 'application/vnd.api+json' } }
let!(:photo) { create(:photo) }
let(:photo_encoded_as_json_api) do
{ "id" => photo.id.to_s,
"type" => "photos",
"attributes" => attributes,
"relationships" => {
"owner" => owner_as_json_api,
"plantings" => plantings_as_json_api,
"harvests" => harvests_as_json_api,
"gardens" => gardens_as_json_api
} }
end
let(:owner_as_json_api) { related_link("members/#{photo.owner_id}") }
let(:harvests_as_json_api) { related_link("harvests?filter[photo_id]=#{photo.id}") }
let(:gardens_as_json_api) { related_link("gardens?filter[photo_id]=#{photo.id}") }
let(:plantings_as_json_api) { related_link("plantings?filter[photo_id]=#{photo.id}") }
let(:attributes) do
{
"thumbnail-url" => photo.thumbnail_url,
"fullsize-url" => photo.fullsize_url,
"link-url" => photo.link_url,
"license-name" => photo.license_name,
"title" => photo.title
}
end
describe '#index' do
before { get '/api/v1/photos', params: {}, headers: }
it { expect(subject['data']).to include(photo_encoded_as_json_api) }
end
describe '#show' do
before { get "/api/v1/photos/#{photo.id}", params: {}, headers: }
it { expect(subject['data']['attributes']).to eq(attributes) }
it { expect(subject['data']['relationships']).to include("plantings" => plantings_as_json_api) }
it { expect(subject['data']['relationships']).to include("harvests" => harvests_as_json_api) }
it { expect(subject['data']['relationships']).to include("owner" => owner_as_json_api) }
it { expect(subject['data']).to eq(photo_encoded_as_json_api) }
end
it '#create' do
expect do
post '/api/v1/photos', params: { 'photo' => { 'name' => 'can i make this' } }, headers:
end.to raise_error ActionController::RoutingError
end
it '#update' do
expect do
post "/api/v1/photos/#{photo.id}", params: { 'photo' => { 'name' => 'can i modify this' } }, headers:
end.to raise_error ActionController::RoutingError
end
it '#delete' do
expect do
delete "/api/v1/photos/#{photo.id}", params: {}, headers:
end.to raise_error ActionController::RoutingError
end
end