-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns-verification.test.ts
More file actions
144 lines (118 loc) · 4.36 KB
/
Copy pathdns-verification.test.ts
File metadata and controls
144 lines (118 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { resolveMock, selectLimitMock, updateWhereMock, dbMock } = vi.hoisted(() => {
const selectLimitMock = vi.fn();
const updateWhereMock = vi.fn().mockResolvedValue(undefined);
const dbMock = {
select: vi.fn().mockReturnValue({
from: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
limit: selectLimitMock
})
})
}),
update: vi.fn().mockReturnValue({
set: vi.fn().mockReturnValue({
where: updateWhereMock
})
}),
insert: vi.fn()
};
return {
resolveMock: vi.fn(),
selectLimitMock,
updateWhereMock,
dbMock
};
});
vi.mock('dns/promises', () => ({
resolve: (...args: unknown[]) => resolveMock(...args)
}));
vi.mock('$lib/server/db', () => ({ db: dbMock }));
vi.mock('$lib/server/db/schema', () => ({
dnsVerifications: { id: 'id', orgId: 'orgId' }
}));
vi.mock('drizzle-orm', () => ({
eq: (a: unknown, b: unknown) => ({ a, b })
}));
import { verifyDns } from '$lib/server/services/dns-verification';
const record = {
id: 'rec-1',
orgId: 'org-1',
domain: 'example.com',
txtRecord: 'postguard-verify=deadbeef'
};
beforeEach(() => {
resolveMock.mockReset();
selectLimitMock.mockReset();
updateWhereMock.mockClear();
selectLimitMock.mockResolvedValue([record]);
});
describe('verifyDns', () => {
it('returns verified: true when the TXT record matches', async () => {
resolveMock.mockResolvedValueOnce([[record.txtRecord]]);
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await verifyDns('org-1');
expect(result).toEqual({ verified: true });
expect(errSpy).not.toHaveBeenCalled();
errSpy.mockRestore();
});
it('returns the "not found in DNS" message when no TXT matches', async () => {
resolveMock.mockResolvedValueOnce([['some-other-txt']]);
const result = await verifyDns('org-1');
expect(result.verified).toBe(false);
expect(result.error).toContain(record.txtRecord);
expect(result.error).toContain(record.domain);
expect(updateWhereMock).toHaveBeenCalledTimes(1);
});
it('returns a "domain not found" message and logs when resolve throws ENOTFOUND', async () => {
const enotfound = Object.assign(new Error('getaddrinfo ENOTFOUND example.com'), {
code: 'ENOTFOUND'
});
resolveMock.mockRejectedValueOnce(enotfound);
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await verifyDns('org-1');
expect(result.verified).toBe(false);
expect(result.error).toBe(`Domain ${record.domain} not found`);
expect(errSpy).toHaveBeenCalledTimes(1);
expect(errSpy.mock.calls[0][1]).toMatchObject({
orgId: 'org-1',
domain: record.domain,
code: 'ENOTFOUND'
});
expect(updateWhereMock).toHaveBeenCalledTimes(1);
errSpy.mockRestore();
});
it('returns a "no TXT records" message and logs when resolve throws ENODATA', async () => {
const enodata = Object.assign(new Error('queryTxt ENODATA example.com'), { code: 'ENODATA' });
resolveMock.mockRejectedValueOnce(enodata);
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await verifyDns('org-1');
expect(result.verified).toBe(false);
expect(result.error).toBe(`No TXT records found for ${record.domain}`);
expect(errSpy).toHaveBeenCalledTimes(1);
expect(errSpy.mock.calls[0][1]).toMatchObject({ code: 'ENODATA' });
errSpy.mockRestore();
});
it('falls back to the generic message and logs when resolve throws a plain Error', async () => {
resolveMock.mockRejectedValueOnce(new Error('something else'));
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await verifyDns('org-1');
expect(result.verified).toBe(false);
expect(result.error).toBe(`Could not resolve DNS for ${record.domain}`);
expect(errSpy).toHaveBeenCalledTimes(1);
expect(errSpy.mock.calls[0][1]).toMatchObject({
orgId: 'org-1',
domain: record.domain,
message: 'something else'
});
expect(updateWhereMock).toHaveBeenCalledTimes(1);
errSpy.mockRestore();
});
it('returns the "no verification record" message when none exists for the org', async () => {
selectLimitMock.mockResolvedValueOnce([]);
const result = await verifyDns('org-with-no-record');
expect(result).toEqual({ verified: false, error: 'No DNS verification record found' });
expect(resolveMock).not.toHaveBeenCalled();
expect(updateWhereMock).not.toHaveBeenCalled();
});
});