text: add test for privacy_aware_path

This commit is contained in:
KernelDeimos
2024-11-21 12:06:02 -05:00
parent d30d62f558
commit 1363de7cbf
3 changed files with 79 additions and 32 deletions
+3 -32
View File
@@ -49,6 +49,7 @@ import { AntiCSRFService } from './services/AntiCSRFService.js';
import { IPCService } from './services/IPCService.js';
import { ExecService } from './services/ExecService.js';
import { DebugService } from './services/DebugService.js';
import { privacy_aware_path } from './util/desktop.js';
const launch_services = async function (options) {
// === Services Data Structures ===
@@ -1461,35 +1462,5 @@ $(document).on('contextmenu', '.disable-context-menu', function(e){
}
})
/**
* Converts a file system path to a privacy-aware path.
* - Paths starting with `~/` are returned unchanged.
* - Paths starting with the user's home path are replaced with `~`.
* - Absolute paths not starting with the user's home path are returned unchanged.
* - Relative paths are prefixed with `~/`.
* - Other paths are returned unchanged.
*
* @param {string} fspath - The file system path to be converted.
* @returns {string} The privacy-aware path.
*/
window.privacy_aware_path = function(fspath){
// e.g. /my_username/test.txt -> ~/test.txt
if(fspath.startsWith('~/'))
return fspath;
// e.g. /my_username/test.txt -> ~/test.txt
else if(fspath.startsWith(
window.home_path.endsWith('/')
? window.home_path
: window.home_path + '/'
))
return fspath.replace(window.home_path, '~');
// e.g. /other_username/test.txt -> /other_username/test.txt
else if(fspath.startsWith('/') && !fspath.startsWith(window.home_path))
return fspath;
// e.g. test.txt -> ~/test.txt
else if(!fspath.startsWith('/'))
return '~/' + fspath;
// e.g. /username/path/to/item -> /username/path/to/item
else
return fspath;
};
// util/desktop.js
window.privacy_aware_path = privacy_aware_path({ window });
+43
View File
@@ -0,0 +1,43 @@
/**
* This file contains functions that are used by Puter's desktop GUI.
* Functions moved here are not bound to the `window` object, making it
* easier to write unit tests for them.
*
* Functions here may be bound to `window` at any of the following locations:
* - src/gui/src/initgui.js
*
* ^ Please add to the above list as necessary when moving functions here.
*/
/**
* Converts a file system path to a privacy-aware path.
* - Paths starting with `~/` are returned unchanged.
* - Paths starting with the user's home path are replaced with `~`.
* - Absolute paths not starting with the user's home path are returned unchanged.
* - Relative paths are prefixed with `~/`.
* - Other paths are returned unchanged.
*
* @param {string} fspath - The file system path to be converted.
* @returns {string} The privacy-aware path.
*/
export const privacy_aware_path = world => function privacy_aware_path (fspath) {
// e.g. /my_username/test.txt -> ~/test.txt
if(fspath.startsWith('~/'))
return fspath;
// e.g. /my_username/test.txt -> ~/test.txt
else if(fspath.startsWith(
world.window.home_path.endsWith('/')
? world.window.home_path
: world.window.home_path + '/'
))
return fspath.replace(world.window.home_path, '~');
// e.g. /other_username/test.txt -> /other_username/test.txt
else if(fspath.startsWith('/') && !fspath.startsWith(world.window.home_path))
return fspath;
// e.g. test.txt -> ~/test.txt
else if(!fspath.startsWith('/'))
return '~/' + fspath;
// e.g. /username/path/to/item -> /username/path/to/item
else
return fspath;
};
+33
View File
@@ -0,0 +1,33 @@
import assert from 'assert';
import { privacy_aware_path } from '../src/util/desktop.js';
const cases = [
{
title: 'path in user home',
username: 'user',
input: '/home/user/test.txt',
expected: '~/test.txt',
},
{
title: 'path on user desktop',
username: 'user',
input: '/home/user/Desktop/test.txt',
expected: '~/Desktop/test.txt',
},
{
title: 'prefix (ed3/ed) bug',
username: 'ed',
input: '/home/ed3/Desktop/test.txt',
expected: '/home/ed3/Desktop/test.txt',
},
];
describe('window.privacy_aware_path', () => {
for (const { title, username, input, expected } of cases) {
it(title, () => {
assert.equal(privacy_aware_path({
window: { home_path: `/home/${username}` },
})(input), expected);
});
}
});