-
Notifications
You must be signed in to change notification settings - Fork 946
Implement useAuthEmulator and remove auth.useEmulator. #4414
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
7 commits
Select commit
Hold shift + click to select a range
706a315
Implement useAuthEmulator and remove auth.useEmulator.
yuchenshi 10d4bef
Remove unused imports.
yuchenshi a5817cb
Create nasty-lemons-bow.md
yuchenshi 871ef0b
Refactor impl into emulator.ts.
yuchenshi 17320c1
Add options to example.
yuchenshi 1fbe32f
Update .changeset/nasty-lemons-bow.md
yuchenshi 858c9a5
Delete nasty-lemons-bow.md
yuchenshi 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
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,156 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 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 * as chaiAsPromised from 'chai-as-promised'; | ||
import * as sinon from 'sinon'; | ||
import * as sinonChai from 'sinon-chai'; | ||
|
||
import { FirebaseError } from '@firebase/util'; | ||
|
||
import { endpointUrl, mockEndpoint } from '../../../test/helpers/api/helper'; | ||
import { testAuth, TestAuth, testUser } from '../../../test/helpers/mock_auth'; | ||
import * as fetch from '../../../test/helpers/mock_fetch'; | ||
import { Endpoint } from '../../api'; | ||
import { User } from '../../model/user'; | ||
import { _castAuth } from './auth_impl'; | ||
import { useAuthEmulator } from './emulator'; | ||
|
||
use(sinonChai); | ||
use(chaiAsPromised); | ||
|
||
describe('core/auth/emulator', () => { | ||
let auth: TestAuth; | ||
let user: User; | ||
let normalEndpoint: fetch.Route; | ||
let emulatorEndpoint: fetch.Route; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
user = testUser(_castAuth(auth), 'uid', 'email', true); | ||
fetch.setUp(); | ||
normalEndpoint = mockEndpoint(Endpoint.DELETE_ACCOUNT, {}); | ||
emulatorEndpoint = fetch.mock( | ||
`http://localhost:2020/${endpointUrl(Endpoint.DELETE_ACCOUNT).replace( | ||
/^.*:\/\//, | ||
'' | ||
)}`, | ||
{} | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fetch.tearDown(); | ||
sinon.restore(); | ||
|
||
// The DOM persists through tests; remove the banner if it is attached | ||
const banner = | ||
typeof document !== 'undefined' | ||
? document.querySelector('.firebase-emulator-warning') | ||
: null; | ||
if (banner) { | ||
banner.parentElement?.removeChild(banner); | ||
} | ||
}); | ||
|
||
context('useAuthEmulator', () => { | ||
it('fails if a network request has already been made', async () => { | ||
await user.delete(); | ||
expect(() => useAuthEmulator(auth, 'http://localhost:2020')).to.throw( | ||
FirebaseError, | ||
'auth/emulator-config-failed' | ||
); | ||
}); | ||
|
||
it('updates the endpoint appropriately', async () => { | ||
useAuthEmulator(auth, 'http://localhost:2020'); | ||
await user.delete(); | ||
expect(normalEndpoint.calls.length).to.eq(0); | ||
expect(emulatorEndpoint.calls.length).to.eq(1); | ||
}); | ||
|
||
it('checks the scheme properly', () => { | ||
expect(() => useAuthEmulator(auth, 'http://localhost:2020')).not.to.throw; | ||
delete auth.config.emulator; | ||
expect(() => useAuthEmulator(auth, 'https://localhost:2020')).not.to | ||
.throw; | ||
delete auth.config.emulator; | ||
expect(() => useAuthEmulator(auth, 'ssh://localhost:2020')).to.throw( | ||
FirebaseError, | ||
'auth/invalid-emulator-scheme' | ||
); | ||
delete auth.config.emulator; | ||
expect(() => useAuthEmulator(auth, 'localhost:2020')).to.throw( | ||
FirebaseError, | ||
'auth/invalid-emulator-scheme' | ||
); | ||
}); | ||
|
||
it('attaches a banner to the DOM', () => { | ||
useAuthEmulator(auth, 'http://localhost:2020'); | ||
if (typeof document !== 'undefined') { | ||
const el = document.querySelector('.firebase-emulator-warning')!; | ||
expect(el).not.to.be.null; | ||
expect(el.textContent).to.eq( | ||
'Running in emulator mode. ' + | ||
'Do not use with production credentials.' | ||
); | ||
} | ||
}); | ||
|
||
it('logs out a warning to the console', () => { | ||
sinon.stub(console, 'info'); | ||
useAuthEmulator(auth, 'http://localhost:2020'); | ||
expect(console.info).to.have.been.calledWith( | ||
'WARNING: You are using the Auth Emulator,' + | ||
' which is intended for local testing only. Do not use with' + | ||
' production credentials.' | ||
); | ||
}); | ||
|
||
it('logs out the warning but has no banner if disableBanner true', () => { | ||
sinon.stub(console, 'info'); | ||
useAuthEmulator(auth, 'http://localhost:2020', { disableWarnings: true }); | ||
expect(console.info).to.have.been.calledWith( | ||
'WARNING: You are using the Auth Emulator,' + | ||
' which is intended for local testing only. Do not use with' + | ||
' production credentials.' | ||
); | ||
if (typeof document !== 'undefined') { | ||
expect(document.querySelector('.firebase-emulator-warning')).to.be.null; | ||
} | ||
}); | ||
}); | ||
|
||
context('toJSON', () => { | ||
it('works when theres no current user', () => { | ||
expect(JSON.stringify(auth)).to.eq( | ||
'{"apiKey":"test-api-key","authDomain":"localhost","appName":"test-app"}' | ||
); | ||
}); | ||
|
||
it('also stringifies the current user', () => { | ||
auth.currentUser = ({ | ||
toJSON: (): object => ({ foo: 'bar' }) | ||
} as unknown) as User; | ||
expect(JSON.stringify(auth)).to.eq( | ||
'{"apiKey":"test-api-key","authDomain":"localhost",' + | ||
'"appName":"test-app","currentUser":{"foo":"bar"}}' | ||
); | ||
}); | ||
}); | ||
}); |
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.
All tests moved from
auth_impl.ts
. No changes except API.