Github user GERey commented on a diff in the pull request:
https://github.com/apache/incubator-usergrid/pull/232#discussion_r28732352
--- Diff:
stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
---
@@ -17,212 +17,238 @@
package org.apache.usergrid.rest.management;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.UUID;
-
-import javax.mail.Folder;
-import javax.mail.Message;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.Store;
-import javax.mail.internet.MimeMultipart;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-
-import com.eaio.uuid.UUIDGen;
-import com.fasterxml.jackson.databind.JsonNode;
-
-import org.junit.Rule;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.representation.Form;
+import org.apache.commons.lang.StringUtils;
+import org.apache.usergrid.management.AccountCreationProps;
+import org.apache.usergrid.persistence.model.util.UUIDGenerator;
+import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
+import org.apache.usergrid.rest.test.resource2point0.model.*;
+import org.apache.usergrid.rest.test.resource2point0.model.Collection;
import org.junit.Test;
import org.jvnet.mock_javamail.Mailbox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.usergrid.management.UserInfo;
-import org.apache.usergrid.persistence.model.util.UUIDGenerator;
-import org.apache.usergrid.rest.AbstractRestIT;
-import org.apache.usergrid.rest.ITSetup;
-import org.apache.usergrid.rest.TestContextSetup;
-import org.apache.usergrid.rest.test.security.TestAppUser;
-import org.apache.usergrid.rest.test.security.TestUser;
-import org.apache.commons.lang.StringUtils;
+import javax.mail.*;
+import javax.mail.internet.MimeMultipart;
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.util.*;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.UniformInterfaceException;
-import com.sun.jersey.core.util.MultivaluedMapImpl;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static
org.apache.usergrid.management.AccountCreationProps.PROPERTIES_ADMIN_RESETPW_URL;
-import static
org.apache.usergrid.management.AccountCreationProps.PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION;
-import static
org.apache.usergrid.management.AccountCreationProps.PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS;
-import static
org.apache.usergrid.management.AccountCreationProps.PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS;
-import static
org.apache.usergrid.management.AccountCreationProps.PROPERTIES_SYSADMIN_EMAIL;
-import static org.apache.usergrid.utils.MapUtils.hashMap;
+import static org.apache.usergrid.management.AccountCreationProps.*;
+import static org.junit.Assert.*;
public class RegistrationIT extends AbstractRestIT {
- private static final Logger logger = LoggerFactory.getLogger(
RegistrationIT.class );
-
- private static final ITSetup setup = ITSetup.getInstance();
-
- @Rule
- public TestContextSetup context = new TestContextSetup( this );
-
-
- @Test
- public void postCreateOrgAndAdmin() throws Exception {
+ private static final Logger logger =
LoggerFactory.getLogger(RegistrationIT.class);
- Map<String, String> originalProperties = getRemoteTestProperties();
+ public Map<String, Object> getRemoteTestProperties() {
+ return
clientSetup.getRestClient().testPropertiesResource().get().getProperties();
+ }
- try {
- setTestProperty( PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS,
"false" );
- setTestProperty( PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS,
"false" );
- setTestProperty( PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION,
"false" );
- setTestProperty( PROPERTIES_SYSADMIN_EMAIL,
"[email protected]" );
+ /**
+ * Sets a management service property locally and remotely.
+ */
+ public void setTestProperty(String key, Object value) {
+ // set the value remotely (in the Usergrid instance running in
Tomcat classloader)
+ Entity props = new Entity();
+ props.put(key, value);
+ clientSetup.getRestClient().testPropertiesResource().post(props);
-// JsonNode node = postCreateOrgAndAdmin( "test-org-1",
"test-user-1", "Test User",
-// "[email protected]", "testpassword" );
+ }
+ public void setTestProperties(Map<String, Object> props) {
+ Entity properties = new Entity();
+ // set the values locally (in the Usergrid instance here in the
JUnit classloader
+ for (String key : props.keySet()) {
+ properties.put(key, props.get(key));
- final String username =
"registrationUser"+UUIDGenerator.newTimeUUID();
- final String email = username+"@usergrid.com" ;
- final String password = "password";
+ }
- final TestUser user1 = new TestAppUser(username , password,
email);
+ // set the values remotely (in the Usergrid instance running in
Tomcat classloader)
+
clientSetup.getRestClient().testPropertiesResource().post(properties);
+ }
- context.withOrg( "org" + UUIDGenerator.newTimeUUID()
).withApp( "app" + UUIDGenerator.newTimeUUID() ).withUser( user1
).createNewOrgAndUser();
- context.createAppForOrg();
+ public String getTokenFromMessage(Message msg) throws IOException,
MessagingException {
+ String body = ((MimeMultipart)
msg.getContent()).getBodyPart(0).getContent().toString();
+ // TODO better token extraction
+ // this is going to get the wrong string if the first part is not
+ // text/plain and the url isn't the last character in the email
+ return StringUtils.substringAfterLast(body, "token=");
+ }
- final UUID owner_uuid = context.getActiveUser().getUuid();
+ public User postAddAdminToOrg(String organizationName, String email,
String password, Token token) throws IOException {
+
+ ApiResponse apiResponse = this
+ .management()
+ .orgs()
+ .organization(organizationName)
+ .users()
+ .getResource(true, token)
+ .type(MediaType.APPLICATION_JSON_TYPE)
+ .accept(MediaType.APPLICATION_ATOM_XML_TYPE)
+ .post(ApiResponse.class, new User().chainPut("email",
email).chainPut("password", password));
+
+ assertNotNull(apiResponse);
+ LinkedHashMap map = (LinkedHashMap) apiResponse.getData();
+ return new User((LinkedHashMap) map.get("user"));
+ }
-// refreshIndex("test-organization", "test-app");
-//
-// UUID owner_uuid =
-// UUID.fromString( node.findPath( "data" ).findPath(
"owner" ).findPath( "uuid" ).textValue() );
+ private Message[] getMessages(String host, String user, String
password) throws MessagingException, IOException {
- List<Message> inbox = org.jvnet.mock_javamail.Mailbox.get(
"[email protected]" );
+ Session session = Session.getDefaultInstance(new Properties());
+ Store store = session.getStore("imap");
+ store.connect(host, user, password);
- assertFalse( inbox.isEmpty() );
+ Folder folder = store.getFolder("inbox");
+ folder.open(Folder.READ_ONLY);
+ Message[] msgs = folder.getMessages();
- Message account_confirmation_message = inbox.get( 0 );
- assertEquals( "User Account Confirmation: " + email,
- account_confirmation_message.getSubject() );
+ for (Message m : msgs) {
+ logger.info("Subject: " + m.getSubject());
+ logger.info(
+ "Body content 0 " + ((MimeMultipart)
m.getContent()).getBodyPart(0).getContent());
+ logger.info(
+ "Body content 1 " + ((MimeMultipart)
m.getContent()).getBodyPart(1).getContent());
+ }
+ return msgs;
+ }
- String token = getTokenFromMessage(
account_confirmation_message );
- logger.info( token );
+ @Test
+ public void postCreateOrgAndAdmin() throws Exception {
- setup.getMgmtSvc().disableAdminUser( owner_uuid );
+ Map<String, Object> originalProperties = getRemoteTestProperties();
- refreshIndex(context.getOrgName(), context.getAppName());
+ try {
+ setTestProperty(PROPERTIES_SYSADMIN_APPROVES_ADMIN_USERS,
"false");
+ setTestProperty(PROPERTIES_SYSADMIN_APPROVES_ORGANIZATIONS,
"false");
+ setTestProperty(PROPERTIES_ADMIN_USERS_REQUIRE_CONFIRMATION,
"false");
+ setTestProperty(PROPERTIES_SYSADMIN_EMAIL,
"sysadmin-1@usergrid");
+ final String username = "registrationUser" +
UUIDGenerator.newTimeUUID();
+ final String email = username + "@usergrid.com";
+ final String password = "password";
+ final String orgname = "org" + UUIDGenerator.newTimeUUID();
+ final String appname = "app" + UUIDGenerator.newTimeUUID();
+
+ Organization organization = this
+ .management()
+ .orgs()
+ .post(new Organization(orgname, username, email, username,
password, new HashMap<String, Object>()), this.getAdminToken());
+ Token orgToken = this.management().token().post(new
Token(username, password));
+
this.management().orgs().organization(organization.getName()).app().post(new
Application(appname), orgToken);
+
+
+ List<Message> inbox =
org.jvnet.mock_javamail.Mailbox.get(email);
+ Message[] messages = inbox.toArray(new Message[inbox.size()]);
+ assertNotEquals(messages.length, 0);
+
+ Message account_confirmation_message = messages[0];
+ assertEquals("User Account Confirmation: " + email,
+ account_confirmation_message.getSubject());
+
+ String token =
getTokenFromMessage(account_confirmation_message);
+ logger.info(token);
+
+
setTestProperty(AccountCreationProps.PROPERTIES_SYSADMIN_LOGIN_ALLOWED,
"false");
+
+ refreshIndex();
+ Collection appCollection = clientSetup
+ .getRestClient()
+ .org(orgname)
+ .app(appname)
+ .withToken(orgToken).get();
+ Entity application = appCollection.next();
+ assertNotNull(application);
+ Entity appUser = clientSetup
+ .getRestClient()
+ .org(orgname)
+ .app(appname)
+ .users()
+ .withToken(orgToken)
+ .post(new User("testuser", "Test User",
"[email protected]", "password"));
try {
- resource().path( "/management/token" ).queryParam(
"grant_type", "password" )
- .queryParam( "username", username ).queryParam(
"password", password )
- .accept( MediaType.APPLICATION_JSON ).type(
MediaType.APPLICATION_JSON_TYPE )
- .get( String.class );
- fail( "request for disabled user should fail" );
+
this.management().orgs().organization(organization.getName()).users().user(username)
+ .getResource(true, getAdminToken())
+ .get(String.class);
+ fail("request for disabled admin should fail");
+ } catch (UniformInterfaceException uie) {
+ assertEquals(401, uie.getResponse().getStatus());
}
- catch ( UniformInterfaceException uie ) {
- ClientResponse.Status status =
uie.getResponse().getClientResponseStatus();
- JsonNode body = mapper.readTree(
uie.getResponse().getEntity( String.class ));
- assertEquals( "user disabled", body.findPath(
"error_description" ).textValue() );
+
setTestProperty(AccountCreationProps.PROPERTIES_SYSADMIN_LOGIN_ALLOWED, "true");
+ Token objToken = management()
+ .token()
+ .post(new Token(username, password));
+ try {
+ clientSetup
+ .getRestClient()
+ .org(orgname)
+ .app(appname)
+ .users()
+ .user("testuser")
+ .revoketokens()
+ .withToken(objToken)
+ .put();
+ } catch (UniformInterfaceException uie0) {
+ fail("Failed to revoke user tokens");
}
-
- setup.getMgmtSvc().deactivateUser(
setup.getEmf().getManagementAppId(), owner_uuid );
try {
- resource().path( "/management/token" ).queryParam(
"grant_type", "password" )
- .queryParam( "username", username ).queryParam(
"password", password)
- .accept( MediaType.APPLICATION_JSON ).type(
MediaType.APPLICATION_JSON_TYPE )
- .get( String.class );
- fail( "request for deactivated user should fail" );
+ clientSetup
+ .getRestClient()
+ .org(orgname)
+ .app(appname)
+ .users()
+ .user("testuser")
+ .withToken(objToken)
+ .put(appUser.chainPut("activated",
"false").chainPut("deactivated", System.currentTimeMillis()));
+ refreshIndex();
+ } catch (UniformInterfaceException uie0) {
+ fail("Failed to deactivate the user");
}
- catch ( UniformInterfaceException uie ) {
- ClientResponse.Status status =
uie.getResponse().getClientResponseStatus();
- JsonNode body = mapper.readTree(
uie.getResponse().getEntity( String.class ));
- assertEquals( "user not activated", body.findPath(
"error_description" ).textValue() );
--- End diff --
We still want to read the error_description to make sure we are getting the
correct text back. It'll make the refactor of the rest tier errors easier to
verify.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---