95 lines
3.4 KiB
JavaScript
95 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
const db = require('../src/core/database');
|
|
const accountsRouter = require('../src/routes/accounts');
|
|
|
|
function routeHandler(routePath) {
|
|
const layer = accountsRouter.stack.find(item => (
|
|
item.route && item.route.path === routePath && item.route.methods.get
|
|
));
|
|
assert(layer, `未找到 GET ${routePath}`);
|
|
return layer.route.stack[0].handle;
|
|
}
|
|
|
|
function invoke(handler, params = {}, remoteAddress = '127.0.0.1') {
|
|
const response = {
|
|
statusCode: 200,
|
|
headers: {},
|
|
body: null,
|
|
status(code) { this.statusCode = code; return this; },
|
|
set(name, value) { this.headers[String(name).toLowerCase()] = value; return this; },
|
|
json(body) { this.body = body; return this; },
|
|
};
|
|
handler({ params, socket: { remoteAddress } }, response);
|
|
return response;
|
|
}
|
|
|
|
(async () => {
|
|
const originalGetAccount = db.getAccount;
|
|
const originalGetAllAccounts = db.getAllAccounts;
|
|
try {
|
|
const account = {
|
|
email: 'ag-credential@example.com',
|
|
ag_status: '凭证已获取',
|
|
ag_refresh_token: 'refresh-token-for-test',
|
|
};
|
|
db.getAccount = () => account;
|
|
db.getAllAccounts = () => [account];
|
|
|
|
const list = invoke(routeHandler('/'));
|
|
assert.strictEqual(list.body.data[0].has_ag_credential, true);
|
|
|
|
const local = invoke(routeHandler('/:email/ag-credential'), {
|
|
email: encodeURIComponent(account.email),
|
|
});
|
|
assert.strictEqual(local.statusCode, 200);
|
|
assert.strictEqual(local.headers['cache-control'], 'no-store');
|
|
assert.strictEqual(local.body.data.type, 'authorized_user');
|
|
assert.strictEqual(local.body.data.refresh_token, account.ag_refresh_token);
|
|
assert(local.body.data.client_id);
|
|
assert(local.body.data.client_secret);
|
|
|
|
const remote = invoke(routeHandler('/:email/ag-credential'), {
|
|
email: encodeURIComponent(account.email),
|
|
}, '192.0.2.25');
|
|
assert.strictEqual(remote.statusCode, 403);
|
|
assert(!JSON.stringify(remote.body).includes(account.ag_refresh_token));
|
|
|
|
const appSource = fs.readFileSync(path.join(__dirname, '..', 'public', 'app.js'), 'utf8');
|
|
const html = fs.readFileSync(path.join(__dirname, '..', 'public', 'index.html'), 'utf8');
|
|
assert.match(html, /<th>授权JSON<\/th>\s*<th>AC凭证<\/th>/);
|
|
assert.match(html, /openAgCredential\(account\)/);
|
|
|
|
let clipboardText = '';
|
|
const context = vm.createContext({
|
|
console, Set, Map, Date, JSON, Math, String, Object, Array, RegExp,
|
|
setTimeout, clearTimeout,
|
|
navigator: { clipboard: { writeText: async text => { clipboardText = text; } } },
|
|
fetch: async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ success: true, data: local.body.data }),
|
|
}),
|
|
});
|
|
vm.runInContext(`${appSource}\nthis.__geminiApp = geminiApp;`, context, { filename: 'public/app.js' });
|
|
const app = context.__geminiApp();
|
|
await app.openAgCredential(account);
|
|
assert.strictEqual(app.showAgCredentialModal, true);
|
|
assert.strictEqual(JSON.parse(app.agCredentialText).refresh_token, account.ag_refresh_token);
|
|
await app.copyAgCredential();
|
|
assert.strictEqual(clipboardText, app.agCredentialText);
|
|
|
|
console.log('ag_credential_access.test.js: PASS');
|
|
} finally {
|
|
db.getAccount = originalGetAccount;
|
|
db.getAllAccounts = originalGetAllAccounts;
|
|
}
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|