Package

Source Code of FacebookOAuth2Test

import com.feth.play.module.pa.providers.oauth2.OAuth2AuthProvider;
import com.feth.play.module.pa.providers.oauth2.facebook.FacebookAuthProvider;
import com.feth.play.module.pa.providers.oauth2.facebook.FacebookAuthUser;
import models.User;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.NoSuchElementException;
import play.libs.ws.WS;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.feth.play.module.pa.providers.oauth2.OAuth2AuthProvider.SettingKeys.CLIENT_ID;
import static com.feth.play.module.pa.providers.oauth2.OAuth2AuthProvider.SettingKeys.CLIENT_SECRET;
import static org.fest.assertions.Assertions.assertThat;

public class FacebookOAuth2Test extends OAuth2Test {

    public static final String FACEBOOK_USER_EMAIL = "ufbullq_fallerman_1414534488@tfbnw.net";
    public static final String FACEBOOK_USER_ID = "100005169708842";

    protected void amendConfiguration(final Map<String, String> additionalConfiguration) {
        additionalConfiguration.put(constructSettingKey(CLIENT_ID), System.getenv("FACEBOOK_CLIENT_ID"));
        additionalConfiguration.put(constructSettingKey(CLIENT_SECRET), System.getenv("FACEBOOK_CLIENT_SECRET"));
    }

    protected String getProviderKey() {
        return FacebookAuthProvider.PROVIDER_KEY;
    }

    protected Class<FacebookAuthProvider> getProviderUnderTest() {
        return FacebookAuthProvider.class;
    }

    @Test
    public void itShouldBePossibleToSignUp() throws InterruptedException {
        signupUser();

        // Make sure the redirect from localhost to fb happened already (and that {@link MyUserServicePlugin#save()} gets called)
        Thread.sleep(3000);

        assertThat(browser.url()).isEqualTo("/#_=_");

        final FacebookAuthUser authUser = (FacebookAuthUser) (MyTestUserServicePlugin.getLastAuthUser());
        assertThat(authUser.getProfileLink()).contains(FACEBOOK_USER_ID);
        assertThat(authUser.getId()).isEqualTo(FACEBOOK_USER_ID);
        assertThat(authUser.getGender()).isEqualTo("female");

        final User user = User.findByEmail(FACEBOOK_USER_EMAIL);
        assertThat(user).isNotNull();
        assertThat(user.firstName).isEqualTo("Mary");
        assertThat(user.lastName).isEqualTo("Fallerman");
        assertThat(user.name).isEqualTo("Mary Ameafigjhhdb Fallerman");
    }

    private void signupUser() {
        goToLogin();
        browser
                .fill("#email").with(FACEBOOK_USER_EMAIL)
                .fill("#pass").with(System.getenv("FACEBOOK_USER_PASSWORD"))
                .find("#u_0_1").click();
        browser.await().untilPage().isLoaded();

        // save browser? no!
        try {
            // try, because this is not checked for test users, because they are not asked
            browser.find("#u_0_2").click();
            browser.find("#checkpointSubmitButton").click();
            browser.await().untilPage().isLoaded();
        } catch (final NoSuchElementException nsee) {
            // mobile
        } catch(final ElementNotVisibleException enve) {
            // desktop
        }

        // check login layout
        checkLoginLayout();

        // confirm login
        browser
                .find("[name='__CONFIRM__']")
                .click();
        browser
                .await()
                .untilPage()
                .isLoaded();

    }

    protected String expectedLoginLayout() {
        return "page";
    }

    private void checkLoginLayout() {
        final String selector = "[name='display']";
        browser.await().atMost(10, TimeUnit.SECONDS).until(selector);
        assertThat(browser.find(selector).getValue()).isEqualTo(expectedLoginLayout());
    }

    /**
     * After the test is finished, revoke the permission to the app so the login dialog appears again on the next login when running the test
     * See https://developers.facebook.com/docs/facebook-login/permissions/v2.1#revokelogin
     */
    @After
    public void shutdown() {
        final FacebookAuthUser authUser = (FacebookAuthUser) (MyTestUserServicePlugin.getLastAuthUser());

        if (authUser == null) {
            // in case the test failed, we don't have an authUser
            return;
        }

        final String url = getConfig().getString("userInfoUrl") + "/permissions";
        WS
                .url(url)
                .setQueryParameter(OAuth2AuthProvider.Constants.ACCESS_TOKEN, authUser.getOAuth2AuthInfo().getAccessToken())
                .setQueryParameter("format", "json")
                .setQueryParameter("method", "delete")
                .get().get(10, TimeUnit.SECONDS);
    }

}
TOP

Related Classes of FacebookOAuth2Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.