usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From toddn...@apache.org
Subject [18/50] [abbrv] incubator-usergrid git commit: Merge branch 'two-dot-o' into two-dot-o-dev
Date Mon, 01 Jun 2015 21:48:27 GMT
Merge branch 'two-dot-o' into two-dot-o-dev

Conflicts:
	stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java
	stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
	stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
	stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java
	stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java
	stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/4b812547
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/4b812547
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/4b812547

Branch: refs/heads/USERGRID-628
Commit: 4b81254776679b9f37a312605017e2537a8e69ee
Parents: 73e2292 a784016
Author: Dave Johnson <dmjohnson@apigee.com>
Authored: Thu May 28 07:51:43 2015 -0400
Committer: Dave Johnson <dmjohnson@apigee.com>
Committed: Thu May 28 07:51:43 2015 -0400

----------------------------------------------------------------------
 README.md                                       |   2 +-
 docs/file-storage-configuration.md              |  39 ++
 docs/get_2.0_running_locally.md                 | 165 ++++++
 docs/index.md                                   |   3 +-
 .../main/resources/usergrid-default.properties  |  24 +-
 .../persistence/cassandra/CassandraService.java |  54 +-
 .../org/apache/usergrid/utils/JsonUtils.java    |   2 +-
 stack/pom.xml                                   |  26 +-
 .../rest/applications/ServiceResource.java      |   2 +-
 .../rest/management/ManagementResource.java     | 403 ++++++++++++++-
 .../organizations/OrganizationsResource.java    |  20 +-
 .../rest/management/users/UserResource.java     |  42 ++
 .../rest/management/users/UsersResource.java    |  45 +-
 .../OAuth2AccessTokenSecurityFilter.java        |   4 +
 .../applications/assets/AssetResourceIT.java    | 302 +++++++----
 .../rest/applications/utils/UserRepo.java       |  36 +-
 .../rest/filters/ContentTypeResourceIT.java     | 177 +++----
 .../rest/management/ManagementResourceIT.java   | 513 ++++++++++++++++++-
 .../rest/management/RegistrationIT.java         | 441 ++++++++--------
 .../rest/test/PropertiesResourceIT.java         |  23 +-
 .../test/resource2point0/AbstractRestIT.java    |   2 -
 .../rest/test/resource2point0/ClientSetup.java  |  19 +-
 .../src/test/resources/cat-larger-than-6mb.jpg  | Bin 0 -> 9799257 bytes
 stack/services/pom.xml                          |  11 +
 .../usergrid/management/ManagementService.java  |   5 +-
 .../cassandra/ManagementServiceImpl.java        |   7 +
 .../apache/usergrid/security/shiro/Realm.java   |  10 +-
 .../usergrid/security/tokens/TokenService.java  |   3 +
 .../tokens/cassandra/TokenServiceImpl.java      |  44 +-
 .../assets/data/AwsSdkS3BinaryStore.java        | 230 +++++++++
 .../services/assets/data/S3BinaryStore.java     | 255 ++++++---
 .../security/tokens/TokenServiceIT.java         |  42 ++
 ugc/README.md                                   |   2 +-
 33 files changed, 2268 insertions(+), 685 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/config/src/main/resources/usergrid-default.properties
----------------------------------------------------------------------
diff --cc stack/config/src/main/resources/usergrid-default.properties
index 9bc95e5,e5e269e..6dab7a8
--- a/stack/config/src/main/resources/usergrid-default.properties
+++ b/stack/config/src/main/resources/usergrid-default.properties
@@@ -80,11 -78,16 +80,15 @@@ cassandra.url=localhost:916
  # Name of Cassandra cluster
  cassandra.cluster=Test Cluster
  
+ # Keyspace names to be used (see also the locks keyspace below)
+ cassandra.system.keyspace=Usergrid
+ cassandra.application.keyspace=Usergrid_Applications
+ 
  cassandra.keyspace.strategy=org.apache.cassandra.locator.SimpleStrategy
  #cassandra.keyspace.strategy=org.apache.cassandra.locator.NetworkTopologyStrategy
 -#cassandra.keyspace.strategy.options.replication_factor=1
 -#cassandra.keyspace.strategy.options.us-east=1
  
  cassandra.keyspace.replication=replication_factor:1
 +#cassandra.keyspace.replication=us-east:3
  
  cassandra.username=
  cassandra.password=

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java
----------------------------------------------------------------------
diff --cc stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java
index b998084,fb71b69..a08c543
--- a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java
+++ b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/CassandraService.java
@@@ -17,60 -17,41 +17,39 @@@
  package org.apache.usergrid.persistence.cassandra;
  
  
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- import java.util.UUID;
- 
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- 
- import org.apache.usergrid.locking.LockManager;
- import org.apache.usergrid.persistence.core.astyanax.CassandraFig;
- import org.apache.usergrid.persistence.hector.CountingMutator;
- import org.apache.usergrid.utils.MapUtils;
- 
  import com.google.inject.Injector;
- 
  import me.prettyprint.cassandra.connection.HConnectionManager;
  import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
- import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
- import me.prettyprint.cassandra.serializers.BytesArraySerializer;
- import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer;
- import me.prettyprint.cassandra.serializers.LongSerializer;
- import me.prettyprint.cassandra.serializers.StringSerializer;
- import me.prettyprint.cassandra.serializers.UUIDSerializer;
+ import me.prettyprint.cassandra.serializers.*;
  import me.prettyprint.cassandra.service.CassandraHostConfigurator;
  import me.prettyprint.cassandra.service.ThriftKsDef;
