2024-04-02 02:53:53 -04:00
# frozen_string_literal: true
RSpec . describe DiscourseIpInfo do
describe " .mmdb_download " do
2024-06-11 19:51:45 -04:00
before { Discourse :: Utils . stubs ( :execute_command ) }
2024-05-09 03:11:56 -04:00
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 " ,
2024-05-15 20:37:34 -04:00
) . with ( basic_auth : %w[ account_id license_key ] ) . to_return (
status : 302 ,
body : " " ,
headers : {
location :
" https://mm-prod-geoip-databases.a2649acb697e2c09b632799562c076f2.r2.cloudflarestorage.com/some-path " ,
} ,
)
stub_request (
:get ,
" https://mm-prod-geoip-databases.a2649acb697e2c09b632799562c076f2.r2.cloudflarestorage.com/some-path " ,
) . with { | req | expect ( req . headers . key? ( " Authorization " ) ) . to eq ( false ) } . to_return ( status : 200 )
2024-05-09 03:11:56 -04:00
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
2024-04-02 02:53:53 -04:00
it " should download the MaxMind databases from the right URL when `maxmind_mirror_url` global setting has been configured " do
2024-04-07 22:27:40 -04:00
global_setting :maxmind_mirror_url , " https://b.www.example.com/mirror "
2024-04-02 02:53:53 -04:00
2024-04-07 22:27:40 -04:00
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 (
2024-04-02 02:53:53 -04:00
status : 200 ,
body : " " ,
)
described_class . mmdb_download ( " GeoLite2-City " )
end
2024-07-29 23:33:20 -04:00
it " should not throw an error and instead log the exception when database file fails to download " do
2024-11-12 19:47:39 -05:00
fake_logger = FakeLogger . new
Rails . logger . broadcast_to ( fake_logger )
2024-07-29 23:33:20 -04:00
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 : 500 , body : nil , headers : { } )
expect do described_class . mmdb_download ( " GeoLite2-City " ) end . not_to raise_error
expect ( fake_logger . warnings . length ) . to eq ( 1 )
expect ( fake_logger . warnings . first ) . to include (
" MaxMind database GeoLite2-City download failed. 500 Error " ,
)
ensure
2024-11-12 19:47:39 -05:00
Rails . logger . stop_broadcasting_to ( fake_logger )
2024-07-29 23:33:20 -04:00
end
2024-04-02 02:53:53 -04:00
end
end