From 3242981eb26908430854515155e94ce0f5b117a6 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 10 Nov 2024 11:39:51 -0800 Subject: [PATCH] Reapply changes to how disabling modeling works ```shell git cherry-pick d82c7f817ae43f95db473b6d3c66bae53b16ae00 --no-commit ``` --- app/controllers/pets_controller.rb | 4 +--- app/models/pet.rb | 4 +++- config/environments/development.rb | 4 ++++ config/environments/production.rb | 4 ++++ config/environments/test.rb | 4 ++++ spec/models/pet_spec.rb | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/pets_controller.rb b/app/controllers/pets_controller.rb index 8dbaf3e4..97b25329 100644 --- a/app/controllers/pets_controller.rb +++ b/app/controllers/pets_controller.rb @@ -1,12 +1,10 @@ class PetsController < ApplicationController rescue_from Neopets::CustomPets::PetNotFound, with: :pet_not_found rescue_from Neopets::CustomPets::DownloadError, with: :pet_download_error + rescue_from Pet::ModelingDisabled, with: :modeling_disabled rescue_from Pet::UnexpectedDataFormat, with: :unexpected_data_format def load - # Uncomment this to temporarily disable modeling for most users. - # return modeling_disabled unless user_signed_in? && current_user.admin? - raise Neopets::CustomPets::PetNotFound unless params[:name] @pet = Pet.load(params[:name]) points = contribute(current_user, @pet) diff --git a/app/models/pet.rb b/app/models/pet.rb index 189794bf..a2f2796a 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -4,6 +4,8 @@ class Pet < ApplicationRecord attr_reader :items, :pet_state, :alt_style def load!(timeout: nil) + raise ModelingDisabled unless Rails.configuration.modeling_enabled + viewer_data_hash = Neopets::CustomPets.fetch_viewer_data(name, timeout:) use_viewer_data(ViewerData.new(viewer_data_hash)) end @@ -59,6 +61,7 @@ class Pet < ApplicationRecord end class UnexpectedDataFormat < RuntimeError;end + class ModelingDisabled < RuntimeError;end # A representation of a Neopets::CustomPets viewer data response, translated # to DTI's database models! @@ -153,4 +156,3 @@ class Pet < ApplicationRecord end end end - diff --git a/config/environments/development.rb b/config/environments/development.rb index 41211e87..8f374d14 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -103,6 +103,10 @@ Rails.application.configure do # Allow connections on Vagrant's private network. config.web_console.permissions = '10.0.2.2' + # Allow pets to model new data. (If modeling is ever broken, disable this in + # production while we fix it!) + config.modeling_enabled = true + # Use a local copy of Impress 2020, presumably running on port 4000. (Can # override this with the IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/config/environments/production.rb b/config/environments/production.rb index 370e0246..58eae953 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -122,6 +122,10 @@ Rails.application.configure do # Skip DNS rebinding protection for the default health check endpoint. # config.host_authorization = { exclude: ->(request) { request.path == "/up" } } + # Allow pets to model new data. (If modeling is ever broken, disable this + # here while we fix it!) + config.modeling_enabled = false + # Use the live copy of Impress 2020. (Can override this with the # IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/config/environments/test.rb b/config/environments/test.rb index f677ede2..e57cbb53 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -62,6 +62,10 @@ Rails.application.configure do # Raise error when a before_action's only/except options reference missing actions config.action_controller.raise_on_missing_callback_actions = true + # Allow pets to model new data. (If modeling is ever broken, disable this in + # production while we fix it!) + config.modeling_enabled = true + # Use a local copy of Impress 2020, presumably running on port 4000. (Can # override this with the IMPRESS_2020_ORIGIN environment variable!) config.impress_2020_origin = ENV.fetch("IMPRESS_2020_ORIGIN", diff --git a/spec/models/pet_spec.rb b/spec/models/pet_spec.rb index 8e9474be..cea72c1d 100644 --- a/spec/models/pet_spec.rb +++ b/spec/models/pet_spec.rb @@ -605,7 +605,7 @@ RSpec.describe Pet, type: :model do context "when modeling is disabled" do before { allow(Rails.configuration).to receive(:modeling_enabled) { false } } - pending("raises an error") do + it("raises an error") do expect { Pet.load("matts_bat") }.to raise_error(Pet::ModelingDisabled) end end