Is it possible to use Cypress e2e testing with a firebase auth project?

后端 未结 6 1418
再見小時候
再見小時候 2021-02-19 03:16

I am exploring Cypress for e2e testing, looks like great software. The problem is Authentication, the Cypress documentation explains why using the UI is very bad here.

So

6条回答
  •  滥情空心
    2021-02-19 03:53

    Ok after much trial and error, I tried solution path 2 and it worked.

    So my auth flow looks like this:

    1. Send POST request (using cybress.request) to https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword, and parse the response. Create an object: response1 = response.body

    2. Send POST request (using cybress.request) to https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo, use the idToken from the prev request. Create an object: user = response2.body.users[0];

    Combine the response in an object, with the following properties:

    const authObject = {
      uid: response1.localId,
      displayName: response1.displayName,
      photoURL: null,
         email: response1.email,
         phoneNumber: null,
         isAnonymous: false,
         providerData: [
           {
              uid: response1.email,
              displayName: response1.displayName,
              photoURL: null,
              email: body.email,
              phoneNumber: null,
              providerId: 'password'
           }
          ],
          'apiKey': apiKey,
          'appName': '[DEFAULT]',
          'authDomain': '',
          'stsTokenManager': {
             'apiKey': apiKey,
             'refreshToken': response1.refreshToken,
             'accessToken': response1.idToken,
             'expirationTime': user.lastLoginAt + Number(response1.expiresIn)
           },
           'redirectEventId': null,
           'lastLoginAt': user.lastLoginAt,
           'createdAt': user.createdAt
        };
    

    Then in cybress, I simply save this object in local storag, in the before hook: localStorage.setItem(firebase:authUser:${apiKey}:[DEFAULT], authObject);

    Maybe not perfect, but it solves the problem. Let me know if you interested in the code, and if you have any knowledge about how to build the "authObject", or solve this problem in another way.

提交回复
热议问题