-
Notifications
You must be signed in to change notification settings - Fork 938
[Auth] Add integration tests (headless & webdriver) for middleware #6161
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
packages/auth/test/integration/flows/middleware_test_generator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import * as sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import {Auth, createUserWithEmailAndPassword, User} from '@firebase/auth'; | ||
import { randomEmail } from '../../helpers/integration/helpers'; | ||
|
||
use(chaiAsPromised); | ||
use(sinonChai); | ||
|
||
export function generateMiddlewareTests(authGetter: () => Auth, signIn: () => Promise<unknown>): void { | ||
context('middleware', () => { | ||
let auth: Auth; | ||
let unsubscribes: Array<() => void>; | ||
|
||
beforeEach(() => { | ||
auth = authGetter(); | ||
unsubscribes = []; | ||
}); | ||
|
||
afterEach(() => { | ||
for (const u of unsubscribes) { | ||
u(); | ||
} | ||
}); | ||
|
||
/** | ||
* Helper function for adding beforeAuthStateChanged that will | ||
* automatically unsubscribe after every test (since some tests may | ||
* perform cleanup after that would be affected by the middleware) | ||
*/ | ||
function beforeAuthStateChanged(callback: (user: User | null) => void | Promise<void>): void { | ||
unsubscribes.push(auth.beforeAuthStateChanged(callback)); | ||
} | ||
|
||
it('can prevent user sign in', async () => { | ||
beforeAuthStateChanged(() => { | ||
throw new Error('stop sign in'); | ||
}); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.be.null; | ||
}); | ||
|
||
it('can prevent user sign in as a promise', async () => { | ||
beforeAuthStateChanged(() => { | ||
return Promise.reject('stop sign in'); | ||
}); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.be.null; | ||
}); | ||
|
||
it('keeps previously-logged in user if blocked', async () => { | ||
// Use a random email/password sign in for the base user | ||
const {user: baseUser} = await createUserWithEmailAndPassword(auth, randomEmail(), 'password'); | ||
|
||
beforeAuthStateChanged(() => { | ||
throw new Error('stop sign in'); | ||
}); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.eq(baseUser); | ||
}); | ||
|
||
it('can allow sign in', async () => { | ||
beforeAuthStateChanged(() => { | ||
// Pass | ||
}); | ||
|
||
await expect(signIn()).not.to.be.rejected; | ||
expect(auth.currentUser).not.to.be.null; | ||
}); | ||
|
||
it('can allow sign in as a promise', async () => { | ||
beforeAuthStateChanged(() => { | ||
return Promise.resolve(); | ||
}); | ||
|
||
await expect(signIn()).not.to.be.rejected; | ||
expect(auth.currentUser).not.to.be.null; | ||
}); | ||
|
||
it('overrides previous user if allowed', async () => { | ||
// Use a random email/password sign in for the base user | ||
const {user: baseUser} = await createUserWithEmailAndPassword(auth, randomEmail(), 'password'); | ||
|
||
beforeAuthStateChanged(() => { | ||
// Pass | ||
}); | ||
|
||
await expect(signIn()).not.to.be.rejected; | ||
expect(auth.currentUser).not.to.eq(baseUser); | ||
}); | ||
|
||
it('will reject if one callback fails', async () => { | ||
// Also check that the function is called multiple | ||
// times | ||
const spy = sinon.spy(); | ||
|
||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(() => { | ||
throw new Error('stop sign in'); | ||
}); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.be.null; | ||
expect(spy).to.have.been.calledThrice; | ||
}); | ||
|
||
it('keeps previously-logged in user if one rejects', async () => { | ||
// Use a random email/password sign in for the base user | ||
const {user: baseUser} = await createUserWithEmailAndPassword(auth, randomEmail(), 'password'); | ||
|
||
// Also check that the function is called multiple | ||
// times | ||
const spy = sinon.spy(); | ||
|
||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(() => { | ||
throw new Error('stop sign in'); | ||
}); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.eq(baseUser); | ||
expect(spy).to.have.been.calledThrice; | ||
}); | ||
|
||
it('allows sign in with multiple callbacks all pass', async () => { | ||
// Use a random email/password sign in for the base user | ||
const {user: baseUser} = await createUserWithEmailAndPassword(auth, randomEmail(), 'password'); | ||
|
||
// Also check that the function is called multiple | ||
// times | ||
const spy = sinon.spy(); | ||
|
||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
beforeAuthStateChanged(spy); | ||
|
||
await expect(signIn()).not.to.be.rejected; | ||
expect(auth.currentUser).not.to.eq(baseUser); | ||
expect(spy).to.have.been.calledThrice; | ||
}); | ||
|
||
it('does not call subsequent callbacks after rejection', async () => { | ||
const firstSpy = sinon.spy(); | ||
const secondSpy = sinon.spy(); | ||
|
||
beforeAuthStateChanged(firstSpy); | ||
beforeAuthStateChanged(() => { | ||
throw new Error('stop sign in'); | ||
}); | ||
beforeAuthStateChanged(secondSpy); | ||
|
||
await expect(signIn()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(firstSpy).to.have.been.calledOnce; | ||
expect(secondSpy).not.to.have.been.called; | ||
}); | ||
|
||
it('can prevent sign-out', async () => { | ||
await signIn(); | ||
const user = auth.currentUser; | ||
|
||
beforeAuthStateChanged(() => { | ||
throw new Error('block sign out'); | ||
}); | ||
|
||
await expect(auth.signOut()).to.be.rejectedWith('auth/login-blocked'); | ||
expect(auth.currentUser).to.eq(user); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesdaniels I tried to be exhaustive with these tests. Please take a look and see if I'm missing any big use case.
Cross-tab testing is done down in the webdriver/* files.
I'll add the onAbort callback to the tests once that's merged