/**
 * END USER LICENSE AGREEMENT (“EULA”)
 *
 * READ THIS AGREEMENT CAREFULLY (date: 9/13/2011):
 * http://www.akiban.com/licensing/20110913
 *
 * BY INSTALLING OR USING ALL OR ANY PORTION OF THE SOFTWARE, YOU ARE ACCEPTING
 * ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT. YOU AGREE THAT THIS
 * AGREEMENT IS ENFORCEABLE LIKE ANY WRITTEN AGREEMENT SIGNED BY YOU.
 *
 * IF YOU HAVE PAID A LICENSE FEE FOR USE OF THE SOFTWARE AND DO NOT AGREE TO
 * THESE TERMS, YOU MAY RETURN THE SOFTWARE FOR A FULL REFUND PROVIDED YOU (A) DO
 * NOT USE THE SOFTWARE AND (B) RETURN THE SOFTWARE WITHIN THIRTY (30) DAYS OF
 * YOUR INITIAL PURCHASE.
 *
 * IF YOU WISH TO USE THE SOFTWARE AS AN EMPLOYEE, CONTRACTOR, OR AGENT OF A
 * CORPORATION, PARTNERSHIP OR SIMILAR ENTITY, THEN YOU MUST BE AUTHORIZED TO SIGN
 * FOR AND BIND THE ENTITY IN ORDER TO ACCEPT THE TERMS OF THIS AGREEMENT. THE
 * LICENSES GRANTED UNDER THIS AGREEMENT ARE EXPRESSLY CONDITIONED UPON ACCEPTANCE
 * BY SUCH AUTHORIZED PERSONNEL.
 *
 * IF YOU HAVE ENTERED INTO A SEPARATE WRITTEN LICENSE AGREEMENT WITH AKIBAN FOR
 * USE OF THE SOFTWARE, THE TERMS AND CONDITIONS OF SUCH OTHER AGREEMENT SHALL
 * PREVAIL OVER ANY CONFLICTING TERMS OR CONDITIONS IN THIS AGREEMENT.
 */

package com.akiban.server.service.security;

import com.akiban.rest.RestService;
import com.akiban.rest.RestServiceImpl;
import com.akiban.server.service.servicemanager.GuicedServiceManager;
import com.akiban.server.test.it.ITBase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Map;

public class SecurityServiceIT extends ITBase
{
    @Override
    protected GuicedServiceManager.BindingsConfigurationProvider serviceBindingsProvider() {
        return super.serviceBindingsProvider()
            .bindAndRequire(SecurityService.class, SecurityServiceImpl.class)
            .bindAndRequire(RestService.class, RestServiceImpl.class);
    }

    @Override
    protected Map<String, String> startupConfigProperties() {
        return uniqueStartupConfigProperties(getClass());
    }

    protected SecurityService securityService() {
        return serviceManager().getServiceByClass(SecurityService.class);
    }

    @Before
    public void createUsers() {
        SecurityService securityService = securityService();
        securityService.addRole("rest-user");
        securityService.addRole("admin");
        securityService.addUser("user1", "password", Arrays.asList("rest-user"));
    }

    @After
    public void cleanUp() {
        securityService().clearAll();
    }

    @Test
    public void getUser() {
        SecurityService securityService = securityService();
        User user = securityService.getUser("user1");
        assertNotNull("user found", user);
        assertTrue("user has role", user.hasRole("rest-user"));
        assertFalse("user does not have role", user.hasRole("admin"));
    }

    @Test
    public void authenticate() {
        assertEquals("user1", securityService().authenticate("user1", "password").getName());
    }

    @Test
    public void restUnauthenticated() {
    }

}

@@ -1583,19 +1583,225 @@
 ava.
-util.Arrays
+io.IOException;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URL;%0Aimport java.util.Arrays;%0Aimport java.util.HashMap
 ;%0Aim
@@ -2267,56 +2267,210 @@
     
-return uniqueStartupConfigProperties(getClass())
+Map%3CString, String%3E properties = new HashMap%3CString, String%3E();%0A        properties.put(%22akserver.http.login%22, %22true%22);%0A        properties.put(%22akserver.postgres.login%22, %22md5%22);%0A        return properties
 ;%0A  
@@ -3461,53 +3461,874 @@
     
-@Test%0A    public void restUnauthenticated() %7B
+private URL getRestURL(String userInfo, String request) %0A            throws MalformedURLException, URISyntaxException %7B%0A        int port = serviceManager().getServiceByClass(com.akiban.http.HttpConductor.class).getPort();%0A        String context = serviceManager().getServiceByClass(com.akiban.rest.RestService.class).getContextPath();%0A        return new URI(%22http%22, userInfo, %22localhost%22, port, context + request, null, null).toURL();%0A    %7D%0A%0A    @Test(expected = IOException.class)%0A    public void restUnauthenticated() throws Exception %7B%0A        URL url = getRestURL(null, %22/version%22);%0A        Object c = url.openConnection().getContent();%0A    %7D%0A%0A    @Test%0A    public void restAuthenticated() throws Exception %7B%0A        URL url = getRestURL(%22user1:password%22, %22/version%22);%0A        Object c = url.openConnection().getContent();%0A        System.out.println(%22*** %22 + c);
 %0A   

Test authenticated services.