Add baseline unit tests and fix home server normalization

This commit is contained in:
Foxyz committed 2026-02-20 17:45:10 +03:00
1 parent a93d619b4c
commit ce7c97d13a
6 files changed
+227 -8

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
import 'package:chat_server/src/handlers/auth_handlers.dart';
import 'package:test/test.dart';
void main() {
group('parseUserId', () {
test('parses valid userId', () {
final parsed = parseUserId('alice@example.com');
expect(parsed, isNotNull);
expect(parsed!.username, 'alice');
expect(parsed.host, 'example.com');
});
test('returns null for invalid userId', () {
expect(parseUserId('alice'), isNull);
expect(parseUserId('alice@'), isNull);
expect(parseUserId('@example.com'), isNull);
expect(parseUserId('alice@example.com@x'), isNull);
});
});
group('defaultSchemeForHost', () {
test('returns http for localhost-style hosts', () {
expect(defaultSchemeForHost('localhost:9870'), 'http');
expect(defaultSchemeForHost('127.0.0.1:9870'), 'http');
expect(defaultSchemeForHost('[::1]:9870'), 'http');
});
test('returns https for non-local hosts', () {
expect(defaultSchemeForHost('chat.example.com'), 'https');
});
});
group('normalizeHomeServer', () {
test('normalizes host without scheme', () {
expect(
normalizeHomeServer('chat.example.com'),
'https://chat.example.com',
);
expect(
normalizeHomeServer('localhost:9870'),
'http://localhost:9870',
);
});
test('strips trailing slash/path/query/fragment', () {
expect(
normalizeHomeServer('https://example.com/a/b?x=1#y'),
'https://example.com',
);
expect(
normalizeHomeServer('https://example.com/'),
'https://example.com',
);
});
test('returns null for invalid input', () {
expect(normalizeHomeServer(''), isNull);
expect(normalizeHomeServer('://bad'), isNull);
});
});
group('homeServerMatchesUserIdHost', () {
test('matches host and explicit port', () {
expect(
homeServerMatchesUserIdHost(
'https://chat.example.com:8443', 'chat.example.com:8443'),
isTrue,
);
expect(
homeServerMatchesUserIdHost(
'https://chat.example.com:8443', 'chat.example.com'),
isFalse,
);
});
test('matches default ports when omitted', () {
expect(
homeServerMatchesUserIdHost(
'https://chat.example.com', 'chat.example.com:443'),
isTrue,
);
expect(
homeServerMatchesUserIdHost(
'http://chat.example.com', 'chat.example.com:80'),
isTrue,
);
});
});
}
+42
View File
@@ -0,0 +1,42 @@
import 'package:chat_server/src/auth/crypto_service.dart';
import 'package:test/test.dart';
void main() {
group('AuthService password hashing', () {
test('hash + verify works', () {
const password = 'pass1234';
final hash = AuthService.hashPassword(password);
expect(hash, isNotEmpty);
expect(AuthService.verifyPassword(password, hash), isTrue);
expect(AuthService.verifyPassword('wrong-password', hash), isFalse);
});
});
group('CryptoService signing', () {
test('signatures verify with matching keypair', () {
final keyPair = CryptoService.generateKeyPair();
const payload = 'alice@localhost:9870:1700000000';
final signature = CryptoService.sign(payload, keyPair.privateKey);
final ok = CryptoService.verify(payload, signature, keyPair.publicKey);
expect(ok, isTrue);
});
test('signature fails for modified payload', () {
final keyPair = CryptoService.generateKeyPair();
const payload = 'payload-a';
const tamperedPayload = 'payload-b';
final signature = CryptoService.sign(payload, keyPair.privateKey);
final ok = CryptoService.verify(
tamperedPayload,
signature,
keyPair.publicKey,
);
expect(ok, isFalse);
});
});
}