Merge pull request #8391 from YevhenBondarenko/features-info-improvements
features info improvements
This commit is contained in:
		
						commit
						e19eaba01d
					
				@ -15,7 +15,6 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.system;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.google.common.util.concurrent.FutureCallback;
 | 
			
		||||
import com.google.protobuf.ProtocolStringList;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
@ -23,10 +22,11 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.common.util.ThingsBoardThreadFactory;
 | 
			
		||||
import org.thingsboard.rule.engine.api.MailService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.SmsService;
 | 
			
		||||
import org.thingsboard.server.common.data.AdminSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageState;
 | 
			
		||||
import org.thingsboard.server.common.data.FeaturesInfo;
 | 
			
		||||
import org.thingsboard.server.common.data.StringUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.SystemInfo;
 | 
			
		||||
import org.thingsboard.server.common.data.SystemInfoData;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
@ -38,7 +38,6 @@ import org.thingsboard.server.common.data.kv.TsKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.msg.queue.ServiceType;
 | 
			
		||||
import org.thingsboard.server.common.stats.TbApiUsageStateClient;
 | 
			
		||||
import org.thingsboard.server.dao.oauth2.OAuth2Service;
 | 
			
		||||
import org.thingsboard.server.dao.service.DataValidator;
 | 
			
		||||
import org.thingsboard.server.dao.settings.AdminSettingsService;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos.ServiceInfo;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.DiscoveryService;
 | 
			
		||||
@ -59,10 +58,10 @@ import java.util.concurrent.Executors;
 | 
			
		||||
import java.util.concurrent.ScheduledExecutorService;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getCpuUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getMemoryUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getDiscSpaceUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getCpuCount;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getCpuUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getDiscSpaceUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getMemoryUsage;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getTotalDiscSpace;
 | 
			
		||||
import static org.thingsboard.common.util.SystemUtil.getTotalMemory;
 | 
			
		||||
 | 
			
		||||
