Skip to content

Commit cb58a66

Browse files
authored
Refactor so every unit test gets its own Auth object (#3129)
* Refactor so every unit test gets its own Auth object * PR feedback * Formatting
1 parent 06847d8 commit cb58a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+715
-335
lines changed

packages-exp/auth-exp/src/api/account_management/account.test.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { ProviderId } from '@firebase/auth-types-exp';
19-
import { FirebaseError } from '@firebase/util';
2018
import { expect, use } from 'chai';
2119
import * as chaiAsPromised from 'chai-as-promised';
22-
import { Endpoint } from '..';
20+
21+
import { ProviderId } from '@firebase/auth-types-exp';
22+
import { FirebaseError } from '@firebase/util';
23+
24+
import { Endpoint } from '../';
2325
import { mockEndpoint } from '../../../test/api/helper';
24-
import { mockAuth } from '../../../test/mock_auth';
26+
import { testAuth } from '../../../test/mock_auth';
2527
import * as mockFetch from '../../../test/mock_fetch';
28+
import { Auth } from '../../model/auth';
2629
import { ServerError } from '../errors';
2730
import { deleteAccount, deleteLinkedAccounts, getAccountInfo } from './account';
2831

@@ -33,13 +36,19 @@ describe('api/account_management/deleteAccount', () => {
3336
idToken: 'id-token'
3437
};
3538

36-
beforeEach(mockFetch.setUp);
39+
let auth: Auth;
40+
41+
beforeEach(async () => {
42+
auth = await testAuth();
43+
mockFetch.setUp();
44+
});
45+
3746
afterEach(mockFetch.tearDown);
3847

3948
it('should POST to the correct endpoint', async () => {
4049
const mock = mockEndpoint(Endpoint.DELETE_ACCOUNT, {});
4150

42-
await deleteAccount(mockAuth, request);
51+
await deleteAccount(auth, request);
4352
expect(mock.calls[0].request).to.eql(request);
4453
expect(mock.calls[0].method).to.eq('POST');
4554
expect(mock.calls[0].headers).to.eql({
@@ -65,7 +74,7 @@ describe('api/account_management/deleteAccount', () => {
6574
400
6675
);
6776

68-
await expect(deleteAccount(mockAuth, request)).to.be.rejectedWith(
77+
await expect(deleteAccount(auth, request)).to.be.rejectedWith(
6978
FirebaseError,
7079
"Firebase: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key. (auth/invalid-user-token)."
7180
);
@@ -79,7 +88,13 @@ describe('api/account_management/deleteLinkedAccounts', () => {
7988
deleteProvider: [ProviderId.GOOGLE]
8089
};
8190

82-
beforeEach(mockFetch.setUp);
91+
let auth: Auth;
92+
93+
beforeEach(async () => {
94+
auth = await testAuth();
95+
mockFetch.setUp();
96+
});
97+
8398
afterEach(mockFetch.tearDown);
8499

85100
it('should POST to the correct endpoint', async () => {
@@ -92,7 +107,7 @@ describe('api/account_management/deleteLinkedAccounts', () => {
92107
]
93108
});
94109

95-
const response = await deleteLinkedAccounts(mockAuth, request);
110+
const response = await deleteLinkedAccounts(auth, request);
96111
expect(response.providerUserInfo[0].providerId).to.eq('google.com');
97112
expect(response.providerUserInfo[0].email).to.eq('[email protected]');
98113
expect(mock.calls[0].request).to.eql(request);
@@ -120,7 +135,7 @@ describe('api/account_management/deleteLinkedAccounts', () => {
120135
400
121136
);
122137

123-
await expect(deleteLinkedAccounts(mockAuth, request)).to.be.rejectedWith(
138+
await expect(deleteLinkedAccounts(auth, request)).to.be.rejectedWith(
124139
FirebaseError,
125140
'Firebase: The specified provider ID is invalid. (auth/invalid-provider-id).'
126141
);
@@ -133,7 +148,13 @@ describe('api/account_management/getAccountInfo', () => {
133148
idToken: 'id-token'
134149
};
135150

136-
beforeEach(mockFetch.setUp);
151+
let auth: Auth;
152+
153+
beforeEach(async () => {
154+
auth = await testAuth();
155+
mockFetch.setUp();
156+
});
157+
137158
afterEach(mockFetch.tearDown);
138159

139160
it('should POST to the correct endpoint', async () => {
@@ -146,7 +167,7 @@ describe('api/account_management/getAccountInfo', () => {
146167
]
147168
});
148169

149-
const response = await getAccountInfo(mockAuth, request);
170+
const response = await getAccountInfo(auth, request);
150171
expect(response.users[0].displayName).to.eq('my-name');
151172
expect(response.users[0].email).to.eq('[email protected]');
152173
expect(mock.calls[0].request).to.eql(request);
@@ -174,7 +195,7 @@ describe('api/account_management/getAccountInfo', () => {
174195
400
175196
);
176197

177-
await expect(getAccountInfo(mockAuth, request)).to.be.rejectedWith(
198+
await expect(getAccountInfo(auth, request)).to.be.rejectedWith(
178199
FirebaseError,
179200
"Firebase: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key. (auth/invalid-user-token)."
180201
);

packages-exp/auth-exp/src/api/account_management/email_and_password.test.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError } from '@firebase/util';
1918
import { expect, use } from 'chai';
2019
import * as chaiAsPromised from 'chai-as-promised';
21-
import { Endpoint } from '..';
20+
21+
import { FirebaseError } from '@firebase/util';
22+
23+
import { Endpoint } from '../';
2224
import { mockEndpoint } from '../../../test/api/helper';
23-
import { mockAuth } from '../../../test/mock_auth';
25+
import { testAuth } from '../../../test/mock_auth';
2426
import * as mockFetch from '../../../test/mock_fetch';
27+
import { Auth } from '../../model/auth';
2528
import { ServerError } from '../errors';
2629
import { resetPassword, updateEmailPassword } from './email_and_password';
2730

@@ -33,15 +36,21 @@ describe('api/account_management/resetPassword', () => {
3336
newPassword: 'new-password'
3437
};
3538

36-
beforeEach(mockFetch.setUp);
39+
let auth: Auth;
40+
41+
beforeEach(async () => {
42+
auth = await testAuth();
43+
mockFetch.setUp();
44+
});
45+
3746
afterEach(mockFetch.tearDown);
3847

3948
it('should POST to the correct endpoint', async () => {
4049
const mock = mockEndpoint(Endpoint.RESET_PASSWORD, {
4150
4251
});
4352

44-
const response = await resetPassword(mockAuth, request);
53+
const response = await resetPassword(auth, request);
4554
expect(response.email).to.eq('[email protected]');
4655
expect(mock.calls[0].request).to.eql(request);
4756
expect(mock.calls[0].method).to.eq('POST');
@@ -68,7 +77,7 @@ describe('api/account_management/resetPassword', () => {
6877
400
6978
);
7079

71-
await expect(resetPassword(mockAuth, request)).to.be.rejectedWith(
80+
await expect(resetPassword(auth, request)).to.be.rejectedWith(
7281
FirebaseError,
7382
'Firebase: We have blocked all requests from this device due to unusual activity. Try again later. (auth/too-many-requests).'
7483
);
@@ -84,15 +93,21 @@ describe('api/account_management/updateEmailPassword', () => {
8493
password: 'new-password'
8594
};
8695

87-
beforeEach(mockFetch.setUp);
96+
let auth: Auth;
97+
98+
beforeEach(async () => {
99+
auth = await testAuth();
100+
mockFetch.setUp();
101+
});
102+
88103
afterEach(mockFetch.tearDown);
89104

90105
it('should POST to the correct endpoint', async () => {
91106
const mock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
92107
idToken: 'id-token'
93108
});
94109

95-
const response = await updateEmailPassword(mockAuth, request);
110+
const response = await updateEmailPassword(auth, request);
96111
expect(response.idToken).to.eq('id-token');
97112
expect(mock.calls[0].request).to.eql(request);
98113
expect(mock.calls[0].method).to.eq('POST');
@@ -119,7 +134,7 @@ describe('api/account_management/updateEmailPassword', () => {
119134
400
120135
);
121136

122-
await expect(updateEmailPassword(mockAuth, request)).to.be.rejectedWith(
137+
await expect(updateEmailPassword(auth, request)).to.be.rejectedWith(
123138
FirebaseError,
124139
'Firebase: The email address is badly formatted. (auth/invalid-email).'
125140
);

packages-exp/auth-exp/src/api/account_management/mfa.test.ts

+33-12
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError } from '@firebase/util';
1918
import { expect, use } from 'chai';
2019
import * as chaiAsPromised from 'chai-as-promised';
21-
import { Endpoint } from '..';
20+
21+
import { FirebaseError } from '@firebase/util';
22+
23+
import { Endpoint } from '../';
2224
import { mockEndpoint } from '../../../test/api/helper';
23-
import { mockAuth } from '../../../test/mock_auth';
25+
import { testAuth } from '../../../test/mock_auth';
2426
import * as mockFetch from '../../../test/mock_fetch';
27+
import { Auth } from '../../model/auth';
2528
import { ServerError } from '../errors';
2629
import { enrollPhoneMfa, startEnrollPhoneMfa, withdrawMfa } from './mfa';
2730

@@ -36,7 +39,13 @@ describe('api/account_management/startEnrollPhoneMfa', () => {
3639
}
3740
};
3841

39-
beforeEach(mockFetch.setUp);
42+
let auth: Auth;
43+
44+
beforeEach(async () => {
45+
auth = await testAuth();
46+
mockFetch.setUp();
47+
});
48+
4049
afterEach(mockFetch.tearDown);
4150

4251
it('should POST to the correct endpoint', async () => {
@@ -46,7 +55,7 @@ describe('api/account_management/startEnrollPhoneMfa', () => {
4655
}
4756
});
4857

49-
const response = await startEnrollPhoneMfa(mockAuth, request);
58+
const response = await startEnrollPhoneMfa(auth, request);
5059
expect(response.phoneSessionInfo.sessionInfo).to.eq('session-info');
5160
expect(mock.calls[0].request).to.eql(request);
5261
expect(mock.calls[0].method).to.eq('POST');
@@ -73,7 +82,7 @@ describe('api/account_management/startEnrollPhoneMfa', () => {
7382
400
7483
);
7584

76-
await expect(startEnrollPhoneMfa(mockAuth, request)).to.be.rejectedWith(
85+
await expect(startEnrollPhoneMfa(auth, request)).to.be.rejectedWith(
7786
FirebaseError,
7887
"Firebase: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key. (auth/invalid-user-token)."
7988
);
@@ -91,7 +100,13 @@ describe('api/account_management/enrollPhoneMfa', () => {
91100
}
92101
};
93102

94-
beforeEach(mockFetch.setUp);
103+
let auth: Auth;
104+
105+
beforeEach(async () => {
106+
auth = await testAuth();
107+
mockFetch.setUp();
108+
});
109+
95110
afterEach(mockFetch.tearDown);
96111

97112
it('should POST to the correct endpoint', async () => {
@@ -100,7 +115,7 @@ describe('api/account_management/enrollPhoneMfa', () => {
100115
idToken: 'id-token'
101116
});
102117

103-
const response = await enrollPhoneMfa(mockAuth, request);
118+
const response = await enrollPhoneMfa(auth, request);
104119
expect(response.displayName).to.eq('my-name');
105120
expect(response.idToken).to.eq('id-token');
106121
expect(mock.calls[0].request).to.eql(request);
@@ -128,7 +143,7 @@ describe('api/account_management/enrollPhoneMfa', () => {
128143
400
129144
);
130145

131-
await expect(enrollPhoneMfa(mockAuth, request)).to.be.rejectedWith(
146+
await expect(enrollPhoneMfa(auth, request)).to.be.rejectedWith(
132147
FirebaseError,
133148
'Firebase: The verification ID used to create the phone auth credential is invalid. (auth/invalid-verification-id).'
134149
);
@@ -142,7 +157,13 @@ describe('api/account_management/withdrawMfa', () => {
142157
mfaEnrollmentId: 'mfa-enrollment-id'
143158
};
144159

145-
beforeEach(mockFetch.setUp);
160+
let auth: Auth;
161+
162+
beforeEach(async () => {
163+
auth = await testAuth();
164+
mockFetch.setUp();
165+
});
166+
146167
afterEach(mockFetch.tearDown);
147168

148169
it('should POST to the correct endpoint', async () => {
@@ -151,7 +172,7 @@ describe('api/account_management/withdrawMfa', () => {
151172
idToken: 'id-token'
152173
});
153174

154-
const response = await withdrawMfa(mockAuth, request);
175+
const response = await withdrawMfa(auth, request);
155176
expect(response.displayName).to.eq('my-name');
156177
expect(response.idToken).to.eq('id-token');
157178
expect(mock.calls[0].request).to.eql(request);
@@ -179,7 +200,7 @@ describe('api/account_management/withdrawMfa', () => {
179200
400
180201
);
181202

182-
await expect(withdrawMfa(mockAuth, request)).to.be.rejectedWith(
203+
await expect(withdrawMfa(auth, request)).to.be.rejectedWith(
183204
FirebaseError,
184205
"Firebase: This user's credential isn't valid for this project. This can happen if the user's token has been tampered with, or if the user isn't for the project associated with this API key. (auth/invalid-user-token)."
185206
);

packages-exp/auth-exp/src/api/account_management/profile.test.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError } from '@firebase/util';
1918
import { expect, use } from 'chai';
2019
import * as chaiAsPromised from 'chai-as-promised';
21-
import { Endpoint } from '..';
20+
21+
import { FirebaseError } from '@firebase/util';
22+
23+
import { Endpoint } from '../';
2224
import { mockEndpoint } from '../../../test/api/helper';
23-
import { mockAuth } from '../../../test/mock_auth';
25+
import { testAuth } from '../../../test/mock_auth';
2426
import * as mockFetch from '../../../test/mock_fetch';
27+
import { Auth } from '../../model/auth';
2528
import { ServerError } from '../errors';
2629
import { updateProfile } from './profile';
2730

@@ -34,7 +37,13 @@ describe('api/account_management/updateProfile', () => {
3437
password: 'my-password'
3538
};
3639

37-
beforeEach(mockFetch.setUp);
40+
let auth: Auth;
41+
42+
beforeEach(async () => {
43+
auth = await testAuth();
44+
mockFetch.setUp();
45+
});
46+
3847
afterEach(mockFetch.tearDown);
3948

4049
it('should POST to the correct endpoint', async () => {
@@ -43,7 +52,7 @@ describe('api/account_management/updateProfile', () => {
4352
4453
});
4554

46-
const response = await updateProfile(mockAuth, request);
55+
const response = await updateProfile(auth, request);
4756
expect(response.displayName).to.eq('my-name');
4857
expect(mock.calls[0].request).to.eql(request);
4958
expect(mock.calls[0].method).to.eq('POST');
@@ -70,7 +79,7 @@ describe('api/account_management/updateProfile', () => {
7079
400
7180
);
7281

73-
await expect(updateProfile(mockAuth, request)).to.be.rejectedWith(
82+
await expect(updateProfile(auth, request)).to.be.rejectedWith(
7483
FirebaseError,
7584
'Firebase: The email address is already in use by another account. (auth/email-already-in-use).'
7685
);

0 commit comments

Comments
 (0)