Google Developer Console Setup
The technique we will use for testing is to use the Google OAuth 2.0 Playground to create a refresh token that can be exchanged for an access token and id token during the testing phase.
Google Project and Application Setup
First, a Google project is required. If you don’t already have a project, you can create one using the Google Cloud Console. More information is available in the Google Cloud APIs Getting Started.
Next, use the Google API Console to create credentials for your web application. In the top navigation, click Create Credentials
and choose OAuth client ID
.
On the Create OAuth client ID
page, enter the following:
- Application Type: Web Application
- Name: Your Web Application Name
- Authorized JavaScript origins: http://localhost:3000
- Authorized redirect URIs: http://localhost:3000/callback and https://developers.google.com/oauthplayground
Once saved, note the client ID and client secret. You can find these under the “OAuth 2.0 Client IDs” on the Google API Credentials page.
Using the Google OAuth 2.0 Playground to Create Testing Credentials
The refresh token from this process is unique to the authenticated Google user. This process must be repeated for each user intended for testing.
Note the client id and client secret from the previous step and visit the Google OAuth 2.0 Playground.
Click the gear
icon in the upper right corner to reveal a OAuth 2.0 configuration
panel. In this panel set the follow:
- OAuth flow: Server-side
- Access type: Offline
- Check
Use your own OAuth credentials
. - OAuth Client ID: Your Google Application Client ID
- OAuth Client secret: Your Google Application Client Secret
Select the Google APIs needed for your application under Step 1 (Select & authorize APIs)
, including the https://www.googleapis.com/auth/userinfo.profile
endpoint under Google OAuth2 API v2
at a minimum. Click Authorize APIs
.
Next, sign in with Google credentials to your test Google user account.
You will be redirected back to the Google OAuth 2.0 Playground under Step 2 (Exchange authorization code for tokens)
. Click the Exchange authorization code for token
button.
You will be taken to Step 3 (Configure request to API)
. Note the returned refresh token to be used with testing.
Setting Google app credentials in Cypress
To have access to test user credentials within our tests we need to configure Cypress to use the Google environment variables set in the .env
file.
// .env REACT_APP_GOOGLE_CLIENTID = 'your-client-id' REACT_APP_GOOGLE_CLIENT_SECRET = 'your-client-secret' GOOGLE_REFRESH_TOKEN = 'your-refresh-token'
In, cypress.config.js :
const { defineConfig } = require('cypress') // Populate process.env with values from .env file require('dotenv').config() module.exports = defineConfig({ env: { googleRefreshToken: process.env.GOOGLE_REFRESH_TOKEN, googleClientId: process.env.REACT_APP_GOOGLE_CLIENTID, googleClientSecret: process.env.REACT_APP_GOOGLE_CLIENT_SECRET, }, })
Custom Command for Google Authentication
Next, we will write a command named loginByGoogleApi
to perform a programmatic login into Google and set an item in localStorage with the authenticated users details, which we will use in our application code to verify we are authenticated under test.
The loginByGoogleApi
command will execute the following steps:
- Use the refresh token from the Google OAuth 2.0 Playground to perform the programmatic login, exchanging the refresh token for an
access_token
. - Use the
access_token
returned to get the Google User profile. - Finally the
googleCypress
localStorage item is set with theaccess token
and user profile.
In , cypress/support/commands.js
// cypress/support/commands.js
Cypress.Commands.add('loginByGoogleApi', () => {
cy.log('Logging in to Google')
cy.request({
method: 'POST',
url: 'https://www.googleapis.com/oauth2/v4/token',
body: {
grant_type: 'refresh_token',
client_id: Cypress.env('googleClientId'),
client_secret: Cypress.env('googleClientSecret'),
refresh_token: Cypress.env('googleRefreshToken'),
},
}).then(({ body }) => {
const { access_token, id_token } = body
cy.request({
method: 'GET',
url: 'https://www.googleapis.com/oauth2/v3/userinfo',
headers: { Authorization: Bearer ${access_token}
},
}).then(({ body }) => {
cy.log(body)
const userItem = {
token: id_token,
user: {
googleId: body.sub,
email: body.email,
givenName: body.given_name,
familyName: body.family_name,
imageUrl: body.picture,
},
}
window.localStorage.setItem('googleCypress', JSON.stringify(userItem))
cy.visit('/')
})
})
})
With our Google app setup properly, necessary environment variables in place, and our loginByGoogleApi
command implemented, we will be able to authenticate with Google while our app is under test. Below is a test to login as a user via Google, complete the onboarding process and logout.
describe('Google', function () { beforeEach(function () { cy.task('db:seed') cy.loginByGoogleApi() }) it('shows onboarding', function () { cy.contains('Get Started').should('be.visible') }) })
Leave a comment