Skip to content

armgit / core

Public Imported from GitHub
0

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

User edit roundtrip test

Christian Bauerea940a3a7d4e
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUser.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUser.java
index 67849e7c6..a80a4315a 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUser.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUser.java
@@ -54,6 +54,8 @@ public interface AdminUser extends AdminContent {
void setUsernameError(boolean error);
+ void setUsernameEditEnabled(boolean enabled);
+
void setFirstName(String firstName);
String getFirstName();
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
index 930779e54..7e1d14150 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
@@ -39,7 +39,7 @@ import org.openremote.manager.shared.security.UserResource;
import org.openremote.manager.shared.validation.ConstraintViolation;
import javax.inject.Inject;
-import java.util.Collection;
+import java.util.*;
import static org.openremote.manager.client.http.RequestExceptionHandler.handleRequestException;
@@ -141,6 +141,7 @@ public class AdminUserActivity
adminContent.enableDelete(false);
adminContent.enableResetPassword(false);
adminContent.enableRoles(false);
+ adminContent.setUsernameEditEnabled(false);
if (userId != null) {
loadUser();
@@ -149,6 +150,7 @@ public class AdminUserActivity
user.setRealm(realm);
writeToView();
adminContent.enableCreate(true);
+ adminContent.setUsernameEditEnabled(true);
}
}
@@ -165,26 +167,27 @@ public class AdminUserActivity
@Override
public void onRoleAssigned(String id, boolean assigned) {
for (Role role : roles) {
- if (role.getId().equals(id)) {
- role.setAssigned(assigned);
+ if (!role.getId().equals(id)) {
+ continue;
+ }
+ role.setAssigned(assigned);
- // A composite role switches all other roles with same name prefix
- if (role.isComposite()) {
- for (Role otherRole : roles) {
- if (otherRole.getName().startsWith(role.getName() + ":")) {
- otherRole.setAssigned(assigned);
- adminContent.toggleRoleAssigned(otherRole.getId(), assigned);
- }
+ // A composite role switches all other roles with same name prefix
+ if (role.isComposite()) {
+ for (Role otherRole : roles) {
+ if (otherRole.getName().startsWith(role.getName() + ":")) {
+ otherRole.setAssigned(assigned);
+ adminContent.toggleRoleAssigned(otherRole.getId(), assigned);
}
- // A non-composite role disables its composite parent
- } else if (!assigned) {
- int colonIndex = role.getName().indexOf(":");
- String prefix = role.getName().substring(0, colonIndex != -1 ? colonIndex : role.getName().length() - 1);
- for (Role otherRole : roles) {
- if (otherRole.getName().equals(prefix)) {
- otherRole.setAssigned(false);
- adminContent.toggleRoleAssigned(otherRole.getId(), false);
- }
+ }
+ // A non-composite role disables its composite parent
+ } else if (!assigned) {
+ int colonIndex = role.getName().indexOf(":");
+ String prefix = role.getName().substring(0, colonIndex != -1 ? colonIndex : role.getName().length() - 1);
+ for (Role otherRole : roles) {
+ if (otherRole.getName().equals(prefix)) {
+ otherRole.setAssigned(false);
+ adminContent.toggleRoleAssigned(otherRole.getId(), false);
}
}
}
@@ -198,7 +201,6 @@ public class AdminUserActivity
adminContent.clearFormMessagesError();
clearViewFieldErrors();
readFromView();
- handlePasswordReset();
requestService.execute(
userMapper,
requestParams -> {
@@ -328,6 +330,7 @@ public class AdminUserActivity
adminContent.enableUpdate(true);
adminContent.enableDelete(true);
adminContent.enableResetPassword(true);
+ adminContent.setUsernameEditEnabled(false);
});
},
ex -> handleRequestException(ex, eventBus, managerMessages)
@@ -340,7 +343,10 @@ public class AdminUserActivity
requestParams -> userResource.getRoles(requestParams, realm, userId),
200,
roles -> {
- this.roles = roles;
+ List<Role> roleList = new ArrayList<>();
+ roleList.addAll(Arrays.asList(roles));
+ Collections.sort(roleList, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
+ this.roles = roleList.toArray(new Role[roleList.size()]);
adminContent.enableRoles(true);
onComplete.run();
},
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
index 14d57ece3..d48b804b7 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserImpl.java
@@ -177,6 +177,11 @@ public class AdminUserImpl extends FormView implements AdminUser {
}
}
+ @Override
+ public void setUsernameEditEnabled(boolean enabled) {
+ usernameInput.setEnabled(enabled);
+ }
+
@Override
public void setFirstName(String firstName) {
firstNameInput.setValue(firstName);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersActivity.java
index d4585e2ee..aedebbefc 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersActivity.java
@@ -85,20 +85,16 @@ public class AdminUsersActivity
}
@Override
- public void start(AcceptsOneWidget container, EventBus eventBus, Collection<EventRegistration> registrations) {
- super.start(container, eventBus, registrations);
- adminContent.setPresenter(this);
+ protected AppActivity<AdminUsersPlace> init(AdminUsersPlace place) {
+ realm = place.getRealm();
+ return super.init(place);
}
@Override
- public void onStop() {
- super.onStop();
- adminContent.setPresenter(null);
- }
+ public void start(AcceptsOneWidget container, EventBus eventBus, Collection<EventRegistration> registrations) {
+ super.start(container, eventBus, registrations);
- @Override
- protected AppActivity<AdminUsersPlace> init(AdminUsersPlace place) {
- realm = place.getRealm();
+ adminContent.setPresenter(this);
requestService.execute(
tenantArrayMapper,
@@ -119,8 +115,12 @@ public class AdminUsersActivity
ex -> handleRequestException(ex, eventBus, managerMessages)
);
}
+ }
- return super.init(place);
+ @Override
+ public void onStop() {
+ super.onStop();
+ adminContent.setPresenter(null);
}
@Override
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersImpl.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersImpl.java
index dbb9097f5..21ba520d3 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersImpl.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUsersImpl.java
@@ -41,13 +41,9 @@ import org.openremote.manager.shared.security.User;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
-import java.util.logging.Logger;
public class AdminUsersImpl extends Composite implements AdminUsers {
- private static final Logger LOG = Logger.getLogger(AdminUsersImpl.class.getName());
-
interface UI extends UiBinder<HTMLPanel, AdminUsersImpl> {
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/service/RequestServiceImpl.java b/manager/client/src/main/java/org/openremote/manager/client/service/RequestServiceImpl.java
index a0fc941b9..9100ae362 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/service/RequestServiceImpl.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/service/RequestServiceImpl.java
@@ -155,6 +155,10 @@ public class RequestServiceImpl implements RequestService {
return;
}
+ if (LOG.isLoggable(Level.FINE)) {
+ LOG.fine("Received response status: " + responseCode);
+ }
+
if (expectedStatusCode != ANY_STATUS_CODE && responseCode != expectedStatusCode) {
onException.accept(new UnexpectedStatusRequestException(responseCode, expectedStatusCode));
return;
diff --git a/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.properties b/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
similarity index 100%
rename from manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.properties
rename to manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
diff --git a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
index 74cce466a..c9b214945 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/SampleDataService.java
@@ -233,7 +233,7 @@ public class SampleDataService implements ContainerService {
RoleRepresentation readRole = rolesResource.get("read").toRepresentation();
RoleRepresentation writeRole = rolesResource.get("write").toRepresentation();
- fromCallable(() -> usersResource.search("admin", null, null, null, null, null))
+ fromCallable(() -> usersResource.search(MASTER_REALM_ADMIN_USER, null, null, null, null, null))
.flatMap(Observable::from)
.map(userRepresentation -> usersResource.get(userRepresentation.getId()))
.subscribe(adminUser -> {
@@ -249,9 +249,10 @@ public class SampleDataService implements ContainerService {
}
);
- // Find out if there is a 'test' user already present, delete it
- fromCallable(() -> usersResource.search("test", null, null, null, null, null))
+ // Find out if there are any users except the admin, delete them
+ fromCallable(() -> usersResource.search(null, null, null))
.flatMap(Observable::from)
+ .filter(userRepresentation-> !userRepresentation.getUsername().equals(MASTER_REALM_ADMIN_USER))
.map(userRepresentation -> usersResource.get(userRepresentation.getId()))
.subscribe(UserResource::remove);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
index 1e3a6d4e4..0009f46b9 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/security/UserResourceImpl.java
@@ -207,8 +207,6 @@ public class UserResourceImpl extends WebResource implements UserResource {
));
}
- Collections.sort(roles, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
-
return roles.toArray(new Role[roles.size()]);
} catch (ClientErrorException ex) {
@@ -296,14 +294,6 @@ public class UserResourceImpl extends WebResource implements UserResource {
);
violations.add(violation);
}
- if (user.getUsername() == null || !user.getUsername().equals(MASTER_REALM_ADMIN_USER)) {
- ConstraintViolation violation = new ConstraintViolation(
- ConstraintViolation.Type.PARAMETER,
- "User.username",
- validationMessages.getString("User.masterAdminUsernameChanged")
- );
- violations.add(violation);
- }
if (violations.size() > 0) {
ConstraintViolationReport report = new ConstraintViolationReport();
report.setParameterViolations(violations.toArray(new ConstraintViolation[violations.size()]));
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/http/SuccessStatusCode.java b/manager/shared/src/main/java/org/openremote/manager/shared/http/SuccessStatusCode.java
index fec15f799..d2aac1cf7 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/http/SuccessStatusCode.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/http/SuccessStatusCode.java
@@ -21,6 +21,9 @@ package org.openremote.manager.shared.http;
import java.lang.annotation.*;
+/**
+ * Mostly used in tests to indicate the expected outcome.
+ */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/security/UserResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/security/UserResource.java
index 025d5f09d..365f8ce8b 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/security/UserResource.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/security/UserResource.java
@@ -58,13 +58,13 @@ public interface UserResource {
@Path("{realm}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
- @SuccessStatusCode(201)
+ @SuccessStatusCode(204)
void create(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @Valid User user);
@DELETE
@Path("{realm}/{userId}")
@Produces(APPLICATION_JSON)
- @SuccessStatusCode(200)
+ @SuccessStatusCode(204)
void delete(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("userId") String userId);
@PUT
@@ -83,6 +83,6 @@ public interface UserResource {
@PUT
@Path("{realm}/{userId}/role")
@Consumes(APPLICATION_JSON)
- @SuccessStatusCode(200)
+ @SuccessStatusCode(204)
void updateRoles(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("userId") String userId, Role[] roles);
}
diff --git a/manager/shared/src/main/resources/ValidationMessages.properties b/manager/shared/src/main/resources/ValidationMessages.properties
index 0ef56cb24..711b01955 100644
--- a/manager/shared/src/main/resources/ValidationMessages.properties
+++ b/manager/shared/src/main/resources/ValidationMessages.properties
@@ -15,4 +15,3 @@ User.lastName.Size = User's last name can not be more than {max} characters long
User.email.Email = Please provide a valid email address.
User.masterAdminDeleted = The master realm admin user can not be deleted.
User.masterAdminDisabled = The master realm admin user can not be disabled.
-User.masterAdminUsernameChanged = The master realm admin username can not be changed.
\ No newline at end of file
diff --git a/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy b/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy
new file mode 100644
index 000000000..3e60b45aa
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/admin/AdminUsersActivityTest.groovy
@@ -0,0 +1,588 @@
+package org.openremote.test.admin
+
+import com.google.gwt.junit.GWTMockUtilities
+import com.google.gwt.place.shared.WithTokenizers
+import com.google.gwt.user.client.ui.AcceptsOneWidget
+import com.google.gwt.user.client.ui.Widget
+import org.openremote.manager.client.ManagerActivityMapper
+import org.openremote.manager.client.ManagerHistoryMapper
+import org.openremote.manager.client.admin.*
+import org.openremote.manager.client.admin.navigation.AdminNavigation
+import org.openremote.manager.client.admin.navigation.AdminNavigationPresenter
+import org.openremote.manager.client.admin.users.*
+import org.openremote.manager.client.event.GoToPlaceEvent
+import org.openremote.manager.client.event.WillGoToPlaceEvent
+import org.openremote.manager.client.event.bus.EventListener
+import org.openremote.manager.client.i18n.ManagerMessages
+import org.openremote.manager.client.service.RequestServiceImpl
+import org.openremote.manager.client.service.SecurityService
+import org.openremote.manager.shared.Consumer
+import org.openremote.manager.shared.Runnable
+import org.openremote.manager.shared.event.Event
+import org.openremote.manager.shared.event.ui.ShowInfoEvent
+import org.openremote.manager.shared.http.EntityReader
+import org.openremote.manager.shared.security.*
+import org.openremote.manager.shared.validation.ConstraintViolationReport
+import org.openremote.test.ClientObjectMapper
+import org.openremote.test.ClientTrait
+import org.openremote.test.ContainerTrait
+import spock.lang.Specification
+import spock.util.concurrent.BlockingVariables
+
+import static org.openremote.manager.shared.Constants.MANAGER_CLIENT_ID
+import static org.openremote.manager.shared.Constants.MASTER_REALM
+
+class AdminUsersActivityTest extends Specification implements ContainerTrait, ClientTrait {
+
+ def "List all, create, update, delete user"() {
+
+ given: "The server container is started"
+ def serverPort = findEphemeralPort()
+ def container = startContainer(defaultConfig(serverPort), defaultServices())
+
+ and: "An authenticated user and client security service"
+ def realm = MASTER_REALM;
+ def accessToken = authenticate(container, realm, MANAGER_CLIENT_ID, "admin", "admin").token
+ def securityService = Stub(SecurityService) {
+ getRealm() >> realm
+ getToken() >> accessToken
+ updateToken(_, _, _) >> { int minValiditySeconds, Consumer<Boolean> successFn, Runnable errorFn ->
+ successFn.accept(true) // The token is always valid (this assumes the test doesn't run very long)
+ };
+ hasResourceRoleOrIsAdmin(_, _) >> { String role, String resource ->
+ return true; // TODO: Should use the parsed token
+ }
+ }
+
+ and: "A client request service and target"
+ def constraintViolationReader = new ClientObjectMapper(container.JSON, ConstraintViolationReport.class) as EntityReader<ConstraintViolationReport>
+ def requestService = new RequestServiceImpl(securityService, constraintViolationReader)
+ def clientTarget = getClientTarget(createClient(container).build(), serverUri(serverPort), realm)
+
+ and: "The expected result"
+ def result = new BlockingVariables(10)
+ result.appEvents = []
+
+ and: "The fake client MVP environment"
+ GWTMockUtilities.disarm()
+ def managerMessages = Mock(ManagerMessages) {
+ userCreated(_) >> {
+ "TestMessageUserCreated:" + it[0]
+ }
+ userUpdated(_) >> {
+ "TestMessageUserUpdated:" + it[0]
+ }
+ userDeleted(_) >> {
+ "TestMessageUserDeleted:" + it[0]
+ }
+ roleLabel(_) >> {
+ "RoleLabel:" + it[0]
+ }
+ passwordsMustMatch() >> {
+ "TestMessagePasswordsMustMatch"
+ }
+ passwordUpdated() >> {
+ "TestMessagePasswordUpdated" }
+
+ requestFailed(_) >> {
+ "TestMessageRequestFailed:" + it[0]
+ }
+ conflictRequest() >> {
+ "TestMessageConflictRequest"
+ }
+ }
+ def eventBus = createEventBus()
+ eventBus.register(null, new EventListener<Event>() {
+ @Override
+ void on(Event event) {
+ result.appEvents += event
+ }
+ })
+ def placeHistoryMapper = createPlaceHistoryMapper(ManagerHistoryMapper.getAnnotation(WithTokenizers.class))
+ def placeController = createPlaceController(securityService, eventBus)
+
+ and: "The views and activities to test"
+ def adminViewWidget = Mock(Widget)
+ def adminView = Mock(AdminView) {
+ asWidget() >> {
+ adminViewWidget
+ }
+ }
+
+ def adminNavigationView = Mock(AdminNavigation)
+ def adminNavigationPresenter = new AdminNavigationPresenter(adminNavigationView, placeHistoryMapper)
+
+ def tenantResource = Stub(TenantResource) {
+ _(*_) >> { callResourceProxy(container.JSON, clientTarget, getDelegate()) }
+ }
+ def userResource = Stub(UserResource) {
+ _(*_) >> { callResourceProxy(container.JSON, clientTarget, getDelegate()) }
+ }
+
+ def adminUsersView = Mock(AdminUsers) {
+ setTenants(_, _) >> { Tenant[] tenants, String selectedRealm ->
+ result.tenants = tenants
+ result.selectedRealm = selectedRealm
+ }
+ setUsers(_) >> {
+ result.users = it[0]
+ }
+ }
+ def tenantArrayMapper = new ClientObjectMapper(container.JSON, Tenant[].class) as TenantArrayMapper
+ def userArrayMapper = new ClientObjectMapper(container.JSON, User[].class) as UserArrayMapper
+ AdminUsersActivity adminUsersActivity
+
+ def adminUserView = Mock(AdminUser)
+ def userMapper = new ClientObjectMapper(container.JSON, User.class) as UserMapper
+ def credentialMapper = new ClientObjectMapper(container.JSON, Credential.class) as CredentialMapper
+ def roleArrayMapper = new ClientObjectMapper(container.JSON, Role[].class) as RoleArrayMapper
+ AdminUserActivity adminUserActivity
+
+ and: "An activity management configuration"
+ def activityDisplay = Mock(AcceptsOneWidget)
+ def activityMapper = new ManagerActivityMapper(
+ securityService,
+ eventBus,
+ managerMessages,
+ {},
+ {},
+ {},
+ {},
+ {},
+ {},
+ {
+ adminUsersActivity = new AdminUsersActivity(adminView, adminNavigationPresenter, adminUsersView, eventBus,
+ managerMessages, placeController, requestService, tenantResource, tenantArrayMapper, userResource, userArrayMapper)
+ return adminUsersActivity
+ },
+ {
+ adminUserActivity = new AdminUserActivity(adminView, adminNavigationPresenter, adminUserView, managerMessages,
+ placeController, eventBus, securityService, requestService, userResource, userMapper, credentialMapper, roleArrayMapper)
+ return adminUserActivity
+ },
+ {}
+ )
+ startActivityManager(activityDisplay, activityMapper, eventBus)
+
+ when: "Navigating to a default place"
+ def placeHistoryHandler = createPlaceHistoryHandler(placeController, placeHistoryMapper, new AdminUsersPlace())
+ placeHistoryHandler.handleCurrentHistory()
+
+ then: "The admin navigation view should have the right place set"
+ 1 * adminNavigationView.onPlaceChange(_ as AdminUsersPlace)
+
+ and: "The activity display should be set to admin view"
+ 1 * activityDisplay.setWidget(adminViewWidget)
+
+ and: "The admin view should have content set"
+ 1 * adminView.setContent(adminUsersView)
+
+ and: "The admin users view should have the right activity set as presenter"
+ 1 * adminUsersView.setPresenter(_ as AdminUsersActivity)
+
+ and: "The view should have received the tenants and selected realm"
+ result.tenants.length == 1
+ result.tenants[0].realm == MASTER_REALM
+ result.tenants[0].enabled
+ result.selectedRealm == null
+
+ when: "The master tenant is selected"
+ adminUsersActivity != null
+ result.appEvents.clear()
+ adminUsersActivity.onTenantSelected(MASTER_REALM)
+
+ then: "The activity should be stopped"
+ 1 * adminUsersView.setPresenter(null)
+ 1 * adminView.setContent(null)
+
+ and: "The admin users activity should be shown with the selected realm"
+ result.appEvents[0] instanceof WillGoToPlaceEvent
+ result.appEvents[0].place instanceof AdminUsersPlace
+ result.appEvents[1] instanceof GoToPlaceEvent
+ result.appEvents[1].place instanceof AdminUsersPlace
+ result.appEvents[1].place.realm == MASTER_REALM
+
+ and: "The view should have received the tenants, selected realm, and users"
+ result.tenants.length == 1
+ result.tenants[0].realm == MASTER_REALM
+ result.tenants[0].enabled
+ result.selectedRealm == MASTER_REALM
+ result.users.length == 2
+
+ when: "The user clicks Create User"
+ result.appEvents.clear()
+ adminUsersActivity.createUser()
+
+ then: "The activity should be stopped"
+ 1 * adminUsersView.setPresenter(null)
+ 1 * adminView.setContent(null)
+
+ and: "The admin user activity should be shown with the selected realm"
+ result.appEvents[0] instanceof WillGoToPlaceEvent
+ result.appEvents[0].place instanceof AdminUserPlace
+ result.appEvents[1] instanceof GoToPlaceEvent
+ result.appEvents[1].place instanceof AdminUserPlace
+ result.appEvents[1].place.realm == MASTER_REALM
+
+ and: "The admin navigation view should have the right place set"
+ 1 * adminNavigationView.onPlaceChange(_ as AdminUserPlace)
+
+ and: "The activity display should be set to admin view"
+ 1 * activityDisplay.setWidget(adminViewWidget)
+
+ and: "The admin view should have content set"
+ 1 * adminView.setContent(adminUserView)
+
+ and: "The admin user view should have the right activity set as presenter"
+ 1 * adminUserView.setPresenter(_ as AdminUserActivity)
+
+ and: "The admin user form should be cleared"
+ 1 * adminUserView.clearRoles()
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+ 1 * adminUserView.enableCreate(false)
+ 1 * adminUserView.enableUpdate(false)
+ 1 * adminUserView.enableDelete(false)
+ 1 * adminUserView.enableResetPassword(false)
+ 1 * adminUserView.enableRoles(false)
+ 1 * adminUserView.setUsernameEditEnabled(false)
+ 1 * adminUserView.setUsername(null)
+ 1 * adminUserView.setFirstName(null)
+ 1 * adminUserView.setLastName(null)
+ 1 * adminUserView.setEmail(null)
+ 1 * adminUserView.setUserEnabled(null)
+ 1 * adminUserView.enableCreate(true)
+ 1 * adminUserView.setUsernameEditEnabled(true)
+
+ when: "The user clicks the Create button"
+ adminUserActivity != null
+ result.appEvents.clear()
+ adminUserActivity.create()
+
+ then: "The activity reads the form"
+ 1 * adminUserView.setFormBusy(true)
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+ 1 * adminUserView.getUsername() >> {
+ "testuser"
+ }
+ 1 * adminUserView.getFirstName() >> {
+ return "Foo"
+ }
+ 1 * adminUserView.getLastName() >> {
+ return "Bar"
+ }
+ 1 * adminUserView.getEmail() >> {
+ return "invalidemail" // This will cause a constraint violation
+ }
+ 1 * adminUserView.getUserEnabled() >> {
+ return true
+ }
+
+ and: "The form errors should be shown"
+ 1 * adminUserView.addFormMessageError("Please provide a valid email address.")
+ 1 * adminUserView.setEmailError(true)
+ 1 * adminUserView.setFormBusy(false)
+
+ when: "The user clicks the Create button"
+ result.appEvents.clear()
+ adminUserActivity.create()
+
+ then: "The activity reads the form"
+ 1 * adminUserView.setFormBusy(true)
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+ 1 * adminUserView.getUsername() >> {
+ "testuser"
+ }
+ 1 * adminUserView.getFirstName() >> {
+ return "Foo"
+ }
+ 1 * adminUserView.getLastName() >> {
+ return "Bar"
+ }
+ 1 * adminUserView.getEmail() >> {
+ }
+ 1 * adminUserView.getUserEnabled() >> {
+ return true
+ }
+
+ and: "The success toast should be shown"
+ result.appEvents[0] instanceof ShowInfoEvent
+ result.appEvents[0].text == "TestMessageUserCreated:testuser"
+
+ and: "The form should be cleared, the activity stopped"
+ 1 * adminUserView.setFormBusy(false)
+ 1 * adminUserView.setPresenter(null)
+ 1 * adminUserView.clearRoles()
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+
+ and: "The admin users activity should be shown"
+ result.appEvents[1] instanceof WillGoToPlaceEvent
+ result.appEvents[1].place instanceof AdminUsersPlace
+ result.appEvents[2] instanceof GoToPlaceEvent
+ result.appEvents[2].place instanceof AdminUsersPlace
+ result.appEvents[2].place.realm == MASTER_REALM
+
+ and: "The view should have received the users"
+ result.users.length == 3
+
+ when: "The new user is selected"
+ result.appEvents.clear()
+ def selectedUser
+ result.users.each {
+ if (it.username == "testuser") {
+ selectedUser = it
+ return
+ }
+ }
+ selectedUser != null
+ adminUsersActivity.onUserSelected(selectedUser)
+
+ then: "The admin user activity should be shown"
+ result.appEvents[0] instanceof WillGoToPlaceEvent
+ result.appEvents[0].place instanceof AdminUserPlace
+ result.appEvents[1] instanceof GoToPlaceEvent
+ result.appEvents[1].place instanceof AdminUserPlace
+ result.appEvents[1].place.realm == MASTER_REALM
+ result.appEvents[1].place.userId == selectedUser.id
+
+ and: "The admin user form should be cleared"
+ 1 * adminUserView.clearRoles()
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+ 1 * adminUserView.enableCreate(false)
+ 1 * adminUserView.enableUpdate(false)
+ 1 * adminUserView.enableDelete(false)
+ 1 * adminUserView.enableResetPassword(false)
+ 1 * adminUserView.enableRoles(false)
+ 1 * adminUserView.setUsernameEditEnabled(false)
+
+ and: "The user should be loaded"
+ 1 * adminUserView.enableRoles(true)
+ 1 * adminUserView.setUsername("testuser")
+ 1 * adminUserView.setFirstName("Foo")
+ 1 * adminUserView.setLastName("Bar")
+ 1 * adminUserView.setEmail("[email protected]")
+ 1 * adminUserView.setUserEnabled(true)
+
+ then: "The roles should be added in the right order"
+ 1 * adminUserView.addRole(!null, "RoleLabel:read", true, false) >> {
+ result.readRoleId = it[0]
+ }
+
+ then: "The roles should be added in the right order"
+ 1 * adminUserView.addRole(!null, "RoleLabel:read-assets", false, false) >> {
+ result.readAssetsRoleId = it[0]
+ }
+
+ then: "The roles should be added in the right order"
+ 1 * adminUserView.addRole(!null, "RoleLabel:read-map", false, false) >> {
+ result.readMapRoleId = it[0]
+ }
+
+ then: "The roles should be added in the right order"
+ 1 * adminUserView.addRole(!null, "RoleLabel:write", true, false)
+
+ then: "The roles should be added in the right order"
+ 1 * adminUserView.addRole(!null, "RoleLabel:write-assets", false, false)
+
+ and: "The form should be enabled"
+ 1 * adminUserView.setFormBusy(false)
+ 1 * adminUserView.enableCreate(false)
+ 1 * adminUserView.enableUpdate(true)
+ 1 * adminUserView.enableDelete(true)
+ 1 * adminUserView.enableResetPassword(true)
+ 1 * adminUserView.setUsernameEditEnabled(false)
+
+ when: "The user selects a composite role"
+ adminUserActivity.onRoleAssigned(result.readRoleId, true)
+
+ then: "The other roles must be assigned"
+ 1 * adminUserView.toggleRoleAssigned(result.readAssetsRoleId, true)
+ 1 * adminUserView.toggleRoleAssigned(result.readMapRoleId, true)
+
+ when: "The user deselects a composite role"
+ adminUserActivity.onRoleAssigned(result.readRoleId, false)
+
+ then: "The other roles must be unassigned"
+ 1 * adminUserView.toggleRoleAssigned(result.readAssetsRoleId, false)
+ 1 * adminUserView.toggleRoleAssigned(result.readMapRoleId, false)
+
+ when: "The user selects a composite role and then deselects one of its component roles"
+ adminUserActivity.onRoleAssigned(result.readRoleId, true)
+ adminUserActivity.onRoleAssigned(result.readMapRoleId, false)
+
+ then: "The composite role must be unassigned"
+ 1 * adminUserView.toggleRoleAssigned(result.readRoleId, false)
+
+ when: "The user clicks the Update button"
+ result.appEvents.clear()
+ adminUserActivity.update()
+
+ then: "The activity reads the tenant form"
+ 1 * adminUserView.getUsername() >> {
+ "testuser"
+ }
+ 1 * adminUserView.getFirstName() >> {
+ return "Foo"
+ }
+ 1 * adminUserView.getLastName() >> {
+ return "Bar"
+ }
+ 1 * adminUserView.getEmail() >> {
+ }
+ 1 * adminUserView.getUserEnabled() >> {
+ return true
+ }
+
+ and: "The form success should be shown"
+ 1 * adminUserView.addFormMessageSuccess("TestMessageUserUpdated:testuser")
+
+ when: "The user clicks the Update button"
+ result.appEvents.clear()
+ adminUserActivity.update()
+
+ then: "The activity reads the tenant form (with invalid passwords)"
+ 1 * adminUserView.getUsername() >> {
+ "testuser"
+ }
+ 1 * adminUserView.getFirstName() >> {
+ return "Foo"
+ }
+ 1 * adminUserView.getLastName() >> {
+ return "Bar"
+ }
+ 1 * adminUserView.getEmail() >> {
+ }
+ 1 * adminUserView.getUserEnabled() >> {
+ return true
+ }
+ 1 * adminUserView.getPassword() >> {
+ return "secret123"
+ }
+ 1 * adminUserView.getPasswordControl() >> {
+ return "secret456"
+ }
+
+ and: "The form errors should be shown"
+ 1 * adminUserView.addFormMessageError("TestMessagePasswordsMustMatch")
+ 1 * adminUserView.setPasswordError(true)
+ 1 * adminUserView.addFormMessageSuccess("TestMessageUserUpdated:testuser")
+
+ when: "The user clicks the Update button"
+ result.appEvents.clear()
+ adminUserActivity.update()
+
+ then: "The activity reads the tenant form (with matching passwords)"
+ 1 * adminUserView.getUsername() >> {
+ "testuser"
+ }
+ 1 * adminUserView.getFirstName() >> {
+ return "Foo"
+ }
+ 1 * adminUserView.getLastName() >> {
+ return "Bar"
+ }
+ 1 * adminUserView.getEmail() >> {
+ }
+ 1 * adminUserView.getUserEnabled() >> {
+ return true
+ }
+ 1 * adminUserView.getPassword() >> {
+ return "secret123"
+ }
+ 1 * adminUserView.getPasswordControl() >> {
+ return "secret123"
+ }
+
+ and: "The form success should be shown"
+ 1 * adminUserView.addFormMessageSuccess("TestMessagePasswordUpdated")
+ 1 * adminUserView.addFormMessageSuccess("TestMessageUserUpdated:testuser")
+
+
+ when: "The user clicks the Delete button"
+ result.appEvents.clear()
+ adminUserActivity.delete()
+
+ then: "The activity clears the form messages"
+ 1 * adminUserView.setFormBusy(true)
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+
+ and: "The success toast should be shown"
+ result.appEvents[0] instanceof ShowInfoEvent
+ result.appEvents[0].text == "TestMessageUserDeleted:testuser"
+
+ and: "The form should be cleared, the activity stopped"
+ 1 * adminUserView.setFormBusy(false)
+ 1 * adminUserView.setPresenter(null)
+ 1 * adminUserView.clearRoles()
+ 1 * adminUserView.clearFormMessagesSuccess()
+ 1 * adminUserView.clearFormMessagesError()
+ 1 * adminUserView.setUsernameError(false)
+ 1 * adminUserView.setFirstNameError(false)
+ 1 * adminUserView.setLastNameError(false)
+ 1 * adminUserView.setEmailError(false)
+ 1 * adminUserView.setUserEnabledError(false)
+ 1 * adminUserView.setPasswordError(false)
+
+ and: "The admin tenants activity should be shown"
+ result.appEvents[1] instanceof WillGoToPlaceEvent
+ result.appEvents[1].place instanceof AdminUsersPlace
+ result.appEvents[2] instanceof GoToPlaceEvent
+ result.appEvents[2].place instanceof AdminUsersPlace
+ result.appEvents[2].place.realm == MASTER_REALM
+
+ and: "The view should have received the tenants and users"
+ result.tenants.length == 1
+ result.tenants[0].realm == MASTER_REALM
+ result.tenants[0].enabled
+ result.selectedRealm == MASTER_REALM
+ result.users.length == 2
+
+ and: "The server should be stopped"
+ stopContainer(container);
+ }
+}