Skip to content

user.delete() implementation #3072

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 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages-exp/auth-exp/src/core/user/token_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ describe('core/user/token_manager', () => {
});
});

describe('#clearRefreshToken', () => {
it('sets refresh token to null', () => {
stsTokenManager.refreshToken = 'refresh-token';
stsTokenManager.clearRefreshToken();
expect(stsTokenManager.refreshToken).to.be.null;
});
});

describe('#getToken', () => {
context('with endpoint setup', () => {
let mock: fetch.Route;
Expand Down
4 changes: 4 additions & 0 deletions packages-exp/auth-exp/src/core/user/token_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export class StsTokenManager {
};
}

clearRefreshToken(): void {
this.refreshToken = null;
}

toPlainObject(): object {
return {
refreshToken: this.refreshToken,
Expand Down
29 changes: 27 additions & 2 deletions packages-exp/auth-exp/src/core/user/user_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,37 @@

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 { mockEndpoint } from '../../../test/api/helper';
import { makeJWT } from '../../../test/jwt';
import { mockAuth } from '../../../test/mock_auth';
import * as fetch from '../../../test/mock_fetch';
import { Endpoint } from '../../api';
import { IdTokenResponse } from '../../model/id_token';
import { StsTokenManager } from './token_manager';
import { UserImpl } from './user_impl';

use(sinonChai);
use(chaiAsPromised);

describe('core/user/user_impl', () => {
const auth = mockAuth;
let stsTokenManager: StsTokenManager;

beforeEach(() => {
fetch.setUp();
stsTokenManager = new StsTokenManager();
});

afterEach(() => {
sinon.restore();
fetch.tearDown();
});

describe('.constructor', () => {
it('attaches required fields', () => {
const user = new UserImpl({ uid: 'uid', auth, stsTokenManager });
Expand Down Expand Up @@ -116,9 +128,22 @@ describe('core/user/user_impl', () => {
});

describe('#delete', () => {
it('throws', () => {
it('calls delete endpoint', async () => {
stsTokenManager.updateFromServerResponse({
idToken: 'id-token',
refreshToken: 'refresh-token-string',
expiresIn: '100000'
} as IdTokenResponse);
const user = new UserImpl({ uid: 'uid', auth, stsTokenManager });
expect(() => user.delete()).to.throw();
const endpoint = mockEndpoint(Endpoint.DELETE_ACCOUNT, {});
const signOut = sinon.stub(auth, 'signOut');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to test side effects instead of mocking this out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do this once #2963 merges


await user.delete();
expect(endpoint.calls[0].request).to.eql({
idToken: 'id-token'
});
expect(signOut).to.have.been.called;
expect(stsTokenManager.refreshToken).to.be.null;
});
});

Expand Down
12 changes: 10 additions & 2 deletions packages-exp/auth-exp/src/core/user/user_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { IdTokenResult } from '@firebase/auth-types-exp';

import { deleteAccount } from '../../api/account_management/account';
import { Auth } from '../../model/auth';
import { User } from '../../model/user';
import { PersistedBlob } from '../persistence';
Expand Down Expand Up @@ -99,8 +100,15 @@ export class UserImpl implements User {
return reload(this);
}

delete(): Promise<void> {
throw new Error('Method not implemented.');
async delete(): Promise<void> {
const idToken = await this.getIdToken();
await deleteAccount(this.auth, { idToken });
this.stsTokenManager.clearRefreshToken();

// TODO: Determine if cancellable-promises are necessary to use in this class so that delete()
// cancels pending actions...

return this.auth.signOut();
}

toPlainObject(): PersistedBlob {
Expand Down