| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import assert from 'node:assert/strict';
- import { afterEach, test } from 'node:test';
- import { applyDnsSetup, testDnsCredential } from '../src/dns-providers.js';
- const originalFetch = globalThis.fetch;
- afterEach(() => {
- globalThis.fetch = originalFetch;
- });
- test('cloudflare provider tests credentials and replaces duplicate SPF records', async () => {
- const calls = [];
- globalThis.fetch = async (url, options = {}) => {
- calls.push({ url: String(url), method: options.method || 'GET', body: options.body });
- if (String(url).includes('/zones?')) return json({ success: true, result: [{ id: 'zone-1', name: 'example.com' }] });
- if (String(url).includes('/zones/zone-1') && !String(url).includes('/dns_records')) {
- return json({ success: true, result: { id: 'zone-1', name: 'example.com' } });
- }
- if (String(url).includes('/dns_records?')) {
- return json({
- success: true,
- result: [
- { id: 'spf-1', type: 'TXT', name: 'example.com', content: 'v=spf1 include:old ~all' },
- { id: 'spf-2', type: 'TXT', name: 'example.com', content: 'v=spf1 include:duplicate ~all' }
- ]
- });
- }
- return json({ success: true, result: { id: 'ok' } });
- };
- const credential = cloudflareCredential();
- assert.equal((await testDnsCredential(credential)).ok, true);
- const result = await applyDnsSetup(domainFixture(), credential, {
- records: [{ key: 'spf', host: 'example.com', type: 'TXT', value: 'v=spf1 ip4:127.0.0.1 ~all' }]
- });
- assert.equal(result.ok, true);
- assert.ok(calls.some((call) => call.method === 'PUT' && call.url.includes('/dns_records/spf-1')));
- assert.ok(calls.some((call) => call.method === 'DELETE' && call.url.includes('/dns_records/spf-2')));
- });
- test('aliyun provider signs and sends create/update record actions', async () => {
- const actions = [];
- globalThis.fetch = async (url) => {
- const params = new URL(String(url)).searchParams;
- const action = params.get('Action');
- actions.push(action);
- if (action === 'DescribeDomainRecords') {
- return json({
- DomainRecords: {
- Record: [{ RecordId: '1', RR: '_dmarc', Type: 'TXT', Value: 'v=DMARC1; p=none' }]
- }
- });
- }
- return json({});
- };
- const credential = aliyunCredential();
- assert.equal((await testDnsCredential(credential)).ok, true);
- const result = await applyDnsSetup(domainFixture(), credential, {
- records: [{ key: 'dmarc', host: '_dmarc.example.com', type: 'TXT', value: 'v=DMARC1; p=reject' }]
- });
- assert.equal(result.ok, true);
- assert.ok(actions.includes('UpdateDomainRecord'));
- });
- test('dnspod provider signs and sends create record actions', async () => {
- const actions = [];
- globalThis.fetch = async (url, options = {}) => {
- assert.equal(String(url), 'https://dnspod.tencentcloudapi.com');
- actions.push(options.headers['X-TC-Action']);
- if (options.headers['X-TC-Action'] === 'DescribeRecordList') {
- return json({ Response: { RecordList: [] } });
- }
- return json({ Response: { RecordId: 123 } });
- };
- const credential = dnspodCredential();
- assert.equal((await testDnsCredential(credential)).ok, true);
- const result = await applyDnsSetup(domainFixture(), credential, {
- records: [{ key: 'dkim', host: 'mh._domainkey.example.com', type: 'TXT', value: 'v=DKIM1; k=rsa; p=abc' }]
- });
- assert.equal(result.ok, true);
- assert.ok(actions.includes('CreateRecord'));
- });
- function cloudflareCredential() {
- return {
- provider: 'cloudflare',
- zoneName: 'example.com',
- defaultTtl: 600,
- credentials: { apiToken: 'token' }
- };
- }
- function aliyunCredential() {
- return {
- provider: 'aliyun',
- zoneName: 'example.com',
- defaultTtl: 600,
- credentials: { accessKeyId: 'id', accessKeySecret: 'secret' }
- };
- }
- function dnspodCredential() {
- return {
- provider: 'dnspod',
- zoneName: 'example.com',
- defaultTtl: 600,
- credentials: { secretId: 'id', secretKey: 'secret' }
- };
- }
- function domainFixture() {
- return {
- domain: 'example.com',
- senderHost: 'mail.example.com',
- sendingIp: '127.0.0.1'
- };
- }
- function json(payload) {
- return {
- ok: true,
- status: 200,
- async json() {
- return payload;
- }
- };
- }
|