129 lines
5.4 KiB
JavaScript
129 lines
5.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const {
|
|
acceptGoogleWorkspaceWelcome,
|
|
isGoogleWorkspaceTwoStepEnrollmentPrompt,
|
|
dismissGoogleWorkspaceTwoStepEnrollmentPrompt,
|
|
_private,
|
|
} = require('../src/services/google_workspace_welcome_service');
|
|
|
|
function makePage({ body, buttonText = 'I understand', url = 'https://accounts.google.com/signin/welcome', clickUrl = 'https://myaccount.google.com/' }) {
|
|
let currentUrl = url;
|
|
let clicked = false;
|
|
let scrolled = false;
|
|
const button = {
|
|
isVisible: async () => true,
|
|
scrollIntoViewIfNeeded: async () => { scrolled = true; },
|
|
click: async () => {
|
|
clicked = true;
|
|
currentUrl = clickUrl;
|
|
},
|
|
};
|
|
return {
|
|
page: {
|
|
url: () => currentUrl,
|
|
locator: selector => {
|
|
if (selector === 'body') return { innerText: async () => body };
|
|
const matches = selector.includes(buttonText);
|
|
return {
|
|
count: async () => matches ? 1 : 0,
|
|
nth: () => button,
|
|
};
|
|
},
|
|
waitForLoadState: async () => {},
|
|
waitForTimeout: async () => {},
|
|
},
|
|
state: () => ({ clicked, scrolled, currentUrl }),
|
|
};
|
|
}
|
|
|
|
(async () => {
|
|
const english = makePage({
|
|
body: 'Welcome to your new account\nYour organization administrator manages this account\nI understand',
|
|
});
|
|
assert.strictEqual(await acceptGoogleWorkspaceWelcome(english.page), true);
|
|
assert.deepStrictEqual(english.state(), {
|
|
clicked: true,
|
|
scrolled: true,
|
|
currentUrl: 'https://myaccount.google.com/',
|
|
});
|
|
|
|
const chinese = makePage({
|
|
body: '欢迎使用您的新账号\n您的组织管理员会管理此账号\n我明白了',
|
|
buttonText: '我明白了',
|
|
});
|
|
assert.strictEqual(await acceptGoogleWorkspaceWelcome(chinese.page), true);
|
|
assert.strictEqual(chinese.state().clicked, true);
|
|
|
|
const unrelated = makePage({ body: 'Google OAuth consent\nI understand' });
|
|
assert.strictEqual(await acceptGoogleWorkspaceWelcome(unrelated.page), false, '普通授权页不得误点');
|
|
assert.strictEqual(unrelated.state().clicked, false);
|
|
|
|
const nonGoogle = makePage({
|
|
body: 'Welcome to your new account\nI understand',
|
|
url: 'https://example.com/welcome',
|
|
});
|
|
assert.strictEqual(await acceptGoogleWorkspaceWelcome(nonGoogle.page), false, '非 Google 页面不得处理');
|
|
|
|
const twoStepPrompt = makePage({
|
|
body: 'Đừng để tài khoản bị khóa\nXác minh 2 bước\nĐĂNG KÝ',
|
|
buttonText: 'ĐĂNG KÝ',
|
|
});
|
|
assert.strictEqual(await isGoogleWorkspaceTwoStepEnrollmentPrompt(twoStepPrompt.page), true);
|
|
assert.strictEqual(twoStepPrompt.state().clicked, false, '两步验证提醒页只识别,不得点击注册');
|
|
|
|
const twoStepInterstitial = makePage({
|
|
body: '',
|
|
url: 'https://myaccount.google.com/interstitials/twosvrequired?hl=vi&continue=https://accounts.google.com/',
|
|
});
|
|
assert.strictEqual(
|
|
await isGoogleWorkspaceTwoStepEnrollmentPrompt(twoStepInterstitial.page),
|
|
true,
|
|
'myaccount twosvrequired 应按 URL 直接识别,不依赖正文是否已加载',
|
|
);
|
|
assert.strictEqual(twoStepInterstitial.state().clicked, false, 'twosvrequired 页面不得点击注册');
|
|
|
|
const vietnameseLater = makePage({
|
|
body: 'Đừng để tài khoản bị khóa\nXác minh 2 bước\nĐĂNG KÝ\nThực hiện sau',
|
|
buttonText: 'Thực hiện sau',
|
|
url: 'https://myaccount.google.com/interstitials/twosvrequired?hl=vi&continue=https://accounts.google.com/',
|
|
clickUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
});
|
|
assert.deepStrictEqual(
|
|
await dismissGoogleWorkspaceTwoStepEnrollmentPrompt(vietnameseLater.page),
|
|
{ detected: true, dismissed: true },
|
|
);
|
|
assert.strictEqual(vietnameseLater.state().clicked, true, '必须点击“Thực hiện sau”,不能反复打开 OAuth 链接');
|
|
assert.strictEqual(vietnameseLater.state().currentUrl, 'https://accounts.google.com/o/oauth2/v2/auth');
|
|
|
|
const englishThisLater = makePage({
|
|
body: 'Your domain will soon enforce 2-Step Verification\nENROLL\nDo this later',
|
|
buttonText: 'Do this later',
|
|
url: 'https://myaccount.google.com/interstitials/twosvrequired?hl=en&continue=https://accounts.google.com/',
|
|
clickUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
});
|
|
assert.deepStrictEqual(
|
|
await dismissGoogleWorkspaceTwoStepEnrollmentPrompt(englishThisLater.page),
|
|
{ detected: true, dismissed: true },
|
|
);
|
|
assert.strictEqual(englishThisLater.state().clicked, true, '英文页面必须点击“Do this later”,不得点击 ENROLL');
|
|
|
|
assert(_private.WELCOME_TEXT_PATTERN.test('Welcome to your new account'));
|
|
const root = path.join(__dirname, '..');
|
|
const loginSource = fs.readFileSync(path.join(root, 'src', 'services', 'login_service.js'), 'utf8');
|
|
const oauthSource = fs.readFileSync(path.join(root, 'src', 'services', 'ag_verify_service.js'), 'utf8');
|
|
assert.match(loginSource, /acceptGoogleWorkspaceWelcome\(page, logCallback\)/);
|
|
assert.match(oauthSource, /acceptGoogleWorkspaceWelcome\(page, _log\)/);
|
|
assert.match(loginSource, /dismissGoogleWorkspaceTwoStepEnrollmentPrompt\(page, logCallback\)/);
|
|
assert.match(oauthSource, /dismissGoogleWorkspaceTwoStepEnrollmentPrompt\(page, _log\)/);
|
|
assert.doesNotMatch(oauthSource, /acceptGoogleWorkspaceTwoStepEnrollment/);
|
|
|
|
console.log('google_workspace_welcome.test.js: PASS');
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|