- import me.prettyprint.hector.api.Cluster;
- import me.prettyprint.hector.api.ConsistencyLevelPolicy;
- import me.prettyprint.hector.api.HConsistencyLevel;
- import me.prettyprint.hector.api.Keyspace;
- import me.prettyprint.hector.api.Serializer;
+ import me.prettyprint.hector.api.*;
 -import me.prettyprint.hector.api.beans.*;
 +import me.prettyprint.hector.api.beans.ColumnSlice;
 +import me.prettyprint.hector.api.beans.DynamicComposite;
 +import me.prettyprint.hector.api.beans.HColumn;
  import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
  import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
  import me.prettyprint.hector.api.factory.HFactory;
  import me.prettyprint.hector.api.mutation.Mutator;
 -import me.prettyprint.hector.api.query.*;
 +import me.prettyprint.hector.api.query.ColumnQuery;
 +import me.prettyprint.hector.api.query.QueryResult;
 +import me.prettyprint.hector.api.query.SliceQuery;
+ import org.apache.usergrid.locking.LockManager;
 -import org.apache.usergrid.persistence.IndexBucketLocator;
 -import org.apache.usergrid.persistence.IndexBucketLocator.IndexType;
 -import org.apache.usergrid.persistence.cassandra.index.IndexBucketScanner;
 -import org.apache.usergrid.persistence.cassandra.index.IndexScanner;
+ import org.apache.usergrid.persistence.core.astyanax.CassandraFig;
+ import org.apache.usergrid.persistence.hector.CountingMutator;
+ import org.apache.usergrid.utils.MapUtils;
+ import org.slf4j.Logger;
+ import org.slf4j.LoggerFactory;
+ 
+ import java.nio.ByteBuffer;
+ import java.util.*;
  
  import static me.prettyprint.cassandra.service.FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE;
- import static me.prettyprint.hector.api.factory.HFactory.createColumn;
- import static me.prettyprint.hector.api.factory.HFactory.createSliceQuery;
- import static me.prettyprint.hector.api.factory.HFactory.createVirtualKeyspace;
+ import static me.prettyprint.hector.api.factory.HFactory.*;
  import static org.apache.commons.collections.MapUtils.getIntValue;
  import static org.apache.commons.collections.MapUtils.getString;
 -import static org.apache.usergrid.persistence.cassandra.ApplicationCF.ENTITY_ID_SETS;
  import static org.apache.usergrid.persistence.cassandra.CassandraPersistenceUtils.batchExecute;
  import static org.apache.usergrid.utils.ConversionUtils.bytebuffer;
 -import static org.apache.usergrid.utils.ConversionUtils.bytebuffers;
  import static org.apache.usergrid.utils.JsonUtils.mapToFormattedJsonString;
  import static org.apache.usergrid.utils.MapUtils.asMap;
  import static org.apache.usergrid.utils.MapUtils.filter;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/pom.xml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/main/java/org/apache/usergrid/rest/management/organizations/OrganizationsResource.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java
----------------------------------------------------------------------
diff --cc stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java
index 4ef693b,10909b4..c9a8468
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java
@@@ -17,54 -17,31 +17,45 @@@
  package org.apache.usergrid.rest.filters;
  
  
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- 
- import javax.ws.rs.core.HttpHeaders;
- import javax.ws.rs.core.MediaType;
- 
- import org.apache.usergrid.rest.test.resource2point0.endpoints.TokenResource;
- import org.apache.usergrid.rest.test.resource2point0.model.Token;
- import org.apache.usergrid.rest.test.resource2point0.model.User;
- import org.junit.Ignore;
- import org.junit.Rule;
- import org.junit.Test;
- 
- import org.apache.usergrid.rest.AbstractRestIT;
- import org.apache.usergrid.rest.TestContextSetup;
- import org.apache.usergrid.utils.JsonUtils;
- import org.apache.usergrid.utils.UUIDUtils;
- 
+ import com.sun.jersey.api.client.ClientResponse;
+ import com.sun.jersey.api.client.WebResource;
+ import com.sun.jersey.api.representation.Form;
 -import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
 +import org.apache.http.Header;
 +import org.apache.http.HttpEntity;
 +import org.apache.http.HttpHost;
 +import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.ParseException;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
 +import org.apache.http.client.methods.HttpPost;
 +import org.apache.http.entity.StringEntity;
 +import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.params.BasicHttpParams;
 +import org.apache.http.util.EntityUtils;
++import org.apache.usergrid.rest.test.resource2point0.model.Token;
++import org.apache.usergrid.rest.test.resource2point0.model.User;
+ import org.apache.usergrid.utils.JsonUtils;
+ import org.apache.usergrid.utils.UUIDUtils;
+ import org.junit.Ignore;
+ import org.junit.Test;
+ 
++import javax.ws.rs.core.HttpHeaders;
+ import javax.ws.rs.core.MediaType;
+ import javax.ws.rs.core.MultivaluedMap;
++import java.io.IOException;
++import java.text.ParseException;
+ import java.util.List;
++import java.util.Map;
  
- import static org.junit.Assert.assertEquals;
  import static org.apache.usergrid.utils.MapUtils.hashMap;
+ import static org.junit.Assert.assertEquals;
  
  
- /** @author tnine */
+ /**
+  * @author tnine
+  */
  
  // @Ignore("Client login is causing tests to fail due to socket closure by grizzly.  Need to re-enable once we're not
  // using grizzly to test")
 -public class ContentTypeResourceIT extends AbstractRestIT {
 +public class ContentTypeResourceIT extends org.apache.usergrid.rest.test.resource2point0.AbstractRestIT {
 +
  
- 
- 
      /**
       * Creates a simple entity of type game. Does not set the content type. The type should be set to json to match the
       * body
@@@ -72,35 -49,22 +63,36 @@@
      @Test
      public void correctHeaders() throws Exception {
  
 -        String json = JsonUtils.mapToFormattedJsonString(hashMap("name", "Solitaire1"));
 +        User user = new User("shawn","shawn","shawn@email.com","aliensquirrel");
 +        this.app().collection("users").post(user);
 +        Token token = this.app().token().post(new Token("shawn","aliensquirrel"));
  
 -        WebResource.Builder builder = app().collection("games").getResource(true)
 -            .queryParam("access_token", this.getAdminToken().getAccessToken())
 -            .type(MediaType.APPLICATION_JSON_TYPE)
 -            .accept(MediaType.APPLICATION_JSON);
 +        Map<String, String> data = hashMap( "name", "Solitaire1" );
  
 -        ClientResponse clientResponse = builder.post(ClientResponse.class, json);
 +        String json = JsonUtils.mapToFormattedJsonString(data);
  
 -        assertEquals(200, clientResponse.getStatus());
 +        DefaultHttpClient client = new DefaultHttpClient();
  
 -        MultivaluedMap<String, String> headers = clientResponse.getHeaders();
 +        HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() );
 +
 +        HttpPost post = new HttpPost( String.format("/%s/%s/games", this.clientSetup.getOrganization().getName(), this.clientSetup.getAppName()) );
 +        post.setEntity(new StringEntity(json));
 +        post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.getAccessToken());
 +        post.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
 +        post.setHeader(HttpHeaders.CONTENT_TYPE, "*/*");
 +
 +        HttpResponse rsp = client.execute( host, post );
 +
 +        printResponse( rsp );
 +
 +        assertEquals( 200, rsp.getStatusLine().getStatusCode() );
 +
 +        Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE );
 +
 +        assertEquals( 1, headers.length );
 +
 +        assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() );
