diff options
Diffstat (limited to 'services/settings/test/unit/test_attachments_downloader.js')
-rw-r--r-- | services/settings/test/unit/test_attachments_downloader.js | 90 |
1 files changed, 79 insertions, 11 deletions
diff --git a/services/settings/test/unit/test_attachments_downloader.js b/services/settings/test/unit/test_attachments_downloader.js index 284294cfde..2f7db36d8b 100644 --- a/services/settings/test/unit/test_attachments_downloader.js +++ b/services/settings/test/unit/test_attachments_downloader.js @@ -1,17 +1,6 @@ -/* import-globals-from ../../../common/tests/unit/head_helpers.js */ - -const { RemoteSettings } = ChromeUtils.importESModule( - "resource://services-settings/remote-settings.sys.mjs" -); -const { UptakeTelemetry } = ChromeUtils.importESModule( - "resource://services-common/uptake-telemetry.sys.mjs" -); const { Downloader } = ChromeUtils.importESModule( "resource://services-settings/Attachments.sys.mjs" ); -const { TelemetryTestUtils } = ChromeUtils.importESModule( - "resource://testing-common/TelemetryTestUtils.sys.mjs" -); const RECORD = { id: "1f3a0802-648d-11ea-bd79-876a8b69c377", @@ -56,6 +45,11 @@ add_setup(() => { "/cdn/main-workspace/some-collection/", do_get_file("test_attachments_downloader") ); + + // For this test, we are using a server other than production. Force + // LOAD_DUMPS to true so that we can still load attachments from dumps. + delete Utils.LOAD_DUMPS; + Utils.LOAD_DUMPS = true; }); async function clear_state() { @@ -619,6 +613,80 @@ add_task(async function test_download_from_dump() { // but added for consistency with other tests tasks around here. add_task(clear_state); +add_task( + async function test_download_from_dump_fails_when_load_dumps_is_false() { + const client = RemoteSettings("dump-collection", { + bucketName: "dump-bucket", + }); + + // Temporarily replace the resource:-URL with another resource:-URL. + const orig_RESOURCE_BASE_URL = Downloader._RESOURCE_BASE_URL; + Downloader._RESOURCE_BASE_URL = "resource://rs-downloader-test"; + const resProto = Services.io + .getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler); + resProto.setSubstitution( + "rs-downloader-test", + Services.io.newFileURI(do_get_file("test_attachments_downloader")) + ); + + function checkInfo( + result, + expectedSource, + expectedRecord = RECORD_OF_DUMP + ) { + Assert.equal( + new TextDecoder().decode(new Uint8Array(result.buffer)), + "This would be a RS dump.\n", + "expected content from dump" + ); + Assert.deepEqual( + result.record, + expectedRecord, + "expected record for dump" + ); + Assert.equal(result._source, expectedSource, "expected source of dump"); + } + + // Download the dump so that we can use it to fill the cache. + const dump1 = await client.attachments.download(RECORD_OF_DUMP, { + // Note: attachmentId not set, so should fall back to record.id. + fallbackToDump: true, + }); + checkInfo(dump1, "dump_match"); + + // Fill the cache with the same data as the dump for the next part. + await client.db.saveAttachment(RECORD_OF_DUMP.id, { + record: RECORD_OF_DUMP, + blob: new Blob([dump1.buffer]), + }); + + // Now turn off loading dumps, and check we no longer load from the dump, + // but use the cache instead. + Utils.LOAD_DUMPS = false; + + const dump2 = await client.attachments.download(RECORD_OF_DUMP, { + // Note: attachmentId not set, so should fall back to record.id. + fallbackToDump: true, + }); + checkInfo(dump2, "cache_match"); + + // When the record is not given, the dump would take precedence over the + // cache but we have disabled dumps, so we should load from the cache. + const dump4 = await client.attachments.download(null, { + attachmentId: RECORD_OF_DUMP.id, + fallbackToCache: true, + fallbackToDump: true, + }); + checkInfo(dump4, "cache_fallback"); + + // Restore, just in case. + Utils.LOAD_DUMPS = true; + Downloader._RESOURCE_BASE_URL = orig_RESOURCE_BASE_URL; + resProto.setSubstitution("rs-downloader-test", null); + } +); + add_task(async function test_attachment_get() { // Since get() is largely a wrapper around the same code as download(), // we only test a couple of parts to check it functions as expected, and |