Skip to content

Commit

Permalink
add some functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Oct 31, 2019
1 parent 60a0759 commit 07be553
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default function ({ loadTestFile }) {
describe('Telemetry', () => {
loadTestFile(require.resolve('./telemetry'));
loadTestFile(require.resolve('./telemetry_local'));
loadTestFile(require.resolve('./opt_in'));
});
}
62 changes: 62 additions & 0 deletions x-pack/test/api_integration/apis/telemetry/opt_in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';

import { FtrProviderContext } from '../../ftr_provider_context';

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

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

before(async () => {
const { body } = await supertest.get('/api/status').expect(200);
kibanaVersion = body.version.number;

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

it('should support sending false', async () => {
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 () => {
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 null', async () => {
await postTelemetryV2Optin(supertest, null, 400);
});

it('should not support sending junk', async () => {
await postTelemetryV2Optin(supertest, 42, 400);
});
});
}

async function postTelemetryV2Optin(supertest: any, value: any, statusCode: number): Promise<any> {
const { body } = await supertest
.post('/api/telemetry/v2/optIn')
.set('kbn-xsrf', 'xxx')
.send({ enabled: value })
.expect(statusCode);

return body;
}

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

0 comments on commit 07be553

Please sign in to comment.