+ 
 -        List<String> contentType = headers.get("Content-Type");
 -        assertEquals(1, contentType.size());
 -        assertEquals(MediaType.APPLICATION_JSON, contentType.get(0));
      }
  
  
@@@ -110,35 -74,22 +102,30 @@@
       */
      @Test
      public void textPlainContentType() throws Exception {
 -        String json = JsonUtils.mapToFormattedJsonString(hashMap("name", "Solitaire2"));
 -        WebResource.Builder builder = app().getResource(true)
 -            .queryParam("access_token", this.getAdminToken().getAccessToken())
 -            .accept(MediaType.APPLICATION_JSON)
 -            .type(MediaType.TEXT_PLAIN_TYPE);
 +        User user = new User("shawn","shawn","shawn@email.com","aliensquirrel");
 +        this.app().collection("users").post(user);
 +        Token token = this.app().token().post(new Token("shawn","aliensquirrel"));
 +        Map<String, String> data = hashMap( "name", "Solitaire2" );
  
 -        ClientResponse clientResponse = builder.post(ClientResponse.class, json);
 +        String json = JsonUtils.mapToFormattedJsonString( data );
  
 -        assertEquals(200, clientResponse.getStatus());
 +        DefaultHttpClient client = new DefaultHttpClient();
  
 -        MultivaluedMap<String, String> headers = clientResponse.getHeaders();
 +        HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() );
  
 -        List<String> contentType = headers.get("Content-Type");
 -        assertEquals(1, contentType.size());
 -        assertEquals(MediaType.APPLICATION_JSON, contentType.get(0));
 +        HttpPost post = new HttpPost( String.format("/%s/%s/games", this.clientSetup.getOrganization().getName(), this.clientSetup.getAppName()) );
 +
 +        post.setEntity( new StringEntity( json ) );
 +        post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + token.getAccessToken() );
 +        post.setHeader( HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON );
 +        post.setHeader( HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN );
 +
 +        HttpResponse rsp = client.execute( host, post );
 +
 +        printResponse( rsp );
 +
 +        assertEquals( 200, rsp.getStatusLine().getStatusCode() );
  
-         Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE );
- 
-         assertEquals( 1, headers.length );
- 
-         assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() );
      }
  
  
@@@ -259,52 -183,31 +232,54 @@@
       */
      @Test
      public void noAcceptGet() throws Exception {
 +        User user = new User("shawn","shawn","shawn@email.com","aliensquirrel");
 +        this.app().collection("users").post(user);
 +        Token token = this.app().token().post(new Token("shawn", "aliensquirrel"));
 +        Map<String, String> data = hashMap("name", "bar");
 +
 +        String json = JsonUtils.mapToFormattedJsonString( data );
 +
 +        DefaultHttpClient client = new DefaultHttpClient();
 +
 +        HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() );
 +
 +        HttpPost post = new HttpPost( String.format("/%s/%s/games", this.clientSetup.getOrganization().getName(), this.clientSetup.getAppName()) );
 +
 +        post.setEntity( new StringEntity( json ) );
 +        post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + token.getAccessToken() );
 +        post.setHeader( HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON );
 +        post.setHeader( HttpHeaders.CONTENT_TYPE, "*/*" );
 +
 +        HttpResponse rsp = client.execute( host, post );
 +
-         printResponse( rsp );
  
-         assertEquals( 200, rsp.getStatusLine().getStatusCode() );
+         WebResource.Builder builder = app().collection("games").getResource(true)
+             .queryParam("access_token", this.getAdminToken().getAccessToken())
+             .type(MediaType.APPLICATION_JSON_TYPE);
  
-         Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE );
+         ClientResponse clientResponse = builder.post(ClientResponse.class, JsonUtils.mapToJsonString(hashMap("name", "bar")));
  
-         assertEquals( 1, headers.length );
+         assertEquals(200, clientResponse.getStatus());
  
-         assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() );
+         MultivaluedMap<String, String> headers = clientResponse.getHeaders();
+ 
+         List<String> contentType = headers.get("Content-Type");
+         assertEquals(1, contentType.size());
+         assertEquals(MediaType.APPLICATION_JSON, contentType.get(0));
  
          //do the get with no content type, it should get set to application/json
 +        HttpPost get = new HttpPost( String.format("/%s/%s/games", this.clientSetup.getOrganization().getName(), this.clientSetup.getAppName()) );
 +
 +        get.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + token.getAccessToken() );
+         clientResponse = builder.get(ClientResponse.class);
  
-         rsp = client.execute( host, get );
- 
-         printResponse( rsp );
- 
-         assertEquals( 200, rsp.getStatusLine().getStatusCode() );
+         assertEquals(200, clientResponse.getStatus());
  
-         headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE );
+         headers = clientResponse.getHeaders();
  
