<commit_before>package org.usergrid.rest.management.users;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.usergrid.management.AccountCreationProps.PROPERTIES_USER_CONFIRMATION_URL;
import static org.usergrid.utils.MapUtils.hashMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Message;
import javax.mail.internet.MimeMultipart;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.mock_javamail.Mailbox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.cassandra.Concurrent;
import org.usergrid.management.ActivationState;
import org.usergrid.management.ApplicationInfo;
import org.usergrid.management.OrganizationInfo;
import org.usergrid.management.OrganizationOwnerInfo;
import org.usergrid.management.UserInfo;
import org.usergrid.persistence.entities.User;
import org.usergrid.rest.AbstractRestIT;
import org.usergrid.rest.TestContextSetup;
import org.usergrid.rest.management.organizations.OrganizationsResource;
import org.usergrid.security.AuthPrincipalInfo;
import org.usergrid.security.AuthPrincipalType;
import org.usergrid.security.tokens.TokenCategory;
import org.usergrid.utils.UUIDUtils;


/**
 * @author zznate
 */
@Concurrent()
public class MUUserResourceIT extends AbstractRestIT
{
    private Logger LOG = LoggerFactory.getLogger(MUUserResourceIT.class);


    @Rule
    public TestContextSetup context = new TestContextSetup( this );


    /**
     * Tests mixed case creation of an administrative user, and failures to authenticate
     * against management interfaces when case is different from user creation case.
     *
     * From USERGRID-2075
     */
    @Test
    public void testCaseSensitivityAdminUser() throws Exception
    {
        LOG.info( "Starting testCaseSensitivityAdminUser()" );
        UserInfo mixcaseUser = setup.getMgmtSvc().createAdminUser( "AKarasulu", "<NAME>",
                "<EMAIL>", "test", true, false );
        AuthPrincipalInfo adminPrincipal = new AuthPrincipalInfo( AuthPrincipalType.ADMIN_USER, mixcaseUser.getUuid(),
                UUIDUtils.newTimeUUID() );
        OrganizationInfo organizationInfo = setup.getMgmtSvc().createOrganization( "MixedCaseOrg", mixcaseUser, true );
        String tokenStr = mgmtToken( "<EMAIL>","test" );

        // Should succeed even when we use all lowercase
        JsonNode node = resource().path("/management/users/<EMAIL>")
            .queryParam( "access_token", tokenStr )
            .accept( MediaType.APPLICATION_JSON )
            .type( MediaType.APPLICATION_JSON_TYPE )
            .get( JsonNode.class );
        logNode( node );
    }


    @Test
    public void testUnconfirmedAdminLogin() throws Exception
    {
        String orgName = this.getClass().getName();
        String appName = "testUnconfirmedAdminLogin";
        String userName = "TestUser";
        String email = "<EMAIL>";
        String passwd = "<PASSWORD>";
        OrganizationOwnerInfo orgOwner;

        orgOwner = setup.getMgmtSvc().createOwnerAndOrganization( orgName, appName, userName, email, passwd, false, false );
      	assertNotNull( orgOwner );

        String token = mgmtToken( userName, passwd );
    }


    @Test
    public void updateManagementUser() throws Exception {
        Map<String, String> payload = hashMap("email",
                "<EMAIL>").map("username", "uort-user-1")
                .map("name", "<NAME>").map("password", "password")
                .map("organization", "uort-org")
                .map("company","Apigee");

        JsonNode node = resource().path("/management/organizations")
                .accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON_TYPE)
                .post(JsonNode.class, payload);
        logNode(node);
        String userId = node.get("data").get("owner").get("uuid").asText();
<commit_msg>Added extra assertion<commit_after>package org.usergrid.rest.management.users;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.usergrid.management.AccountCreationProps.PROPERTIES_USER_CONFIRMATION_URL;
import static org.usergrid.utils.MapUtils.hashMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Message;
import javax.mail.internet.MimeMultipart;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.mock_javamail.Mailbox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.usergrid.cassandra.Concurrent;
import org.usergrid.management.ActivationState;
import org.usergrid.management.ApplicationInfo;
import org.usergrid.management.OrganizationInfo;
import org.usergrid.management.OrganizationOwnerInfo;
import org.usergrid.management.UserInfo;
import org.usergrid.persistence.entities.User;
import org.usergrid.rest.AbstractRestIT;
import org.usergrid.rest.TestContextSetup;
import org.usergrid.rest.management.organizations.OrganizationsResource;
import org.usergrid.security.AuthPrincipalInfo;
import org.usergrid.security.AuthPrincipalType;
import org.usergrid.security.tokens.TokenCategory;
import org.usergrid.utils.UUIDUtils;


/**
 * @author zznate
 */
@Concurrent()
public class MUUserResourceIT extends AbstractRestIT
{
    private Logger LOG = LoggerFactory.getLogger(MUUserResourceIT.class);


    @Rule
    public TestContextSetup context = new TestContextSetup( this );


    /**
     * Tests mixed case creation of an administrative user, and failures to authenticate
     * against management interfaces when case is different from user creation case.
     *
     * From USERGRID-2075
     */
    @Test
    public void testCaseSensitivityAdminUser() throws Exception
    {
        LOG.info( "Starting testCaseSensitivityAdminUser()" );
        UserInfo mixcaseUser = setup.getMgmtSvc().createAdminUser( "AKarasulu", "<NAME>",
                "<EMAIL>", "test", true, false );
        AuthPrincipalInfo adminPrincipal = new AuthPrincipalInfo( AuthPrincipalType.ADMIN_USER, mixcaseUser.getUuid(),
                UUIDUtils.newTimeUUID() );
        OrganizationInfo organizationInfo = setup.getMgmtSvc().createOrganization( "MixedCaseOrg", mixcaseUser, true );
        String tokenStr = mgmtToken( "<EMAIL>","test" );

        // Should succeed even when we use all lowercase
        JsonNode node = resource().path("/management/users/<EMAIL>")
            .queryParam( "access_token", tokenStr )
            .accept( MediaType.APPLICATION_JSON )
            .type( MediaType.APPLICATION_JSON_TYPE )
            .get( JsonNode.class );
        logNode( node );
    }


    @Test
    public void testUnconfirmedAdminLogin() throws Exception
    {
        String orgName = this.getClass().getName();
        String appName = "testUnconfirmedAdminLogin";
        String userName = "TestUser";
        String email = "<EMAIL>";
        String passwd = "<PASSWORD>";
        OrganizationOwnerInfo orgOwner;

        orgOwner = setup.getMgmtSvc().createOwnerAndOrganization( orgName, appName, userName, email, passwd, false, false );
      	assertNotNull( orgOwner );

      String returnedUsername = orgOwner.getOwner().getUsername();

      assertEquals(userName, returnedUsername);

        String token = mgmtToken( userName, passwd );
    }


    @Test
    public void updateManagementUser() throws Exception {
        Map<String, String> payload = hashMap("email",
                "<EMAIL>").map("username", "uort-user-1")
                .map("name", "<NAME>").map("password", "password")
                .map("organization", "uort-org")
                .map("company","Apigee");

        JsonNode node = resource().path("/management/organizations")
                .accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON_TYPE)
                .post(JsonNode.class, payload);
        logNode(node);
        String userId = node.get("data").get("owner").get("uuid").asText();

{'fraction_non_alphanumeric': 0.10969884346730792, 'fraction_numerical': 0.0016031146226955226, 'mean_word_length': 3.6236103758602436, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565688', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2439, 'n_words': 509}