Files
pipelines/app/test/features/chat/files/files_upload_elevation_test.dart
T
alan 9702d68d2d Ask for the password once and retry when a machine requires elevation
Carry the protocol error code, wrap each machine's gateway so an
elevation_required refusal shows one native password dialog, elevates that
connection and retries; send the upload token with file uploads; add a Settings
sign-in security section with the weak-password warning and sign out
everywhere; accept the __Host- session cookie.
2026-11-10 01:21:28 +00:00

124 lines
3.3 KiB
Dart

import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/elevation/machine_elevation.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/files/files_upload.dart';
import '../../../core/servers/elevation/elevating_gateway_test.dart'
show FakeMachine;
void main() {
late FilesUploadPost original;
setUp(() => original = postFileUpload);
tearDown(() => postFileUpload = original);
final file = ChatPickedFile(
name: 'notes.txt',
mime: 'text/plain',
bytes: Uint8List.fromList([1, 2, 3]),
);
const refusal = (
status: 403,
body: '{"code":"elevation_required","error":"Confirm your password."}',
);
const stored = (status: 200, body: '{"path":"/home/demo/notes.txt"}');
test(
'a refused upload elevates its machine and retries with the token',
() async {
final machine = FakeMachine();
var prompts = 0;
final elevation = MachineElevation(
name: 'laptop',
gateway: machine,
prompt: (m) async {
prompts++;
return await m.submit('right') == null;
},
);
final sent = <Map<String, String>>[];
postFileUpload =
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
sent.add(headers);
return headers['X-Pipelines-Elevation'] == 'tok-1'
? stored
: refusal;
};
final path = await uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
);
expect(path, '/home/demo/notes.txt');
expect(prompts, 1);
expect(sent, [
<String, String>{},
{'X-Pipelines-Elevation': 'tok-1'},
]);
// A live elevation rides on the next upload without asking again.
await uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
);
expect(prompts, 1);
expect(sent.last, {'X-Pipelines-Elevation': 'tok-1'});
},
);
test('a cancelled confirmation reports the refusal', () async {
final machine = FakeMachine();
final elevation = MachineElevation(
name: 'laptop',
gateway: machine,
prompt: (_) async => false,
);
var posts = 0;
postFileUpload =
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
posts++;
return refusal;
};
await expectLater(
uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
),
throwsA(
isA<FilesUploadException>().having(
(e) => e.message,
'message',
contains('confirm the password'),
),
),
);
expect(posts, 1);
});
test('refusal detection needs the typed code', () {
expect(refusedForElevation(refusal), isTrue);
expect(
refusedForElevation((status: 403, body: 'folder access denied')),
isFalse,
);
expect(refusedForElevation((status: 200, body: refusal.body)), isFalse);
});
}