Files
chat-server/test/auth_helpers_test.dart
T

90 lines
2.5 KiB
Dart

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,
);
});
});
}