Skip to content

Commit 9cf56da

Browse files
committed
added serverApp auth user tests
1 parent 8e6ab2d commit 9cf56da

File tree

3 files changed

+237
-2
lines changed

3 files changed

+237
-2
lines changed

packages/auth/karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function getTestFiles(argv) {
6565
'src/**/*.test.ts',
6666
'test/helpers/**/*.test.ts',
6767
'test/integration/flows/anonymous.test.ts',
68-
'test/integration/flows/email.test.ts'
68+
'test/integration/flows/email.test.ts',
69+
'test/integration/flows/firebaseserverapp.test.ts'
6970
];
7071
}
7172
}

packages/auth/scripts/run_node_tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let testConfig = [
4848
];
4949

5050
if (argv.integration) {
51-
testConfig = ['test/integration/flows/{email,anonymous}.test.ts'];
51+
testConfig = ['test/integration/flows/{email,anonymous,firebaseserverapp}.test.ts'];
5252
if (argv.local) {
5353
testConfig.push('test/integration/flows/*.local.test.ts');
5454
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect, use } from 'chai';
19+
import chaiAsPromised from 'chai-as-promised';
20+
21+
// eslint-disable-next-line import/no-extraneous-dependencies
22+
import {
23+
Auth,
24+
OperationType,
25+
createUserWithEmailAndPassword,
26+
getAdditionalUserInfo,
27+
getAuth,
28+
onAuthStateChanged,
29+
signInAnonymously,
30+
updateProfile,
31+
} from '@firebase/auth';
32+
import { FirebaseError } from '@firebase/util';
33+
import { initializeServerApp } from '@firebase/app';
34+
35+
import {
36+
cleanUpTestInstance,
37+
getTestInstance,
38+
randomEmail
39+
} from '../../helpers/integration/helpers';
40+
import { generateMiddlewareTests } from './middleware_test_generator';
41+
42+
use(chaiAsPromised);
43+
44+
const sleep = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay))
45+
const signInWaitDuration = 200;
46+
47+
describe('Integration test: automatic user login via idToken in server apps', () => {
48+
let auth: Auth;
49+
let serverAppAuth: Auth;
50+
51+
beforeEach(() => {
52+
auth = getTestInstance();
53+
});
54+
55+
afterEach(() => {
56+
cleanUpTestInstance(auth);
57+
cleanUpTestInstance(serverAppAuth);
58+
});
59+
60+
it('signs in with anonymous user', async () => {
61+
const userCred = await signInAnonymously(auth);
62+
expect(auth.currentUser).to.eq(userCred.user);
63+
expect(userCred.operationType).to.eq(OperationType.SIGN_IN);
64+
const user = userCred.user;
65+
expect(user).to.equal(auth.currentUser);
66+
expect(user.isAnonymous).to.be.true;
67+
expect(user.uid).to.be.a('string');
68+
expect(user.emailVerified).to.be.false;
69+
expect(user.providerData.length).to.equal(0);
70+
71+
const authIdToken = await user.getIdToken();
72+
const firebaseServerAppSettings = { authIdToken };
73+
74+
const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings);
75+
serverAppAuth = getAuth(serverApp);
76+
let numberServerLogins = 0;
77+
onAuthStateChanged(serverAppAuth, function(serverAuthUser) {
78+
if (serverAuthUser) {
79+
numberServerLogins++;
80+
81+
// Note, the serverAuthUser does not fully equal the standard Auth user
82+
// since the serverAuthUser does not have a refresh token.
83+
expect(user.uid).to.be.equal(serverAuthUser.uid);
84+
expect(user.isAnonymous).to.be.equal(serverAuthUser.isAnonymous);
85+
expect(user.emailVerified).to.be.equal(serverAuthUser.emailVerified);
86+
expect(user.providerData.length).to.eq(serverAuthUser.providerData.length);
87+
}
88+
});
89+
await sleep(signInWaitDuration);
90+
expect(numberServerLogins).to.equal(1);
91+
});
92+
93+
it('getToken operations fullfilled or rejected', async () => {
94+
const userCred = await signInAnonymously(auth);
95+
expect(auth.currentUser).to.eq(userCred.user);
96+
expect(userCred.operationType).to.eq(OperationType.SIGN_IN);
97+
const user = userCred.user;
98+
expect(user).to.equal(auth.currentUser);
99+
expect(user.isAnonymous).to.be.true;
100+
expect(user.uid).to.be.a('string');
101+
102+
const authIdToken = await user.getIdToken();
103+
const firebaseServerAppSettings = { authIdToken };
104+
105+
const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings);
106+
serverAppAuth = getAuth(serverApp);
107+
let numberServerLogins = 0;
108+
onAuthStateChanged(serverAppAuth, function(serverAuthUser) {
109+
if (serverAuthUser) {
110+
numberServerLogins++;
111+
expect(user.uid).to.be.equal(serverAuthUser.uid);
112+
expect(serverAppAuth.currentUser).to.equal(serverAuthUser);
113+
expect(serverAuthUser.getIdToken)
114+
}
115+
});
116+
await sleep(signInWaitDuration);
117+
expect(numberServerLogins).to.equal(1);
118+
expect(serverAppAuth.currentUser).to.not.be.null;
119+
if(serverAppAuth.currentUser) {
120+
const idToken = await serverAppAuth.currentUser.getIdToken(/*forceRefresh=*/false);
121+
expect(idToken).to.not.be.null;
122+
expect(serverAppAuth.currentUser.getIdToken(/*forceRefresh=*/true)).to.be.rejected;
123+
}
124+
});
125+
126+
it('signs in with email crednetial user', async () => {
127+
const email = randomEmail();
128+
const password = 'password';
129+
const userCred = await createUserWithEmailAndPassword(
130+
auth,
131+
email,
132+
password
133+
);
134+
const user = userCred.user;
135+
expect(auth.currentUser).to.eq(userCred.user);
136+
expect(userCred.operationType).to.eq(OperationType.SIGN_IN);
137+
138+
const additionalUserInfo = getAdditionalUserInfo(userCred)!;
139+
expect(additionalUserInfo.isNewUser).to.be.true;
140+
expect(additionalUserInfo.providerId).to.eq('password');
141+
expect(user.isAnonymous).to.be.false;
142+
expect(user.email).to.equal(email);
143+
144+
const authIdToken = await user.getIdToken();
145+
const firebaseServerAppSettings = { authIdToken };
146+
147+
const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings);
148+
serverAppAuth = getAuth(serverApp);
149+
let numberServerLogins = 0;
150+
onAuthStateChanged(serverAppAuth, function(serverAuthUser) {
151+
if (serverAuthUser) {
152+
numberServerLogins++;
153+
expect(serverAppAuth.currentUser).to.equal(serverAuthUser);
154+
expect(user.uid).to.be.equal(serverAuthUser.uid);
155+
expect(serverAuthUser.refreshToken).to.be.empty;
156+
expect(user.isAnonymous).to.be.equal(serverAuthUser.isAnonymous);
157+
expect(user.emailVerified).to.be.equal(serverAuthUser.emailVerified);
158+
expect(user.providerData.length).to.eq(serverAuthUser.providerData.length);
159+
expect(user.email).to.equal(serverAuthUser.email);
160+
}
161+
});
162+
await sleep(signInWaitDuration);
163+
expect(numberServerLogins).to.equal(1);
164+
});
165+
166+
it('can reload user', async () => {
167+
const userCred = await signInAnonymously(auth);
168+
expect(auth.currentUser).to.eq(userCred.user);
169+
170+
const user = userCred.user;
171+
expect(user).to.equal(auth.currentUser);
172+
expect(user.uid).to.be.a('string');
173+
174+
const authIdToken = await user.getIdToken();
175+
const firebaseServerAppSettings = { authIdToken };
176+
177+
const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings);
178+
serverAppAuth = getAuth(serverApp);
179+
let numberServerLogins = 0;
180+
onAuthStateChanged(serverAppAuth, function(serverAuthUser) {
181+
if (serverAuthUser) {
182+
numberServerLogins++;
183+
expect(user.uid).to.be.equal(serverAuthUser.uid);
184+
expect(serverAppAuth.currentUser).to.equal(serverAuthUser);
185+
}
186+
});
187+
await sleep(signInWaitDuration);
188+
expect(serverAppAuth.currentUser).to.not.be.null;
189+
if(serverAppAuth.currentUser) {
190+
await serverAppAuth.currentUser.reload();
191+
}
192+
expect(numberServerLogins).to.equal(1);
193+
});
194+
195+
it('can update server based user profile', async () => {
196+
const userCred = await signInAnonymously(auth);
197+
expect(auth.currentUser).to.eq(userCred.user);
198+
199+
const user = userCred.user;
200+
expect(user).to.equal(auth.currentUser);
201+
expect(user.uid).to.be.a('string');
202+
expect(user.displayName).to.be.null;
203+
204+
const authIdToken = await user.getIdToken();
205+
const firebaseServerAppSettings = { authIdToken };
206+
207+
const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings);
208+
serverAppAuth = getAuth(serverApp);
209+
let numberServerLogins = 0;
210+
const newDisplayName = "newName";
211+
onAuthStateChanged(serverAppAuth, function(serverAuthUser) {
212+
if (serverAuthUser) {
213+
numberServerLogins++;
214+
expect(serverAppAuth.currentUser).to.equal(serverAuthUser);
215+
expect(user.uid).to.be.equal(serverAuthUser.uid);
216+
expect(user.displayName).to.be.null;
217+
updateProfile(serverAuthUser, {
218+
displayName: newDisplayName
219+
});
220+
}
221+
});
222+
await sleep(signInWaitDuration);
223+
expect(serverAppAuth.currentUser).to.not.be.null;
224+
if(serverAppAuth.currentUser) {
225+
await serverAppAuth.currentUser.reload();
226+
}
227+
228+
expect(numberServerLogins).to.equal(1);
229+
expect(serverAppAuth.currentUser).to.not.be.null;
230+
expect(serverAppAuth.currentUser?.displayName).to.not.be.null;
231+
expect(serverAppAuth.currentUser?.displayName).to.equal(newDisplayName);
232+
});
233+
234+
});

0 commit comments

Comments
 (0)