1
- import PsqlUtils from "../../Utils/PsqlUtils/PsqlUtils" ;
2
- import { FirewallConstants } from "../../Constants" ;
1
+ import { HttpClient } from '@actions/http-client' ;
2
+ import PsqlToolRunner from "../../Utils/PsqlUtils/PsqlToolRunner" ;
3
+ import PsqlUtils , { IPResponse } from "../../Utils/PsqlUtils/PsqlUtils" ;
3
4
4
5
jest . mock ( '../../Utils/PsqlUtils/PsqlToolRunner' ) ;
5
- const CONFIGURED = "configured" ;
6
+ jest . mock ( '@actions/http-client' ) ;
6
7
7
8
describe ( 'Testing PsqlUtils' , ( ) => {
8
9
afterEach ( ( ) => {
9
- jest . clearAllMocks ( )
10
+ jest . resetAllMocks ( ) ;
10
11
} ) ;
11
12
12
- let detectIPAddressSpy : any ;
13
- beforeAll ( ( ) => {
14
- detectIPAddressSpy = PsqlUtils . detectIPAddress = jest . fn ( ) . mockImplementation ( ( connString : string ) => {
15
- let psqlError ;
16
- if ( connString != CONFIGURED ) {
17
- psqlError = `psql: error: could not connect to server: FATAL: no pg_hba.conf entry for host "1.2.3.4", user "<user>", database "<db>"` ;
18
- }
19
- let ipAddress = '' ;
20
- if ( psqlError ) {
21
- const ipAddresses = psqlError . match ( FirewallConstants . ipv4MatchPattern ) ;
22
- if ( ipAddresses ) {
23
- ipAddress = ipAddresses [ 0 ] ;
24
- } else {
25
- throw new Error ( `Unable to detect client IP Address: ${ psqlError } ` ) ;
26
- }
27
- }
28
- return ipAddress ;
13
+ test ( 'detectIPAddress should return ip address' , async ( ) => {
14
+ const psqlError : string = `psql: error: could not connect to server: FATAL: no pg_hba.conf entry for host "1.2.3.4", user "<user>", database "<db>"` ;
15
+
16
+ jest . spyOn ( PsqlToolRunner , 'executePsqlCommand' ) . mockImplementation ( async ( _connectionString : string , _command : string , options : any = { } ) => {
17
+ options . listeners . stderr ( Buffer . from ( psqlError ) ) ;
18
+ throw new Error ( psqlError ) ;
19
+ } ) ;
20
+ jest . spyOn ( HttpClient . prototype , 'getJson' ) . mockResolvedValue ( {
21
+ statusCode : 200 ,
22
+ result : {
23
+ ip : '1.2.3.4' ,
24
+ } ,
25
+ headers : { } ,
29
26
} ) ;
30
- } ) ;
31
27
32
- test ( 'detectIPAddress should return ip address' , async ( ) => {
33
- await PsqlUtils . detectIPAddress ( "" ) ;
34
- expect ( detectIPAddressSpy ) . toReturnWith ( "1.2.3.4" ) ;
28
+ return PsqlUtils . detectIPAddress ( "" ) . then ( ipAddress => expect ( ipAddress ) . toEqual ( "1.2.3.4" ) ) ;
35
29
} ) ;
36
30
37
31
test ( 'detectIPAddress should return empty string' , async ( ) => {
38
- await PsqlUtils . detectIPAddress ( CONFIGURED ) ;
39
- expect ( detectIPAddressSpy ) . toReturnWith ( "" ) ;
32
+ return PsqlUtils . detectIPAddress ( "" ) . then ( ipAddress => expect ( ipAddress ) . toEqual ( "" ) ) ;
40
33
} )
41
34
42
35
} ) ;
0 commit comments