Skip to content

Commit

Permalink
Change: Improve License model and display descriptive appliance model…
Browse files Browse the repository at this point in the history
…s and model types #3374

Improve displaying license
  • Loading branch information
bjoernricks authored Mar 16, 2022
2 parents 2429654 + 549e59f commit 4c5c4d1
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 51 deletions.
9 changes: 5 additions & 4 deletions src/gmp/commands/__tests__/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('LicenseCommand tests', () => {
},
appliance: {
model: 'trial',
model_type: '450',
model_type: 'virtual',
sensor: false,
},
},
Expand All @@ -63,13 +63,14 @@ describe('LicenseCommand tests', () => {
const {data: license} = resp;
expect(license.id).toEqual('12345');
expect(license.customerName).toEqual('Monsters Inc.');
expect(license.creationDate).toEqual(parseDate('2021-08-27T06:05:21Z'));
expect(license.version).toEqual('1.0.0');
expect(license.created).toEqual(parseDate('2021-08-27T06:05:21Z'));
expect(license.begins).toEqual(parseDate('2021-08-27T07:05:21Z'));
expect(license.expires).toEqual(parseDate('2021-09-04T07:05:21Z'));
expect(license.comment).toEqual('foo');
expect(license.model).toEqual('trial');
expect(license.modelType).toEqual('450');
expect(license.applianceModel).toEqual('trial');
expect(license.applianceModelType).toEqual('virtual');
expect(license.type).toEqual('trial');
});
});
});
20 changes: 2 additions & 18 deletions src/gmp/commands/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,10 @@

import registerCommand from 'gmp/command';

import {parseDate} from 'gmp/parser';
import {License} from 'gmp/models/license';

import GMPCommand from './gmp';

export class License {
constructor({content, status}) {
this.status = status;
this.id = content?.meta?.id;
this.customerName = content?.meta?.customer_name;
this.creationDate = parseDate(content?.meta?.created);
this.version = content?.meta?.version;
this.begins = parseDate(content?.meta?.begins);
this.expires = parseDate(content?.meta?.expires);
this.comment = content?.meta?.comment;

this.model = content?.appliance?.model;
this.modelType = content?.appliance?.model_type;
}
}

