Files
redux-scraper/spec/lib/scraper/fa_http_client_config_spec.rb
Dylan Knutson 9e8f2651db Refactor FA cookie management and enhance testing
- Updated the FA cookie management in the Scraper::FaHttpClientConfig class to validate cookie formats and handle cookies more robustly.
- Removed the fa.yml configuration file and integrated cookie retrieval directly within the class.
- Added comprehensive tests for cookie handling in the new fa_http_client_config_spec.rb file.
- Updated the Gemfile to include parallel_tests in the test and development group.
- Modified the .rspec file to enable color output for better readability in test results.
- Simplified test commands in the justfile for improved usability.
- Adjusted the rails_helper.rb to ensure the test environment is correctly set up.
2024-12-30 02:00:30 +00:00

116 lines
3.3 KiB
Ruby

require "rails_helper"
RSpec.describe Scraper::FaHttpClientConfig do
describe "#cookies" do
let(:config) { described_class.new }
context "when all cookies are valid" do
before do
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-a",
).and_return("12345678-1234-1234-1234-123456789abc")
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-b",
).and_return("abcdef12-3456-7890-1234-567890abcdef")
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-oaid",
).and_return("0123456789abcdef0123456789abcdef")
end
it "returns the correct cookie structure" do
cookies = config.cookies
expect(cookies).to match(
[
{
domain: ".furaffinity.net",
cookies: [
{
name: "a",
value: "12345678-1234-1234-1234-123456789abc",
path: "/",
},
{
name: "b",
value: "abcdef12-3456-7890-1234-567890abcdef",
path: "/",
},
],
},
{
domain: "rv.furaffinity.net",
cookies: [
{
name: "OAID",
value: "0123456789abcdef0123456789abcdef",
path: "/",
},
],
},
],
)
end
end
context "when cookies are missing" do
before do
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-a",
).and_return(nil)
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-b",
).and_return("abcdef12-3456-7890-1234-567890abcdef")
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-oaid",
).and_return("0123456789abcdef0123456789abcdef")
end
it "raises an error" do
expect { config.cookies }.to raise_error("a cookie is not set")
end
end
context "when cookies have invalid format" do
before do
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-a",
).and_return("12345678-1234-1234-1234-123456789abc")
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-b",
).and_return("invalid-uuid-format")
allow(GlobalState).to receive(:get).with(
"furaffinity-cookie-oaid",
).and_return("0123456789abcdef0123456789abcdef")
end
it "raises an error" do
expect { config.cookies }.to raise_error("b cookie has invalid format")
end
end
end
describe "#allowed_domains" do
it "returns the default allowed domains" do
config = described_class.new
expect(config.allowed_domains).to eq(
%w[*.furaffinity.net *.facdn.net ipinfo.io],
)
end
end
describe "#redirect_limit" do
it "returns the configured redirect limit" do
config = described_class.new
expect(config.redirect_limit).to eq(4)
end
end
describe "#ratelimit" do
it "returns the configured rate limits" do
config = described_class.new
expect(config.ratelimit).to eq(
[["d.furaffinity.net", :none], ["*.facdn.net", :none], ["*", 1]],
)
end
end
end