Fixed live test + added missing expect test for logout

This commit is contained in:
Andrei Savu 2012-02-01 18:33:33 +02:00
parent 782872ac33
commit c10680647c
5 changed files with 70 additions and 40 deletions

View File

@ -18,17 +18,15 @@
*/ */
package org.jclouds.cloudstack.functions; package org.jclouds.cloudstack.functions;
import java.io.File; import com.google.common.base.Function;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.cloudstack.domain.LoginResponse; import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.features.SessionClient; import org.jclouds.cloudstack.features.SessionClient;
import org.jclouds.crypto.CryptoStreams; import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import com.google.common.base.Function; import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
@Singleton @Singleton
public class LoginWithPasswordCredentials implements Function<Credentials, LoginResponse> { public class LoginWithPasswordCredentials implements Function<Credentials, LoginResponse> {
@ -42,7 +40,8 @@ public class LoginWithPasswordCredentials implements Function<Credentials, Login
@Override @Override
public LoginResponse apply(Credentials input) { public LoginResponse apply(Credentials input) {
String username = input.identity; String username = input.identity;
String domain = "ROOT"; String domain = ""; // empty = ROOT domain
// domain may be present // domain may be present
if (username.indexOf('/') != -1) { if (username.indexOf('/') != -1) {
File domainUsername = new File(username); File domainUsername = new File(username);

View File

@ -27,7 +27,6 @@ import org.jclouds.cloudstack.domain.ApiKeyPair;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextFactory;
import java.util.Properties; import java.util.Properties;
@ -45,10 +44,14 @@ public class ApiKeyPairs {
/** /**
* Retrieve the API key pair for a given CloudStack user * Retrieve the API key pair for a given CloudStack user
* *
* @param endpoint CloudStack API endpoint (e.g. http://72.52.126.25/client/api/) * @param endpoint
* @param username User account name * CloudStack API endpoint (e.g. http://72.52.126.25/client/api/)
* @param password User password * @param username
* @param domain Domain name. If empty defaults to ROOT * User account name
* @param password
* User password
* @param domain
* Domain name. If empty defaults to ROOT
* @return * @return
*/ */
public static ApiKeyPair getApiKeyPairForUser(String endpoint, String username, String password, String domain) { public static ApiKeyPair getApiKeyPairForUser(String endpoint, String username, String password, String domain) {
@ -60,6 +63,7 @@ public class ApiKeyPairs {
CloudStackClient client = CloudStackClient.class.cast(context.getProviderSpecificContext().getApi()); CloudStackClient client = CloudStackClient.class.cast(context.getProviderSpecificContext().getApi());
Set<Account> listOfAccounts = client.getAccountClient().listAccounts(); Set<Account> listOfAccounts = client.getAccountClient().listAccounts();
domain = (domain.equals("") || domain.equals("/")) ? "ROOT" : domain;
for (Account account : listOfAccounts) { for (Account account : listOfAccounts) {
for (User user : account.getUsers()) { for (User user : account.getUsers()) {
if (user.getName().equals(username) && user.getDomain().equals(domain)) { if (user.getName().equals(username) && user.getDomain().equals(domain)) {
@ -91,7 +95,7 @@ public class ApiKeyPairs {
overrides.put(PROVIDER + ".endpoint", checkNotNull(endpoint, "endpoint")); overrides.put(PROVIDER + ".endpoint", checkNotNull(endpoint, "endpoint"));
overrides.put(PROVIDER + ".identity", overrides.put(PROVIDER + ".identity",
String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username"))); String.format("%s/%s", checkNotNull(domain, "domain"), checkNotNull(username, "username")));
overrides.put(PROVIDER + ".credential", CryptoStreams.md5Hex(checkNotNull(password, "password"))); overrides.put(PROVIDER + ".credential", checkNotNull(password, "password"));
return overrides; return overrides;
} }

View File

@ -18,13 +18,7 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import static org.jclouds.crypto.CryptoStreams.md5Hex; import com.google.common.collect.ImmutableMultimap;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import org.jclouds.cloudstack.CloudStackContext; import org.jclouds.cloudstack.CloudStackContext;
import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.LoginResponse; import org.jclouds.cloudstack.domain.LoginResponse;
@ -32,11 +26,16 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap; import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import static org.jclouds.crypto.CryptoStreams.md5Hex;
import static org.testng.Assert.assertEquals;
/** /**
* Tests behavior of {@code SessionClient} * Tests behavior of {@code SessionClient}
* *
* @author Andrei Savu * @author Andrei Savu
*/ */
@Test(groups = "live", singleThreaded = true, testName = "SessionClientExpectTest") @Test(groups = "live", singleThreaded = true, testName = "SessionClientExpectTest")
@ -44,42 +43,54 @@ public class SessionClientExpectTest extends BaseCloudStackRestClientExpectTest<
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void testLoginWhenResponseIs2xxIncludesJSessionId() throws IOException { public void testLoginWhenResponseIs2xxIncludesJSessionId() throws IOException {
String domain = "Partners/jCloud"; String domain = "Partners/jCloud";
String user = "jcloud"; String user = "jcloud";
String password = "jcl0ud"; String password = "jcl0ud";
String md5password = md5Hex(password); String md5password = md5Hex(password);
HttpRequest request = HttpRequest.builder() HttpRequest request = HttpRequest.builder()
.method("GET") .method("GET")
.endpoint( .endpoint(
URI.create("http://localhost:8080/client/api?response=json&command=login&" + URI.create("http://localhost:8080/client/api?response=json&command=login&" +
"username="+user+"&password=" + md5password+ "&domain=" + URLEncoder.encode(domain))) "username=" + user + "&password=" + md5password + "&domain=" + URLEncoder.encode(domain)))
.headers( .headers(
ImmutableMultimap.<String, String>builder() ImmutableMultimap.<String, String>builder()
.put("Accept", "application/json") .put("Accept", "application/json")
.build()) .build())
.build(); .build();
String jSessionId = "90DD65D13AEAA590ECCA312D150B9F6D"; String jSessionId = "90DD65D13AEAA590ECCA312D150B9F6D";
SessionClient client = requestSendsResponse(request, SessionClient client = requestSendsResponse(request,
HttpResponse.builder() HttpResponse.builder()
.statusCode(200) .statusCode(200)
.headers( .headers(
ImmutableMultimap.<String, String>builder() ImmutableMultimap.<String, String>builder()
.put("Set-Cookie", "JSESSIONID="+jSessionId+"; Path=/client") .put("Set-Cookie", "JSESSIONID=" + jSessionId + "; Path=/client")
.build()) .build())
.payload(payloadFromResource("/loginresponse.json")) .payload(payloadFromResource("/loginresponse.json"))
.build()); .build());
assertEquals(client.loginUserInDomainWithHashOfPassword(user, domain, md5password).toString(), assertEquals(client.loginUserInDomainWithHashOfPassword(user, domain, md5password).toString(),
LoginResponse.builder().timeout(1800).lastName("Kiran").registered(false).username("jcloud").firstName("Vijay")
LoginResponse.builder().timeout(1800).lastName("Kiran").registered(false).username("jcloud").firstName("Vijay") .domainId(11).accountType(Account.Type.DOMAIN_ADMIN).userId(19).sessionKey(
.domainId(11).accountType(Account.Type.DOMAIN_ADMIN).userId(19).sessionKey( "uYT4/MNiglgAKiZRQkvV8QP8gn0=").jSessionId(jSessionId).accountName("jcloud").build().toString());
"uYT4/MNiglgAKiZRQkvV8QP8gn0=").jSessionId(jSessionId).accountName("jcloud").build().toString());
} }
//TODO: logout.
public void testLogout() throws IOException {
HttpRequest request = HttpRequest.builder()
.method("GET")
.endpoint(
URI.create("http://localhost:8080/client/api?response=json&command=logout&sessionkey=dummy-session-key"))
.build();
SessionClient client = requestSendsResponse(request,
HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResource("/logoutresponse.json"))
.build());
client.logoutUser("dummy-session-key");
}
@Override @Override
protected SessionClient clientFrom(CloudStackContext context) { protected SessionClient clientFrom(CloudStackContext context) {

View File

@ -19,6 +19,7 @@
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.util.ApiKeyPairs; import org.jclouds.cloudstack.util.ApiKeyPairs;
import org.jclouds.crypto.CryptoStreams; import org.jclouds.crypto.CryptoStreams;
@ -27,6 +28,7 @@ import org.testng.annotations.Test;
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount; import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser; import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/** /**
* Tests behavior of {@code SessionClient} * Tests behavior of {@code SessionClient}
@ -50,10 +52,12 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
String expectedUsername = prefix + "-user"; String expectedUsername = prefix + "-user";
assertEquals(testUser.getName(), expectedUsername); assertEquals(testUser.getName(), expectedUsername);
checkLoginAsTheNewUser(expectedUsername);
assertEquals( assertEquals(
globalAdminClient.getUserClient().registerUserKeys(testUser.getId()), globalAdminClient.getUserClient().registerUserKeys(testUser.getId()),
ApiKeyPairs.getApiKeyPairForUser(System.getProperty("test.cloudstack.endpoint"), ApiKeyPairs.getApiKeyPairForUser(
prefix + "-user", CryptoStreams.md5Hex("password"), "/") System.getProperty("test.cloudstack.endpoint"), prefix + "-user", "password", "")
); );
} finally { } finally {
@ -63,4 +67,15 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
globalAdminClient.getAccountClient().deleteAccount(testAccount.getId()); globalAdminClient.getAccountClient().deleteAccount(testAccount.getId());
} }
} }
private void checkLoginAsTheNewUser(String expectedUsername) {
LoginResponse response = globalAdminClient.getSessionClient()
.loginUserInDomainWithHashOfPassword(expectedUsername, "", CryptoStreams.md5Hex("password"));
assertNotNull(response);
assertNotNull(response.getSessionKey());
assertNotNull(response.getJSessionId());
client.getSessionClient().logoutUser(response.getSessionKey());
}
} }

View File

@ -0,0 +1 @@
{ "logoutresponse" : { "description" : "success" } }