-
Notifications
You must be signed in to change notification settings - Fork 937
Add updateProfile, updateEmail, updatePassword #3122
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 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6d57cd2
Profile management
sam-gc d637e2e
Add updateProfile, updateEmail, updatePassword
sam-gc 5645660
Fixing up tests
sam-gc 1918a18
Update tests further
sam-gc 1ed60a1
Add re-exports for public functions
sam-gc 847f6d8
Fix up authCredential types to use public versions
sam-gc 91e3998
Formatting
sam-gc f22451f
Code cleanup
sam-gc 366a6de
Formatting
sam-gc d948ca3
PR feedback
sam-gc 6e22793
Formatting
sam-gc 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
261 changes: 261 additions & 0 deletions
261
packages-exp/auth-exp/src/core/user/account_info.test.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,261 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 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 { ProviderId, UserInfo } from '@firebase/auth-types-exp'; | ||
|
||
// import { UserInfo } from '@firebase/auth-types-exp'; | ||
import { mockEndpoint } from '../../../test/api/helper'; | ||
import { TestAuth, testAuth, testUser } from '../../../test/mock_auth'; | ||
import * as fetch from '../../../test/mock_fetch'; | ||
import { Endpoint } from '../../api'; | ||
import { User } from '../../model/user'; | ||
import { updateEmail, updatePassword, updateProfile } from './account_info'; | ||
|
||
use(chaiAsPromised); | ||
use(sinonChai); | ||
|
||
const PASSWORD_PROVIDER: UserInfo = { | ||
providerId: ProviderId.PASSWORD, | ||
uid: 'uid', | ||
email: 'email', | ||
displayName: 'old-name', | ||
phoneNumber: 'phone-number', | ||
photoURL: 'old-url' | ||
}; | ||
|
||
describe('core/user/profile', () => { | ||
let user: User; | ||
let auth: TestAuth; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
user = testUser(auth, 'uid', '', true); | ||
fetch.setUp(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
fetch.tearDown(); | ||
}); | ||
|
||
describe('#updateProfile', () => { | ||
it('returns immediately if profile object is empty', async () => { | ||
const ep = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {}); | ||
await updateProfile(user, {}); | ||
expect(ep.calls).to.be.empty; | ||
}); | ||
|
||
it('calls the setAccountInfo endpoint', async () => { | ||
const ep = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {}); | ||
|
||
await updateProfile(user, { | ||
displayName: 'displayname', | ||
photoURL: 'photo' | ||
}); | ||
expect(ep.calls[0].request).to.eql({ | ||
idToken: 'access-token', | ||
displayName: 'displayname', | ||
photoUrl: 'photo' | ||
}); | ||
}); | ||
|
||
it('sets the fields on the user based on the response', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
displayName: 'response-name', | ||
photoUrl: 'response-photo' | ||
}); | ||
|
||
await updateProfile(user, { | ||
displayName: 'displayname', | ||
photoURL: 'photo' | ||
}); | ||
expect(user.displayName).to.eq('response-name'); | ||
expect(user.photoURL).to.eq('response-photo'); | ||
}); | ||
|
||
it('sets the fields on the password provider', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
displayName: 'response-name', | ||
photoUrl: 'response-photo' | ||
}); | ||
user.providerData = [{ ...PASSWORD_PROVIDER }]; | ||
|
||
await updateProfile(user, { | ||
displayName: 'displayname', | ||
photoURL: 'photo' | ||
}); | ||
const provider = user.providerData[0]; | ||
expect(provider.displayName).to.eq('response-name'); | ||
expect(provider.photoURL).to.eq('response-photo'); | ||
}); | ||
}); | ||
|
||
describe('#updateEmail', () => { | ||
it('calls the setAccountInfo endpoint and reloads the user', async () => { | ||
const set = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {}); | ||
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { | ||
users: [{ localId: 'new-uid-to-prove-refresh-got-called' }] | ||
}); | ||
|
||
await updateEmail(user, '[email protected]'); | ||
expect(set.calls[0].request).to.eql({ | ||
idToken: 'access-token', | ||
email: '[email protected]' | ||
}); | ||
|
||
expect(user.uid).to.eq('new-uid-to-prove-refresh-got-called'); | ||
}); | ||
}); | ||
|
||
describe('#updatePassword', () => { | ||
it('calls the setAccountInfo endpoint and reloads the user', async () => { | ||
const set = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {}); | ||
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { | ||
users: [{ localId: 'new-uid-to-prove-refresh-got-called' }] | ||
}); | ||
|
||
await updatePassword(user, 'pass'); | ||
expect(set.calls[0].request).to.eql({ | ||
idToken: 'access-token', | ||
password: 'pass' | ||
}); | ||
|
||
expect(user.uid).to.eq('new-uid-to-prove-refresh-got-called'); | ||
}); | ||
}); | ||
|
||
describe('notifications', () => { | ||
let idTokenChange: sinon.SinonStub; | ||
|
||
beforeEach(async () => { | ||
idTokenChange = sinon.stub(); | ||
auth.onIdTokenChanged(idTokenChange); | ||
|
||
// Flush token change promises which are floating | ||
await auth.updateCurrentUser(user); | ||
auth._isInitialized = true; | ||
idTokenChange.resetHistory(); | ||
}); | ||
|
||
describe('#updateProfile', () => { | ||
it('triggers a token update if necessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'new-id-token', | ||
refreshToken: 'new-refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updateProfile(user, { displayName: 'd' }); | ||
expect(idTokenChange).to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
|
||
it('does NOT trigger a token update if unnecessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'access-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updateProfile(user, { displayName: 'd' }); | ||
expect(idTokenChange).not.to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
}); | ||
|
||
describe('#updateEmail', () => { | ||
beforeEach(() => { | ||
// This is necessary because this method calls reload; we don't care about that though, | ||
// for these tests we're looking at the change listeners | ||
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { users: [{}] }); | ||
}); | ||
|
||
it('triggers a token update if necessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'new-id-token', | ||
refreshToken: 'new-refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updatePassword(user, '[email protected]'); | ||
expect(idTokenChange).to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
|
||
it('does NOT trigger a token update if unnecessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'access-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updateEmail(user, '[email protected]'); | ||
expect(idTokenChange).not.to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
}); | ||
|
||
describe('#updatePassword', () => { | ||
beforeEach(() => { | ||
// This is necessary because this method calls reload; we don't care about that though, | ||
// for these tests we're looking at the change listeners | ||
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, { users: [{}] }); | ||
}); | ||
|
||
it('triggers a token update if necessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'new-id-token', | ||
refreshToken: 'new-refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updatePassword(user, 'pass'); | ||
expect(idTokenChange).to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
|
||
it('does NOT trigger a token update if unnecessary', async () => { | ||
mockEndpoint(Endpoint.SET_ACCOUNT_INFO, { | ||
idToken: 'access-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: 300 | ||
}); | ||
|
||
await updatePassword(user, 'pass'); | ||
expect(idTokenChange).not.to.have.been.called; | ||
expect(auth.persistenceLayer.lastObjectSet).to.eql( | ||
user.toPlainObject() | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.