From 63d65c4e6b79893418b2c668c3b9d394ac66ca7f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 30 Sep 2025 20:29:11 +0930 Subject: [PATCH] Merge pull request #4277 from Growstuff/add-activity-update-coverage Add test coverage for updating an activity via the API --- .../api/v1/activities_request_spec.rb | 43 ++++++++++++++++++- spec/requests/api/v1/gardens_request_spec.rb | 6 +-- spec/requests/api/v1/harvests_request_spec.rb | 6 +-- .../requests/api/v1/plantings_request_spec.rb | 6 +-- spec/requests/api/v1/seeds_request_spec.rb | 6 +-- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/spec/requests/api/v1/activities_request_spec.rb b/spec/requests/api/v1/activities_request_spec.rb index 0eb5e98a8..3cbf7428a 100644 --- a/spec/requests/api/v1/activities_request_spec.rb +++ b/spec/requests/api/v1/activities_request_spec.rb @@ -5,8 +5,14 @@ require 'rails_helper' RSpec.describe 'Activities', type: :request do subject { JSON.parse response.body } - let(:headers) { { 'Accept' => 'application/vnd.api+json' } } - let!(:activity) { FactoryBot.create(:activity, garden: create(:garden), planting: create(:planting)) } + let(:member) { create(:member) } + let(:token) do + member.regenerate_api_token + member.api_token.token + end + let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } + let!(:activity) { FactoryBot.create(:activity, owner: member, garden: create(:garden, owner: member), planting: create(:planting, owner: member)) } let!(:activity2) { FactoryBot.create(:activity) } it '#index' do @@ -53,4 +59,37 @@ RSpec.describe 'Activities', type: :request do expect(subject['data'][1]['id']).to eq(activity2.id.to_s) end end + + context '#update' do + let(:params) do + { + 'data' => { + 'type' => 'activities', + 'id' => activity.id.to_s, + 'attributes' => { + 'description' => 'A new description', + 'finished' => true, + 'due-date' => '2025-10-31' + } + } + } + end + + it 'updates the activity' do + patch "/api/v1/activities/#{activity.id}", params: params.to_json, headers: auth_headers + + expect(response).to have_http_status(:ok) + + # Check response + expect(subject['data']['attributes']['description']).to eq('A new description') + expect(subject['data']['attributes']['finished']).to eq(true) + expect(subject['data']['attributes']['due-date']).to eq('2025-10-31') + + # Check database + activity.reload + expect(activity.description).to eq('A new description') + expect(activity.finished).to eq(true) + expect(activity.due_date.to_s).to eq('2025-10-31') + end + end end diff --git a/spec/requests/api/v1/gardens_request_spec.rb b/spec/requests/api/v1/gardens_request_spec.rb index de3906db8..b32e94701 100644 --- a/spec/requests/api/v1/gardens_request_spec.rb +++ b/spec/requests/api/v1/gardens_request_spec.rb @@ -85,7 +85,7 @@ RSpec.describe 'Gardens', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:garden_params) do { data: { @@ -116,7 +116,7 @@ RSpec.describe 'Gardens', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:garden) { create(:garden, owner: member) } let(:other_member_garden) { create(:garden) } let(:update_params) do @@ -164,7 +164,7 @@ RSpec.describe 'Gardens', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let!(:garden) { create(:garden, owner: member) } let(:other_member_garden) { create(:garden) } diff --git a/spec/requests/api/v1/harvests_request_spec.rb b/spec/requests/api/v1/harvests_request_spec.rb index 38d6777c1..4ea2c339c 100644 --- a/spec/requests/api/v1/harvests_request_spec.rb +++ b/spec/requests/api/v1/harvests_request_spec.rb @@ -117,7 +117,7 @@ RSpec.describe 'Harvests', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:crop) { create(:crop) } let(:planting) { create(:planting, owner: member) } let(:plant_part) { create(:plant_part) } @@ -156,7 +156,7 @@ RSpec.describe 'Harvests', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:harvest) { create(:harvest, owner: member) } let(:other_member_harvest) { create(:harvest) } let(:update_params) do @@ -205,7 +205,7 @@ RSpec.describe 'Harvests', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let!(:harvest) { create(:harvest, owner: member) } let(:other_member_harvest) { create(:harvest) } diff --git a/spec/requests/api/v1/plantings_request_spec.rb b/spec/requests/api/v1/plantings_request_spec.rb index 7d334d539..4e1812d13 100644 --- a/spec/requests/api/v1/plantings_request_spec.rb +++ b/spec/requests/api/v1/plantings_request_spec.rb @@ -102,7 +102,7 @@ RSpec.describe 'Plantings', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:crop) { create(:crop) } let(:garden) { create(:garden, owner: member) } let(:planting_params) do @@ -140,7 +140,7 @@ RSpec.describe 'Plantings', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:planting) { create(:planting, owner: member) } let(:other_member_planting) { create(:planting) } let(:update_params) do @@ -189,7 +189,7 @@ RSpec.describe 'Plantings', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let!(:planting) { create(:planting, owner: member) } let(:other_member_planting) { create(:planting) } diff --git a/spec/requests/api/v1/seeds_request_spec.rb b/spec/requests/api/v1/seeds_request_spec.rb index ea30c7924..5404631f6 100644 --- a/spec/requests/api/v1/seeds_request_spec.rb +++ b/spec/requests/api/v1/seeds_request_spec.rb @@ -68,7 +68,7 @@ RSpec.describe 'Seeds', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:crop) { create(:crop) } let(:seed_params) do { @@ -103,7 +103,7 @@ RSpec.describe 'Seeds', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:crop) { create(:crop) } let(:seed) { create(:seed, owner: member, crop: crop) } let(:other_member_seed) { create(:seed) } @@ -152,7 +152,7 @@ RSpec.describe 'Seeds', type: :request do member.api_token.token end let(:headers) { { 'Accept' => 'application/vnd.api+json', 'Content-Type' => 'application/vnd.api+json' } } - let(:auth_headers) { headers.merge('Authorization' => "Token token=#{token}") } + let(:auth_headers) { headers.merge('Authorization' => "Bearer #{token}") } let(:crop) { create(:crop) } let!(:seed) { create(:seed, owner: member, crop: crop) } let(:other_member_seed) { create(:seed) }