-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSqlUtils.test.ts
More file actions
77 lines (63 loc) · 2.94 KB
/
SqlUtils.test.ts
File metadata and controls
77 lines (63 loc) · 2.94 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
import * as exec from "@actions/exec";
import SqlUtils from "../src/SqlUtils";
import SqlConnectionConfig from '../src/SqlConnectionConfig';
describe('SqlUtils tests', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('detectIPAddress should return ipaddress', async () => {
const execSpy = jest.spyOn(exec, 'exec').mockImplementation((_commandLine, _args, options) => {
let sqlClientError = `Client with IP address '1.2.3.4' is not allowed to access the server.`;
options!.listeners!.stderr!(Buffer.from(sqlClientError));
return Promise.reject(1);
});
const ipAddress = await SqlUtils.detectIPAddress(getConnectionConfig());
expect(execSpy).toHaveBeenCalledTimes(1);
expect(ipAddress).toBe('1.2.3.4');
});
it('detectIPAddress should return empty', async () => {
const execSpy = jest.spyOn(exec, 'exec').mockResolvedValue(0);
const ipAddress = await SqlUtils.detectIPAddress(getConnectionConfig());
expect(execSpy).toHaveBeenCalledTimes(1);
expect(ipAddress).toBe('');
});
it('detectIPAddress should throw error', async () => {
const execSpy = jest.spyOn(exec, 'exec').mockRejectedValue(1);
let error: Error | undefined;
try {
await SqlUtils.detectIPAddress(getConnectionConfig());
}
catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error!.message).toMatch('Failed to add firewall rule. Unable to detect client IP Address.');
expect(execSpy).toHaveBeenCalledTimes(2);
});
it('detectIPAddress should retry connection with DB if master connection fails', async () => {
// Mock failure on first call and success on subsequent
const execSpy = jest.spyOn(exec, 'exec').mockRejectedValueOnce(1).mockResolvedValue(0);
const ipAddress = await SqlUtils.detectIPAddress(getConnectionConfig());
expect(execSpy).toHaveBeenCalledTimes(2);
expect(ipAddress).toBe('');
});
it('detectIPAddress should fail if retry fails again', async () => {
const execSpy = jest.spyOn(exec, 'exec').mockRejectedValueOnce(1).mockImplementation((_commandLine, _args, options) => {
options!.listeners!.stderr!(Buffer.from('Custom connection error message.'));
return Promise.reject(1);
});
let error: Error | undefined;
try {
await SqlUtils.detectIPAddress(getConnectionConfig());
}
catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error!.message).toMatch('Failed to add firewall rule. Unable to detect client IP Address.');
expect(execSpy).toHaveBeenCalledTimes(2);
});
});
function getConnectionConfig(): SqlConnectionConfig {
return new SqlConnectionConfig('Server=testServer.database.windows.net;Initial Catalog=testDB;User Id=testUser;Password=placeholder');
}