Merge pull request #4277 from Growstuff/add-activity-update-coverage

Add test coverage for updating an activity via the API
This commit is contained in:
google-labs-jules[bot]
2025-09-30 20:29:11 +09:30
committed by GitHub
parent 852ac600f4
commit 63d65c4e6b
5 changed files with 53 additions and 14 deletions

View File

@@ -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

View File

@@ -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) }

View File

@@ -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) }

View File

@@ -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) }

View File

@@ -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) }