-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cy.login): login from different users using cy.login() #14
Comments
It is not currently supported, but this is a feature I for sure want to add. What are you thinking for how to select it? Maybe passing the param name into |
Yeah I think it's good.
And then use it like |
Oh, I really like that idea! Much reads much more clearly. We may want to make that an object though, so we can select by key (I think that is what you actually meant to put): {
"TEST_UIDS": {
"role1": "<some_uid>",
"role2": "<some_uid>"
}
} Cool, I'll add this as a feature to build. Feel free to make a PR if you get a chance, if not I will try to get to it when I can. |
I tried to fix it but currently I don't have time for it. |
When starting to investigate I realized that this could create some difficult if multiple logins happen without logout happening first. It is tough to estimate a time, I'll continue to investigate and post updates here. |
I just add my two cents here: |
I'd be happy to help chip in on making a PR for this. I need this for my team. We have about 4 portals into our app (client with 5 roles, tester portal, captain portal and super portal). I can easily use the current functionality for tester, captain and super. However, the client portal with 5 roles will prove to be difficult. Couldn't we check the uid that is currently logged in, and if that uid does not match the uid we want to login with, then we logout and then do the login process? |
After thinking about it more, I'm thinking that using:
and then logging out and back in, when needed will give me what I need. Its not ideal and love the idea of TEST_UID being an object. But this might be a good workaround |
Any update on this please. |
I would like to have it like this: cy.firebaseSignUp(email, password)
cy.firebaseSignIn(email, password) other authentication options would also be interesting I think there is some coupling between two independent problems here:
Basically, my goal is to have the testing suite to create a new user every time I run the test, avoiding the state of previous tests to influence the result, or requiring a clean up of the state after each run. The current library implementation forces the usage of a single user or a specific authentication method. IMHO, that is not ideal. |
* feat(core): add createCustomToken command [14 of cypress-firebase](prescottprue/cypress-firebase#14)
Work for this is being included in v0.10.0 - it calls a firebase-tools-extra command to generate the custom token dynamically then logs in. @fabiocarneiro signing in with credentials such as email and password was initially skipped since that would mean either exposing the credentials in the test code or passing them through from the environment. I'm open to the idea of creating custom commands for doing this, but as you said it is really part of a different concern. In this issue I'm going to address passing a UID to login to a different user (logging in through custom auth token) - we can address creating user's during the test as a different feature. I made #88 to track that. Something to note is that you aren't locked into a specific auth method per say - you can login any way you want as a user of your app, then grab the UID from the Auth tab of Firebase. Then if you use that UID, it will login with a custom token, but that still works with accounts that were created by other auth methods. This has been preferred for my apps so we aren't building up user accounts over time just from tests running. |
* feat(core): add createCustomToken command [14 of cypress-firebase](prescottprue/cypress-firebase#14)
@prescottprue what I don't like here is the assumption that you will use one user for all your tests and that the authentication happens outside of Cypress, and then the token is used afterwards. |
@fabiocarneiro The logic that is being added in v0.10.0 will allow for logging into different users by passing their UID into |
Does that use the admin api? |
* feat(login): add support for passing UID - #14 * feat(types): update type of `login` function to include optional `uid` param - #14 * feat(tests): add tests for emulator support - #73 * chore(deps): update [firebase-tools-extra version](https://github.com/prescottprue/firebase-tools-extra/releases/tag/v0.5.0) for custom token generation used in new `login` functionality - #73
It does - it calls |
Closing since this if a feature within v0.10.0. Anyone that tries it out - reach out if it doesn't work as expected |
@prescottprue The only concern I have with that is that it is assuming I do have specific accounts that I want to test with, and I will hardcode the ids on my code. This adds some fragility, as there can be different states based on these accounts on different runs and the accounts can be modified/removed outside of the scope of the application you are testing. It would still be nice to have support to create an account during the test. Also, cy.firebaseSignUp({email, password})
cy.firebaseSignIn({email, password}) PS1: |
I totally agree and this is really what I was looking for. Ideally, each test should be isolated from one another, and having hard-coded UIDs destroys that isolation. Because one test may update the user profile (e.g. custom claims), and another may depend on the auth claim not changing. Running the tests in the reverse order causes a failing test. Alternatively, if you don't use that format, it would be okay to have: cy.createUser({ uid, email, password, customClaims, etc })
cy.removeUser(uid) |
Found a workaround to creating users in the code and cleaning up users afterward. You can essentially add your own cypress commands to create and log-in users by using the front-end firebase code. And if you use firebase's REST API, you can delete all the user accounts after each test. https://firebase.google.com/docs/reference/rest/auth#section-auth-emulator-clearaccounts Here are the commands I added to // commands.ts
Cypress.Commands.add('createUser', (email: string, password: string) => {
Cypress.log({
displayName: 'createUser',
consoleProps: () => {
return { email, password }
},
})
return firebase.auth().createUserWithEmailAndPassword(email, password)
})
Cypress.Commands.add('loginWithEmail', (email: string, password: string) => {
Cypress.log({
displayName: 'loginWithEmail',
consoleProps: () => {
return { email, password }
},
})
return firebase.auth().signInWithEmailAndPassword(email, password)
})
Cypress.Commands.add('deleteAllUsers', (email: string) => {
Cypress.log({
displayName: 'deleteAllUsers',
consoleProps: () => {
return { email }
},
})
return cy.request({
url: 'http://localhost:9099/emulator/v1/projects/{project-id}/accounts',
method: 'DELETE',
auth: { bearer: 'owner' },
})
}) To use these:
|
@Nick-Mazuk Looks good and thanks for sharing. I didn't use cypress recently but will for sure come back to it at some point. This should be added to the library. Maybe open a PR? |
Is it a good idea exposing the auth interface on the window so I can access directly inside Cypress and call |
@felippepuhle You shouldn't need to be creating the user through email/password for things to work - just using a new or existing auth user's UID in If you are running your tests against a full Firebase project instead of the emulators, you can also create a user through your app the normal way, then use that UID with Testing the UI for loggin in with phone number is a different case - for those you will want to have a known user within your auth and actually authenticate with Firebase through the UI instead of using If you need more info, feel free to open a discussion 😄 |
@prescottprue thanks for the explanation and tips! Yeah, we're running our tests against a full Firebase project... We want to make our tests as isolated as possible (we're running them in parallel in more than 1 browser, and we already fel in some race conditions where 2 browsers were testing the same screen with the same user and the test fails due to that - wrong values on assertions). Our signup flow is kinda slow (there're a lot of screens, that's the reason we don't want to create a new user through the UI each test), I'm considering using this project to handle the authentication but I'm missing this signup stuff... is there a way we can implement both createUser and deleteUser? I can help with that, just need some help to better understand the project. Thanks! |
Does the framework support login via different users?
I have several roles in my app and I want to test each one of them.
Can I declare on different TEST_UIDs in the config in use different users in the tests or there is a workaround for this?
The text was updated successfully, but these errors were encountered: