// Copyright 2007 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.enterprise.connector.afyd;
import com.google.enterprise.connector.spi.ConfigureResponse;
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.Login;
import com.google.gdata.data.BaseEntry;
import com.google.gdata.util.AuthenticationException;
import junit.framework.TestCase;
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
/**
* This class is a test case that verifies several properties of the
* AfydConnectorType implementation, namely:
* (1) bad authentication credentials are detected
* (2) insufficient authorization situation is detected
* (3) a valid configuration is accepted
* (4) The data stored in the configuration map is placed, without modification
* into the HTML form snippet.
*
* @author amsmith@google.com (Adam Smith)
*/
public class AfydConnectorTypeTest extends TestCase {
private Locale locale;
private UserService service;
protected void setUp() throws Exception {
super.setUp();
locale = Locale.getDefault();
service = getRiggedUserService();
}
public void testBadAuthentication() {
AfydConnectorType type = new AfydConnectorType(service);
Map badLoginMap = new HashMap();
badLoginMap.put(AfydConnectorType.DOMAIN_KEY, "example.com");
badLoginMap.put(AfydConnectorType.EMAIL_KEY, "lolcats@example.com");
badLoginMap.put(AfydConnectorType.PASSWORD_KEY, "mrrrreow");
assertNotNull(type.validateConfig(badLoginMap, locale));
}
public void testBadAuthorization() {
AfydConnectorType type = new AfydConnectorType(service);
Map notAdminMap = new HashMap();
notAdminMap.put(AfydConnectorType.DOMAIN_KEY, "example.com");
notAdminMap.put(AfydConnectorType.EMAIL_KEY, "lolspies@example.com");
notAdminMap.put(AfydConnectorType.PASSWORD_KEY, "secret");
assertNotNull(type.validateConfig(notAdminMap, locale));
}
public void testGoodAuthenticationAndAuthorization () {
AfydConnectorType type = new AfydConnectorType(service);
Map goodMap = new HashMap();
goodMap.put(AfydConnectorType.DOMAIN_KEY, "example.com");
goodMap.put(AfydConnectorType.EMAIL_KEY, "admin@example.com");
goodMap.put(AfydConnectorType.PASSWORD_KEY, "secret");
assertNull(type.validateConfig(goodMap, locale));
}
public void testConfigMapPopulatesForm() {
AfydConnectorType type = new AfydConnectorType(service);
Map configMap = new HashMap();
String nonce = "\u9762" + System.currentTimeMillis();
String domain = "DOMAIN-" + nonce;
String email = "EMAIL-" + nonce;
String password = "PASSWORD-" + nonce;
configMap.put(AfydConnectorType.DOMAIN_KEY, domain);
configMap.put(AfydConnectorType.EMAIL_KEY, email);
configMap.put(AfydConnectorType.PASSWORD_KEY, password);
ConfigureResponse response = type.getPopulatedConfigForm(configMap, locale);
String snippet = response.getFormSnippet();
assertTrue(snippet.contains(domain));
assertTrue(snippet.contains(email));
assertTrue(snippet.contains(password));
}
private static UserService getRiggedUserService() {
return
new UserService("Google-AfydConnectorTypeTest-0") {
public void setUserCredentials(String email, String password)
throws AuthenticationException {
if (email.endsWith("@example.com") && password.equals("secret")) {
return;
} else {
throw new AuthenticationException("Failed rigged login.");
}
}
public BaseEntry getEntry(URL url, Class klass) {
if (url.toString().endsWith("admin")) {
return new UserEntry() {
public Login getLogin() {
Login login = new Login();
login.setAdmin(Boolean.TRUE);
return login;
}
};
} else {
return new UserEntry() {
public Login getLogin() {
Login login = new Login();
login.setAdmin(Boolean.FALSE);
return login;
}
};
}
}
};
}
}