/*
Copyright 2012 - 2014 Jerome Leleu
import org.pac4j.openid.profile.OpenIdProfile;
(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 org.pac4j.openid.client;
import static org.junit.Assert.assertNotNull;
import java.util.Locale;
import org.pac4j.core.client.Client;
import org.pac4j.core.client.Mechanism;
import org.pac4j.core.client.ClientIT;
import org.pac4j.core.profile.Gender;
import org.pac4j.core.profile.ProfileHelper;
import org.pac4j.core.profile.UserProfile;
import org.pac4j.core.util.TestsConstants;
import org.pac4j.openid.profile.yahoo.YahooOpenIdProfile;
import com.esotericsoftware.kryo.Kryo;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
/**
* This class tests the {@link GoogleOpenIdClient} class by simulating a complete authentication.
*
* @author Patrice de Saint Steban
* @since 1.6.0
*/
@SuppressWarnings("rawtypes")
public class YahooOpenIdClientIT extends ClientIT implements TestsConstants {
@Override
protected Client getClient() {
final YahooOpenIdClient client = new YahooOpenIdClient();
client.setCallbackUrl(PAC4J_BASE_URL);
return client;
}
@Override
protected String getCallbackUrl(final WebClient webClient, final HtmlPage authorizationPage) throws Exception {
HtmlForm form = authorizationPage.getFormByName("login_form");
final HtmlTextInput email = form.getInputByName("login");
email.setValueAttribute("testscribeup@yahoo.fr");
final HtmlPasswordInput passwd = form.getInputByName("passwd");
passwd.setValueAttribute("testpwdscribeup");
final HtmlButton button = form.getButtonByName(".save");
final HtmlPage confirmPage = button.click();
String callbackUrl = confirmPage.getUrl().toString();
try {
form = confirmPage.getFormByName("rcForm");
final HtmlSubmitInput submit = form.getInputByName("agree");
final HtmlPage callbackPage = submit.click();
callbackUrl = callbackPage.getUrl().toString();
}
catch (Exception e) {
logger.info("accept page not exist : {}", e.getMessage());
}
logger.debug("callbackUrl : {}", callbackUrl);
return callbackUrl;
}
@Override
protected void registerForKryo(final Kryo kryo) {
kryo.register(YahooOpenIdProfile.class);
}
private int verifyCalls = 0;
@Override
protected void verifyProfile(final UserProfile userProfile) {
if (verifyCalls++ == 2 && userProfile == null) //Verify3 not work because the ProfileHelper has package class name directly in the code
return;
final YahooOpenIdProfile profile = (YahooOpenIdProfile) userProfile;
assertNotNull(profile);
logger.debug("userProfile : {}", profile);
final String id = "mnsYAxIag.AfFGVrKZckRIVkvVYLEYRM4Q--#02050";
assertEquals("https://me.yahoo.com/a/" + id, profile.getId());
assertEquals(YahooOpenIdProfile.class.getSimpleName() + UserProfile.SEPARATOR
+ "https://me.yahoo.com/a/" + id, profile.getTypedId());
assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), YahooOpenIdProfile.class));
assertCommonProfile(userProfile, "testscribeup@yahoo.fr", null, null, null, null,
Gender.UNSPECIFIED, Locale.FRANCE, null, null, null);
assertEquals(2, profile.getAttributes().size());
}
@Override
protected Mechanism getMechanism() {
return Mechanism.OPENID_PROTOCOL;
}
}