dns-providers.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. function cloudflareCredential() {
  78. return {
  79. provider: 'cloudflare',
  80. zoneName: 'example.com',
  81. defaultTtl: 600,
  82. credentials: { apiToken: 'token' }
  83. };
  84. }
  85. function aliyunCredential() {
  86. return {
  87. provider: 'aliyun',
  88. zoneName: 'example.com',
  89. defaultTtl: 600,
  90. credentials: { accessKeyId: 'id', accessKeySecret: 'secret' }
  91. };
  92. }
  93. function dnspodCredential() {
  94. return {
  95. provider: 'dnspod',
  96. zoneName: 'example.com',
  97. defaultTtl: 600,
  98. credentials: { secretId: 'id', secretKey: 'secret' }
  99. };
  100. }
  101. function domainFixture() {
  102. return {
  103. domain: 'example.com',
  104. senderHost: 'mail.example.com',
  105. sendingIp: '127.0.0.1'
  106. };
  107. }
  108. function json(payload) {
  109. return {
  110. ok: true,
  111. status: 200,
  112. async json() {
  113. return payload;
  114. }
  115. };
  116. }