Files
redux-scraper/spec/controllers/global_states_controller_spec.rb
Dylan Knutson ec26e425c6 Add FA Cookies management functionality
- Introduced methods for managing FurAffinity cookies in the GlobalStatesController, including `fa_cookies`, `edit_fa_cookies`, and `update_fa_cookies`.
- Added a new policy for managing FA cookies, restricting access to admin users.
- Created views for displaying and editing FA cookies, enhancing user interaction.
- Updated routes to include paths for FA cookies management.
- Added comprehensive tests for the new functionality in the GlobalStatesController spec.
2024-12-30 01:39:21 +00:00

254 lines
7.4 KiB
Ruby

require "rails_helper"
RSpec.describe GlobalStatesController, type: :controller do
let(:admin_user) { create(:user, role: :admin) }
let(:valid_attributes) do
{ key: "test_key", value: "test_value", value_type: "string" }
end
let(:invalid_attributes) { { key: "", value: "", value_type: "" } }
let(:global_state) { create(:global_state, valid_attributes) }
before { sign_in admin_user }
describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end
end
describe "GET #new" do
it "returns a success response" do
get :new
expect(response).to be_successful
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new GlobalState" do
expect {
post :create, params: { global_state: valid_attributes }
}.to change(GlobalState, :count).by(1)
end
it "redirects to the global states list" do
post :create, params: { global_state: valid_attributes }
expect(response).to redirect_to(global_states_path)
end
end
context "with invalid params" do
it "returns unprocessable entity status" do
post :create, params: { global_state: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe "GET #edit" do
it "returns a success response" do
get :edit, params: { id: global_state.to_param }
expect(response).to be_successful
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { { value: "new_value" } }
it "updates the requested global_state" do
put :update,
params: {
id: global_state.to_param,
global_state: new_attributes,
}
global_state.reload
expect(global_state.value).to eq("new_value")
end
it "redirects to the global states list" do
put :update,
params: {
id: global_state.to_param,
global_state: new_attributes,
}
expect(response).to redirect_to(global_states_path)
end
end
context "with invalid params" do
it "returns unprocessable entity status" do
put :update,
params: {
id: global_state.to_param,
global_state: invalid_attributes,
}
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe "DELETE #destroy" do
let!(:global_state) { create(:global_state) }
it "destroys the requested global_state" do
expect {
delete :destroy, params: { id: global_state.to_param }
}.to change(GlobalState, :count).by(-1)
end
it "redirects to the global_states list" do
delete :destroy, params: { id: global_state.to_param }
expect(response).to redirect_to(global_states_path)
end
end
describe "FA Cookies Management" do
let(:fa_cookie_keys) do
%w[furaffinity-cookie-a furaffinity-cookie-b furaffinity-cookie-oaid]
end
describe "GET #fa_cookies" do
context "when no FA cookie states exist" do
before { GlobalState.delete_all }
it "returns a success response" do
get :fa_cookies
expect(response).to be_successful
end
it "assigns new GlobalState objects for all required cookies" do
get :fa_cookies
assigned_cookies = assigns(:fa_cookies)
expect(assigned_cookies.size).to eq(3)
expect(assigned_cookies).to all(be_new_record)
expect(assigned_cookies.map(&:key)).to match_array(fa_cookie_keys)
expect(assigned_cookies.map(&:value_type).uniq).to eq(["string"])
end
end
context "when FA cookie states exist" do
it "assigns all FA cookie states" do
cookie_states =
fa_cookie_keys.map do |key|
create(
:global_state,
key: key,
value: "test",
value_type: :string,
)
end
get :fa_cookies
expect(assigns(:fa_cookies)).to match_array(cookie_states)
end
end
end
describe "GET #edit_fa_cookies" do
context "when no FA cookie states exist" do
before { GlobalState.delete_all }
it "returns a success response" do
get :edit_fa_cookies
expect(response).to be_successful
end
it "assigns new GlobalState objects for all required cookies" do
get :edit_fa_cookies
assigned_cookies = assigns(:fa_cookies)
expect(assigned_cookies.size).to eq(3)
expect(assigned_cookies).to all(be_new_record)
expect(assigned_cookies.map(&:key)).to match_array(fa_cookie_keys)
expect(assigned_cookies.map(&:value_type).uniq).to eq(["string"])
end
end
context "when FA cookie states exist" do
it "assigns all FA cookie states" do
cookie_states =
fa_cookie_keys.map do |key|
create(
:global_state,
key: key,
value: "test",
value_type: :string,
)
end
get :edit_fa_cookies
expect(assigns(:fa_cookies)).to match_array(cookie_states)
end
end
end
describe "PATCH #update_fa_cookies" do
context "when no FA cookie states exist" do
before { GlobalState.delete_all }
let(:new_values) { fa_cookie_keys.index_with { |_key| "new_value" } }
it "creates all cookie states" do
expect {
patch :update_fa_cookies, params: { fa_cookies: new_values }
}.to change(GlobalState, :count).by(3)
fa_cookie_keys.each do |key|
state = GlobalState.find_by(key: key)
expect(state).to be_present
expect(state.value).to eq("new_value")
expect(state.value_type).to eq("string")
end
end
it "redirects to the fa_cookies page" do
patch :update_fa_cookies, params: { fa_cookies: new_values }
expect(response).to redirect_to(fa_cookies_global_states_path)
end
end
context "when FA cookie states exist" do
let(:new_values) { fa_cookie_keys.index_with { |_key| "new_value" } }
it "updates existing records" do
existing_states =
fa_cookie_keys.map do |key|
create(
:global_state,
key: key,
value: "old_value",
value_type: :string,
)
end
patch :update_fa_cookies, params: { fa_cookies: new_values }
existing_states.each do |state|
expect(state.reload.value).to eq("new_value")
end
end
end
context "with invalid params" do
let(:invalid_values) { { "furaffinity-cookie-a" => "" } }
it "returns unprocessable entity status" do
patch :update_fa_cookies, params: { fa_cookies: invalid_values }
expect(response).to have_http_status(:unprocessable_entity)
end
it "assigns @fa_cookies with all required keys" do
patch :update_fa_cookies, params: { fa_cookies: invalid_values }
assigned_cookies = assigns(:fa_cookies)
expect(assigned_cookies).to be_present
expect(assigned_cookies.map(&:key)).to match_array(fa_cookie_keys)
end
end
end
end
end