-         assertEquals( 1, headers.length );
- 
-         assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() );
+         contentType = headers.get("Content-Type");
+         assertEquals(1, contentType.size());
+         assertEquals(MediaType.APPLICATION_JSON, contentType.get(0));
      }
  
  

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java
----------------------------------------------------------------------
diff --cc stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java
index c9c27e1,7955ebc..e35d339
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/ManagementResourceIT.java
@@@ -25,44 -21,71 +21,71 @@@ import com.fasterxml.jackson.databind.J
  import com.sun.jersey.api.client.ClientResponse.Status;
  import com.sun.jersey.api.client.UniformInterfaceException;
  import com.sun.jersey.api.representation.Form;
+ import org.apache.commons.lang.RandomStringUtils;
+ import org.apache.usergrid.management.OrganizationOwnerInfo;
+ import org.apache.usergrid.persistence.index.utils.UUIDUtils;
 -import org.apache.usergrid.rest.AbstractRestIT;
+ import org.apache.usergrid.rest.management.organizations.OrganizationsResource;
++import org.apache.usergrid.rest.test.resource2point0.model.*;
++import org.apache.usergrid.rest.test.resource2point0.model.Collection;
+ import org.junit.Test;
+ import org.slf4j.Logger;
+ import org.slf4j.LoggerFactory;
  
+ import javax.ws.rs.core.MediaType;
  import java.io.IOException;
+ import java.util.*;
  
++import static org.apache.usergrid.rest.AbstractRestIT.logNode;
+ import static org.apache.usergrid.rest.management.ManagementResource.USERGRID_CENTRAL_URL;
  import static org.apache.usergrid.utils.MapUtils.hashMap;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertTrue;
- 
+ import static org.junit.Assert.*;
  
 -
  /**
   * @author tnine
   */
--
 -public class ManagementResourceIT extends AbstractRestIT {
 +public class ManagementResourceIT extends org.apache.usergrid.rest.test.resource2point0.AbstractRestIT {
  
+     private static final Logger logger = LoggerFactory.getLogger(ManagementResourceIT.class);
+ 
 -
      public ManagementResourceIT() throws Exception {
  
      }
  
  
      /**
+      * Test if we can reset our password as an admin
+      */
+     @Test
+     public void setSelfAdminPasswordAsAdmin() {
+ 
+         String newPassword = "foo";
+ 
 -        Map<String, String> data = new HashMap<String, String>();
++        Map<String, Object> data = new HashMap<>();
+         data.put( "newpassword", newPassword );
+         data.put( "oldpassword", "test" );
+ 
 -        // change the password as admin. The old password isn't required
 -        JsonNode node = resource().path( "/management/users/test/password" ).accept( MediaType.APPLICATION_JSON )
 -                                  .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class, data );
 -
 -        assertNull( getError( node ) );
++        JsonNode responseNode = resource().path( "/management/users/test/password" )
++            .accept( MediaType.APPLICATION_JSON )
++            .type( MediaType.APPLICATION_JSON_TYPE )
++            .post( JsonNode.class, data );
++        logNode( responseNode );
+ 
 -        adminAccessToken = mgmtToken( "test", newPassword );
++        String adminAccessToken = context().getToken().getAccessToken();
+ 
+         data.put( "oldpassword", newPassword );
+         data.put( "newpassword", "test" );
+ 
 -        node = resource().path( "/management/users/test/password" ).queryParam( "access_token", adminAccessToken )
 -                         .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
 -                         .post( JsonNode.class, data );
++        responseNode = resource().path( "/management/users/test/password" )
++            .queryParam( "access_token", adminAccessToken )
++            .accept( MediaType.APPLICATION_JSON )
++            .type( MediaType.APPLICATION_JSON_TYPE )
++            .post( JsonNode.class, data );
+ 
 -        assertNull( getError( node ) );
++        logNode( responseNode );
+     }
+ 
+ 
 -
+     /**
       * Test that admins can't view organizations they're not authorized to view.
       */
      @Test
@@@ -211,54 -251,488 +234,490 @@@
          Map<String, String> actorMap = new HashMap<String, String>();
          actorMap.put( "displayName", name );
          actorMap.put( "username", user );
 -        activityPayload.put( "actor", actorMap );
 -        resource().path( "/" + orgInfo.getName() + "/" + appInfo.getName() + "/users/" + user + "/activities" )
 -                  .queryParam( "access_token", this.adminToken() ).accept( MediaType.APPLICATION_JSON )
 -                  .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, activityPayload );
 +        activityPayload.put("actor", actorMap);
 +        Entity entity = this.app().collection("users").entity(user).collection("activities").post(new Entity(activityPayload));