@ -90,6 +89,8 @@ public class DefaultSystemInfoService extends TbApplicationEventListener<Partiti
 | 
			
		||||
    private final TbApiUsageStateClient apiUsageStateClient;
 | 
			
		||||
    private final AdminSettingsService adminSettingsService;
 | 
			
		||||
    private final OAuth2Service oAuth2Service;
 | 
			
		||||
    private final MailService mailService;
 | 
			
		||||
    private final SmsService smsService;
 | 
			
		||||
    private volatile ScheduledExecutorService scheduler;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -135,18 +136,37 @@ public class DefaultSystemInfoService extends TbApplicationEventListener<Partiti
 | 
			
		||||
    public FeaturesInfo getFeaturesInfo() {
 | 
			
		||||
        FeaturesInfo featuresInfo = new FeaturesInfo();
 | 
			
		||||
        featuresInfo.setEmailEnabled(isEmailEnabled());
 | 
			
		||||
        featuresInfo.setSmsEnabled(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "sms") != null);
 | 
			
		||||
        featuresInfo.setSmsEnabled(smsService.isConfigured(TenantId.SYS_TENANT_ID));
 | 
			
		||||
        featuresInfo.setOauthEnabled(oAuth2Service.findOAuth2Info().isEnabled());
 | 
			
		||||
        featuresInfo.setTwoFaEnabled(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "twoFaSettings") != null);
 | 
			
		||||
        featuresInfo.setNotificationEnabled(adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "notifications") != null);
 | 
			
		||||
        featuresInfo.setTwoFaEnabled(isTwoFaEnabled());
 | 
			
		||||
        featuresInfo.setNotificationEnabled(isSlackEnabled());
 | 
			
		||||
        return featuresInfo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean isEmailEnabled() {
 | 
			
		||||
        AdminSettings mailSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "mail");
 | 
			
		||||
        if (mailSettings != null) {
 | 
			
		||||
            JsonNode mailFrom = mailSettings.getJsonValue().get("mailFrom");
 | 
			
		||||
            return StringUtils.isNotEmpty(mailFrom.asText()) && !"ThingsBoard <sysadmin@localhost.localdomain>".equalsIgnoreCase(mailFrom.asText());
 | 
			
		||||
        try {
 | 
			
		||||
            mailService.testConnection(TenantId.SYS_TENANT_ID);
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean isTwoFaEnabled() {
 | 
			
		||||
        AdminSettings twoFaSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "twoFaSettings");
 | 
			
		||||
        if (twoFaSettings != null) {
 | 
			
		||||
            var providers = twoFaSettings.getJsonValue().get("providers");
 | 
			
		||||
            if (providers != null) {
 | 
			
		||||
                return providers.size() > 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean isSlackEnabled() {
 | 
			
		||||
        AdminSettings notifications = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "notifications");
 | 
			
		||||
        if (notifications != null) {
 | 
			
		||||
            return notifications.getJsonValue().get("deliveryMethodsConfigs").has("SLACK");
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -21,8 +21,12 @@ import com.google.common.collect.Lists;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.mockito.Mockito;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.test.mock.mockito.MockBean;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.rule.engine.api.MailService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.SmsService;
 | 
			
		||||
import org.thingsboard.server.common.data.AdminSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageState;
 | 
			
		||||
import org.thingsboard.server.common.data.Customer;
 | 
			
		||||
@ -53,6 +57,7 @@ import org.thingsboard.server.common.data.query.TsValue;
 | 
			
		||||
import org.thingsboard.server.common.data.security.Authority;
 | 
			
		||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
 | 
			
		||||
import org.thingsboard.server.common.stats.TbApiUsageStateClient;
 | 
			
		||||
import org.thingsboard.server.dao.settings.AdminSettingsService;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityCountCmd;
 | 
			
		||||
import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityCountUpdate;
 | 
			
		||||
@ -76,6 +81,15 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TbTenantProfileCache tenantProfileCache;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private AdminSettingsService adminSettingsService;
 | 
			
		||||
 | 
			
		||||
    @MockBean
 | 
			
		||||
    private MailService mailService;
 | 
			
		||||
 | 
			
		||||
    @MockBean
 | 
			
		||||
    private SmsService smsService;
 | 
			
		||||
 | 
			
		||||
    //For system administrator
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testTenantsCountWsCmd() throws Exception {
 | 
			
		||||
@ -266,6 +280,19 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testGetFeaturesInfo() throws Exception {
 | 
			
		||||
        String mail = "test@thingsboard.org";
 | 
			
		||||
        Mockito.doAnswer(invocation -> {
 | 
			
		||||
            AdminSettings mailSettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "mail");
 | 
			
		||||
            JsonNode mailFrom = mailSettings.getJsonValue().get("mailFrom");
 | 
			
		||||
            if (!mailFrom.asText().equals(mail)) {
 | 
			
		||||
                throw new Exception();
 | 
			
		||||
            }
 | 
			
		||||
            return null;
 | 
			
		||||
        }).when(mailService).testConnection(TenantId.SYS_TENANT_ID);
 | 
			
		||||
 | 
			
		||||
        Mockito.when(smsService.isConfigured(TenantId.SYS_TENANT_ID))
 | 
			
		||||
                .then(a -> adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "sms") != null);
 | 
			
		||||
 | 
			
		||||
        loginSysAdmin();
 | 
			
		||||
 | 
			
		||||
        FeaturesInfo featuresInfo = doGet("/api/admin/featuresInfo", FeaturesInfo.class);
 | 
			
		||||
@ -279,7 +306,7 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
        AdminSettings mailSettings = doGet("/api/admin/settings/mail", AdminSettings.class);
 | 
			
		||||
 | 
			
		||||
        JsonNode jsonValue = mailSettings.getJsonValue();
 | 
			
		||||
        ((ObjectNode) jsonValue).put("mailFrom", "test@thingsboard.org");
 | 
			
		||||
        ((ObjectNode) jsonValue).put("mailFrom", mail);
 | 
			
		||||
        mailSettings.setJsonValue(jsonValue);
 | 
			
		||||
 | 
			
		||||
        doPost("/api/admin/settings", mailSettings).andExpect(status().isOk());
 | 
			
		||||
@ -305,7 +332,12 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
 | 
			
		||||
        AdminSettings twoFaSettingsSettings = new AdminSettings();
 | 
			
		||||
        twoFaSettingsSettings.setKey("twoFaSettings");
 | 
			
		||||
        twoFaSettingsSettings.setJsonValue(JacksonUtil.newObjectNode());
 | 
			
		||||
 | 
			
		||||
        var twoFaSettings = JacksonUtil.newObjectNode();
 | 
			
		||||
        var providers = JacksonUtil.newArrayNode();
 | 
			
		||||
        providers.add(JacksonUtil.newObjectNode());
 | 
			
		||||
        twoFaSettings.set("providers", providers);
 | 
			
		||||
        twoFaSettingsSettings.setJsonValue(twoFaSettings);
 | 
			
		||||
        doPost("/api/admin/settings", twoFaSettingsSettings).andExpect(status().isOk());
 | 
			
		||||
 | 
			
		||||
        featuresInfo = doGet("/api/admin/featuresInfo", FeaturesInfo.class);
 | 
			
		||||
@ -317,7 +349,13 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
 | 
			
		||||
        AdminSettings notificationsSettings = new AdminSettings();
 | 
			
		||||
        notificationsSettings.setKey("notifications");
 | 
			
		||||
        notificationsSettings.setJsonValue(JacksonUtil.newObjectNode());
 | 
			
		||||
 | 
			
		||||
        var notificationSettings = JacksonUtil.newObjectNode();
 | 
			
		||||
        var deliveryMethodsConfigs = JacksonUtil.newObjectNode();
 | 
			
		||||
        deliveryMethodsConfigs.set("SLACK", JacksonUtil.newObjectNode());
 | 
			
		||||
        notificationSettings.set("deliveryMethodsConfigs", deliveryMethodsConfigs);
 | 
			
		||||
 | 
			
		||||
        notificationsSettings.setJsonValue(notificationSettings);
 | 
			
		||||
        doPost("/api/admin/settings", notificationsSettings).andExpect(status().isOk());
 | 
			
		||||
 | 
			
		||||
        featuresInfo = doGet("/api/admin/featuresInfo", FeaturesInfo.class);
 | 
			
		||||
@ -338,6 +376,10 @@ public abstract class BaseHomePageApiTest extends AbstractControllerTest {
 | 
			
		||||
        Assert.assertTrue(featuresInfo.isTwoFaEnabled());
 | 
			
		||||
        Assert.assertTrue(featuresInfo.isNotificationEnabled());
 | 
			
		||||
        Assert.assertTrue(featuresInfo.isOauthEnabled());
 | 
			
		||||
 | 
			
		||||
        adminSettingsService.deleteAdminSettingsByTenantIdAndKey(TenantId.SYS_TENANT_ID, "notifications");
 | 
			
		||||
        adminSettingsService.deleteAdminSettingsByTenantIdAndKey(TenantId.SYS_TENANT_ID, "twoFaSettings");
 | 
			
		||||
        adminSettingsService.deleteAdminSettingsByTenantIdAndKey(TenantId.SYS_TENANT_ID, "sms");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user