dns-providers.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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('one-click dns setup only applies records under the user domain zone', async () => {
  78. const calls = [];
  79. globalThis.fetch = async (url, options = {}) => {
  80. calls.push({ url: String(url), method: options.method || 'GET' });
  81. if (String(url).includes('/zones?')) return json({ success: true, result: [{ id: 'zone-1', name: 'example.com' }] });
  82. if (String(url).includes('/dns_records?')) return json({ success: true, result: [] });
  83. return json({ success: true, result: { id: 'ok' } });
  84. };
  85. const result = await applyDnsSetup(domainFixture(), cloudflareCredential(), {
  86. records: [
  87. {
  88. key: 'dkim',
  89. host: 'mh._domainkey.example.com',
  90. type: 'TXT',
  91. value: 'v=DKIM1; k=rsa; p=abc',
  92. status: 'missing'
  93. },
  94. {
  95. key: 'sender-a',
  96. host: 'in.ss5.xyz',
  97. type: 'A',
  98. value: '127.0.0.1',
  99. status: 'ok'
  100. }
  101. ]
  102. });
  103. assert.equal(result.ok, true);
  104. assert.equal(result.results.length, 1);
  105. assert.equal(result.results[0].key, 'dkim');
  106. assert.equal(calls.filter((call) => call.method === 'POST').length, 1);
  107. });
  108. test('cloudflare one-click dns can use the current domain zone with a multi-zone token', async () => {
  109. const calls = [];
  110. globalThis.fetch = async (url, options = {}) => {
  111. calls.push({ url: String(url), method: options.method || 'GET' });
  112. if (String(url).includes('/zones?name=other.com')) {
  113. return json({ success: true, result: [{ id: 'zone-other', name: 'other.com' }] });
  114. }
  115. if (String(url).includes('/dns_records?')) return json({ success: true, result: [] });
  116. return json({ success: true, result: { id: 'ok' } });
  117. };
  118. const result = await applyDnsSetup(
  119. { ...domainFixture(), domain: 'other.com', senderHost: 'mail.other.com' },
  120. cloudflareCredential(),
  121. {
  122. records: [
  123. {
  124. key: 'verification',
  125. host: '_mailhub.other.com',
  126. type: 'TXT',
  127. value: 'mailhub-verification=token',
  128. status: 'missing'
  129. }
  130. ]
  131. }
  132. );
  133. assert.equal(result.ok, true);
  134. assert.equal(result.results[0].ok, true);
  135. assert.ok(calls.some((call) => call.url.includes('/zones?name=other.com')));
  136. assert.ok(calls.some((call) => call.method === 'POST' && call.url.includes('/zones/zone-other/dns_records')));
  137. });
  138. test('cloudflare one-click dns discovers the parent zone for subdomain sending domains', async () => {
  139. const calls = [];
  140. globalThis.fetch = async (url, options = {}) => {
  141. calls.push({ url: String(url), method: options.method || 'GET' });
  142. if (String(url).includes('/zones?name=sender.a4sky.com')) {
  143. return json({ success: true, result: [] });
  144. }
  145. if (String(url).includes('/zones?name=a4sky.com')) {
  146. return json({ success: true, result: [{ id: 'zone-a4sky', name: 'a4sky.com' }] });
  147. }
  148. if (String(url).includes('/dns_records?')) return json({ success: true, result: [] });
  149. return json({ success: true, result: { id: 'ok' } });
  150. };
  151. const result = await applyDnsSetup(
  152. { ...domainFixture(), domain: 'sender.a4sky.com', senderHost: 'in.ss5.xyz' },
  153. cloudflareCredential(),
  154. {
  155. records: [
  156. {
  157. key: 'verification',
  158. host: '_mailhub.sender.a4sky.com',
  159. type: 'TXT',
  160. value: 'mailhub-verification=token',
  161. status: 'missing'
  162. }
  163. ]
  164. }
  165. );
  166. assert.equal(result.ok, true);
  167. assert.equal(result.results[0].ok, true);
  168. assert.ok(calls.some((call) => call.url.includes('/zones?name=sender.a4sky.com')));
  169. assert.ok(calls.some((call) => call.url.includes('/zones?name=a4sky.com')));
  170. assert.ok(calls.some((call) => call.method === 'POST' && call.url.includes('/zones/zone-a4sky/dns_records')));
  171. });
  172. function cloudflareCredential() {
  173. return {
  174. provider: 'cloudflare',
  175. zoneName: 'example.com',
  176. defaultTtl: 600,
  177. credentials: { apiToken: 'token' }
  178. };
  179. }
  180. function aliyunCredential() {
  181. return {
  182. provider: 'aliyun',
  183. zoneName: 'example.com',
  184. defaultTtl: 600,
  185. credentials: { accessKeyId: 'id', accessKeySecret: 'secret' }
  186. };
  187. }
  188. function dnspodCredential() {
  189. return {
  190. provider: 'dnspod',
  191. zoneName: 'example.com',
  192. defaultTtl: 600,
  193. credentials: { secretId: 'id', secretKey: 'secret' }
  194. };
  195. }
  196. function domainFixture() {
  197. return {
  198. domain: 'example.com',
  199. senderHost: 'mail.example.com',
  200. sendingIp: '127.0.0.1'
  201. };
  202. }
  203. function json(payload) {
  204. return {
  205. ok: true,
  206. status: 200,
  207. async json() {
  208. return payload;
  209. }
  210. };
  211. }