- 
      }
  
  
      @Test
      public void mgmtCreateAndGetApplication() throws Exception {
  
 -        Map<String, String> data = new HashMap<String, String>();
 -        data.put( "name", "mgmt-org-app" );
  
 -        String orgName = orgInfo.getName();
  
          // POST /applications
 -        JsonNode appdata = mapper.readTree( resource().path( "/management/orgs/" + orgName + "/applications" )
 -                                                      .queryParam( "access_token", this.adminToken() )
 -                                                      .accept( MediaType.APPLICATION_JSON )
 -                                                      .type( MediaType.APPLICATION_JSON_TYPE )
 -                                                      .post( String.class, data ) );
 -        logNode( appdata );
 -        appdata = getEntity( appdata, 0 );
 -
 -        refreshIndex( this.orgInfo.getName(), this.appInfo.getName() );
 -
 -        assertEquals( orgName.toLowerCase() + "/mgmt-org-app", appdata.get( "name" ).asText() );
 -        assertNotNull( appdata.get( "metadata" ) );
 -        assertNotNull( appdata.get( "metadata" ).get( "collections" ) );
 -        assertNotNull( appdata.get( "metadata" ).get( "collections" ).get( "roles" ) );
 -        assertNotNull( appdata.get( "metadata" ).get( "collections" ).get( "roles" ).get( "title" ) );
 -        assertEquals( "Roles", appdata.get( "metadata" ).get( "collections" ).get( "roles" ).get( "title" ).asText() );
 -        assertEquals( 3, appdata.get( "metadata" ).get( "collections" ).get( "roles" ).get( "count" ).asInt() );
 -
 -        refreshIndex( this.orgInfo.getName(), this.appInfo.getName() );
 +        ApiResponse apiResponse = management().orgs().organization(clientSetup.getOrganizationName()).app().post(new Application("mgmt-org-app"));
 +
 +
 +        refreshIndex();
 +
 +        Entity appdata = apiResponse.getEntities().get(0);
 +        assertEquals((clientSetup.getOrganizationName() + "/mgmt-org-app").toLowerCase(), appdata.get("name").toString().toLowerCase());
 +        assertNotNull(appdata.get("metadata"));
 +        Map metadata =(Map) appdata.get( "metadata" );
 +        assertNotNull(metadata.get("collections"));
 +        Map collections =  ((Map)metadata.get("collections"));
 +        assertNotNull(collections.get("roles"));
 +        Map roles =(Map) collections.get("roles");
 +        assertNotNull(roles.get("title"));
 +        assertEquals("Roles", roles.get("title").toString());
 +        assertEquals(3, roles.size());
 +
 +        refreshIndex(   );
  
          // GET /applications/mgmt-org-app
 -        appdata = mapper.readTree(
 -                resource().path( "/management/orgs/" + orgInfo.getUuid() + "/applications/mgmt-org-app" )
 -                          .queryParam( "access_token", this.adminToken() )
 -                          .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
 -                          .get( String.class ) );
 -        logNode( appdata );
 -
 -        assertEquals( orgName.toLowerCase(), appdata.get( "organization" ).asText() );
 -        assertEquals( "mgmt-org-app", appdata.get( "applicationName" ).asText() );
 -        assertEquals( "http://sometestvalue/" + orgName.toLowerCase() + "/mgmt-org-app",
 -                appdata.get( "uri" ).textValue() );
 -        appdata = getEntity( appdata, 0 );
 -
 -        assertEquals( orgName.toLowerCase() + "/mgmt-org-app", appdata.get( "name" ).asText() );
 -        assertEquals( "Roles", appdata.get( "metadata" ).get( "collections" ).get( "roles" ).get( "title" ).asText() );
 -        assertEquals( 3, appdata.get( "metadata" ).get( "collections" ).get( "roles" ).get( "count" ).asInt() );
 +
 +
 +        Entity app = management().orgs().organization(clientSetup.getOrganizationName()).app().addToPath("mgmt-org-app").get();
 +
 +
 +        assertEquals(this.clientSetup.getOrganizationName().toLowerCase(), app.get("organization").toString());
 +        assertEquals( "mgmt-org-app", app.get( "applicationName" ).toString() );
 +        assertEquals( "http://sometestvalue/" + this.clientSetup.getOrganizationName().toLowerCase() + "/mgmt-org-app",
 +            app.get( "uri" ).toString() );
 +
 +        assertEquals( clientSetup.getOrganizationName().toLowerCase() + "/mgmt-org-app", app.get( "name" ).toString() );
 +        metadata =(Map) appdata.get( "metadata" );
 +        collections =  ((Map)metadata.get("collections"));
 +        roles =(Map) collections.get("roles");
 +
 +        assertEquals( "Roles", roles.get("title").toString() );
 +        assertEquals(3, roles.size());
      }
