Skip to content

Commit

Permalink
fix: error in metric client when env profile is used and .ask holder …
Browse files Browse the repository at this point in the history
…exists
  • Loading branch information
kakha urigashvili authored and RonWang committed Oct 6, 2020
1 parent aea5f88 commit fa9529a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/clients/metric-client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const uuid = require('uuid/v4');
const axios = require('axios');
const AppConfig = require('@src/model/app-config');
const profileHelper = require('@src/utils/profile-helper');
const { METRICS } = require('@src/utils/constants');
const pck = require('../../../package.json');

Expand Down Expand Up @@ -150,6 +151,7 @@ class MetricClient {
}

_isEnabled() {
if (profileHelper.isEnvProfile()) return true;
if (process.env.ASK_SHARE_USAGE === 'false') return false;
if (!AppConfig.configFileExists()) return false;

Expand All @@ -159,6 +161,7 @@ class MetricClient {

_getMachineId() {
if (!this.enabled) return;
if (profileHelper.isEnvProfile()) return 'all_environmental';
const appConfig = AppConfig.getInstance();
if (!appConfig.getMachineId()) {
appConfig.setMachineId(uuid());
Expand Down
27 changes: 21 additions & 6 deletions test/unit/clients/metric-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const chaiUuid = require('chai-uuid');
const chaiJsonSchema = require('chai-json-schema');
const sinon = require('sinon');
const { METRICS } = require('@src/utils/constants');
const profileHelper = require('@src/utils/profile-helper');
const AppConfig = require('@src/model/app-config');
const { MetricClient, MetricActionResult } = require('@src/clients/metric-client');
const jsonSchema = require('@test/fixture/ask-devtools-metrics.schema.json');
Expand All @@ -17,13 +18,15 @@ describe('Clients test - cli metric client', () => {
const getMachineIdStub = sinon.stub();
const setMachineIdStub = sinon.stub();
const writeConfigStub = sinon.stub();
let isEnvProfileStub;
let configExistsStub;
let shareUsageVariableValue;

beforeEach(() => {
shareUsageVariableValue = process.env.ASK_SHARE_USAGE;
delete process.env.ASK_SHARE_USAGE;
configExistsStub = sinon.stub(AppConfig, 'configFileExists').returns(true);
isEnvProfileStub = sinon.stub(profileHelper, 'isEnvProfile').returns(false);
sinon.stub(AppConfig.prototype, 'read');
sinon.stub(AppConfig, 'getInstance').returns({
getShareUsage: getShareUsageStub.returns(true),
Expand Down Expand Up @@ -208,12 +211,11 @@ describe('Clients test - cli metric client', () => {

it('| sends metrics, expect metrics not to be send to metric server when enabled is false', async () => {
configExistsStub.returns(false);
client = new MetricClient();
httpClientPostStub = sinon.stub(client.httpClient, 'post');

const disabledClient = new MetricClient();

// call
const { success } = await disabledClient.sendData();
const { success } = await client.sendData();

// verify
expect(success).eql(true);
Expand All @@ -222,18 +224,31 @@ describe('Clients test - cli metric client', () => {

it('| sends metrics, expect metrics not to be send to metric server when ASK_SHARE_USAGE is false', async () => {
process.env.ASK_SHARE_USAGE = false;
client = new MetricClient();
httpClientPostStub = sinon.stub(client.httpClient, 'post');

const disabledClient = new MetricClient();

// call
const { success } = await disabledClient.sendData();
const { success } = await client.sendData();

// verify
expect(success).eql(true);
expect(httpClientPostStub.called).eql(false);
});

it('| sends metrics, expect metrics be send to metric server when using env profile', async () => {
isEnvProfileStub.returns(true);
client = new MetricClient();
httpClientPostStub = sinon.stub(client.httpClient, 'post').resolves({});

// call
const { success } = await client.sendData();

// verify
expect(success).eql(true);
expect(httpClientPostStub.called).eql(true);
expect(httpClientPostStub.args[0][1]).contains('"machine_id":"all_environmental"');
});

it('| sends metrics, expect to retry on transmission error', async () => {
httpClientPostStub = sinon.stub(client.httpClient, 'post').rejects();

Expand Down

0 comments on commit fa9529a

Please sign in to comment.