129 lines
4.5 KiB
JavaScript
129 lines
4.5 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;
|
|
const originalGetStagingBatch = db.getStagingBatch;
|
|
try {
|
|
const authJson = {
|
|
code: 'code-1',
|
|
access_token: 'access-1',
|
|
refresh_token: 'refresh-1',
|
|
id_token: 'id-1',
|
|
};
|
|
const account = {
|
|
email: 'auth@example.com',
|
|
openai_auth_status: '已提取 JSON',
|
|
openai_auth_json: JSON.stringify(authJson),
|
|
chatgpt_session: JSON.stringify({ user: { email: 'auth@example.com' } }),
|
|
};
|
|
db.getAccount = () => account;
|
|
db.getAllAccounts = () => [account];
|
|
|
|
const list = invoke(routeHandler('/'));
|
|
assert.strictEqual(list.statusCode, 200);
|
|
assert.strictEqual(list.body.data[0].openai_auth_json, undefined);
|
|
assert.strictEqual(list.body.data[0].has_openai_auth, true);
|
|
assert.strictEqual(list.body.data[0].openai_auth_status, '已提取 JSON');
|
|
|
|
const local = invoke(routeHandler('/:email/openai-auth'), {
|
|
email: encodeURIComponent(account.email),
|
|
});
|
|
assert.strictEqual(local.statusCode, 200);
|
|
assert.strictEqual(local.headers['cache-control'], 'no-store');
|
|
assert.strictEqual(local.headers.pragma, 'no-cache');
|
|
assert.deepStrictEqual(local.body.data, authJson);
|
|
|
|
const remote = invoke(routeHandler('/:email/openai-auth'), {
|
|
email: encodeURIComponent(account.email),
|
|
}, '192.168.1.20');
|
|
assert.strictEqual(remote.statusCode, 403);
|
|
|
|
db.getStagingBatch = () => ({
|
|
batch: { id: 1, selected_fields: JSON.stringify(['email', 'openai_auth_json']) },
|
|
accounts: [account],
|
|
});
|
|
const stagedLocal = invoke(routeHandler('/staging/:batchId'), { batchId: '1' });
|
|
assert.strictEqual(stagedLocal.body.data.accounts[0].openai_auth_json, account.openai_auth_json);
|
|
const stagedRemote = invoke(routeHandler('/staging/:batchId'), { batchId: '1' }, '192.168.1.20');
|
|
assert.strictEqual(stagedRemote.statusCode, 403);
|
|
|
|
const appSource = fs.readFileSync(path.join(__dirname, '..', 'public', 'app.js'), 'utf8');
|
|
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 url => ({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ success: true, data: authJson, url }),
|
|
}),
|
|
});
|
|
vm.runInContext(`${appSource}\nthis.__geminiApp = geminiApp;`, context, { filename: 'public/app.js' });
|
|
const app = context.__geminiApp();
|
|
await app.openOpenAIAuth(account);
|
|
assert.strictEqual(app.showOpenAIAuthModal, true);
|
|
assert.strictEqual(app.openAIAuthLoading, false);
|
|
assert.strictEqual(JSON.parse(app.openAIAuthText).refresh_token, 'refresh-1');
|
|
await app.copyOpenAIAuth();
|
|
assert.strictEqual(clipboardText, app.openAIAuthText);
|
|
|
|
app._exportList = [{ email: account.email }];
|
|
Object.keys(app.exportFields).forEach(key => { app.exportFields[key] = false; });
|
|
app.exportFields.email = true;
|
|
app.exportFields.openai_auth_json = true;
|
|
app.exportMode = { file: false, clipboard: true, staging: false };
|
|
app.addLog = () => {};
|
|
await app.confirmExport();
|
|
assert.strictEqual(clipboardText, `${account.email}----${JSON.stringify(authJson)}`);
|
|
|
|
console.log('openai_auth_access.test.js: PASS');
|
|
} finally {
|
|
db.getAccount = originalGetAccount;
|
|
db.getAllAccounts = originalGetAllAccounts;
|
|
db.getStagingBatch = originalGetStagingBatch;
|
|
}
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|