Skip to content

Commit

Permalink
[Telemetry] Fix cloud tests (NP version of #59040) (#61271)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo authored Mar 25, 2020
1 parent 46e495f commit 11bcfae
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function getTelemetryAllowChangingOptInStatus({
return configTelemetryAllowChangingOptInStatus;
}

if (typeof telemetrySavedObject.telemetryAllowChangingOptInStatus === 'undefined') {
if (typeof telemetrySavedObject.allowChangingOptInStatus === 'undefined') {
return configTelemetryAllowChangingOptInStatus;
}

return telemetrySavedObject.telemetryAllowChangingOptInStatus;
return telemetrySavedObject.allowChangingOptInStatus;
}
2 changes: 1 addition & 1 deletion src/plugins/telemetry/common/telemetry_config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface TelemetrySavedObjectAttributes {
lastVersionChecked?: string;
sendUsageFrom?: 'browser' | 'server';
lastReported?: number;
telemetryAllowChangingOptInStatus?: boolean;
allowChangingOptInStatus?: boolean;
userHasSeenNotice?: boolean;
reportFailureCount?: number;
reportFailureVersion?: string;
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/telemetry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export class TelemetryPlugin implements Plugin {
reportFailureVersion: {
type: 'keyword',
},
allowChangingOptInStatus: {
type: 'boolean',
},
},
},
});
Expand Down
55 changes: 52 additions & 3 deletions x-pack/test/api_integration/apis/telemetry/opt_in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,74 @@

import expect from '@kbn/expect';

import { TelemetrySavedObjectAttributes } from 'src/plugins/telemetry/server/telemetry_repository';
import { FtrProviderContext } from '../../ftr_provider_context';

export default function optInTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');

describe('/api/telemetry/v2/optIn API', () => {
let defaultAttributes: TelemetrySavedObjectAttributes;
let kibanaVersion: any;

before(async () => {
const kibanaVersionAccessor = kibanaServer.version;
kibanaVersion = await kibanaVersionAccessor.get();
defaultAttributes =
(await getSavedObjectAttributes(supertest).catch(err => {
if (err.message === 'expected 200 "OK", got 404 "Not Found"') {
return null;
}
throw err;
})) || {};

expect(typeof kibanaVersion).to.eql('string');
expect(kibanaVersion.length).to.be.greaterThan(0);
});

it('should support sending false', async () => {
afterEach(async () => {
await updateSavedObjectAttributes(supertest, defaultAttributes);
});

it('should support sending false with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(supertest, {
...defaultAttributes,
allowChangingOptInStatus: true,
});
await postTelemetryV2Optin(supertest, false, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest);
expect(enabled).to.be(false);
expect(lastVersionChecked).to.be(kibanaVersion);
});

it('should support sending true', async () => {
it('should support sending true with allowChangingOptInStatus true', async () => {
await updateSavedObjectAttributes(supertest, {
...defaultAttributes,
allowChangingOptInStatus: true,
});
await postTelemetryV2Optin(supertest, true, 200);
const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest);
expect(enabled).to.be(true);
expect(lastVersionChecked).to.be(kibanaVersion);
});

it('should not support sending false with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(supertest, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
await postTelemetryV2Optin(supertest, false, 400);
});

it('should not support sending true with allowChangingOptInStatus false', async () => {
await updateSavedObjectAttributes(supertest, {
...defaultAttributes,
allowChangingOptInStatus: false,
});
await postTelemetryV2Optin(supertest, true, 400);
});

it('should not support sending null', async () => {
await postTelemetryV2Optin(supertest, null, 400);
});
Expand All @@ -57,7 +94,19 @@ async function postTelemetryV2Optin(supertest: any, value: any, statusCode: numb
return body;
}

async function getSavedObjectAttributes(supertest: any): Promise<any> {
async function updateSavedObjectAttributes(
supertest: any,
attributes: TelemetrySavedObjectAttributes
): Promise<any> {
return await supertest
.post('/api/saved_objects/telemetry/telemetry')
.query({ overwrite: true })
.set('kbn-xsrf', 'xxx')
.send({ attributes })
.expect(200);
}

async function getSavedObjectAttributes(supertest: any): Promise<TelemetrySavedObjectAttributes> {
const { body } = await supertest.get('/api/saved_objects/telemetry/telemetry').expect(200);
return body.attributes;
}

0 comments on commit 11bcfae

Please sign in to comment.