diff --git a/app/controllers/api/v1/instances/terms_of_service_controller.rb b/app/controllers/api/v1/instances/terms_of_service_controller.rb new file mode 100644 index 0000000000..9968b41317 --- /dev/null +++ b/app/controllers/api/v1/instances/terms_of_service_controller.rb @@ -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 diff --git a/app/controllers/api/v1/instances/terms_of_services_controller.rb b/app/controllers/api/v1/instances/terms_of_services_controller.rb deleted file mode 100644 index a32438e31d..0000000000 --- a/app/controllers/api/v1/instances/terms_of_services_controller.rb +++ /dev/null @@ -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 diff --git a/config/routes/api.rb b/config/routes/api.rb index 61d74f4cd9..696981aad2 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -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 diff --git a/spec/requests/api/v1/instances/terms_of_services_spec.rb b/spec/requests/api/v1/instances/terms_of_services_spec.rb index 5feb49f48d..3a352a6d0c 100644 --- a/spec/requests/api/v1/instances/terms_of_services_spec.rb +++ b/spec/requests/api/v1/instances/terms_of_services_spec.rb @@ -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