fix: automate graduate timetable read and refine blue glass Dock
This commit is contained in:
@@ -2,6 +2,26 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hepan_everyday/features/school/school_login_flow.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'repeated school login bounces have a bounded automatic redirect budget',
|
||||
() {
|
||||
final flow = SchoolLoginFlow();
|
||||
final target = Uri.parse(
|
||||
'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
);
|
||||
final login = Uri.parse('https://idas.uestc.edu.cn/authserver/login');
|
||||
final home = Uri.parse('https://yjsjy.uestc.edu.cn/pyxx/home/index');
|
||||
flow.begin(target);
|
||||
for (var i = 0; i < 2; i++) {
|
||||
flow.onLoaded(login, authenticated: false);
|
||||
expect(flow.onLoaded(home, authenticated: true), target);
|
||||
}
|
||||
flow.onLoaded(login, authenticated: false);
|
||||
expect(flow.onLoaded(home, authenticated: true), isNull);
|
||||
flow.begin(target);
|
||||
expect(flow.onLoaded(home, authenticated: true), target);
|
||||
},
|
||||
);
|
||||
test('wait for student authentication then request timetable once', () {
|
||||
final flow = SchoolLoginFlow();
|
||||
final target = Uri.parse(
|
||||
|
||||
@@ -2,6 +2,31 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hepan_everyday/features/school/school_navigation_policy.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'official HTTP graduate child frames can load for same-origin DOM reading',
|
||||
() {
|
||||
expect(
|
||||
SchoolNavigationPolicy.canNavigate(
|
||||
Uri.parse('http://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1'),
|
||||
isMainFrame: false,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
for (final value in [
|
||||
'http://evil.test/pyxx/pygl/xskbcx/index/1',
|
||||
'http://yjsjy.uestc.edu.cn:81/pyxx/pygl/xskbcx/index/1',
|
||||
'http://user@yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1',
|
||||
]) {
|
||||
expect(
|
||||
SchoolNavigationPolicy.canNavigate(
|
||||
Uri.parse(value),
|
||||
isMainFrame: false,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
test('secure captcha frames are allowed without authorizing credential injection', () {
|
||||
final captcha = Uri.parse('https://captcha.example.test/challenge');
|
||||
expect(
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// Run with Node: exercises the exact WebView script without executing school HTML scripts.
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
const source = fs.readFileSync(path.join(__dirname, '../../lib/features/school/school_page_probe.dart'), 'utf8');
|
||||
const script = source.match(/schoolPageProbeScript = r'''([\s\S]*?)''';/)[1];
|
||||
const home = 'https://yjsjy.uestc.edu.cn/pyxx/home/index';
|
||||
const timetable = 'https://yjsjy.uestc.edu.cn/pyxx/pygl/xskbcx/index/1';
|
||||
|
||||
function term(id) {
|
||||
const result = {
|
||||
selectedIndex: 1,
|
||||
options: [false, false].map(selected => ({
|
||||
selected,
|
||||
setAttribute() { this.selected = true; },
|
||||
removeAttribute() { this.selected = false; },
|
||||
})),
|
||||
cloneNode() { return term(id); },
|
||||
get outerHTML() {
|
||||
return `<select id="${id}">${this.options.map((o, i) =>
|
||||
`<option value="${i}"${o.selected ? ' selected="selected"' : ''}>Term ${i}</option>`).join('')}</select>`;
|
||||
},
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
function doc(url, { password = false, chrome = false, table = false, exams = false, frames = [] } = {}) {
|
||||
return {
|
||||
location: new URL(url),
|
||||
querySelector(selector) {
|
||||
if (selector === 'input[type="password"]') return password ? {} : null;
|
||||
if (selector === '#navbar' || selector === '#main-container') return chrome ? {} : null;
|
||||
if (selector === '#tbl') return table ? {outerHTML: '<table id="tbl">synthetic timetable</table>'} : null;
|
||||
if (selector === '#drpxq') return table ? term('drpxq') : null;
|
||||
if (selector === '#sample-table-1') return exams ? {outerHTML: '<table id="sample-table-1">synthetic exams</table>'} : null;
|
||||
if (selector === '#xqbh') return exams ? term('xqbh') : null;
|
||||
return null;
|
||||
},
|
||||
querySelectorAll(selector) { return selector === 'iframe,frame' ? frames : []; },
|
||||
};
|
||||
}
|
||||
|
||||
function probe(document, logout = () => {}) {
|
||||
return JSON.parse(vm.runInNewContext(script, {
|
||||
URL, document, location: document.location, window: {logout},
|
||||
}));
|
||||
}
|
||||
|
||||
assert.equal(probe(doc(home)).authenticated, true, 'real logout function on known graduate home');
|
||||
assert.equal(probe(doc(timetable, {chrome: true})).authenticated, true, 'graduate shell plus logout');
|
||||
assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx?targetUrl=-1', {chrome: true})).authenticated, true, 'graduate root shell');
|
||||
assert.equal(probe(doc(home), undefined).authenticated, true); // default function
|
||||
assert.equal(probe(doc(home), null).authenticated, false, 'home URL alone is insufficient');
|
||||
assert.equal(probe(doc('https://yjsjy.uestc.edu.cn/pyxx/jzsso/login')).authenticated, false, 'callback alone is insufficient');
|
||||
|
||||
const top = probe(doc(timetable, {table: true}));
|
||||
assert.equal(top.pages.length, 1);
|
||||
assert.match(top.pages[0].html, /value="1" selected="selected"/);
|
||||
assert.doesNotMatch(top.pages[0].html, /value="0" selected/);
|
||||
|
||||
const inaccessible = {};
|
||||
Object.defineProperty(inaccessible, 'contentDocument', {get() { throw new Error('cross-origin'); }});
|
||||
const embedded = probe(doc(home, {frames: [
|
||||
inaccessible,
|
||||
{contentDocument: doc('https://captcha.example.test/pyxx/timetable', {table: true})},
|
||||
{contentDocument: doc('http://yjsjy.uestc.edu.cn/pyxx/timetable', {table: true})},
|
||||
{contentDocument: doc(timetable, {password: true, table: true})},
|
||||
{contentDocument: doc(timetable, {frames: [{contentDocument: doc(timetable, {table: true})}]})},
|
||||
]}));
|
||||
assert.equal(embedded.pages.length, 1, 'only nested same-origin graduate timetable is captured');
|
||||
assert.equal(embedded.pages[0].url, timetable);
|
||||
assert.equal(probe(doc(home, {password: true, table: true})).pages.length, 0);
|
||||
assert.equal(probe(doc('https://idas.uestc.edu.cn/authserver/login', {table: true})).pages.length, 0);
|
||||
for (const url of [
|
||||
'https://yjsjy.uestc.edu.cn.evil.test/pyxx/home/index',
|
||||
'https://yjsjy.uestc.edu.cn:444/pyxx/home/index',
|
||||
'https://user@yjsjy.uestc.edu.cn/pyxx/home/index',
|
||||
'file:///pyxx/home/index',
|
||||
]) assert.equal(probe(doc(url, {table: true})).pages.length, 0);
|
||||
assert.equal(probe(doc('http://yjsjy.uestc.edu.cn/pyxx/home/index', {table: true})).pages.length, 1);
|
||||
assert.equal(probe(doc(home, {exams: true})).pages[0].kind, 'exams');
|
||||
console.log('School WebView probe: authentication, table selection, frames, password exclusion, origins passed.');
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
|
||||
@@ -8,6 +7,7 @@ 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();
|
||||
@@ -25,6 +25,170 @@ void main() {
|
||||
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());
|
||||
@@ -32,8 +196,14 @@ void main() {
|
||||
await flushSchoolEvents();
|
||||
expect(session.connectionState, SchoolConnectionState.authenticating);
|
||||
expect(session.finding, isFalse);
|
||||
platform.web.javascript = (script) async =>
|
||||
script.contains('outerHTML') ? jsonEncode(examHtml()) : true;
|
||||
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());
|
||||
@@ -47,7 +217,11 @@ void main() {
|
||||
'obsolete main-frame error cannot interrupt a newer exam read',
|
||||
() async {
|
||||
await session.openExams();
|
||||
platform.web.javascript = (_) async => jsonEncode(examHtml());
|
||||
platform.web.javascript = (_) async => schoolPageEvidence(
|
||||
topUrl: platform.web.url!,
|
||||
html: examHtml(),
|
||||
kind: 'exams',
|
||||
);
|
||||
navigate(SchoolSession.examUrl.toString());
|
||||
await flushSchoolEvents();
|
||||
platform.delegate.resourceError(
|
||||
|
||||
Reference in New Issue
Block a user