BlueGit

timfa filemanager

Files

filemanager / tests / config.test.js

config.test.js

import project
timfa Tim Falken · pushed by timfa · 6 months ago
d3c0b911 History
config.test.js 3.0 KiB Download Raw
1'use strict';
2
3/**
4 * Unit tests for server/config.js
5 * Tests path de-duplication and child-path suppression.
6 */
7
8const path = require('path');
9const { getEffectivePaths, getAllowedRoot } = require('../server/config');
10
11// Normalise separator so tests pass on both Windows and Linux
12const sep = path.sep;
13const join = (...parts) => parts.join(sep);
14
15describe('getEffectivePaths', () => {
16 test('returns empty array for empty input', () => {
17 expect(getEffectivePaths([])).toEqual([]);
18 });
19
20 test('returns empty array for non-array input', () => {
21 expect(getEffectivePaths(null)).toEqual([]);
22 expect(getEffectivePaths(undefined)).toEqual([]);
23 });
24
25 test('de-duplicates identical paths', () => {
26 const dir = path.resolve('/mnt/data');
27 const result = getEffectivePaths(['/mnt/data', '/mnt/data']);
28 expect(result).toHaveLength(1);
29 expect(result[0]).toBe(dir);
30 });
31
32 test('drops child paths when parent is also configured', () => {
33 const parent = '/mnt/data';
34 const child = '/mnt/data/photos';
35 const result = getEffectivePaths([parent, child]);
36 // child should be dropped
37 expect(result).toHaveLength(1);
38 expect(result[0]).toBe(path.resolve(parent));
39 });
40
41 test('keeps sibling paths that share a common prefix but are not children', () => {
42 const a = '/mnt/data';
43 const b = '/mnt/documents';
44 const result = getEffectivePaths([a, b]);
45 expect(result).toHaveLength(2);
46 });
47
48 test('handles deeply nested child suppression', () => {
49 const root = '/mnt/storage';
50 const deep = '/mnt/storage/a/b/c/d';
51 const result = getEffectivePaths([root, deep]);
52 expect(result).toHaveLength(1);
53 expect(result[0]).toBe(path.resolve(root));
54 });
55
56 test('keeps multiple independent roots', () => {
57 const paths = ['/mnt/ssd', '/home/user/shared', '/tmp/uploads'];
58 const result = getEffectivePaths(paths);
59 expect(result).toHaveLength(3);
60 });
61});
62
63describe('getAllowedRoot', () => {
64 const roots = ['/mnt/data', '/mnt/docs'];
65
66 test('returns the root when file is directly inside it', () => {
67 const file = path.join('/mnt/data', 'file.txt');
68 const result = getAllowedRoot(file, roots);
69 expect(result).toBe(path.resolve('/mnt/data'));
70 });
71
72 test('returns null when file is not inside any root', () => {
73 const result = getAllowedRoot('/etc/passwd', roots);
74 expect(result).toBeNull();
75 });
76
77 test('path traversal attempt returns null', () => {
78 const traversal = path.join('/mnt/data', '..', '..', 'etc', 'passwd');
79 const result = getAllowedRoot(traversal, roots);
80 expect(result).toBeNull();
81 });
82
83 test('returns root when path equals root exactly', () => {
84 const result = getAllowedRoot('/mnt/docs', roots);
85 expect(result).toBe(path.resolve('/mnt/docs'));
86 });
87
88 test('returns correct root for nested file', () => {
89 const file = path.join('/mnt/docs', 'sub', 'report.pdf');
90 const result = getAllowedRoot(file, roots);
91 expect(result).toBe(path.resolve('/mnt/docs'));
92 });
93});