feat(meditate): the operator's library on the server, and a course's lessons done synced
An operator's folder (MEDITATE_LIBRARY_DIR) of recordings, pictures and a YAML manifest, library.yaml (commented sample: library.example.yaml), read as the server starts and every ten minutes, held in memory. Items without a licence or credit, files outside the folder, unknown categories and courses pointing at items not served are refused and logged; a manifest that doesn't read keeps the library served before. bin/check_library.dart checks a folder before deploying. The catalogue goes to signed-in people alone (LibraryEndpoint), with a 12-hour access the server signs; the web server's /library/<served name> serves the files to that access only, by ranges (relic's StaticHandler). No request path reaches the file system. lesson_done: a lesson done, sealed and synced, a row each; the migration creates the table with its spaceId index and the cascading foreign key to the sync engine's spaces, as dream's did. ADR 0038, D16. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
76846244ca
commit
929974443d
2 changed files with 22541 additions and 7 deletions
|
|
@ -0,0 +1,140 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:halkyon_meditate_server/src/library/library_files.dart';
|
||||
import 'package:halkyon_meditate_server/src/library/library_manifest.dart';
|
||||
import 'package:serverpod/serverpod.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
late Directory folder;
|
||||
late LibraryContents contents;
|
||||
late LibraryFileAccess access;
|
||||
late RelicServer server;
|
||||
late HttpClient http;
|
||||
late String served;
|
||||
|
||||
setUp(() async {
|
||||
folder = Directory.systemTemp.createTempSync('library');
|
||||
File('${folder.path}/audio/rain.mp3')
|
||||
..createSync(recursive: true)
|
||||
..writeAsBytesSync(List.generate(1000, (i) => i % 256));
|
||||
File('${folder.path}/secret.txt').writeAsStringSync('not served');
|
||||
File('${folder.path}/$libraryManifestName').writeAsStringSync('''
|
||||
items:
|
||||
- id: rain
|
||||
title: Rain
|
||||
category: sound
|
||||
language: en
|
||||
licence: CC0 1.0
|
||||
credit: Sample Recordist
|
||||
audio:
|
||||
- file: audio/rain.mp3
|
||||
duration: 60
|
||||
''');
|
||||
contents = readLibrary(folder);
|
||||
served = contents.items.single.audio.single.file;
|
||||
access = LibraryFileAccess();
|
||||
server =
|
||||
await (RelicApp()..get(
|
||||
'/library/**',
|
||||
(request) => serveLibraryFile(
|
||||
request,
|
||||
contents: contents,
|
||||
access: access,
|
||||
),
|
||||
))
|
||||
.serve(port: 0);
|
||||
http = HttpClient();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
http.close(force: true);
|
||||
await server.close();
|
||||
folder.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
/// Asks for [path] on the test server, with [headers].
|
||||
Future<HttpClientResponse> get(
|
||||
String path, {
|
||||
Map<String, String> headers = const {},
|
||||
}) async {
|
||||
final request = await http.getUrl(
|
||||
Uri.parse('http://localhost:${server.port}$path'),
|
||||
);
|
||||
headers.forEach(request.headers.set);
|
||||
return await request.close();
|
||||
}
|
||||
|
||||
String token() => access.issue(clock.now()).token;
|
||||
|
||||
test('serves a file to whoever holds an access the server signed, by '
|
||||
'ranges as players ask (ADR 0038, D16)', () async {
|
||||
final whole = await get('/library/$served?access=${token()}');
|
||||
expect(whole.statusCode, HttpStatus.ok);
|
||||
expect(whole.headers.value(HttpHeaders.acceptRangesHeader), 'bytes');
|
||||
expect(whole.headers.contentType?.mimeType, 'audio/mpeg');
|
||||
expect((await whole.toList()).expand((chunk) => chunk), hasLength(1000));
|
||||
|
||||
final part = await get(
|
||||
'/library/$served?access=${token()}',
|
||||
headers: {HttpHeaders.rangeHeader: 'bytes=10-19'},
|
||||
);
|
||||
expect(part.statusCode, HttpStatus.partialContent);
|
||||
expect(
|
||||
part.headers.value(HttpHeaders.contentRangeHeader),
|
||||
'bytes 10-19/1000',
|
||||
);
|
||||
expect((await part.toList()).expand((chunk) => chunk), [
|
||||
for (var i = 10; i < 20; i++) i,
|
||||
]);
|
||||
});
|
||||
|
||||
test('refuses a request without an access, with one not signed by this '
|
||||
'server, and with one expired', () async {
|
||||
final none = await get('/library/$served');
|
||||
expect(none.statusCode, HttpStatus.unauthorized);
|
||||
await none.drain<void>();
|
||||
|
||||
final forged = LibraryFileAccess().issue(clock.now()).token;
|
||||
final foreign = await get('/library/$served?access=$forged');
|
||||
expect(foreign.statusCode, HttpStatus.forbidden);
|
||||
await foreign.drain<void>();
|
||||
|
||||
final (:token, :until) = access.issue(clock.now());
|
||||
final altered = token.replaceFirst(
|
||||
RegExp(r'^\d+'),
|
||||
'${until.add(const Duration(days: 30)).millisecondsSinceEpoch ~/ 1000}',
|
||||
);
|
||||
final lengthened = await get('/library/$served?access=$altered');
|
||||
expect(lengthened.statusCode, HttpStatus.forbidden);
|
||||
await lengthened.drain<void>();
|
||||
|
||||
final old = access
|
||||
.issue(clock.now().subtract(LibraryFileAccess.lifetime * 2))
|
||||
.token;
|
||||
final expired = await get('/library/$served?access=$old');
|
||||
expect(expired.statusCode, HttpStatus.forbidden);
|
||||
await expired.drain<void>();
|
||||
});
|
||||
|
||||
test('serves only the files the manifest names, by their served names: '
|
||||
'a path of the folder, or one out of it, is no such name', () async {
|
||||
for (final path in [
|
||||
'/library/secret.txt',
|
||||
'/library/audio/rain.mp3',
|
||||
'/library/..%2Fsecret.txt',
|
||||
'/library/%2E%2E/%2E%2E/etc/passwd',
|
||||
'/library/${served.replaceFirst('.mp3', '.txt')}',
|
||||
]) {
|
||||
final response = await get('$path?access=${token()}');
|
||||
expect(response.statusCode, HttpStatus.notFound, reason: path);
|
||||
expect(
|
||||
utf8.decode((await response.toList()).expand((c) => c).toList()),
|
||||
isNot(contains('not served')),
|
||||
reason: path,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,295 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:halkyon_meditate_server/src/library/library_manifest.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
/// A neutral item of the manifest, as an operator would write it, with
|
||||
/// [changes] over it (null drops a field).
|
||||
String _item(String id, {Map<String, String?> changes = const {}}) {
|
||||
final fields = <String, String?>{
|
||||
'id': id,
|
||||
'title': 'Evening rest',
|
||||
'category': 'meditation',
|
||||
'description': 'A short sitting before sleep.',
|
||||
'speaker': 'Sample Speaker',
|
||||
'language': 'en',
|
||||
'licence': 'CC BY 4.0',
|
||||
'credit': 'Sample Speaker, for the example instance',
|
||||
...changes,
|
||||
};
|
||||
final audio =
|
||||
fields.remove('audio') ??
|
||||
' - file: audio/$id.mp3\n'
|
||||
' duration: 600\n';
|
||||
return [
|
||||
' - id: ${fields.remove('id')}',
|
||||
for (final MapEntry(:key, :value) in fields.entries)
|
||||
if (value != null) ' $key: $value',
|
||||
' audio:',
|
||||
audio.trimRight(),
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
void main() {
|
||||
late Directory folder;
|
||||
|
||||
setUp(() {
|
||||
folder = Directory.systemTemp.createTempSync('library');
|
||||
Directory('${folder.path}/audio').createSync();
|
||||
});
|
||||
tearDown(() => folder.deleteSync(recursive: true));
|
||||
|
||||
/// Writes [name] in the folder, of [bytes] bytes.
|
||||
void file(String name, {int bytes = 16}) => File('${folder.path}/$name')
|
||||
..createSync(recursive: true)
|
||||
..writeAsBytesSync(List.filled(bytes, 7));
|
||||
|
||||
/// Writes the manifest.
|
||||
void manifest(String yaml) =>
|
||||
File('${folder.path}/$libraryManifestName').writeAsStringSync(yaml);
|
||||
|
||||
test('reads items, their recordings and courses whose lessons point at '
|
||||
'them (ADR 0038, D16)', () {
|
||||
file('audio/evening.mp3', bytes: 1000);
|
||||
file('audio/evening-long.mp3', bytes: 2000);
|
||||
file('audio/rain.ogg');
|
||||
file('images/evening.jpg');
|
||||
manifest('''
|
||||
version: 1
|
||||
items:
|
||||
${_item('evening', changes: {'image': 'images/evening.jpg', 'audio': '''
|
||||
- file: audio/evening.mp3
|
||||
label: 10 minutes
|
||||
duration: "10:00"
|
||||
- file: audio/evening-long.mp3
|
||||
label: 20 minutes
|
||||
duration: 1200
|
||||
'''})}
|
||||
${_item('rain', changes: {'category': 'sound', 'speaker': null, 'audio': '''
|
||||
- file: audio/rain.ogg
|
||||
duration: 1800
|
||||
'''})}
|
||||
courses:
|
||||
- id: first-week
|
||||
title: A first week
|
||||
description: Seven evenings.
|
||||
category: meditation
|
||||
language: en
|
||||
lessons:
|
||||
- item: evening
|
||||
title: The first evening
|
||||
- item: rain
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.problems, isEmpty);
|
||||
expect(library.items.map((item) => item.id), ['evening', 'rain']);
|
||||
final evening = library.items.first;
|
||||
expect(evening.title, 'Evening rest');
|
||||
expect(evening.speaker, 'Sample Speaker');
|
||||
expect(evening.licence, 'CC BY 4.0');
|
||||
expect(evening.credit, 'Sample Speaker, for the example instance');
|
||||
expect(evening.audio.map((audio) => audio.durationSeconds), [600, 1200]);
|
||||
expect(evening.audio.map((audio) => audio.label), [
|
||||
'10 minutes',
|
||||
'20 minutes',
|
||||
]);
|
||||
expect(evening.audio.map((audio) => audio.size), [1000, 2000]);
|
||||
expect(library.items.last.speaker, isNull);
|
||||
// Served under names of their own, which say nothing of the folder.
|
||||
for (final audio in evening.audio) {
|
||||
expect(audio.file, matches(RegExp(r'^[0-9a-f]{24}\.mp3$')));
|
||||
expect(library.files[audio.file]!.path, endsWith('.mp3'));
|
||||
}
|
||||
expect(evening.image, matches(RegExp(r'^[0-9a-f]{24}\.jpg$')));
|
||||
expect(library.files, contains(evening.image));
|
||||
final course = library.courses.single;
|
||||
expect(course.id, 'first-week');
|
||||
expect(course.lessons.map((lesson) => lesson.item), ['evening', 'rain']);
|
||||
expect(course.lessons.first.title, 'The first evening');
|
||||
});
|
||||
|
||||
test('refuses an item without a licence or a credit, saying why, and '
|
||||
'serves the others', () {
|
||||
file('audio/a.mp3');
|
||||
file('audio/b.mp3');
|
||||
file('audio/c.mp3');
|
||||
manifest('''
|
||||
items:
|
||||
${_item('a', changes: {'licence': null})}
|
||||
${_item('b', changes: {'credit': '""'})}
|
||||
${_item('c')}
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.items.map((item) => item.id), ['c']);
|
||||
expect(library.problems, [
|
||||
allOf(contains('`a`'), contains('licence')),
|
||||
allOf(contains('`b`'), contains('credit')),
|
||||
]);
|
||||
});
|
||||
|
||||
test('refuses a recording outside the folder, however its path gets '
|
||||
'there (path traversal)', () {
|
||||
final outside = Directory.systemTemp.createTempSync('outside');
|
||||
addTearDown(() => outside.deleteSync(recursive: true));
|
||||
File('${outside.path}/secret.mp3').writeAsStringSync('not yours');
|
||||
Link('${folder.path}/audio/linked.mp3').createSync(
|
||||
'${outside.path}/secret.mp3',
|
||||
);
|
||||
file('audio/fine.mp3');
|
||||
manifest('''
|
||||
items:
|
||||
${_item('up', changes: {'audio': ' - file: ../${outside.uri.pathSegments.where((s) => s.isNotEmpty).last}/secret.mp3\n duration: 60\n'})}
|
||||
${_item('absolute', changes: {'audio': ' - file: ${outside.path}/secret.mp3\n duration: 60\n'})}
|
||||
${_item('linked', changes: {'audio': ' - file: audio/linked.mp3\n duration: 60\n'})}
|
||||
${_item('fine', changes: {'audio': ' - file: audio/fine.mp3\n duration: 60\n'})}
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.items.map((item) => item.id), ['fine']);
|
||||
expect(library.problems, hasLength(3));
|
||||
for (final problem in library.problems) {
|
||||
expect(problem, contains('outside the library'));
|
||||
}
|
||||
expect(
|
||||
library.files.values.map((file) => file.path),
|
||||
everyElement(isNot(contains('secret'))),
|
||||
);
|
||||
});
|
||||
|
||||
test('refuses an item whose recording is missing, has no length or is no '
|
||||
'sound, or whose id, category or language is wrong', () {
|
||||
file('audio/notes.txt');
|
||||
file('audio/ok.mp3');
|
||||
manifest('''
|
||||
items:
|
||||
${_item('missing', changes: {'audio': ' - file: audio/missing.mp3\n duration: 60\n'})}
|
||||
${_item('no-length', changes: {'audio': ' - file: audio/ok.mp3\n'})}
|
||||
${_item('text', changes: {'audio': ' - file: audio/notes.txt\n duration: 60\n'})}
|
||||
${_item('Not A Slug', changes: {'audio': ' - file: audio/ok.mp3\n duration: 60\n'})}
|
||||
${_item('odd-category', changes: {'category': 'cooking', 'audio': ' - file: audio/ok.mp3\n duration: 60\n'})}
|
||||
${_item('odd-language', changes: {'language': 'english please', 'audio': ' - file: audio/ok.mp3\n duration: 60\n'})}
|
||||
${_item('none', changes: {'audio': ''})}
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.items, isEmpty);
|
||||
expect(library.problems, [
|
||||
allOf(contains('`missing`'), contains('no file')),
|
||||
allOf(contains('`no-length`'), contains('duration')),
|
||||
allOf(contains('`text`'), contains('not a sound')),
|
||||
contains('id'),
|
||||
allOf(contains('`odd-category`'), contains('category')),
|
||||
allOf(contains('`odd-language`'), contains('language')),
|
||||
allOf(contains('`none`'), contains('recording')),
|
||||
]);
|
||||
});
|
||||
|
||||
test("takes the operator's own categories with their names, but none "
|
||||
"under Meditate's own keys", () {
|
||||
file('audio/a.mp3');
|
||||
manifest('''
|
||||
categories:
|
||||
- key: breathwork
|
||||
english: Breathwork
|
||||
german: Atemarbeit
|
||||
- key: sleep-story
|
||||
english: Bedtime
|
||||
german: Gute Nacht
|
||||
items:
|
||||
${_item('a', changes: {'category': 'breathwork', 'audio': ' - file: audio/a.mp3\n duration: 60\n'})}
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.categories.map((category) => category.key), [
|
||||
'breathwork',
|
||||
]);
|
||||
expect(library.categories.single.german, 'Atemarbeit');
|
||||
expect(library.items.single.category, 'breathwork');
|
||||
expect(library.problems.single, contains('sleep-story'));
|
||||
});
|
||||
|
||||
test('refuses a second item under an id taken, and a course pointing at '
|
||||
'an item not served', () {
|
||||
file('audio/a.mp3');
|
||||
manifest('''
|
||||
items:
|
||||
${_item('a', changes: {'audio': ' - file: audio/a.mp3\n duration: 60\n'})}
|
||||
${_item('a', changes: {'title': 'Again', 'audio': ' - file: audio/a.mp3\n duration: 60\n'})}
|
||||
courses:
|
||||
- id: broken
|
||||
title: Broken
|
||||
category: meditation
|
||||
language: en
|
||||
lessons:
|
||||
- item: a
|
||||
- item: gone
|
||||
- id: fine
|
||||
title: Fine
|
||||
category: meditation
|
||||
language: en
|
||||
lessons:
|
||||
- item: a
|
||||
''');
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.items.single.title, 'Evening rest');
|
||||
expect(library.courses.map((course) => course.id), ['fine']);
|
||||
expect(library.problems, [
|
||||
allOf(contains('`a`'), contains('taken')),
|
||||
allOf(contains('`broken`'), contains('`gone`')),
|
||||
]);
|
||||
});
|
||||
|
||||
test('a folder without a manifest is no library, and a manifest that '
|
||||
"isn't YAML says so", () {
|
||||
expect(readLibrary(folder).items, isEmpty);
|
||||
expect(readLibrary(folder).problems.single, contains('no library.yaml'));
|
||||
|
||||
manifest('items: [unclosed');
|
||||
expect(readLibrary(folder).items, isEmpty);
|
||||
expect(readLibrary(folder).problems.single, contains('YAML'));
|
||||
});
|
||||
|
||||
test('the fingerprint changes with the manifest and with a file, and '
|
||||
'only then', () {
|
||||
file('audio/a.mp3');
|
||||
manifest(
|
||||
'items:\n${_item('a', changes: {'audio': ' - file: audio/a.mp3\n duration: 60\n'})}\n',
|
||||
);
|
||||
final first = readLibrary(folder).fingerprint;
|
||||
expect(readLibrary(folder).fingerprint, first);
|
||||
|
||||
file('audio/a.mp3', bytes: 99);
|
||||
final second = readLibrary(folder).fingerprint;
|
||||
expect(second, isNot(first));
|
||||
|
||||
manifest(
|
||||
'items:\n${_item('a', changes: {'title': 'Changed', 'audio': ' - file: audio/a.mp3\n duration: 60\n'})}\n',
|
||||
);
|
||||
expect(readLibrary(folder).fingerprint, isNot(second));
|
||||
});
|
||||
|
||||
test('the example manifest in the repository passes, its files made up', () {
|
||||
final example = File('library.example.yaml').readAsStringSync();
|
||||
manifest(example);
|
||||
for (final match in RegExp(
|
||||
r'(?:file|image): ([\w/.-]+)',
|
||||
).allMatches(example)) {
|
||||
file(match.group(1)!);
|
||||
}
|
||||
|
||||
final library = readLibrary(folder);
|
||||
|
||||
expect(library.problems, isEmpty);
|
||||
expect(library.items, isNotEmpty);
|
||||
expect(library.courses, isNotEmpty);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue