dns-providers.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import assert from 'node:assert/strict';
  2. import { afterEach, test } from 'node:test';
  3. import { applyDnsSetup, testDnsCredential } from '../src/dns-providers.js';
  4. const originalFetch = globalThis.fetch;
  5. afterEach(() => {
  6. globalThis.fetch = originalFetch;
  7. });
  8. test('cloudflare provider tests credentials and replaces duplicate SPF records', async () => {
  9. const calls = [];
  10. globalThis.fetch = async (url, options = {}) => {
  11. calls.push({ url: String(url), method: options.method || 'GET', body: options.body });
  12. if (String(url).includes('/zones?')) return json({ success: true, result: [{ id: 'zone-1', name: 'example.com' }] });
  13. if (String(url).includes('/zones/zone-1') && !String(url).includes('/dns_records')) {
  14. return json({ success: true, result: { id: 'zone-1', name: 'example.com' } });
  15. }
  16. if (String(url).includes('/dns_records?')) {
  17. return json({
  18. success: true,
  19. result: [
  20. { id: 'spf-1', type: 'TXT', name: 'example.com', content: 'v=spf1 include:old ~all' },
  21. { id: 'spf-2', type: 'TXT', name: 'example.com', content: 'v=spf1 include:duplicate ~all' }
  22. ]
  23. });
  24. }
  25. return json({ success: true, result: { id: 'ok' } });
  26. };
  27. const credential = cloudflareCredential();
  28. assert.equal((await testDnsCredential(credential)).ok, true);
  29. const result = await applyDnsSetup(domainFixture(), credential, {
  30. records: [{ key: 'spf', host: 'example.com', type: 'TXT', value: 'v=spf1 ip4:127.0.0.1 ~all' }]
  31. });
  32. assert.equal(result.ok, true);
  33. assert.ok(calls.some((call) => call.method === 'PUT' && call.url.includes('/dns_records/spf-1')));
  34. assert.ok(calls.some((call) => call.method === 'DELETE' && call.url.includes('/dns_records/spf-2')));
  35. });
  36. test('aliyun provider signs and sends create/update record actions', async () => {
  37. const actions = [];
  38. globalThis.fetch = async (url) => {
  39. const params = new URL(String(url)).searchParams;
  40. const action = params.get('Action');
  41. actions.push(action);
  42. if (action === 'DescribeDomainRecords') {
  43. return json({
  44. DomainRecords: {
  45. Record: [{ RecordId: '1', RR: '_dmarc', Type: 'TXT', Value: 'v=DMARC1; p=none' }]
  46. }
  47. });
  48. }
  49. return json({});
  50. };
  51. const credential = aliyunCredential();
  52. assert.equal((await testDnsCredential(credential)).ok, true);
  53. const result = await applyDnsSetup(domainFixture(), credential, {
  54. records: [{ key: 'dmarc', host: '_dmarc.example.com', type: 'TXT', value: 'v=DMARC1; p=reject' }]
  55. });
  56. assert.equal(result.ok, true);
  57. assert.ok(actions.includes('UpdateDomainRecord'));
  58. });
  59. test('dnspod provider signs and sends create record actions', async () => {
  60. const actions = [];
  61. globalThis.fetch = async (url, options = {}) => {
  62. assert.equal(String(url), 'https://dnspod.tencentcloudapi.com');
  63. actions.push(options.headers['X-TC-Action']);
  64. if (options.headers['X-TC-Action'] === 'DescribeRecordList') {
  65. return json({ Response: { RecordList: [] } });
  66. }
  67. return json({ Response: { RecordId: 123 } });
  68. };
  69. const credential = dnspodCredential();
  70. assert.equal((await testDnsCredential(credential)).ok, true);
  71. const result = await applyDnsSetup(domainFixture(), credential, {
  72. records: [{ key: 'dkim', host: 'mh._domainkey.example.com', type: 'TXT', value: 'v=DKIM1; k=rsa; p=abc' }]
  73. });
  74. assert.equal(result.ok, true);
  75. assert.ok(actions.includes('CreateRecord'));
  76. });
  77. test('skips external sender host when it already resolves correctly', async () => {
  78. const result = await applyDnsSetup(domainFixture(), cloudflareCredential(), {
  79. records: [
  80. {
  81. key: 'sender-a',
  82. host: 'in.ss5.xyz',
  83. type: 'A',
  84. value: '127.0.0.1',
  85. status: 'ok'
  86. }
  87. ]
  88. });
  89. assert.equal(result.ok, true);
  90. assert.equal(result.results[0].skipped, true);
  91. });
  92. test('fails external sender host when it is not already correct', async () => {
  93. const result = await applyDnsSetup(domainFixture(), cloudflareCredential(), {
  94. records: [
  95. {
  96. key: 'sender-a',
  97. host: 'in.ss5.xyz',
  98. type: 'A',
  99. value: '127.0.0.1',
  100. status: 'warn'
  101. }
  102. ]
  103. });
  104. assert.equal(result.ok, false);
  105. assert.equal(result.results[0].skipped, true);
  106. assert.match(result.results[0].error, /不在 DNS Zone/);
  107. });
  108. function cloudflareCredential() {
  109. return {
  110. provider: 'cloudflare',
  111. zoneName: 'example.com',
  112. defaultTtl: 600,
  113. credentials: { apiToken: 'token' }
  114. };
  115. }
  116. function aliyunCredential() {
  117. return {
  118. provider: 'aliyun',
  119. zoneName: 'example.com',
  120. defaultTtl: 600,
  121. credentials: { accessKeyId: 'id', accessKeySecret: 'secret' }
  122. };
  123. }
  124. function dnspodCredential() {
  125. return {
  126. provider: 'dnspod',
  127. zoneName: 'example.com',
  128. defaultTtl: 600,
  129. credentials: { secretId: 'id', secretKey: 'secret' }
  130. };
  131. }
  132. function domainFixture() {
  133. return {
  134. domain: 'example.com',
  135. senderHost: 'mail.example.com',
  136. sendingIp: '127.0.0.1'
  137. };
  138. }
  139. function json(payload) {
  140. return {
  141. ok: true,
  142. status: 200,
  143. async json() {
  144. return payload;
  145. }
  146. };
  147. }