test_Safari_permissions.js (4102B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests that if the migrator does not have the permission to 8 * read from the Safari data directory, that it can request 9 * permission to read from it, if the system allows. 10 */ 11 12 const { SafariProfileMigrator } = ChromeUtils.importESModule( 13 "resource:///modules/SafariProfileMigrator.sys.mjs" 14 ); 15 16 const { sinon } = ChromeUtils.importESModule( 17 "resource://testing-common/Sinon.sys.mjs" 18 ); 19 20 const { MockFilePicker } = ChromeUtils.importESModule( 21 "resource://testing-common/MockFilePicker.sys.mjs" 22 ); 23 24 let gDataDir; 25 26 add_setup(async () => { 27 let tempDir = do_get_tempdir(); 28 gDataDir = PathUtils.join(tempDir.path, "Safari"); 29 await IOUtils.makeDirectory(gDataDir); 30 31 registerFakePath("ULibDir", tempDir); 32 33 MockFilePicker.init(/* mock BrowsingContext */ { window: globalThis }); 34 registerCleanupFunction(() => { 35 MockFilePicker.cleanup(); 36 }); 37 }); 38 39 /** 40 * Tests that canGetPermissions will return false if the platform does 41 * not allow for folder selection in the native file picker, and returns 42 * the data path otherwise. 43 */ 44 add_task(async function test_canGetPermissions() { 45 let sandbox = sinon.createSandbox(); 46 registerCleanupFunction(() => { 47 sandbox.restore(); 48 }); 49 50 let migrator = new SafariProfileMigrator(); 51 // Not being able to get a folder picker is not a problem on macOS, but 52 // we'll test that case anyways. 53 let canGetPermissionsStub = sandbox 54 .stub(MigrationUtils, "canGetPermissionsOnPlatform") 55 .resolves(false); 56 57 Assert.ok( 58 !(await migrator.canGetPermissions()), 59 "Should not be able to get permissions." 60 ); 61 62 canGetPermissionsStub.resolves(true); 63 64 Assert.equal( 65 await migrator.canGetPermissions(), 66 gDataDir, 67 "Should be able to get the permissions path." 68 ); 69 70 sandbox.restore(); 71 }); 72 73 /** 74 * Tests that getPermissions will show the native file picker in a 75 * loop until either the user cancels or selects a folder that grants 76 * read permissions to the data directory. 77 */ 78 add_task(async function test_getPermissions() { 79 let sandbox = sinon.createSandbox(); 80 registerCleanupFunction(() => { 81 sandbox.restore(); 82 }); 83 84 let migrator = new SafariProfileMigrator(); 85 sandbox.stub(MigrationUtils, "canGetPermissionsOnPlatform").resolves(true); 86 let hasPermissionsStub = sandbox 87 .stub(migrator, "hasPermissions") 88 .resolves(false); 89 90 Assert.equal( 91 await migrator.canGetPermissions(), 92 gDataDir, 93 "Should be able to get the permissions path." 94 ); 95 96 let filePickerSeenCount = 0; 97 98 let filePickerShownPromise = new Promise(resolve => { 99 MockFilePicker.showCallback = () => { 100 Assert.ok(true, "Filepicker shown."); 101 MockFilePicker.useDirectory([gDataDir]); 102 filePickerSeenCount++; 103 if (filePickerSeenCount > 3) { 104 Assert.ok(true, "File picker looped 3 times."); 105 hasPermissionsStub.resolves(true); 106 resolve(); 107 } 108 }; 109 }); 110 MockFilePicker.returnValue = MockFilePicker.returnOK; 111 112 // This is a little awkward, but we need to ensure that the 113 // filePickerShownPromise resolves first before we await 114 // the getPermissionsPromise in order to get the correct 115 // filePickerSeenCount. 116 let getPermissionsPromise = migrator.getPermissions(); 117 await filePickerShownPromise; 118 Assert.ok( 119 await getPermissionsPromise, 120 "Should report that we got permissions." 121 ); 122 123 // Make sure that the user can also hit "cancel" and that we 124 // file picker loop. 125 126 hasPermissionsStub.resolves(false); 127 128 filePickerSeenCount = 0; 129 filePickerShownPromise = new Promise(resolve => { 130 MockFilePicker.showCallback = () => { 131 Assert.ok(true, "Filepicker shown."); 132 filePickerSeenCount++; 133 Assert.equal(filePickerSeenCount, 1, "Saw the picker once."); 134 resolve(); 135 }; 136 }); 137 MockFilePicker.returnValue = MockFilePicker.returnCancel; 138 Assert.ok( 139 !(await migrator.getPermissions()), 140 "Should report that we didn't get permissions." 141 ); 142 await filePickerShownPromise; 143 144 sandbox.restore(); 145 });