Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

NotificationResourceTest WIP

Rich Turnerb6205a48e527
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationResourceTest.groovy
similarity index 56%
rename from test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
rename to test/src/test/groovy/org/openremote/test/notification/NotificationResourceTest.groovy
index 2020847d3..1bfe4e436 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationResourceTest.groovy
@@ -3,14 +3,24 @@ package org.openremote.test.notification
import org.openremote.container.timer.TimerService
import org.openremote.manager.notification.NotificationService
import org.openremote.manager.notification.PushNotificationHandler
+import org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
-import org.openremote.model.notification.*
+import org.openremote.model.attribute.AttributeRef
+import org.openremote.model.console.ConsoleProvider
+import org.openremote.model.console.ConsoleRegistration
+import org.openremote.model.console.ConsoleResource
+import org.openremote.model.notification.Notification
+import org.openremote.model.notification.NotificationResource
+import org.openremote.model.notification.NotificationSendResult
+import org.openremote.model.notification.PushNotificationMessage
import org.openremote.model.query.UserQuery
import org.openremote.model.query.filter.UserAssetPredicate
+import org.openremote.model.value.ObjectValue
+import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
-import spock.lang.Shared
+import spock.lang.Ignore
import spock.lang.Specification
import javax.ws.rs.WebApplicationException
@@ -19,10 +29,13 @@ import static org.openremote.container.util.MapAccess.getString
import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD
import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD_DEFAULT
import static org.openremote.model.Constants.*
+import static org.openremote.model.notification.PushNotificationAction.writeAttributeValueAction
+import static org.openremote.model.value.Values.parse
-class NotificationTest extends Specification implements ManagerContainerTrait {
+@Ignore
+class NotificationResourceTest extends Specification implements ManagerContainerTrait {
- def "Store notification tokens for user devices"() {
+ def "Check full notification resource behaviour"() {
def notificationIds = []
def targetTypes = []
@@ -48,12 +61,13 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
def serverPort = findEphemeralPort()
def services = defaultServices()
((NotificationService)services.find {it instanceof NotificationService}).notificationHandlerMap.put("push", mockPushNotificationHandler)
- def container = startContainer(defaultConfig(serverPort), services)
+ def container = startContainerWithPseudoClock(defaultConfig(serverPort), services)
def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
+ def notificationService = container.getService(NotificationService.class)
and: "an authenticated test user"
- def realm = "customerA"
+ def realm = keycloakDemoSetup.customerATenant.realm
def testuser3AccessToken = authenticate(
container,
realm,
@@ -78,25 +92,116 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
getString(container.getConfig(), SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)
).token
- def notification = new PushNotificationMessage(
- title: "The Title",
- appUrl: '#test',
- message: "Message",
- )
-
- def alertAction = new PushNotificationAction()
-
- alertAction.setTitle("TEST_ACTION")
- alertAction.setActionType(ActionType.ACTUATOR)
- notification.addAction(alertAction)
+ def notification = new Notification("TestAction",
+ new PushNotificationMessage("Test Action",
+ "Click to cancel",
+ writeAttributeValueAction(
+ new AttributeRef(
+ managerDemoSetup.apartment1LivingroomId,
+ "alarmEnabled"
+ ),
+ Values.create(false)),null, null), null)
and: "the notification resource"
- // For user operations
- def testuser3NotificationResource = getClientTarget(serverUri(serverPort), realm, testuser3AccessToken).proxy(NotificationResource.class)
def testuser2NotificationResource = getClientTarget(serverUri(serverPort), realm, testuser2AccessToken).proxy(NotificationResource.class)
-
- // For superuser operations
+ def testuser3NotificationResource = getClientTarget(serverUri(serverPort), realm, testuser3AccessToken).proxy(NotificationResource.class)
def adminNotificationResource = getClientTarget(serverUri(serverPort), MASTER_REALM, adminAccessToken).proxy(NotificationResource.class)
+ def anonymousNotificationResource = getClientTarget(serverUri(serverPort), keycloakDemoSetup.customerATenant.realm).proxy(NotificationResource.class)
+ def testuser2ConsoleResource = getClientTarget(serverUri(serverPort), realm, testuser2AccessToken).proxy(ConsoleResource.class)
+ def testuser3ConsoleResource = getClientTarget(serverUri(serverPort), realm, testuser3AccessToken).proxy(ConsoleResource.class)
+ def adminConsoleResource = getClientTarget(serverUri(serverPort), MASTER_REALM, adminAccessToken).proxy(ConsoleResource.class)
+ def anonymousConsoleResource = getClientTarget(serverUri(serverPort), realm).proxy(ConsoleResource.class)
+
+ when: "various consoles are registered"
+ def consoleRegistration = new ConsoleRegistration(null,
+ "Test Console",
+ "1.0",
+ "Android 7.0",
+ new HashMap<String, ConsoleProvider>() {
+ {
+ put("geofence", new ConsoleProvider(
+ ORConsoleGeofenceAssetAdapter.NAME,
+ true,
+ false,
+ false,
+ null
+ ))
+ put("push", new ConsoleProvider(
+ "fcm",
+ true,
+ true,
+ false,
+ (ObjectValue)parse("{token: \"23123213ad2313b0897efd\"}").orElse(null)
+ ))
+ }
+ })
+ def testuser2Console = testuser2ConsoleResource.register(null, consoleRegistration)
+ def testuser3Console = testuser3ConsoleResource.register(null, consoleRegistration)
+ def adminConsole = adminConsoleResource.register(null, consoleRegistration)
+ def anonymousConsole = anonymousConsoleResource.register(null, consoleRegistration)
+
+ then: "the consoles should have been created"
+ testuser2Console.id != null
+ testuser3Console.id != null
+ adminConsole.id != null
+ anonymousConsole.id != null
+
+ when: "the admin user sends a push notification to an entire realm"
+
+ then: "all consoles in that realm should have been sent a notification"
+
+ when: "a regular user sends a push notification to an entire realm"
+
+ then: "access should be forbidden"
+ ex = thrown()
+ ex.response.status == 403
+
+ when: "the admin user sends a notification to testuser2"
+ notification.setTargets(new Notification.Targets(User))
+ adminNotificationResource.sendNotification(null, notification)
+
+ then: "the notification should have been sent"
+ notificationIds.size() == 1
+
+ when: "a regular user sends a push notification to another realm user"
+
+ then: "access should be forbidden"
+ ex = thrown()
+ ex.response.status == 403
+
+ when: "a restricted user sends a push notification to another realm user"
+
+ then: "access should be forbidden"
+ ex = thrown()
+ ex.response.status == 403
+
+ when: "the admin user sends a push notification to the Consoles asset in customerA realm"
+
+ then: "the notification should have been sent"
+
+ when: "a regular user sends a push notification to the Consoles asset in customerA realm"
+
+ then: "access should be forbidden"
+ ex = thrown()
+ ex.response.status == 403
+
+ when: "a restricted user sends a push notification to the Consoles asset in customerA realm"
+
+ then: "access should be forbidden"
+ ex = thrown()
+ ex.response.status == 403
+
+ when: "a regular user is linked to the Consoles asset in customerA realm and they send a notification to this asset"
+
+ then: "the notification should have been sent"
+
+
+
+
+
+
+
+ and: ""
expect: "there should be no user registered to receive notifications"
notificationService.findAllUsersWithToken().isEmpty()