export class LicenseCommand extends GMPCommand {
constructor(http) {
super(http, {cmd: 'get_license'});
Expand All @@ -47,7 +31,7 @@ export class LicenseCommand extends GMPCommand {
return this.httpGet().then(response => {
const {data: envelope} = response;
const {get_license_response: licenseResponse} = envelope.get_license;
const license = new License(licenseResponse.license);
const license = License.fromElement(licenseResponse.license);

return response.setData(license);
});
Expand Down
87 changes: 87 additions & 0 deletions src/gmp/models/__tests__/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Copyright (C) 2022 Greenbone Networks GmbH
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {parseDate} from 'gmp/parser';

import {License} from '../license';

describe('License tests', () => {
test('should init license data via constructor', () => {
const license = new License({
status: 'active',
id: '12345',
version: '1.0.0',
title: 'Test License',
type: 'trial',
customerName: 'Monsters Inc.',
created: parseDate('2021-08-27T06:05:21Z'),
begins: parseDate('2021-08-27T07:05:21Z'),
expires: parseDate('2021-09-04T07:05:21Z'),
comment: 'foo',
applianceModel: 'trial',
applianceModelType: 'virtual',
sensor: false,
});

expect(license.id).toEqual('12345');
expect(license.customerName).toEqual('Monsters Inc.');
expect(license.version).toEqual('1.0.0');
expect(license.created).toEqual(parseDate('2021-08-27T06:05:21Z'));
expect(license.begins).toEqual(parseDate('2021-08-27T07:05:21Z'));
expect(license.expires).toEqual(parseDate('2021-09-04T07:05:21Z'));
expect(license.comment).toEqual('foo');
expect(license.applianceModel).toEqual('trial');
expect(license.applianceModelType).toEqual('virtual');
expect(license.type).toEqual('trial');
});

test('should parse license data from element', () => {
const license = License.fromElement({
status: 'active',
content: {
meta: {
id: '12345',
version: '1.0.0',
title: 'Test License',
type: 'trial',
customer_name: 'Monsters Inc.',
created: '2021-08-27T06:05:21Z',
begins: '2021-08-27T07:05:21Z',
expires: '2021-09-04T07:05:21Z',
comment: 'foo',
},
appliance: {
model: 'trial',
model_type: 'virtual',
sensor: false,
},
},
});

expect(license.id).toEqual('12345');
expect(license.customerName).toEqual('Monsters Inc.');
expect(license.version).toEqual('1.0.0');
expect(license.created).toEqual(parseDate('2021-08-27T06:05:21Z'));
expect(license.begins).toEqual(parseDate('2021-08-27T07:05:21Z'));
expect(license.expires).toEqual(parseDate('2021-09-04T07:05:21Z'));
expect(license.comment).toEqual('foo');
expect(license.applianceModel).toEqual('trial');
expect(license.applianceModelType).toEqual('virtual');
expect(license.type).toEqual('trial');
});
});
122 changes: 122 additions & 0 deletions src/gmp/models/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* Copyright (C) 2022 Greenbone Networks GmbH
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import _ from 'gmp/locale';

import {parseDate} from 'gmp/parser';

import {isDefined} from 'gmp/utils/identity';

const LICENSE_MODEL = {
trial: 'Greenbone Enterprise TRIAL',
'25v': 'Greenbone Enterprise 25V',
25: 'Greenbone Enterprise 25',
35: 'Greenbone Enterprise 35',
maven: 'Greenbone Enterprise MAVEN',
one: 'Greenbone Enterprise ONE',
100: 'Greenbone Enterprise 100',
150: 'Greenbone Enterprise 150',
ceno: 'Greenbone Enterprise CENO',
deca: 'Greenbone Enterprise DECA',
400: 'Greenbone Enterprise 400',
'400r2': 'Greenbone Enterprise 400',
450: 'Greenbone Enterprise 450',
'450r2': 'Greenbone Enterprise 450',
tera: 'Greenbone Enterprise TERA',
500: 'Greenbone Enterprise 500',
510: 'Greenbone Enterprise 510',
550: 'Greenbone Enterprise 550',
600: 'Greenbone Enterprise 600',
'600r2': 'Greenbone Enterprise 600',
peta: 'Greenbone Enterprise PETA',
650: 'Greenbone Enterprise 650',
'650r2': 'Greenbone Enterprise 650',
exa: 'Greenbone Enterprise EXA',
5300: 'Greenbone Enterprise 5300',
6400: 'Greenbone Enterprise 6400',
5400: 'Greenbone Enterprise 5400',
6500: 'Greenbone Enterprise 6500',
expo: 'Greenbone Enterprise EXPO',
'150c-siesta': 'Greenbone Enterprise 150C-SiESTA',
};

export const getLicenseApplianceModelName = value => {
const name = LICENSE_MODEL[value];
return isDefined(name) ? name : value;
};

export const getLicenseApplianceModelType = value => {
if (!isDefined(value)) {
return value;
}
if (value === 'virtual') {
return 'Virtual Appliance';
}
if (value === 'hardware') {
return 'Hardware Appliance';
}
return _('Unknown');
};

export class License {
constructor({
id,
status,
customerName,
created,
version,
begins,
expires,
comment,
type,
applianceModel,
applianceModelType,
}) {
this.status = status;
this.id = id;
this.customerName = customerName;
this.version = version;
this.created = created;
this.begins = begins;
this.expires = expires;
this.comment = comment;
this.type = type;

this.applianceModel = applianceModel;
this.applianceModelType = applianceModelType;
}

static fromElement(element) {
const {content, status} = element;
return new License({
status: status,
id: content?.meta?.id,
customerName: content?.meta?.customer_name,
created: parseDate(content?.meta?.created),
version: content?.meta?.version,
begins: parseDate(content?.meta?.begins),
expires: parseDate(content?.meta?.expires),
comment: content?.meta?.comment,
type: content?.meta?.type,
applianceModel: content?.appliance?.model,
applianceModelType: content?.appliance?.model_type,
});
}
}

export default License;
18 changes: 10 additions & 8 deletions src/web/components/notification/__tests__/licensenotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import React from 'react';

import Capabilities from 'gmp/capabilities/capabilities';

import {License} from 'gmp/commands/license';
import {License} from 'gmp/models/license';

import {rendererWith, wait} from 'web/utils/testing';

import LicenseNotification from '../licensenotification';

const data1 = new License({
const data1 = License.fromElement({
status: 'active',
content: {
meta: {
Expand All @@ -40,14 +40,14 @@ const data1 = new License({
comment: 'Han shot first',
},
appliance: {
model: 'trial',
model_type: '450',
model: '450',
model_type: 'hardware',
sensor: false,
},
},
});

const data2 = new License({
const data2 = License.fromElement({
status: 'active',
content: {
meta: {
Expand All @@ -62,8 +62,8 @@ const data2 = new License({
comment: 'Han shot first',
},
appliance: {
model: 'trial',
model_type: '450',
model: '450',
model_type: 'hardware',
sensor: false,
},
},
Expand Down Expand Up @@ -103,7 +103,9 @@ describe('LicenseNotification tests', () => {

await wait();

expect(baseElement).toHaveTextContent('Your trial license ends in 26 days');
expect(baseElement).toHaveTextContent(
'Your Greenbone Enterprise License ends in 26 days',
);
});

test('should not render if >30 days valid', async () => {
Expand Down
11 changes: 6 additions & 5 deletions src/web/components/notification/licensenotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ const LicenseNotification = ({capabilities, onCloseClick}) => {
const days = license?.expires
? date(license?.expires).diff(date(), 'days')
: undefined;
const model = license?.model;

if (!isDefined(days) || days > LICENSE_EXPIRATION_THRESHOLD) {
return null;
}

const titleMessage = _('Your {{model}} license ends in {{days}} days!', {
model,
days,
});
const titleMessage = _(
'Your Greenbone Enterprise License ends in {{days}} days!',
{
days,
},
);

const message = capabilities.mayOp('modify_license')
? _(
Expand Down
Loading

0 comments on commit 4c5c4d1

Please sign in to comment.