How to use protractor to check Google OAuth

I want to end to end

test my angular-firebase app with a protractor, but I am using Google OAuth to authenticate users.

The only option for me is to use the protractor web browser to log into google. Not only is this hectic, it is also insecure because I would literally put my google credentials on git.

I would appreciate any advice on how to do this, because it is very important for me to verify my application without authentication. I can get little access.

+3


source to share


2 answers


You can see how it works https://github.com/pinoyyid/ngGAPI . This library (discalimer - I'm a contributor) is designed to provide a convenient way to integrate with Google APIs in AngularJS and work with OAuth along the way.

From README.md ...



One of the challenges of developing apps that look to Google Drive to achieve headless, end-to-end testing when purchasing an access token usually requires logging into a browser session. ngGAPI handles this by allowing you to set the refresh token and client secret directly into the configuration, which allows your application to get access tokens without being logged in. See fooobar.com/questions/147860 / ... for the steps required to get such a refresh token.

// set your own credentials for automated testing e2e. NB, for security, credentials should be stored in a separate .js file which is located in .gitignore OauthServiceProvider.setTestingRefreshToken (MY_REFRESHTOKEN). OauthServiceProvider.setTestingClientSecret (MY_CLIENTSECRET)

If you're in a situation where you need to use server-based CI, the trick is to create a google account as a victim and create a refresh token for that. So the worst case scenario is that Eve gets the ability to firebase against an empty account.

+3


source


While I agree that ideally you would like to avoid testing the OAuth2 feature itself, sometimes it's easier / necessary to go through these steps.

I am using something like this which uses a different answer .

Credentials don't have to be in code, and I don't even like them in files: I prefer to supply them on the command line, either as arguments, or, as in this case, via environment variables.



loginWithGoogle( 
    process.env.PROJ_TEST1_GMAIL_USER,
    process.env.PROJ_TEST1_GMAIL_PASS
)

/**
* Uses the dreaded `sleep` method because finding the password 
* by any css selector tried fails.
* @param {string} username - A Google username.
* @param {string} passphrase - A Google passpharse.
* @return {Promise.<void>} Promise resolved when logged in.
*/
var loginWithGoogle = function (username, passphrase) {
    return selectWindow(1).then( () => {
            return browser.driver.findElement(by.css('[type="email"]'))
            .then( (el) => {
                el.sendKeys( username + protractor.Key.ENTER);
            }).then( () => {
                browser.driver.sleep(1000);
            }).then( () => {
                browser.actions().sendKeys( passphrase + protractor.Key.ENTER ).perform();
            });
    })
}

/**
* Focus the browser to the specified  window.
* [Implementation by and thanks to]{@link /questions/1184883/protractor-e2e-testing-error-object-object-object-has-no-method-getwindowhandle}
* @param  {Number} index The 0-based index of the window (eg 0=main, 1=popup)
* @return {webdriver.promise.Promise.<void>} Promise resolved when the index window is focused.
*/
var selectWindow = (index) => {
    browser.driver.wait(function() {
        return browser.driver.getAllWindowHandles().then( (handles) => {
            if (handles.length > index) {
                return true;
            }
        });
    });

    return browser.driver.getAllWindowHandles().then( (handles) => {
        return browser.driver.switchTo().window(handles[index]);
    });
};

      

One day I have to figure out why the commented code won't run: until then, it works.

0


source







All Articles