+ 
+     @Test
+     public void tokenTtl() throws Exception {
+ 
+         long ttl = 2000;
+ 
+         JsonNode node = resource().path( "/management/token" ).queryParam( "grant_type", "password" )
+                                   .queryParam( "username", "test@usergrid.com" ).queryParam( "password", "test" )
+                                   .queryParam( "ttl", String.valueOf( ttl ) ).accept( MediaType.APPLICATION_JSON )
+                                   .get( JsonNode.class );
+ 
+         long startTime = System.currentTimeMillis();
+ 
+         String token = node.get( "access_token" ).textValue();
+ 
+         assertNotNull( token );
+ 
+         JsonNode userdata = resource().path( "/management/users/test@usergrid.com" ).queryParam( "access_token", token )
+                                       .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+ 
+         assertEquals( "test@usergrid.com", userdata.get( "data" ).get( "email" ).asText() );
+ 
+         // wait for the token to expire
+         Thread.sleep( ttl - (System.currentTimeMillis() - startTime) + 1000 );
+ 
+         Status responseStatus = null;
+         try {
+             userdata = resource().path( "/management/users/test@usergrid.com" ).accept( MediaType.APPLICATION_JSON )
+                                  .type( MediaType.APPLICATION_JSON_TYPE ).get( JsonNode.class );
+         }
+         catch ( UniformInterfaceException uie ) {
+             responseStatus = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.UNAUTHORIZED, responseStatus );
+     }
+ 
+ 
+     @Test
+     public void token() throws Exception {
+         JsonNode node = resource().path( "/management/token" ).queryParam( "grant_type", "password" )
+                                   .queryParam( "username", "test@usergrid.com" ).queryParam( "password", "test" )
+                                   .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+ 
+         logNode( node );
+         String token = node.get( "access_token" ).textValue();
+         assertNotNull( token );
+ 
+         // set an organization property
+         HashMap<String, Object> payload = new HashMap<String, Object>();
+         Map<String, Object> properties = new HashMap<String, Object>();
+         properties.put( "securityLevel", 5 );
+         payload.put( OrganizationsResource.ORGANIZATION_PROPERTIES, properties );
+         node = resource().path( "/management/organizations/test-organization" )
 -                         .queryParam( "access_token", superAdminToken() ).accept( MediaType.APPLICATION_JSON )
 -                         .type( MediaType.APPLICATION_JSON_TYPE ).put( JsonNode.class, payload );
++            .queryParam( "access_token", clientSetup.getSuperuserToken().getAccessToken() )
++            .accept( MediaType.APPLICATION_JSON )
++            .type( MediaType.APPLICATION_JSON_TYPE )
++            .put( JsonNode.class, payload );
+ 
+         // ensure the organization property is included
+         node = resource().path( "/management/token" ).queryParam( "access_token", token )
+                          .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+         logNode( node );
+ 
+         JsonNode securityLevel = node.findValue( "securityLevel" );
+         assertNotNull( securityLevel );
+         assertEquals( 5L, securityLevel.asLong() );
+     }
+ 
+ 
+     @Test
+     public void meToken() throws Exception {
+         JsonNode node = resource().path( "/management/me" ).queryParam( "grant_type", "password" )
+                                   .queryParam( "username", "test@usergrid.com" ).queryParam( "password", "test" )
+                                   .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+ 
+         logNode( node );
+         String token = node.get( "access_token" ).textValue();
+         assertNotNull( token );
+ 
+         node = resource().path( "/management/me" ).queryParam( "access_token", token )
+                          .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+         logNode( node );
+ 
+         assertNotNull( node.get( "passwordChanged" ) );
+         assertNotNull( node.get( "access_token" ) );
+         assertNotNull( node.get( "expires_in" ) );
+         JsonNode userNode = node.get( "user" );
+         assertNotNull( userNode );
+         assertNotNull( userNode.get( "uuid" ) );
+         assertNotNull( userNode.get( "username" ) );
+         assertNotNull( userNode.get( "email" ) );
+         assertNotNull( userNode.get( "name" ) );
+         assertNotNull( userNode.get( "properties" ) );
+         JsonNode orgsNode = userNode.get( "organizations" );
+         assertNotNull( orgsNode );
+         JsonNode orgNode = orgsNode.get( "test-organization" );
+         assertNotNull( orgNode );
+         assertNotNull( orgNode.get( "name" ) );
+         assertNotNull( orgNode.get( "properties" ) );
+     }
+ 
+ 
+     @Test
+     public void meTokenPost() throws Exception {
+         Map<String, String> payload =
+                 hashMap( "grant_type", "password" ).map( "username", "test@usergrid.com" ).map( "password", "test" );
+ 
+         JsonNode node = resource().path( "/management/me" ).accept( MediaType.APPLICATION_JSON )
+                                   .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class, payload );
+ 
+         logNode( node );
+         String token = node.get( "access_token" ).textValue();
+ 
+         assertNotNull( token );
+ 
+         node = resource().path( "/management/me" ).queryParam( "access_token", token )
+                          .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+         logNode( node );
+     }
+ 
+ 
+     @Test
+     public void meTokenPostForm() {
+ 
+         Form form = new Form();
+         form.add( "grant_type", "password" );
+         form.add( "username", "test@usergrid.com" );
+         form.add( "password", "test" );
+ 
+         JsonNode node = resource().path( "/management/me" ).accept( MediaType.APPLICATION_JSON )
+                                   .type( MediaType.APPLICATION_FORM_URLENCODED_TYPE )
+                                   .entity( form, MediaType.APPLICATION_FORM_URLENCODED_TYPE ).post( JsonNode.class );
+ 
+         logNode( node );
+         String token = node.get( "access_token" ).textValue();
+ 
+         assertNotNull( token );
+ 
+         node = resource().path( "/management/me" ).queryParam( "access_token", token )
+                          .accept( MediaType.APPLICATION_JSON ).get( JsonNode.class );
+         logNode( node );
+     }
+ 
+ 
+     @Test
+     public void ttlNan() throws Exception {
+ 
+         Map<String, String> payload =
+                 hashMap( "grant_type", "password" ).map( "username", "test@usergrid.com" ).map( "password", "test" )
+                                                    .map( "ttl", "derp" );
+ 
+         Status responseStatus = null;
+         try {
+             resource().path( "/management/token" ).accept( MediaType.APPLICATION_JSON )
+                       .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class, payload );
+         }
+         catch ( UniformInterfaceException uie ) {
+             responseStatus = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.BAD_REQUEST, responseStatus );
+     }
+ 
+ 
+     @Test
+     public void ttlOverMax() throws Exception {
+ 
+         Map<String, String> payload =
+                 hashMap( "grant_type", "password" ).map( "username", "test@usergrid.com" ).map( "password", "test" )
+                                                    .map( "ttl", Long.MAX_VALUE + "" );
+ 
+         Status responseStatus = null;
+ 
+         try {
+             resource().path( "/management/token" ).accept( MediaType.APPLICATION_JSON )
+                       .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class, payload );
+         }
+         catch ( UniformInterfaceException uie ) {
+             responseStatus = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.BAD_REQUEST, responseStatus );
+     }
+ 
+ 
+     @Test
+     public void revokeToken() throws Exception {
 -        String token1 = super.adminToken();
 -        String token2 = super.adminToken();
++        String token1 = context().getToken().getAccessToken();
++        String token2 = context().getToken().getAccessToken();
+ 
 -        JsonNode response = resource().path( "/management/users/test" ).queryParam( "access_token", token1 )
 -                                      .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
 -                                      .get( JsonNode.class );
++        JsonNode response = resource().path( "/management/users/test" )
++            .queryParam( "access_token", token1 )
++            .accept( MediaType.APPLICATION_JSON )
++            .type( MediaType.APPLICATION_JSON_TYPE )
++            .get( JsonNode.class );
+ 
+         assertEquals( "test@usergrid.com", response.get( "data" ).get( "email" ).asText() );
+ 
 -        response = resource().path( "/management/users/test" ).queryParam( "access_token", token2 )
 -                             .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
 -                             .get( JsonNode.class );
++        response = resource().path( "/management/users/test" )
++            .queryParam( "access_token", token2 )
++            .accept( MediaType.APPLICATION_JSON )
++            .type( MediaType.APPLICATION_JSON_TYPE )
++            .get( JsonNode.class );
+ 
+         assertEquals( "test@usergrid.com", response.get( "data" ).get( "email" ).asText() );
+ 
+         // now revoke the tokens
 -        response =
 -                resource().path( "/management/users/test/revoketokens" ).queryParam( "access_token", superAdminToken() )
 -                          .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
 -                          .post( JsonNode.class );
++        response = resource().path( "/management/users/test/revoketokens" )
++                    .queryParam( "access_token", this.clientSetup.getSuperuserToken().getAccessToken() )
++                    .accept( MediaType.APPLICATION_JSON )
++                    .type( MediaType.APPLICATION_JSON_TYPE )
++                    .post( JsonNode.class );
+ 
+         // the tokens shouldn't work
+ 
+         Status status = null;
+ 
+         try {
+             response = resource().path( "/management/users/test" ).queryParam( "access_token", token1 )
+                                  .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                                  .get( JsonNode.class );
+         }
+         catch ( UniformInterfaceException uie ) {
+             status = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.UNAUTHORIZED, status );
+ 
+         status = null;
+ 
+         try {
+             response = resource().path( "/management/users/test" ).queryParam( "access_token", token2 )
+                                  .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                                  .get( JsonNode.class );
+         }
+         catch ( UniformInterfaceException uie ) {
+             status = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.UNAUTHORIZED, status );
+ 
 -        String token3 = super.adminToken();
 -        String token4 = super.adminToken();
++        String token3 = context().getToken().getAccessToken();
++        String token4 = context().getToken().getAccessToken();
+ 
+         response = resource().path( "/management/users/test" ).queryParam( "access_token", token3 )
+                              .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                              .get( JsonNode.class );
+ 
+         assertEquals( "test@usergrid.com", response.get( "data" ).get( "email" ).asText() );
+ 
+         response = resource().path( "/management/users/test" ).queryParam( "access_token", token4 )
+                              .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                              .get( JsonNode.class );
+ 
+         assertEquals( "test@usergrid.com", response.get( "data" ).get( "email" ).asText() );
+ 
+         // now revoke the token3
+         response = resource().path( "/management/users/test/revoketoken" ).queryParam( "access_token", token3 )
+                              .queryParam( "token", token3 ).accept( MediaType.APPLICATION_JSON )
+                              .type( MediaType.APPLICATION_JSON_TYPE ).post( JsonNode.class );
+ 
+         // the token3 shouldn't work
+ 
+         status = null;
+ 
+         try {
+             response = resource().path( "/management/users/test" ).queryParam( "access_token", token3 )
+                                  .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                                  .get( JsonNode.class );
+         }
+         catch ( UniformInterfaceException uie ) {
+             status = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.UNAUTHORIZED, status );
+ 
+         status = null;
+ 
+         try {
+             response = resource().path( "/management/users/test" ).queryParam( "access_token", token4 )
+                                  .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
+                                  .get( JsonNode.class );
+ 
+             status = Status.OK;
+         }
+         catch ( UniformInterfaceException uie ) {
+             status = uie.getResponse().getClientResponseStatus();
+         }
+ 
+         assertEquals( Status.OK, status );
+     }
+ 
+ 
+     @Test
+     public void testValidateExternalToken() throws Exception {
+ 
+         // create a new admin user, get access token
+ 
+         String rand = RandomStringUtils.randomAlphanumeric(10);
+         final String username = "user_" + rand;
 -        OrganizationOwnerInfo orgInfo = setup.getMgmtSvc().createOwnerAndOrganization(
 -                username, username, "Test User", username + "@example.com", "password" );
++        management().orgs().post(
++            new Organization( username, username, username+"@example.com", username, "password", null ) );
+ 
+         Map<String, Object> loginInfo = new HashMap<String, Object>() {{
+             put("username", username );
+             put("password", "password");
+             put("grant_type", "password");
+         }};
+         JsonNode accessInfoNode = resource().path("/management/token")
+             .type( MediaType.APPLICATION_JSON_TYPE )
+             .post( JsonNode.class, loginInfo );
+         String accessToken = accessInfoNode.get( "access_token" ).textValue();
+ 
+         // set the Usergrid Central SSO URL because Tomcat port is dynamically assigned
+ 
 -        String suToken = superAdminToken();
++        String suToken = clientSetup.getSuperuserToken().getAccessToken();
+         Map<String, String> props = new HashMap<String, String>();
+         props.put( USERGRID_CENTRAL_URL, getBaseURI().toURL().toExternalForm() );
+         resource().path( "/testproperties" )
+                 .queryParam( "access_token", suToken)
+                 .accept( MediaType.APPLICATION_JSON )
+                 .type( MediaType.APPLICATION_JSON_TYPE )
+                 .post( props );
+ 
+         // attempt to validate the token, must be valid
+ 
+         JsonNode validatedNode = resource().path( "/management/externaltoken" )
+             .queryParam( "access_token", suToken ) // as superuser
+             .queryParam( "ext_access_token", accessToken )
+             .queryParam( "ttl", "1000" )
+             .get( JsonNode.class );
+         String validatedAccessToken = validatedNode.get( "access_token" ).textValue();
+         assertEquals( accessToken, validatedAccessToken );
+ 
+         // attempt to validate an invalid token, must fail
+ 
+         try {
+             resource().path( "/management/externaltoken" )
+                 .queryParam( "access_token", suToken ) // as superuser
+                 .queryParam( "ext_access_token", "rubbish_token")
+                 .queryParam( "ttl", "1000" )
+                 .get( JsonNode.class );
+             fail("Validation should have failed");
+         } catch ( UniformInterfaceException actual ) {
+             assertEquals( 404, actual.getResponse().getStatus() );
+             String errorMsg = actual.getResponse().getEntity( JsonNode.class ).get( "error_description" ).toString();
+             logger.error( "ERROR: " + errorMsg );
+             assertTrue( errorMsg.contains( "Cannot find Admin User" ) );
+         }
+ 
+ 
+ 
+         // TODO: how do we test the create new user and organization case?
+ 
+ 
+ 
+         // unset the Usergrid Central SSO URL so it does not interfere with other tests
+ 
+         props.put( USERGRID_CENTRAL_URL, "" );
+         resource().path( "/testproperties" )
+                 .queryParam( "access_token", suToken)
+                 .accept( MediaType.APPLICATION_JSON )
+                 .type( MediaType.APPLICATION_JSON_TYPE )
+                 .post( props );
+ 
+     }
+ 
+ 
+     @Test
+     public void testSuperuserOnlyWhenValidateExternalTokensEnabled() throws Exception {
+ 
+         // create an org and an admin user
+ 
+         String rand = RandomStringUtils.randomAlphanumeric( 10 );
+         final String username = "user_" + rand;
 -        OrganizationOwnerInfo orgInfo = setup.getMgmtSvc().createOwnerAndOrganization(
 -                username, username, "Test User", username + "@example.com", "password" );
++        management().orgs().post(
++            new Organization( username, username, username+"@example.com", username, "password", null ) );
+ 
+         // turn on validate external tokens by setting the usergrid.central.url
+ 
 -        String suToken = superAdminToken();
++        String suToken = clientSetup.getSuperuserToken().getAccessToken();
+         Map<String, String> props = new HashMap<String, String>();
+         props.put( USERGRID_CENTRAL_URL, getBaseURI().toURL().toExternalForm());
+         resource().path( "/testproperties" )
+                 .queryParam( "access_token", suToken)
+                 .accept( MediaType.APPLICATION_JSON )
+                 .type( MediaType.APPLICATION_JSON_TYPE )
+                 .post( props );
+ 
+         // calls to login as an Admin User must now fail
+ 
+         try {
+ 
+             Map<String, Object> loginInfo = new HashMap<String, Object>() {{
+                 put("username", username );
+                 put("password", "password");
+                 put("grant_type", "password");
+             }};
+             JsonNode accessInfoNode = resource().path("/management/token")
+                     .type( MediaType.APPLICATION_JSON_TYPE )
+                     .post( JsonNode.class, loginInfo );
+             fail("Login as Admin User must fail when validate external tokens is enabled");
+ 
+         } catch ( UniformInterfaceException actual ) {
+             assertEquals( 400, actual.getResponse().getStatus() );
+             String errorMsg = actual.getResponse().getEntity( JsonNode.class ).get( "error_description" ).toString();
+             logger.error( "ERROR: " + errorMsg );
+             assertTrue( errorMsg.contains( "Admin Users must login via" ));
+ 
+         } catch ( Exception e ) {
+             fail( "We expected a UniformInterfaceException" );
+         }
+ 
+         // login as superuser must succeed
+ 
+         Map<String, Object> loginInfo = new HashMap<String, Object>() {{
+             put("username", "superuser");
+             put("password", "superpassword");
+             put("grant_type", "password");
+         }};
+         JsonNode accessInfoNode = resource().path("/management/token")
+                 .type( MediaType.APPLICATION_JSON_TYPE )
+                 .post( JsonNode.class, loginInfo );
+         String accessToken = accessInfoNode.get( "access_token" ).textValue();
+         assertNotNull( accessToken );
+ 
+         // turn off validate external tokens by un-setting the usergrid.central.url
+ 
+         props.put( USERGRID_CENTRAL_URL, "" );
+         resource().path( "/testproperties" )
+                 .queryParam( "access_token", suToken)
+                 .accept( MediaType.APPLICATION_JSON )
+                 .type( MediaType.APPLICATION_JSON_TYPE )
+                 .post( props );
+     }
+ 
  }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
