126 lines
4.6 KiB
JavaScript
126 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
fetchGoogleAccounts,
|
|
parseSupplierAccounts,
|
|
_private: { API_V2_URL },
|
|
} = require('../src/services/google_account_supplier_service');
|
|
const {
|
|
_private: { DEFAULT_API_URL, parseCredentialInput, normalizeTimelive, decodeSettings },
|
|
} = require('../src/services/google_account_credentials_store');
|
|
|
|
(async () => {
|
|
const plain = parseSupplierAccounts([
|
|
'first@example.test|Pass-001|recovery-one@example.test',
|
|
'second@example.test|Pass-002|recovery-two@example.test',
|
|
'FIRST@example.test|Pass-duplicate|ignored@example.test',
|
|
'invalid-row',
|
|
].join('<br/>'), 10);
|
|
assert.deepStrictEqual(plain.accounts, [
|
|
{ email: 'first@example.test', password: 'Pass-001', recovery_email: 'recovery-one@example.test' },
|
|
{ email: 'second@example.test', password: 'Pass-002', recovery_email: 'recovery-two@example.test' },
|
|
]);
|
|
assert.strictEqual(plain.duplicateCount, 1);
|
|
assert.strictEqual(plain.invalidCount, 1);
|
|
assert.strictEqual(plain.accounts[0].recovery_email, 'recovery-one@example.test', '第三列应保存为辅助邮箱');
|
|
|
|
const json = parseSupplierAccounts(JSON.stringify({
|
|
success: true,
|
|
data: ['third@example.test|Pass-003|ignored@example.test'],
|
|
}), 1);
|
|
assert.deepStrictEqual(json.accounts, [{
|
|
email: 'third@example.test',
|
|
password: 'Pass-003',
|
|
recovery_email: 'ignored@example.test',
|
|
}]);
|
|
|
|
assert.deepStrictEqual(
|
|
parseCredentialInput('https://sv9.api999api.com/google/api.php?key_value=test-key-123&timelive=90'),
|
|
{ apiKey: 'test-key-123', apiUrl: 'https://sv9.api999api.com/google/api.php', timelive: 90 },
|
|
);
|
|
assert.deepStrictEqual(parseCredentialInput('test-key-456', 120), {
|
|
apiKey: 'test-key-456',
|
|
apiUrl: DEFAULT_API_URL,
|
|
timelive: 120,
|
|
});
|
|
assert.deepStrictEqual(
|
|
parseCredentialInput('https://sv9.api999api.com/google/l0z/api.php', 60, {
|
|
fallbackApiKey: 'saved-key-789',
|
|
}),
|
|
{
|
|
apiKey: 'saved-key-789',
|
|
apiUrl: 'https://sv9.api999api.com/google/l0z/api.php',
|
|
timelive: 60,
|
|
},
|
|
'已有 Key 时应允许只保存 l0z 基础接口地址',
|
|
);
|
|
assert.deepStrictEqual(decodeSettings(JSON.stringify({ version: 1, timelive: 45 })), {
|
|
apiUrl: DEFAULT_API_URL,
|
|
timelive: 45,
|
|
}, '旧配置应自动使用原 V2 地址');
|
|
assert.strictEqual(normalizeTimelive('', 60), 60);
|
|
assert.throws(
|
|
() => parseCredentialInput('https://example.test/google/apiv2.php?key_value=bad'),
|
|
error => error && error.code === 'GOOGLE_ACCOUNT_URL_INVALID',
|
|
);
|
|
|
|
let requestedUrl = null;
|
|
const result = await fetchGoogleAccounts({
|
|
apiKey: 'secret-key-never-log',
|
|
timelive: 45,
|
|
quantity: 2,
|
|
fetchImpl: async url => {
|
|
requestedUrl = new URL(String(url));
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get: () => null },
|
|
text: async () => 'one@example.test|OnePass|ignored@example.test\ntwo@example.test|TwoPass|ignored@example.test',
|
|
};
|
|
},
|
|
});
|
|
assert.strictEqual(`${requestedUrl.origin}${requestedUrl.pathname}`, API_V2_URL);
|
|
assert.strictEqual(requestedUrl.searchParams.get('key_value'), 'secret-key-never-log');
|
|
assert.strictEqual(requestedUrl.searchParams.get('timelive'), '45');
|
|
assert.strictEqual(requestedUrl.searchParams.get('quantity'), '2');
|
|
assert.strictEqual(result.accounts.length, 2);
|
|
|
|
let l0zRequestedUrl = null;
|
|
await fetchGoogleAccounts({
|
|
apiKey: 'secret-key-never-log',
|
|
apiUrl: 'https://sv9.api999api.com/google/l0z/api.php',
|
|
timelive: 45,
|
|
quantity: 1,
|
|
fetchImpl: async url => {
|
|
l0zRequestedUrl = new URL(String(url));
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get: () => null },
|
|
text: async () => 'l0z@example.test|L0zPass|recovery@example.test',
|
|
};
|
|
},
|
|
});
|
|
assert.strictEqual(`${l0zRequestedUrl.origin}${l0zRequestedUrl.pathname}`, 'https://sv9.api999api.com/google/l0z/api.php');
|
|
assert.strictEqual(l0zRequestedUrl.searchParams.get('key_value'), 'secret-key-never-log');
|
|
assert.strictEqual(l0zRequestedUrl.searchParams.get('timelive'), '45');
|
|
assert.strictEqual(l0zRequestedUrl.searchParams.has('quantity'), false, 'l0z V1 接口不应附加 V2 quantity 参数');
|
|
|
|
await assert.rejects(
|
|
fetchGoogleAccounts({
|
|
apiKey: 'secret-key-never-log',
|
|
timelive: 45,
|
|
quantity: 1,
|
|
fetchImpl: async () => { throw new Error('request failed and secret-key-never-log'); },
|
|
}),
|
|
error => error && error.code === 'GOOGLE_ACCOUNT_API_NETWORK'
|
|
&& !error.message.includes('secret-key-never-log'),
|
|
);
|
|
|
|
console.log('google_account_supplier_service.test.js: PASS');
|
|
})().catch(error => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|