committed by
GitHub
16 changed files with 950 additions and 72 deletions
@ -0,0 +1,67 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.mail; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonInclude; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.rule.engine.api.TbEmail; |
|||
import org.thingsboard.server.common.data.ApiFeature; |
|||
import org.thingsboard.server.common.data.ApiUsageRecordState; |
|||
import org.thingsboard.server.common.data.ApiUsageStateValue; |
|||
|
|||
/** |
|||
* Describes a mail send that the Edge delegates to the Cloud. On the Edge, a mail send that depends |
|||
* on admin-configured (tenant/system) mail settings is packaged into this request and enqueued as a |
|||
* SEND_EMAIL cloud event. On the Cloud, {@code method} selects the matching {@code MailService} call |
|||
* so the Cloud resolves the config, renders the template and transmits via its own SMTP. |
|||
*/ |
|||
@Data |
|||
@Builder |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@JsonInclude(JsonInclude.Include.NON_NULL) |
|||
public class EdgeMailRequest { |
|||
|
|||
public enum MailMethod { |
|||
SEND_BASIC, // sendEmail(to, subject, message)
|
|||
SEND_TB_EMAIL, // send(TbEmail)
|
|||
ACTIVATION, // sendActivationEmail(activationLink, ttlMs, to)
|
|||
ACCOUNT_ACTIVATED, // sendAccountActivatedEmail(loginLink, to)
|
|||
RESET_PASSWORD, // sendResetPasswordEmail(passwordResetLink, ttlMs, to)
|
|||
PASSWORD_WAS_RESET,// sendPasswordWasResetEmail(loginLink, to)
|
|||
TWO_FA, // sendTwoFaVerificationEmail(to, verificationCode, expirationTimeSeconds)
|
|||
ACCOUNT_LOCKOUT, // sendAccountLockoutEmail(lockoutEmail, to, maxFailedLoginAttempts)
|
|||
API_USAGE_STATE, // sendApiFeatureStateEmail(apiFeature, stateValue, to, recordState)
|
|||
TEST_MAIL // sendTestMail(config, to)
|
|||
} |
|||
|
|||
private MailMethod method; |
|||
|
|||
private String to; |
|||
private String subject; |
|||
private String message; |
|||
|
|||
private TbEmail tbEmail; |
|||
|
|||
private String activationLink; |
|||
private String loginLink; |
|||
private String passwordResetLink; |
|||
private Long ttlMs; |
|||
|
|||
private String verificationCode; |
|||
private Integer expirationTimeSeconds; |
|||
|
|||
private String lockoutEmail; |
|||
private Integer maxFailedLoginAttempts; |
|||
|
|||
private ApiFeature apiFeature; |
|||
private ApiUsageStateValue stateValue; |
|||
private ApiUsageRecordState recordState; |
|||
|
|||
private JsonNode testConfig; |
|||
|
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.notification; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonInclude; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
|
|||
/** |
|||
* Describes a notification delivery that the Edge delegates to the Cloud, for channels whose credentials |
|||
* live only on the Cloud (they are stored in the {@code notifications} admin settings that no longer sync |
|||
* to the Edge). {@code method} selects the channel: |
|||
* <ul> |
|||
* <li>{@code SEND_SLACK} - the Cloud resolves the Slack bot token and posts {@code message} (plus any |
|||
* {@code files}) to {@code conversationId}. The whole send is delegated.</li> |
|||
* <li>{@code SEND_MOBILE_PUSH} - the Edge keeps the notification record, device-token lookup and unread |
|||
* count locally and delegates only the FCM push: the Cloud resolves the Firebase credentials and pushes |
|||
* the already-built payload ({@code subject}/{@code body}/{@code data}/{@code badge}) to every |
|||
* {@code fcmToken}.</li> |
|||
* </ul> |
|||
*/ |
|||
@Data |
|||
@Builder |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@JsonInclude(JsonInclude.Include.NON_NULL) |
|||
public class EdgeNotificationRequest { |
|||
|
|||
public enum NotificationMethod { |
|||
SEND_SLACK, |
|||
SEND_MOBILE_PUSH |
|||
} |
|||
|
|||
private NotificationMethod method; |
|||
|
|||
// SEND_SLACK
|
|||
private String conversationId; |
|||
private String message; |
|||
private List<SlackFileData> files; |
|||
|
|||
// SEND_MOBILE_PUSH
|
|||
private Set<String> fcmTokens; |
|||
private String subject; |
|||
private String body; |
|||
private Map<String, String> data; |
|||
private Integer badge; |
|||
|
|||
@Data |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
public static class SlackFileData { |
|||
private String name; |
|||
private String type; |
|||
private byte[] data; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.sms; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonInclude; |
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Builder; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
import org.thingsboard.server.common.data.sms.config.TestSmsRequest; |
|||
|
|||
/** |
|||
* Describes an SMS send that the Edge delegates to the Cloud. On the Edge, a send that depends on |
|||
* admin-configured (tenant/system) SMS provider settings cannot be resolved locally, so the call is |
|||
* packaged into this request and enqueued as a SEND_SMS cloud event; on the Cloud, {@code method} |
|||
* selects the matching {@code SmsService} call so the Cloud resolves the config and transmits via its |
|||
* own provider. |
|||
*/ |
|||
@Data |
|||
@Builder |
|||
@NoArgsConstructor |
|||
@AllArgsConstructor |
|||
@JsonInclude(JsonInclude.Include.NON_NULL) |
|||
public class EdgeSmsRequest { |
|||
|
|||
public enum SmsMethod { |
|||
SEND_SMS, // sendSms(customerId, numbersTo, message)
|
|||
SEND_TEST_SMS // sendTestSms(testSmsRequest)
|
|||
} |
|||
|
|||
private SmsMethod method; |
|||
|
|||
private String[] numbers; |
|||
private String message; |
|||
|
|||
private TestSmsRequest testSmsRequest; |
|||
|
|||
} |
|||
@ -1,66 +0,0 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.edge; |
|||
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import com.google.protobuf.AbstractMessage; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.AdminSettings; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
import org.thingsboard.server.gen.edge.v1.AdminSettingsUpdateMsg; |
|||
|
|||
@DaoSqlTest |
|||
public class AdminSettingsEdgeTest extends AbstractEdgeTest { |
|||
|
|||
@Autowired |
|||
private AdminSettingsService adminSettingsService; |
|||
|
|||
@Test |
|||
public void testAdminSettings() throws Exception { |
|||
loginSysAdmin(); |
|||
|
|||
// save
|
|||
AdminSettings adminSettings = new AdminSettings(); |
|||
adminSettings.setKey("edgeTest"); |
|||
ObjectNode jsonValue = JacksonUtil.newObjectNode(); |
|||
jsonValue.put("key1", "value1"); |
|||
adminSettings.setJsonValue(jsonValue); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
AdminSettings savedAdminSettings = doPost("/api/admin/settings", adminSettings, AdminSettings.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof AdminSettingsUpdateMsg); |
|||
AdminSettingsUpdateMsg adminSettingsUpdateMsg = (AdminSettingsUpdateMsg) latestMessage; |
|||
AdminSettings adminSettingsMsg = JacksonUtil.fromString(adminSettingsUpdateMsg.getEntity(), AdminSettings.class, true); |
|||
Assert.assertNotNull(adminSettingsMsg); |
|||
Assert.assertEquals("edgeTest", adminSettingsMsg.getKey()); |
|||
Assert.assertEquals("value1", adminSettingsMsg.getJsonValue().get("key1").asText()); |
|||
|
|||
// update
|
|||
ObjectNode updatedJsonValue = (ObjectNode) savedAdminSettings.getJsonValue(); |
|||
updatedJsonValue.put("key2", "value2"); |
|||
savedAdminSettings.setJsonValue(updatedJsonValue); |
|||
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doPost("/api/admin/settings", savedAdminSettings, AdminSettings.class); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
|
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof AdminSettingsUpdateMsg); |
|||
adminSettingsUpdateMsg = (AdminSettingsUpdateMsg) latestMessage; |
|||
adminSettingsMsg = JacksonUtil.fromString(adminSettingsUpdateMsg.getEntity(), AdminSettings.class, true); |
|||
Assert.assertNotNull(adminSettingsMsg); |
|||
Assert.assertEquals("edgeTest", adminSettingsMsg.getKey()); |
|||
Assert.assertEquals("value1", adminSettingsMsg.getJsonValue().get("key1").asText()); |
|||
Assert.assertEquals("value2", adminSettingsMsg.getJsonValue().get("key2").asText()); |
|||
|
|||
adminSettingsService.deleteAdminSettingsByTenantIdAndKey(savedAdminSettings.getTenantId(), "edgeTest"); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,197 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.edge; |
|||
|
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import org.junit.Test; |
|||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.rule.engine.api.MailService; |
|||
import org.thingsboard.rule.engine.api.TbEmail; |
|||
import org.thingsboard.server.common.data.ApiFeature; |
|||
import org.thingsboard.server.common.data.ApiUsageStateValue; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.SendEmailUplinkMsg; |
|||
import org.thingsboard.server.service.mail.EdgeMailRequest; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.ArgumentMatchers.isNull; |
|||
import static org.mockito.Mockito.timeout; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
/** |
|||
* Verifies the send-email uplink: when the Edge delegates a mail send, the Cloud receives the |
|||
* SendEmailUplinkMsg and invokes its MailService (which resolves the config, renders and transmits). Here |
|||
* the EdgeImitator plays the Edge and sends the uplink; the Cloud's MailService is mocked so we can assert |
|||
* the delegated call arrives with the expected arguments (the Cloud, not the Edge, uses the tenant id of |
|||
* the connected edge). |
|||
*/ |
|||
@DaoSqlTest |
|||
public class SendEmailEdgeTest extends AbstractEdgeTest { |
|||
|
|||
@MockitoBean |
|||
private MailService mailService; |
|||
|
|||
@Test |
|||
public void testSendBasicEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.SEND_BASIC) |
|||
.to("recipient@thingsboard.io") |
|||
.subject("Edge test subject") |
|||
.message("<b>Edge test body</b>") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendEmail(eq(tenantId), eq("recipient@thingsboard.io"), eq("Edge test subject"), eq("<b>Edge test body</b>")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendTbEmailFromEdgeProcessedByCloud() throws Exception { |
|||
TbEmail tbEmail = TbEmail.builder() |
|||
.from("noreply@thingsboard.io") |
|||
.to("recipient@thingsboard.io") |
|||
.subject("Rule chain email") |
|||
.body("Alarm!") |
|||
.html(false) |
|||
.build(); |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.SEND_TB_EMAIL) |
|||
.tbEmail(tbEmail) |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)).send(eq(tenantId), isNull(), any(TbEmail.class)); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendActivationEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.ACTIVATION) |
|||
.activationLink("http://localhost/activate?token=abc") |
|||
.ttlMs(60000L) |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendActivationEmail(eq("http://localhost/activate?token=abc"), eq(60000L), eq("recipient@thingsboard.io")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendAccountActivatedEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.ACCOUNT_ACTIVATED) |
|||
.loginLink("http://localhost/login") |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendAccountActivatedEmail(eq("http://localhost/login"), eq("recipient@thingsboard.io")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendResetPasswordEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.RESET_PASSWORD) |
|||
.passwordResetLink("http://localhost/reset?token=abc") |
|||
.ttlMs(60000L) |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendResetPasswordEmail(eq("http://localhost/reset?token=abc"), eq(60000L), eq("recipient@thingsboard.io")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendPasswordWasResetEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.PASSWORD_WAS_RESET) |
|||
.loginLink("http://localhost/login") |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendPasswordWasResetEmail(eq("http://localhost/login"), eq("recipient@thingsboard.io")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendTwoFaVerificationEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.TWO_FA) |
|||
.to("recipient@thingsboard.io") |
|||
.verificationCode("123456") |
|||
.expirationTimeSeconds(120) |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendTwoFaVerificationEmail(eq("recipient@thingsboard.io"), eq("123456"), eq(120)); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendAccountLockoutEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.ACCOUNT_LOCKOUT) |
|||
.lockoutEmail("locked@thingsboard.io") |
|||
.to("recipient@thingsboard.io") |
|||
.maxFailedLoginAttempts(5) |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendAccountLockoutEmail(eq("locked@thingsboard.io"), eq("recipient@thingsboard.io"), eq(5)); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendApiUsageStateEmailFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.API_USAGE_STATE) |
|||
.apiFeature(ApiFeature.EMAIL) |
|||
.stateValue(ApiUsageStateValue.DISABLED) |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendApiFeatureStateEmail(eq(ApiFeature.EMAIL), eq(ApiUsageStateValue.DISABLED), eq("recipient@thingsboard.io"), isNull()); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendTestMailFromEdgeProcessedByCloud() throws Exception { |
|||
JsonNode testConfig = JacksonUtil.newObjectNode().put("smtpHost", "localhost"); |
|||
EdgeMailRequest request = EdgeMailRequest.builder() |
|||
.method(EdgeMailRequest.MailMethod.TEST_MAIL) |
|||
.testConfig(testConfig) |
|||
.to("recipient@thingsboard.io") |
|||
.build(); |
|||
|
|||
sendEmailUplink(request); |
|||
|
|||
verify(mailService, timeout(30000)) |
|||
.sendTestMail(any(JsonNode.class), eq("recipient@thingsboard.io")); |
|||
} |
|||
|
|||
private void sendEmailUplink(EdgeMailRequest request) throws Exception { |
|||
SendEmailUplinkMsg sendEmailUplinkMsg = SendEmailUplinkMsg.newBuilder() |
|||
.setTenantIdMSB(tenantId.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()) |
|||
.setRequest(JacksonUtil.toString(request)) |
|||
.build(); |
|||
sendUplinkMsgAndWaitForResponse(builder -> builder.addSendEmailUplinkMsg(sendEmailUplinkMsg)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,112 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.edge; |
|||
|
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.rule.engine.api.notification.FirebaseService; |
|||
import org.thingsboard.rule.engine.api.notification.SlackService; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
|||
import org.thingsboard.server.common.data.notification.settings.MobileAppNotificationDeliveryMethodConfig; |
|||
import org.thingsboard.server.common.data.notification.settings.NotificationDeliveryMethodConfig; |
|||
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; |
|||
import org.thingsboard.server.common.data.notification.settings.SlackNotificationDeliveryMethodConfig; |
|||
import org.thingsboard.server.dao.notification.NotificationSettingsService; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.SendNotificationUplinkMsg; |
|||
import org.thingsboard.server.service.notification.EdgeNotificationRequest; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.anyString; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.Mockito.timeout; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
/** |
|||
* Verifies the send-notification uplink: when the Edge delegates a Slack or mobile-push notification, the |
|||
* Cloud receives the SendNotificationUplinkMsg, resolves the channel credentials from its own notification |
|||
* settings and delivers. The EdgeImitator plays the Edge and sends the uplink; the Cloud's SlackService and |
|||
* FirebaseService are mocked so we can assert the delegated call arrives with the expected arguments. |
|||
*/ |
|||
@DaoSqlTest |
|||
public class SendNotificationEdgeTest extends AbstractEdgeTest { |
|||
|
|||
@MockitoBean |
|||
private SlackService slackService; |
|||
|
|||
@MockitoBean |
|||
private FirebaseService firebaseService; |
|||
|
|||
@Autowired |
|||
private NotificationSettingsService notificationSettingsService; |
|||
|
|||
private void prepareNotificationSettings() { |
|||
// Slack is configured at tenant level.
|
|||
SlackNotificationDeliveryMethodConfig slack = new SlackNotificationDeliveryMethodConfig(); |
|||
slack.setBotToken("xoxb-edge-test-token"); |
|||
NotificationSettings tenantSettings = new NotificationSettings(); |
|||
Map<NotificationDeliveryMethod, NotificationDeliveryMethodConfig> tenantConfigs = new HashMap<>(); |
|||
tenantConfigs.put(NotificationDeliveryMethod.SLACK, slack); |
|||
tenantSettings.setDeliveryMethodsConfigs(tenantConfigs); |
|||
notificationSettingsService.saveNotificationSettings(tenantId, tenantSettings); |
|||
|
|||
// Mobile app credentials can only be configured at system level.
|
|||
MobileAppNotificationDeliveryMethodConfig mobile = new MobileAppNotificationDeliveryMethodConfig(); |
|||
mobile.setFirebaseServiceAccountCredentials("{\"type\":\"service_account\"}"); |
|||
NotificationSettings systemSettings = new NotificationSettings(); |
|||
Map<NotificationDeliveryMethod, NotificationDeliveryMethodConfig> systemConfigs = new HashMap<>(); |
|||
systemConfigs.put(NotificationDeliveryMethod.MOBILE_APP, mobile); |
|||
systemSettings.setDeliveryMethodsConfigs(systemConfigs); |
|||
notificationSettingsService.saveNotificationSettings(TenantId.SYS_TENANT_ID, systemSettings); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendSlackNotificationFromEdgeProcessedByCloud() throws Exception { |
|||
prepareNotificationSettings(); |
|||
EdgeNotificationRequest request = EdgeNotificationRequest.builder() |
|||
.method(EdgeNotificationRequest.NotificationMethod.SEND_SLACK) |
|||
.conversationId("C0123456789") |
|||
.message("Edge Slack notification") |
|||
.build(); |
|||
|
|||
sendNotificationUplink(request); |
|||
|
|||
verify(slackService, timeout(30000)) |
|||
.sendMessage(eq(tenantId), anyString(), eq("C0123456789"), eq("Edge Slack notification")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendMobilePushFromEdgeProcessedByCloud() throws Exception { |
|||
prepareNotificationSettings(); |
|||
EdgeNotificationRequest request = EdgeNotificationRequest.builder() |
|||
.method(EdgeNotificationRequest.NotificationMethod.SEND_MOBILE_PUSH) |
|||
.fcmTokens(Set.of("fcm-token-1")) |
|||
.subject("Edge subject") |
|||
.body("Edge body") |
|||
.badge(3) |
|||
.build(); |
|||
|
|||
sendNotificationUplink(request); |
|||
|
|||
verify(firebaseService, timeout(30000)) |
|||
.sendMessage(eq(tenantId), anyString(), eq("fcm-token-1"), eq("Edge subject"), eq("Edge body"), any(), eq(3)); |
|||
} |
|||
|
|||
private void sendNotificationUplink(EdgeNotificationRequest request) throws Exception { |
|||
SendNotificationUplinkMsg sendNotificationUplinkMsg = SendNotificationUplinkMsg.newBuilder() |
|||
.setTenantIdMSB(tenantId.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()) |
|||
.setRequest(JacksonUtil.toString(request)) |
|||
.build(); |
|||
|
|||
sendUplinkMsgAndWaitForResponse(builder -> builder.addSendNotificationUplinkMsg(sendNotificationUplinkMsg)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,71 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.edge; |
|||
|
|||
import org.junit.Test; |
|||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.rule.engine.api.SmsService; |
|||
import org.thingsboard.server.common.data.sms.config.TestSmsRequest; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.SendSmsUplinkMsg; |
|||
import org.thingsboard.server.service.sms.EdgeSmsRequest; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.ArgumentMatchers.eq; |
|||
import static org.mockito.ArgumentMatchers.isNull; |
|||
import static org.mockito.Mockito.timeout; |
|||
import static org.mockito.Mockito.verify; |
|||
|
|||
/** |
|||
* Verifies the send-sms uplink: when the Edge delegates an SMS send, the Cloud receives the |
|||
* SendSmsUplinkMsg and invokes its SmsService (which resolves the config and transmits). Here the |
|||
* EdgeImitator plays the Edge and sends the uplink; the Cloud's SmsService is mocked so we can assert the |
|||
* delegated call arrives with the expected arguments (the Cloud, not the Edge, uses the tenant id of the |
|||
* connected edge). |
|||
*/ |
|||
@DaoSqlTest |
|||
public class SendSmsEdgeTest extends AbstractEdgeTest { |
|||
|
|||
@MockitoBean |
|||
private SmsService smsService; |
|||
|
|||
@Test |
|||
public void testSendSmsFromEdgeProcessedByCloud() throws Exception { |
|||
EdgeSmsRequest request = EdgeSmsRequest.builder() |
|||
.method(EdgeSmsRequest.SmsMethod.SEND_SMS) |
|||
.numbers(new String[]{"+15551234567"}) |
|||
.message("Edge test message") |
|||
.build(); |
|||
|
|||
sendSmsUplink(request); |
|||
|
|||
verify(smsService, timeout(30000)) |
|||
.sendSms(eq(tenantId), isNull(), any(String[].class), eq("Edge test message")); |
|||
} |
|||
|
|||
@Test |
|||
public void testSendTestSmsFromEdgeProcessedByCloud() throws Exception { |
|||
TestSmsRequest testSmsRequest = new TestSmsRequest(); |
|||
testSmsRequest.setNumberTo("+15551234567"); |
|||
testSmsRequest.setMessage("Test message"); |
|||
EdgeSmsRequest request = EdgeSmsRequest.builder() |
|||
.method(EdgeSmsRequest.SmsMethod.SEND_TEST_SMS) |
|||
.testSmsRequest(testSmsRequest) |
|||
.build(); |
|||
|
|||
sendSmsUplink(request); |
|||
|
|||
verify(smsService, timeout(30000)).sendTestSms(any(TestSmsRequest.class)); |
|||
} |
|||
|
|||
private void sendSmsUplink(EdgeSmsRequest request) throws Exception { |
|||
SendSmsUplinkMsg sendSmsUplinkMsg = SendSmsUplinkMsg.newBuilder() |
|||
.setTenantIdMSB(tenantId.getId().getMostSignificantBits()) |
|||
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()) |
|||
.setRequest(JacksonUtil.toString(request)) |
|||
.build(); |
|||
sendUplinkMsgAndWaitForResponse(builder -> builder.addSendSmsUplinkMsg(sendSmsUplinkMsg)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,112 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.edge.rpc.processor.settings; |
|||
|
|||
import org.junit.jupiter.api.Assertions; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.extension.ExtendWith; |
|||
import org.junit.jupiter.params.ParameterizedTest; |
|||
import org.junit.jupiter.params.provider.ValueSource; |
|||
import org.mockito.Mock; |
|||
import org.mockito.junit.jupiter.MockitoExtension; |
|||
import org.springframework.test.util.ReflectionTestUtils; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.AdminSettings; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.id.AdminSettingsId; |
|||
import org.thingsboard.server.common.data.id.EdgeId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
import org.thingsboard.server.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.EdgeVersion; |
|||
import org.thingsboard.server.service.edge.EdgeContextComponent; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
import static org.mockito.ArgumentMatchers.any; |
|||
import static org.mockito.Mockito.lenient; |
|||
import static org.mockito.Mockito.when; |
|||
|
|||
@ExtendWith(MockitoExtension.class) |
|||
public class AdminSettingsEdgeProcessorTest { |
|||
|
|||
@Mock |
|||
private EdgeContextComponent edgeCtx; |
|||
@Mock |
|||
private AdminSettingsService adminSettingsService; |
|||
|
|||
private final TenantId tenantId = new TenantId(UUID.randomUUID()); |
|||
private AdminSettingsEdgeProcessor processor; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
processor = new AdminSettingsEdgeProcessor(); |
|||
ReflectionTestUtils.setField(processor, "edgeCtx", edgeCtx); |
|||
lenient().when(edgeCtx.getAdminSettingsService()).thenReturn(adminSettingsService); |
|||
} |
|||
|
|||
@Test |
|||
public void testTenantScopedAllowListedKeyIsSyncedToEdge() { |
|||
AdminSettings general = adminSettings(tenantId, "general"); |
|||
when(adminSettingsService.findAdminSettingsById(any(), any())).thenReturn(general); |
|||
|
|||
DownlinkMsg downlink = processor.convertEdgeEventToDownlink(edgeEvent(tenantId), EdgeVersion.V_4_2_2_4); |
|||
|
|||
Assertions.assertNotNull(downlink); |
|||
Assertions.assertEquals(1, downlink.getAdminSettingsUpdateMsgCount()); |
|||
AdminSettings synced = JacksonUtil.fromString(downlink.getAdminSettingsUpdateMsg(0).getEntity(), AdminSettings.class, true); |
|||
Assertions.assertNotNull(synced); |
|||
Assertions.assertEquals("general", synced.getKey()); |
|||
} |
|||
|
|||
@Test |
|||
public void testTenantScopedNonAllowListedKeyIsNotSyncedToEdge() { |
|||
AdminSettings mail = adminSettings(tenantId, "mail"); |
|||
when(adminSettingsService.findAdminSettingsById(any(), any())).thenReturn(mail); |
|||
|
|||
DownlinkMsg downlink = processor.convertEdgeEventToDownlink(edgeEvent(tenantId), EdgeVersion.V_4_2_2_4); |
|||
|
|||
Assertions.assertNull(downlink); |
|||
} |
|||
|
|||
@Test |
|||
public void testSystemScopedAllowListedKeyIsSyncedToEdge() { |
|||
AdminSettings connectivity = adminSettings(TenantId.SYS_TENANT_ID, "connectivity"); |
|||
when(adminSettingsService.findAdminSettingsById(any(), any())).thenReturn(connectivity); |
|||
|
|||
DownlinkMsg downlink = processor.convertEdgeEventToDownlink(edgeEvent(TenantId.SYS_TENANT_ID), EdgeVersion.V_4_2_2_4); |
|||
|
|||
Assertions.assertNotNull(downlink); |
|||
Assertions.assertEquals(1, downlink.getAdminSettingsUpdateMsgCount()); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@ValueSource(strings = {"mail", "jwt", "sms", "notifications"}) |
|||
public void testSystemScopedNonAllowListedKeyIsNotSyncedToEdge(String key) { |
|||
AdminSettings settings = adminSettings(TenantId.SYS_TENANT_ID, key); |
|||
when(adminSettingsService.findAdminSettingsById(any(), any())).thenReturn(settings); |
|||
|
|||
DownlinkMsg downlink = processor.convertEdgeEventToDownlink(edgeEvent(TenantId.SYS_TENANT_ID), EdgeVersion.V_4_2_2_4); |
|||
|
|||
Assertions.assertNull(downlink); |
|||
} |
|||
|
|||
private EdgeEvent edgeEvent(TenantId eventTenantId) { |
|||
EdgeEvent edgeEvent = new EdgeEvent(); |
|||
edgeEvent.setTenantId(eventTenantId); |
|||
edgeEvent.setEdgeId(new EdgeId(UUID.randomUUID())); |
|||
edgeEvent.setEntityId(UUID.randomUUID()); |
|||
return edgeEvent; |
|||
} |
|||
|
|||
private AdminSettings adminSettings(TenantId settingsTenantId, String key) { |
|||
AdminSettings adminSettings = new AdminSettings(); |
|||
adminSettings.setId(new AdminSettingsId(UUID.randomUUID())); |
|||
adminSettings.setTenantId(settingsTenantId); |
|||
adminSettings.setKey(key); |
|||
adminSettings.setJsonValue(JacksonUtil.newObjectNode()); |
|||
return adminSettings; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue