-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1175 from snyk/feat/log-endpoint
Show message when using a custom API endpoint and validate its value
- Loading branch information
Showing
3 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CustomError } from './custom-error'; | ||
|
||
export class InvalidEndpointConfigError extends CustomError { | ||
private static ERROR_MESSAGE = | ||
"Invalid 'endpoint' config option. Endpoint must be a full and valid URL including protocol and for Snyk.io it should contain path to '/api'"; | ||
|
||
constructor() { | ||
super(InvalidEndpointConfigError.ERROR_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { test, tearDown } from 'tap'; | ||
import * as Proxyquire from 'proxyquire'; | ||
const proxyquire = Proxyquire.noPreserveCache(); | ||
import { InvalidEndpointConfigError } from '../src/lib/errors/invalid-endpoint-config-error'; | ||
|
||
const DEFAULT_API = 'https://snyk.io/api/v1'; | ||
const originalSnykApiEndpoint = process.env.SNYK_API; | ||
delete process.env.SNYK_API; | ||
|
||
tearDown(() => { | ||
process.env.SNYK_API = originalSnykApiEndpoint; | ||
}); | ||
|
||
test('uses default endpoint when none is provided by user', (t) => { | ||
const config = proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: () => { | ||
// No user options provided | ||
return; | ||
}, | ||
}, | ||
}, | ||
}); | ||
t.equal(config.API, DEFAULT_API); | ||
t.end(); | ||
}); | ||
|
||
test('uses default endpoint when user endpoint is the same', (t) => { | ||
const config = proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: (key) => { | ||
if (key === 'endpoint') { | ||
return DEFAULT_API; | ||
} | ||
return; | ||
}, | ||
}, | ||
}, | ||
}); | ||
t.equal(config.API, DEFAULT_API); | ||
t.end(); | ||
}); | ||
|
||
test('uses a valid custom endpoint when provided', (t) => { | ||
const providedEndpoint = 'https://myendpoint.local/api'; | ||
const config = proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: (key) => { | ||
if (key === 'endpoint') { | ||
return providedEndpoint; | ||
} | ||
return; | ||
}, | ||
}, | ||
}, | ||
}); | ||
t.equal(config.API, providedEndpoint); | ||
t.end(); | ||
}); | ||
|
||
test('uses a valid custom endpoint when provided by SNYK_API environment', (t) => { | ||
const providedEndpoint = 'https://myendpoint.local/api'; | ||
process.env.SNYK_API = providedEndpoint; | ||
const config = proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: () => { | ||
// No user options provided | ||
return; | ||
}, | ||
}, | ||
}, | ||
}); | ||
t.equal(config.API, providedEndpoint); | ||
delete process.env.SNYK_API; | ||
t.end(); | ||
}); | ||
|
||
test('uses a valid custom localhost endpoint when provided', (t) => { | ||
const providedEndpoint = 'http://localhost:8000'; | ||
const config = proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: (key) => { | ||
if (key === 'endpoint') { | ||
return providedEndpoint; | ||
} | ||
return; | ||
}, | ||
}, | ||
}, | ||
}); | ||
t.equal(config.API, providedEndpoint); | ||
t.end(); | ||
}); | ||
|
||
test('throws an error when endpoint option is not a valid URL', (t) => { | ||
const providedEndpoint = 'myendpoint.local/api'; | ||
t.throws( | ||
() => | ||
proxyquire('../src/lib/config', { | ||
'./user-config': { | ||
config: { | ||
get: (key) => { | ||
if (key === 'endpoint') { | ||
return providedEndpoint; | ||
} | ||
return; | ||
}, | ||
}, | ||
}, | ||
}), | ||
InvalidEndpointConfigError, | ||
); | ||
t.end(); | ||
}); |