Use resources to declare API TOS access endpoints (#38416)

This commit is contained in:
Matt Jankowski
2026-03-26 10:07:06 -04:00
committed by GitHub
parent e81a4e258c
commit b321d5d377
4 changed files with 83 additions and 37 deletions

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
class Api::V1::Instances::TermsOfServiceController < Api::V1::Instances::BaseController
before_action :cache_even_if_authenticated!
def index
@terms_of_service = TermsOfService.current || raise(ActiveRecord::RecordNotFound)
render json: @terms_of_service, serializer: REST::TermsOfServiceSerializer
end
def show
@terms_of_service = TermsOfService.published.find_by!(effective_date: params[:date])
render json: @terms_of_service, serializer: REST::TermsOfServiceSerializer
end
end

View File

@@ -1,23 +0,0 @@
# frozen_string_literal: true
class Api::V1::Instances::TermsOfServicesController < Api::V1::Instances::BaseController
before_action :set_terms_of_service
def show
cache_even_if_authenticated!
render json: @terms_of_service, serializer: REST::TermsOfServiceSerializer
end
private
def set_terms_of_service
@terms_of_service = begin
if params[:date].present?
TermsOfService.published.find_by!(effective_date: params[:date])
else
TermsOfService.current
end
end
not_found if @terms_of_service.nil?
end
end

View File

@@ -145,14 +145,13 @@ namespace :api, format: false do
resources :peers, only: [:index]
resources :rules, only: [:index]
resources :domain_blocks, only: [:index]
resources :terms_of_service, only: [:index, :show], param: :date
resource :privacy_policy, only: [:show]
resource :terms_of_service, only: [:show]
resource :extended_description, only: [:show]
resource :translation_languages, only: [:show]
resource :languages, only: [:show]
resource :activity, only: [:show], controller: :activity
get '/terms_of_service/:date', to: 'terms_of_services#show'
end
end

View File

@@ -4,21 +4,76 @@ require 'rails_helper'
RSpec.describe 'Terms of Service' do
describe 'GET /api/v1/instance/terms_of_service' do
before do
Fabricate(:terms_of_service)
context 'with a current TOS record' do
before do
Fabricate(:terms_of_service)
end
it 'returns http success' do
get api_v1_instance_terms_of_service_index_path
expect(response)
.to have_http_status(200)
expect(response.media_type)
.to eq('application/json')
expect(response.parsed_body)
.to be_present
.and include(:content)
end
end
it 'returns http success' do
get api_v1_instance_terms_of_service_path
context 'without a current TOS record' do
it 'returns http success' do
get api_v1_instance_terms_of_service_index_path
expect(response)
.to have_http_status(200)
expect(response.content_type)
.to start_with('application/json')
expect(response)
.to have_http_status(404)
expect(response.media_type)
.to eq('application/json')
expect(response.parsed_body)
.to be_present
.and include(:content)
expect(response.parsed_body)
.to be_present
.and include(error: /not found/i)
end
end
end
describe 'GET /api/v1/instance/terms_of_service/:date' do
context 'with an effective TOS record' do
before do
travel_to 2.days.ago do
Fabricate(:terms_of_service, effective_date: 2.days.from_now, published_at: Date.current)
end
end
it 'returns http success' do
get api_v1_instance_terms_of_service_path(date: Date.current.to_s)
expect(response)
.to have_http_status(200)
expect(response.media_type)
.to eq('application/json')
expect(response.parsed_body)
.to be_present
.and include(:content)
end
end
context 'without an effective TOS record' do
it 'returns http not found' do
get api_v1_instance_terms_of_service_path(date: Date.current.to_s)
expect(response)
.to have_http_status(404)
expect(response.media_type)
.to eq('application/json')
expect(response.parsed_body)
.to be_present
.and include(error: /not found/i)
end
end
end
end