Added subdomain checks on tenant update
MODIFY
src/server/rest/v1/service/TenantService.ts
src/server/rest/v1/service/TenantService.ts
@@ -231,24 +231,12 @@ export default class TenantService {
module: MODULE_NAME, method: 'handleCreateTenant'
});
}
- // Check the Tenant's name
- let foundTenant = await TenantStorage.getTenantByName(filteredRequest.name);
- if (foundTenant) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.TENANT_NAME_ALREADY_EXIST,
- message: `The tenant with name '${filteredRequest.name}' already exists`,
- module: MODULE_NAME, method: 'handleCreateTenant',
- user: req.user,
- action: action
- });
- }
// Get the Tenant with ID (subdomain)
- foundTenant = await TenantStorage.getTenantBySubdomain(filteredRequest.subdomain);
+ const foundTenant = await TenantStorage.getTenantBySubdomain(filteredRequest.subdomain);
if (foundTenant) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.TENANT_SUBDOMAIN_ALREADY_EXIST,
+ errorCode: HTTPError.TENANT_ALREADY_EXIST,
message: `The tenant with subdomain '${filteredRequest.subdomain}' already exists`,
module: MODULE_NAME, method: 'handleCreateTenant',
user: req.user,
@@ -340,6 +328,18 @@ export default class TenantService {
const tenant = await TenantStorage.getTenant(filteredRequest.id);
UtilsService.assertObjectExists(action, tenant, `Tenant ID '${filteredRequest.id}' does not exist`,
MODULE_NAME, 'handleUpdateTenant', req.user);
+ // Check subdomain
+ const foundTenant = await TenantStorage.getTenantBySubdomain(filteredRequest.subdomain);
+ if (filteredRequest.subdomain !== tenant.subdomain && foundTenant) {
+ throw new AppError({
+ source: Constants.CENTRAL_SERVER,
+ errorCode: HTTPError.TENANT_ALREADY_EXIST,
+ message: `The tenant with subdomain '${filteredRequest.subdomain}' already exists`,
+ module: MODULE_NAME, method: 'handleCreateTenant',
+ user: req.user,
+ action: action
+ });
+ }
// Check if smart charging is deactivated in all site areas when deactivated in super tenant
if (filteredRequest.components && filteredRequest.components.smartCharging &&
tenant.components && tenant.components.smartCharging &&

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -254,7 +254,6 @@ export default class MongoDBStorage {
// Tenants
await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'tenants', [
{ fields: { subdomain: 1 }, options: { unique: true } },
- { fields: { name: 1 }, options: { unique: true } }
]);
// Users
await this.handleIndexesInCollection(Constants.DEFAULT_TENANT, 'users', [

MODIFY
src/types/HTTPError.ts
src/types/HTTPError.ts
@@ -35,8 +35,7 @@ export enum HTTPError {
USER_ACCOUNT_INACTIVE_ERROR = 594,
USER_ACCOUNT_CHANGED = 595,
TENANT_COMPONENT_CHANGED = 596,
- TENANT_NAME_ALREADY_EXIST = 597,
- TENANT_SUBDOMAIN_ALREADY_EXIST = 598,
+ TENANT_ALREADY_EXIST = 597,
INVALID_CAPTCHA = 530,
INVALID_TOKEN_ERROR = 540,

MODIFY
test/api/TenantTest.ts
test/api/TenantTest.ts
@@ -2,6 +2,8 @@ import chai, { expect } from 'chai';
import CentralServerService from '../api/client/CentralServerService';
import Factory from '../factories/Factory';
+import { HTTPError } from '../../src/types/HTTPError';
+import { StatusCodes } from 'http-status-codes';
import Tenant from '../../src/types/Tenant';
import TestUtils from './TestUtils';
import chaiSubset from 'chai-subset';
@@ -133,7 +135,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.getEntityById(
CentralServerService.defaultInstance.tenantApi, { id: '' }, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to read an invalid tenant', async () => {
@@ -141,7 +143,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.getEntityById(
CentralServerService.defaultInstance.tenantApi, { id: 'youAreInvalid' }, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to read an unknown tenant', async () => {
@@ -149,7 +151,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.getEntityById(
CentralServerService.defaultInstance.tenantApi, { id: '123456789012345678901234' }, false);
// Check
- expect(response.status).to.equal(550);
+ expect(response.status).to.equal(HTTPError.OBJECT_DOES_NOT_EXIST_ERROR);
});
it('Should not be possible to create a tenant without email', async () => {
@@ -160,7 +162,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant without a name', async () => {
@@ -171,7 +173,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant without a subdomain', async () => {
@@ -182,7 +184,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an empty email', async () => {
@@ -193,7 +195,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an empty name', async () => {
@@ -204,7 +206,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an empty subdomain', async () => {
@@ -215,7 +217,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an invalid email', async () => {
@@ -226,7 +228,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an invalid name', async () => {
@@ -237,7 +239,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an invalid subdomain', async () => {
@@ -248,7 +250,7 @@ describe('Tenant tests', function() {
const response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(500);
+ expect(response.status).to.equal(StatusCodes.INTERNAL_SERVER_ERROR);
});
it('Should not be possible to create a tenant with an already existing subdomain', async () => {
@@ -259,7 +261,7 @@ describe('Tenant tests', function() {
let response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(200);
+ expect(response.status).to.equal(StatusCodes.OK);
// Create
tenant = Factory.tenant.build();
@@ -268,27 +270,7 @@ describe('Tenant tests', function() {
response = await CentralServerService.defaultInstance.createEntity(
CentralServerService.defaultInstance.tenantApi, tenant, false);
// Check
- expect(response.status).to.equal(598);
- });
-
- it('Should not be possible to create a tenant with an already existing name', async () => {
- // Create
- let tenant: Tenant = Factory.tenant.build();
- const tenantName = tenant.name;
- // Call
- let response = await CentralServerService.defaultInstance.createEntity(
- CentralServerService.defaultInstance.tenantApi, tenant, false);
- // Check
- expect(response.status).to.equal(200);
-
- // Create
- tenant = Factory.tenant.build();
- tenant.name = tenantName;
- // Call
- response = await CentralServerService.defaultInstance.createEntity(
- CentralServerService.defaultInstance.tenantApi, tenant, false);
- // Check
- expect(response.status).to.equal(597);
+ expect(response.status).to.equal(HTTPError.TENANT_ALREADY_EXIST);
});
});
});

Added subdomain checks on tenant update