315 lines
11 KiB
Dart
315 lines
11 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
|
|
import 'package:hepan_everyday/features/school/school_session.dart';
|
|
import 'package:hepan_everyday/features/school/school_exam_adapter.dart';
|
|
|
|
import '../support/fake_school_web.dart';
|
|
import 'exam_adapter_test.dart' show examHtml;
|
|
import 'recognized_timetable_test.dart' show fixture;
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
late FakeSchoolPlatform platform;
|
|
late SchoolSession session;
|
|
setUp(() {
|
|
platform = FakeSchoolPlatform();
|
|
WebViewPlatform.instance = platform;
|
|
session = SchoolSession();
|
|
});
|
|
tearDown(() => session.dispose());
|
|
void navigate(String url) {
|
|
platform.web.url = url;
|
|
platform.delegate.started(url);
|
|
platform.delegate.finished(url);
|
|
}
|
|
|
|
test('graduate logout function automatically opens timetable without an exit anchor', () async {
|
|
await session.open();
|
|
platform.web.javascript = (script) async =>
|
|
script.contains("typeof window.logout")
|
|
? schoolPageEvidence(topUrl: platform.web.url!, authenticated: true)
|
|
: false;
|
|
navigate('http://yjsjy.uestc.edu.cn/pyxx/home/index');
|
|
await flushSchoolEvents();
|
|
expect(platform.web.loads.last, SchoolSession.timetableUrl.toString());
|
|
});
|
|
|
|
test('manual graduate page reads recognized embedded timetable instead of restarting login', () async {
|
|
await session.open();
|
|
const home = 'https://yjsjy.uestc.edu.cn/pyxx/home/index';
|
|
platform.web.url = home;
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: home,
|
|
html: fixture(),
|
|
documentUrl: SchoolSession.timetableUrl.toString(),
|
|
);
|
|
final draft = await session.readTimetable();
|
|
expect(draft.courses.length, 4);
|
|
expect(platform.web.loads.length, 1);
|
|
});
|
|
|
|
test(
|
|
'closed authentication screen ignores later page-finished callbacks',
|
|
() async {
|
|
await session.open();
|
|
session.setReadingActive(true);
|
|
session.setReadingActive(false);
|
|
platform.web.javascript = (_) async =>
|
|
schoolPageEvidence(topUrl: platform.web.url!, html: fixture());
|
|
navigate(SchoolSession.timetableUrl.toString());
|
|
await flushSchoolEvents();
|
|
expect(session.pendingDraft, isNull);
|
|
expect(session.finding, isFalse);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'asynchronous timetable DOM is imported without another navigation',
|
|
() async {
|
|
await session.open();
|
|
session.setReadingActive(true);
|
|
var ready = false;
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: platform.web.url!,
|
|
html: ready ? fixture() : null,
|
|
);
|
|
navigate(SchoolSession.timetableUrl.toString());
|
|
await flushSchoolEvents();
|
|
expect(session.pendingDraft, isNull);
|
|
expect(session.finding, isTrue);
|
|
ready = true;
|
|
await Future<void>.delayed(const Duration(milliseconds: 850));
|
|
await flushSchoolEvents();
|
|
expect(session.pendingDraft!.courses.length, 4);
|
|
expect(platform.web.loads.length, 1);
|
|
},
|
|
);
|
|
|
|
test('stale script response cannot cancel polling on a newer page', () async {
|
|
await session.open();
|
|
session.setReadingActive(true);
|
|
final old = Completer<Object>();
|
|
platform.web.javascript = (_) => old.future;
|
|
navigate('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
|
await flushSchoolEvents();
|
|
var ready = false;
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: platform.web.url!,
|
|
html: ready ? fixture() : null,
|
|
);
|
|
navigate(SchoolSession.timetableUrl.toString());
|
|
await flushSchoolEvents();
|
|
old.complete(
|
|
schoolPageEvidence(
|
|
topUrl: 'https://yjsjy.uestc.edu.cn/pyxx/home/index',
|
|
authenticated: true,
|
|
html: fixture(),
|
|
),
|
|
);
|
|
await flushSchoolEvents();
|
|
ready = true;
|
|
await Future<void>.delayed(const Duration(milliseconds: 850));
|
|
await flushSchoolEvents();
|
|
expect(session.pendingDraft?.courses.length, 4);
|
|
});
|
|
|
|
test('external document evidence cannot become a school import', () async {
|
|
await session.open();
|
|
platform.web.url = SchoolSession.timetableUrl.toString();
|
|
for (final url in [
|
|
'https://evil.test/pyxx/pygl/xskbcx/index/1',
|
|
'https://yjsjy.uestc.edu.cn:444/pyxx/pygl/xskbcx/index/1',
|
|
'http://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
|
'https://user@yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
|
]) {
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: platform.web.url!,
|
|
documentUrl: url,
|
|
html: fixture(),
|
|
);
|
|
await expectLater(session.readTimetable(), throwsA(isA<Exception>()));
|
|
}
|
|
expect(session.pendingDraft, isNull);
|
|
});
|
|
|
|
test('read-current recovery imports a frame and never reloads SSO', () async {
|
|
await session.open();
|
|
const home = 'http://yjsjy.uestc.edu.cn/pyxx/home/index';
|
|
platform.web.url = home;
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: home,
|
|
documentUrl: home,
|
|
authenticated: true,
|
|
html: fixture(),
|
|
);
|
|
await session.readCurrentPage();
|
|
expect(session.pendingDraft?.courses.length, 4);
|
|
expect(platform.web.loads.length, 1);
|
|
});
|
|
|
|
test(
|
|
'authenticated graduate root dashboard automatically opens timetable',
|
|
() async {
|
|
await session.open();
|
|
const home = 'http://yjsjy.uestc.edu.cn/pyxx?targetUrl=-1';
|
|
final decision = await platform.delegate.navigation(
|
|
const NavigationRequest(url: home, isMainFrame: true),
|
|
);
|
|
expect(decision, NavigationDecision.navigate);
|
|
platform.web.javascript = (_) async =>
|
|
schoolPageEvidence(topUrl: home, authenticated: true);
|
|
navigate(home);
|
|
await flushSchoolEvents();
|
|
expect(platform.web.loads.last, SchoolSession.timetableUrl.toString());
|
|
},
|
|
);
|
|
|
|
test(
|
|
'obsolete automatic request failure preserves a newer pending import',
|
|
() async {
|
|
await session.open();
|
|
final oldRequest = Completer<void>();
|
|
platform.web.request = (_) => oldRequest.future;
|
|
platform.web.javascript = (_) async =>
|
|
schoolPageEvidence(topUrl: platform.web.url!, authenticated: true);
|
|
navigate('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
|
await flushSchoolEvents();
|
|
platform.web.javascript = (_) async =>
|
|
schoolPageEvidence(topUrl: platform.web.url!, html: fixture());
|
|
navigate(SchoolSession.timetableUrl.toString());
|
|
await flushSchoolEvents();
|
|
final fresh = session.pendingDraft;
|
|
expect(fresh, isNotNull);
|
|
oldRequest.completeError(StateError('obsolete request failure'));
|
|
await flushSchoolEvents();
|
|
expect(session.pendingDraft, same(fresh));
|
|
expect(session.message, '读取成功,正在保存…');
|
|
},
|
|
);
|
|
|
|
test('actual session follows service login then reads exam HTML', () async {
|
|
await session.openExams();
|
|
expect(platform.web.loads.single, SchoolSession.authUrl.toString());
|
|
navigate('https://idas.uestc.edu.cn/authserver/login?service=graduate');
|
|
await flushSchoolEvents();
|
|
expect(session.connectionState, SchoolConnectionState.authenticating);
|
|
expect(session.finding, isFalse);
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: platform.web.url!,
|
|
authenticated: true,
|
|
html: platform.web.url == SchoolSession.examUrl.toString()
|
|
? examHtml()
|
|
: null,
|
|
kind: 'exams',
|
|
);
|
|
navigate('http://yjsjy.uestc.edu.cn/pyxx/home/index');
|
|
await flushSchoolEvents();
|
|
expect(platform.web.loads.last, SchoolSession.examUrl.toString());
|
|
expect(session.finding, isTrue);
|
|
navigate(SchoolSession.examUrl.toString());
|
|
await flushSchoolEvents();
|
|
expect(session.pendingExams!.records.single.name, '矩阵理论');
|
|
expect(session.connectionState, SchoolConnectionState.connected);
|
|
});
|
|
test(
|
|
'obsolete main-frame error cannot interrupt a newer exam read',
|
|
() async {
|
|
await session.openExams();
|
|
platform.web.javascript = (_) async => schoolPageEvidence(
|
|
topUrl: platform.web.url!,
|
|
html: examHtml(),
|
|
kind: 'exams',
|
|
);
|
|
navigate(SchoolSession.examUrl.toString());
|
|
await flushSchoolEvents();
|
|
platform.delegate.resourceError(
|
|
const WebResourceError(
|
|
errorCode: -2,
|
|
description: 'old request',
|
|
isForMainFrame: true,
|
|
url: 'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
|
),
|
|
);
|
|
await flushSchoolEvents();
|
|
expect(session.finding, isTrue);
|
|
expect(session.message, '读取成功,正在保存…');
|
|
},
|
|
);
|
|
test(
|
|
'open waits for logout so old clearing cannot blank the new login',
|
|
() async {
|
|
await session.open();
|
|
platform.cookies.clearing = Completer<bool>();
|
|
final closing = session.logout();
|
|
await flushSchoolEvents();
|
|
final reopening = session.openExams();
|
|
await flushSchoolEvents();
|
|
platform.cookies.clearing!.complete(true);
|
|
await closing;
|
|
await reopening;
|
|
expect(session.hasOpened, isTrue);
|
|
expect(platform.web.loads.last, SchoolSession.authUrl.toString());
|
|
expect(session.requestingExams, isTrue);
|
|
},
|
|
);
|
|
test(
|
|
'acknowledging an old saved draft preserves a newer pending import',
|
|
() async {
|
|
await session.openExams();
|
|
final old = SchoolExamAdapter.extract(examHtml());
|
|
session.pendingExams = old;
|
|
final generation = session.pageGeneration;
|
|
await session.openExams();
|
|
final newer = SchoolExamAdapter.extract(examHtml(room: '品学楼A101'));
|
|
session.pendingExams = newer;
|
|
expect(
|
|
session.saved(success: true, generation: generation, exams: old),
|
|
isFalse,
|
|
);
|
|
expect(session.pendingExams, same(newer));
|
|
expect(
|
|
session.saved(
|
|
success: true,
|
|
generation: session.pageGeneration,
|
|
exams: newer,
|
|
),
|
|
isTrue,
|
|
);
|
|
expect(session.pendingExams, isNull);
|
|
},
|
|
);
|
|
test(
|
|
'native logout failure is reported and subsequent login can recover',
|
|
() async {
|
|
await session.open();
|
|
session.connectionState = SchoolConnectionState.connected;
|
|
platform.cookies.clearing = Completer<bool>();
|
|
final closing = session.logout();
|
|
final result = closing.then((_) => true, onError: (_) => false);
|
|
platform.cookies.clearing!.completeError(
|
|
StateError('cookie clearing failed'),
|
|
);
|
|
expect(await result, isTrue);
|
|
expect(session.connectionState, isNot(SchoolConnectionState.connected));
|
|
expect(session.message, contains('退出未完成'));
|
|
await session.openExams();
|
|
expect(platform.web.loads.last, SchoolSession.authUrl.toString());
|
|
},
|
|
);
|
|
test('university HTTP identity redirect upgrades to HTTPS with service preserved', () async {
|
|
await session.open();
|
|
const redirect =
|
|
'http://idas.uestc.edu.cn/authserver/login?service=http%3A%2F%2Fyjsjy.uestc.edu.cn%2Fpyxx%2Fjzsso%2Flogin';
|
|
final decision = await platform.delegate.navigation(
|
|
const NavigationRequest(url: redirect, isMainFrame: true),
|
|
);
|
|
expect(decision, NavigationDecision.prevent);
|
|
final opened = Uri.parse(platform.web.loads.last);
|
|
expect(opened.host, 'idas.uestc.edu.cn');
|
|
expect(opened.scheme, 'https');
|
|
expect(opened.query, Uri.parse(redirect).query);
|
|
});
|
|
}
|