Skip to content

Commit

Permalink
fix: getIpFamily not detecting IPv6 support, ref [Question] - Connect…
Browse files Browse the repository at this point in the history
… VS Code with Snyk stuck #116 [ROAD-604] (#123)
  • Loading branch information
michelkaporin authored Jan 4, 2022
1 parent 6f55bd6 commit 8af7fe1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Fetch sources
uses: actions/checkout@v2

- name: Install dependencies
run: npm install

- name: Patch to preview version
run: npm run patch-preview

Expand Down
33 changes: 26 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
"@types/lodash": "^4.14.161",
"@types/marked": "^3.0.0",
"@types/mocha": "^8.0.3",
"@types/needle": "^2.5.2",
"@types/node": "^14.6.2",
"@types/sinon": "^10.0.2",
"@types/uuid": "^8.3.0",
Expand Down Expand Up @@ -376,7 +377,7 @@
"@itly/plugin-schema-validator": "^2.4.0",
"@itly/plugin-segment-node": "^2.4.0",
"@itly/sdk": "^2.3.1",
"@snyk/code-client": "^4.5.0",
"@snyk/code-client": "^4.5.1",
"analytics-node": "^4.0.1",
"axios": "^0.21.1",
"glob": "^7.2.0",
Expand Down
32 changes: 32 additions & 0 deletions src/test/unit/base/services/authenticationService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as codeClient from '@snyk/code-client';
import { getIpFamily } from '@snyk/code-client';
import { strictEqual } from 'assert';
import needle, { NeedleResponse } from 'needle';
import sinon from 'sinon';
import { IBaseSnykModule } from '../../../../snyk/base/modules/interfaces';
import { AuthenticationService } from '../../../../snyk/base/services/authenticationService';
Expand Down Expand Up @@ -29,6 +31,8 @@ suite('AuthenticationService', () => {
} as unknown) as IConfiguration;
});

teardown(() => sinon.restore());

test("Logs 'Authentication Button is Clicked' analytical event", async () => {
const getIpFamilyStub = sinon.stub(codeClient, 'getIpFamily').resolves(undefined);

Expand All @@ -49,4 +53,32 @@ suite('AuthenticationService', () => {

strictEqual(logAuthenticateButtonIsClickedFake.calledOnce, true);
});

// TODO: the following two tests are more of integration tests, since the second requires access to the network layer. Move it to integration test as part of ROAD-625.
test('getIpFamily returns undefined when IPv6 not supported', async () => {
const ipv6ErrorCode = 'EADDRNOTAVAIL';

// code-client calls 'needle', thus it's the easiest place to stub the response when IPv6 is not supported by the OS network stack. Otherwise, Node internals must be stubbed to return the error.
sinon.stub(needle, 'request').callsFake((_, uri, data, opts, callback) => {
if (!callback) throw new Error();
callback(
({
code: ipv6ErrorCode,
errno: ipv6ErrorCode,
} as unknown) as Error,
({} as unknown) as NeedleResponse,
null,
);
return needle.post(uri, data, opts);
});

const ipFamily = await getIpFamily('https://dev.snyk.io');

strictEqual(ipFamily, undefined);
});

test('getIpFamily returns 6 when IPv6 supported', async () => {
const ipFamily = await getIpFamily('https://dev.snyk.io');
strictEqual(ipFamily, 6);
});
});

0 comments on commit 8af7fe1

Please sign in to comment.