----------------------------------------------------------------------
diff --cc stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
index f949da3,1e94a2d..14c81be
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/management/RegistrationIT.java
@@@ -312,14 -301,13 +301,14 @@@ public class RegistrationIT extends Abs
              String adminUserName = "AdminUserFromOtherOrg";
              String adminUserEmail = "AdminUserFromOtherOrg@otherorg.com";
  
-             UserInfo adminUser = setup.getMgmtSvc().createAdminUser(
-                     adminUserEmail, adminUserEmail, adminUserEmail, "password1", true, false );
 -            User adminUser = (User) management().users().post(new User(adminUserEmail, adminUserEmail, adminUserEmail, "password1"));
++            User adminUser = (User) management().users().post(
++                User.class, new User(adminUserEmail, adminUserEmail, adminUserEmail, "password1"));
  
-             refreshIndex("test-organization", "test-app");
+             refreshIndex();
  
-             assertNotNull( adminUser );
-             Message[] msgs = getMessages( "otherorg.com", adminUserName, "password1" );
-             assertEquals( 1, msgs.length );
+             assertNotNull(adminUser);
+             Message[] msgs = getMessages("otherorg.com", adminUserName, "password1");
+             assertEquals(1, msgs.length);
  
              // add existing admin user to org
  

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/AbstractRestIT.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
----------------------------------------------------------------------
diff --cc stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
index 207a962,e033c2d..c712605
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource2point0/ClientSetup.java
@@@ -95,10 -94,16 +99,17 @@@ public class ClientSetup implements Tes
          String methodName = description.getMethodName();
          String name = testClass + "." + methodName;
  
-         restClient.superuserSetup();
-         superuserToken = restClient.management().token().post(Token.class, new Token( superuserName, superuserPassword ) );
-         restClient.management().token().setToken( superuserToken );
- 
+         try {
+             restClient.superuserSetup();
 -            superuserToken = restClient.management().token().post( new Token( superuserName, superuserPassword ) );
++            superuserToken = restClient.management().token().post(Token.class, new Token( superuserName, superuserPassword ) );
++            restClient.management().token().setToken( superuserToken );
+         } catch ( Exception e ) {
+             if ( logger.isDebugEnabled() ) {
+                 logger.debug( "Error creating superuser, may already exist", e );
+             } else {
+                 logger.warn( "Error creating superuser, may already exist");
+             }
+         }
  
          username = "user_"+name + UUIDUtils.newTimeUUID();
          password = username;

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/services/pom.xml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/services/src/main/java/org/apache/usergrid/management/ManagementService.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/services/src/main/java/org/apache/usergrid/management/cassandra/ManagementServiceImpl.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/services/src/main/java/org/apache/usergrid/security/shiro/Realm.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4b812547/stack/services/src/test/java/org/apache/usergrid/security/tokens/TokenServiceIT.java
----------------------------------------------------------------------


Mime
View raw message