Add tests Setting entity auth
MODIFY
test/api/SettingTest.ts
test/api/SettingTest.ts
@@ -6,10 +6,12 @@ import chai, { expect } from 'chai';
import CentralServerService from './client/CentralServerService';
import ContextDefinition from './context/ContextDefinition';
+import ContextProvider from './context/ContextProvider';
import { CryptoKeyProperties } from '../types/Setting';
import Tenant from '../types/Tenant';
+import TenantContext from './context/TenantContext';
import TestConstants from './client/utils/TestConstants';
-import TestData from './client/utils/TestData';
+// import TestData from './client/utils/TestData';
import Utils from '../../src/utils/Utils';
import _ from 'lodash';
import chaiSubset from 'chai-subset';
@@ -19,7 +21,20 @@ import responseHelper from '../helpers/responseHelper';
chai.use(chaiSubset);
chai.use(responseHelper);
-const testData: TestData = new TestData();
+class TestData {
+ public data: any;
+ public superCentralService: CentralServerService;
+ public centralService: CentralServerService;
+ public credentials: any = {};
+ public tenantContext: TenantContext;
+ public centralUserContext: any;
+ public centralUserService: CentralServerService;
+ public userContext: any;
+ public userService: CentralServerService;
+}
+
+const testData = new TestData();
+
let initialTenant: Tenant;
// Update pricing setting to have sensitive data to test on it
@@ -110,8 +125,7 @@ async function updateCryptoSettingsAndCheckResultSuccessful(crtData, blockCypher
describe('Setting tests', function() {
this.timeout(30000);
- before(async function() {
- // Init values
+ before(async () => {
testData.superCentralService = new CentralServerService(null, { email: config.get('superadmin.username'), password: config.get('superadmin.password') });
testData.centralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS, { email: config.get('admin.username'), password: config.get('admin.password') });
testData.credentials.email = config.get('admin.username');
@@ -120,7 +134,12 @@ describe('Setting tests', function() {
testData.credentials.tenantId = response ? response.data.result[0].id : '';
initialTenant = (await testData.superCentralService.tenantApi.readById(testData.credentials.tenantId)).data;
- await updatePricingWithSensitiveDataAndCheckResultSuccessful();
+ testData.tenantContext = await ContextProvider.defaultInstance.getTenantContext(ContextDefinition.TENANT_CONTEXTS.TENANT_ORGANIZATION);
+ testData.centralUserContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ testData.centralUserService = new CentralServerService(
+ testData.tenantContext.getTenant().subdomain,
+ testData.centralUserContext
+ );
});
after(async function() {
@@ -131,10 +150,6 @@ describe('Setting tests', function() {
expect(res.status).to.equal(200);
});
- afterEach(async function() {
- await resetCryptoSettingToDefault();
- });
-
describe('Success cases (tenant utall)', () => {
it('Check that retrieving refund settings filtered by identifier returns just one result', async () => {
// Retrieve the setting id
@@ -172,6 +187,11 @@ describe('Setting tests', function() {
expect(read.status).to.equal(200);
expect(read.data.count).to.equal(1);
});
+ it('Check that retrieving crypto settings filtered by identifier returns one result', async () => {
+ const read = await testData.centralService.settingApi.readAll({ 'Identifier': 'crypto' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.equal(200);
+ expect(read.data.count).to.equal(1);
+ });
it('Check that retrieving setting by id is working', async () => {
// Retrieve the setting id
const read = await testData.centralService.settingApi.readAll({ 'Identifier' : 'pricing' }, { limit: TestConstants.UNLIMITED, skip: 0 });
@@ -221,11 +241,16 @@ describe('Setting tests', function() {
update = await testData.centralService.updateEntity(testData.centralService.settingApi, oldSetting);
expect(update.status).to.equal(200);
});
- it('Check that retrieving crypto settings filtered by identifier returns one result', async () => {
- const read = await testData.centralService.settingApi.readAll({ 'Identifier': 'crypto' }, { limit: TestConstants.UNLIMITED, skip: 0 });
- expect(read.status).to.equal(200);
- expect(read.data.count).to.equal(1);
+
+ describe('Crypto settings update tests', () => {
+ before(async function() {
+ await updatePricingWithSensitiveDataAndCheckResultSuccessful();
});
+
+ afterEach(async function() {
+ await resetCryptoSettingToDefault();
+ });
+
it('Check crypto settings update - change key', async () => {
const crtCryptoData = await getCurrentCryptoDataAndCheckResultSuccessful();
await updateCryptoSettingsAndCheckResultSuccessful(crtCryptoData.data.result[0], crtCryptoData.data.result[0].content.crypto.keyProperties.blockCypher,
@@ -451,8 +476,19 @@ describe('Setting tests', function() {
// Update encryption to aes-*-ccm
await updateCryptoSettingsAndCheckResultSuccessful(crtCryptoData.data.result[0], 'aes', crtCryptoData.data.result[0].content.crypto.keyProperties.blockSize, 'ccm');
});
+
+ });
});
+
describe('Error cases (tenant utall)', () => {
+ before(async function() {
+ await updatePricingWithSensitiveDataAndCheckResultSuccessful();
+ });
+
+ afterEach(async function() {
+ await resetCryptoSettingToDefault();
+ });
+ describe('Crypto settings tests', () => {
it('Check crypto settings update fails - CRYPTO_KEY_LENGTH_INVALID (513)', async () => {
// Retrieve the crypto setting id
const crtCryptoData = await getCurrentCryptoDataAndCheckResultSuccessful();
@@ -490,3 +526,121 @@ describe('Setting tests', function() {
});
});
});
+
+ describe('Settings authorization tests', () => {
+ describe('With admin user', () => {
+ it('Should be able to read all settings', async () => {
+ const read = await testData.centralService.settingApi.readAll({}, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.equal(200);
+ });
+ it('Should be able to read setting by identifier', async () => {
+ const read = await testData.centralService.settingApi.readAll({ 'Identifier' : 'pricing' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.equal(200);
+ });
+ it('Should be able to read setting by id', async () => {
+ const read = await testData.centralService.settingApi.readAll({ 'Identifier' : 'pricing' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.equal(200);
+ const response = await testData.centralService.settingApi.readById(read.data.result[0].id);
+ expect(response.status).to.equal(200);
+ });
+ it('Should be able to create a new setting', async () => {
+ const settingData = {
+ 'identifier' : 'test',
+ 'content' : {
+ 'type' : 'simple'
+ }
+ };
+ const create = await testData.centralService.settingApi.create(settingData);
+ expect(create.status).to.equal(200);
+ });
+ it('Should be able to update a setting', async () => {
+ const read = await testData.centralService.settingApi.readAll({ 'Identifier' : 'test' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ read.data.result[0].content.type = 'updated';
+ const update = await testData.centralService.settingApi.update(read.data.result[0]);
+ expect(update.status).to.equal(200);
+ const readUpdated = await testData.centralService.settingApi.readAll({ 'Identifier' : 'test' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(readUpdated.data.result[0].content.type).to.equal('updated');
+ });
+ it('Should be able to delete the created setting', async () => {
+ const read = await testData.centralService.settingApi.readAll({ 'Identifier' : 'test' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ const create = await testData.centralService.settingApi.delete(read.data.result[0].id);
+ expect(create.status).to.equal(200);
+ });
+ });
+
+ describe('Settings authorization tests - basic user', () => {
+ before(function() {
+ testData.userContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ if (testData.userContext === testData.centralUserContext) {
+ testData.userService = testData.centralUserService;
+ } else {
+ testData.userService = new CentralServerService(
+ testData.tenantContext.getTenant().subdomain,
+ testData.userContext
+ );
+ }
+ });
+
+ it('Should not be able to read all settings', async () => {
+ try {
+ const read = await testData.userService.settingApi.readAll({}, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ it('Should not be able to read setting by identifier', async () => {
+ try {
+ const read = await testData.userService.settingApi.readAll({ 'Identifier' : 'pricing' }, { limit: TestConstants.UNLIMITED, skip: 0 });
+ expect(read.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ it('Should not be able to read setting by id', async () => {
+ try {
+ const read = await testData.userService.settingApi.readById('5c6c8e8ee7fd060008215e30');
+ expect(read.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ it('Should not be able to create a new setting', async () => {
+ const settingData = {
+ 'identifier' : 'test',
+ 'content' : {
+ 'type' : 'simple'
+ }
+ };
+ try {
+ const create = await testData.userService.settingApi.create(settingData);
+ expect(create.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ it('Should not be able to update a setting', async () => {
+ const settingData = {
+ 'identifier' : 'test',
+ 'content' : {
+ 'type' : 'updated'
+ }
+ };
+ try {
+ const update = await testData.userService.settingApi.update(settingData);
+ expect(update.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ it('Should not be able to delete the created setting', async () => {
+ try {
+ const deleted = await testData.userService.settingApi.delete('5c6c8e8ee7fd060008215e30');
+ expect(deleted.status).to.not.equal(200);
+ } catch (error) {
+ expect(error.actual).to.equal(403);
+ }
+ });
+ });
+ });
+});

Add tests Setting entity auth