discourse/lib/discourse_ip_info_spec.rb
Alan Guo Xiang Tan 7079698cdf
FIX: Use MaxMind supplied permalinks to download MaxMind databases (#26847)
This commit switches `DiscourseIpInfo.mmdb_download` to use the
permalinks supplied by MaxMind to download the MaxMind databases as
specified in
https://dev.maxmind.com/geoip/updating-databases#directly-downloading-databases
which states:

```
To directly download databases, follow these steps:

1. In the "Download Links" column, click "Get Permalink(s)" for the desired database.
2. Copy the permalink(s) provided in the modal window.
3. Provide your account ID and your license key using Basic Authentication to authenticate.
```

Previously we are downloading from `https://download.maxmind.com/app/geoip_download` but this is not
documented anyway on MaxMind's docs so this URL can in theory break
in the future without warning. Therefore, we are taking a proactive
approach to download the databases from MaxMind the recommended way
instead of relying on a hidden URL. This old way of downloading the
databases with only a license key will be deprecated in 3.3 and be
removed in 3.4.
2024-05-09 15:11:56 +08:00

51 lines
2.0 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DiscourseIpInfo do
describe ".mmdb_download" do
it "should download the MaxMind databases from MaxMind's download permalinks when `maxmind_license_key` and `maxmind_account_id` global setting has been set" do
global_setting :maxmind_license_key, "license_key"
global_setting :maxmind_account_id, "account_id"
stub_request(
:get,
"https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz",
).with(basic_auth: %w[account_id license_key]).to_return(status: 200, body: "", headers: {})
described_class.mmdb_download("GeoLite2-City")
end
it "should download the MaxMind databases from MaxMind's undocumented download URL when `maxmind_license_key` global setting has been set but not `maxmind_account_id` for backwards compatibility reasons" do
global_setting :maxmind_license_key, "license_key"
stub_request(
:get,
"https://download.maxmind.com/app/geoip_download?license_key=license_key&edition_id=GeoLite2-City&suffix=tar.gz",
).to_return(status: 200, body: "", headers: {})
described_class.mmdb_download("GeoLite2-City")
end
it "should download the MaxMind databases from the right URL when `maxmind_mirror_url` global setting has been configured" do
global_setting :maxmind_mirror_url, "https://b.www.example.com/mirror"
stub_request(:get, "https://b.www.example.com/mirror/GeoLite2-City.tar.gz").to_return(
status: 200,
body: "",
)
described_class.mmdb_download("GeoLite2-City")
end
it "should download the MaxMind databases from the right URL when `maxmind_mirror_url` global setting has been configured and has a trailing slash" do
global_setting :maxmind_mirror_url, "https://b.www.example.com/mirror/"
stub_request(:get, "https://b.www.example.com/mirror/GeoLite2-City.tar.gz").to_return(
status: 200,
body: "",
)
described_class.mmdb_download("GeoLite2-City")
end
end
end