|
| 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 | + signOut, |
| 31 | + updateProfile |
| 32 | +} from '@firebase/auth'; |
| 33 | +import { isBrowser } from '@firebase/util'; |
| 34 | +import { initializeServerApp } from '@firebase/app'; |
| 35 | + |
| 36 | +import { |
| 37 | + cleanUpTestInstance, |
| 38 | + getTestInstance, |
| 39 | + randomEmail |
| 40 | +} from '../../helpers/integration/helpers'; |
| 41 | + |
| 42 | +use(chaiAsPromised); |
| 43 | + |
| 44 | +const signInWaitDuration = 200; |
| 45 | + |
| 46 | +describe('Integration test: Auth FirebaseServerApp tests', () => { |
| 47 | + let auth: Auth; |
| 48 | + let serverAppAuth: Auth | null; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + auth = getTestInstance(); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + if (serverAppAuth) { |
| 56 | + await signOut(serverAppAuth); |
| 57 | + serverAppAuth = null; |
| 58 | + } |
| 59 | + await cleanUpTestInstance(auth); |
| 60 | + }); |
| 61 | + |
| 62 | + it('signs in with anonymous user', async () => { |
| 63 | + if (isBrowser()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + const userCred = await signInAnonymously(auth); |
| 67 | + expect(auth.currentUser).to.eq(userCred.user); |
| 68 | + expect(userCred.operationType).to.eq(OperationType.SIGN_IN); |
| 69 | + const user = userCred.user; |
| 70 | + expect(user).to.equal(auth.currentUser); |
| 71 | + expect(user.isAnonymous).to.be.true; |
| 72 | + expect(user.uid).to.be.a('string'); |
| 73 | + expect(user.emailVerified).to.be.false; |
| 74 | + expect(user.providerData.length).to.equal(0); |
| 75 | + |
| 76 | + const authIdToken = await user.getIdToken(); |
| 77 | + const firebaseServerAppSettings = { authIdToken }; |
| 78 | + |
| 79 | + const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings); |
| 80 | + serverAppAuth = getAuth(serverApp); |
| 81 | + let numberServerLogins = 0; |
| 82 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 83 | + if (serverAuthUser) { |
| 84 | + numberServerLogins++; |
| 85 | + |
| 86 | + // Note, the serverAuthUser does not fully equal the standard Auth user |
| 87 | + // since the serverAuthUser does not have a refresh token. |
| 88 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 89 | + expect(user.isAnonymous).to.be.equal(serverAuthUser.isAnonymous); |
| 90 | + expect(user.emailVerified).to.be.equal(serverAuthUser.emailVerified); |
| 91 | + expect(user.providerData.length).to.eq( |
| 92 | + serverAuthUser.providerData.length |
| 93 | + ); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + await new Promise(resolve => { |
| 98 | + setTimeout(resolve, signInWaitDuration); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(numberServerLogins).to.equal(1); |
| 102 | + }); |
| 103 | + |
| 104 | + it('getToken operations fullfilled or rejected', async () => { |
| 105 | + if (isBrowser()) { |
| 106 | + return; |
| 107 | + } |
| 108 | + const userCred = await signInAnonymously(auth); |
| 109 | + expect(auth.currentUser).to.eq(userCred.user); |
| 110 | + expect(userCred.operationType).to.eq(OperationType.SIGN_IN); |
| 111 | + const user = userCred.user; |
| 112 | + expect(user).to.equal(auth.currentUser); |
| 113 | + expect(user.isAnonymous).to.be.true; |
| 114 | + expect(user.uid).to.be.a('string'); |
| 115 | + |
| 116 | + const authIdToken = await user.getIdToken(); |
| 117 | + const firebaseServerAppSettings = { authIdToken }; |
| 118 | + |
| 119 | + const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings); |
| 120 | + serverAppAuth = getAuth(serverApp); |
| 121 | + let numberServerLogins = 0; |
| 122 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 123 | + if (serverAuthUser) { |
| 124 | + numberServerLogins++; |
| 125 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 126 | + expect(serverAppAuth).to.not.be.null; |
| 127 | + expect(serverAuthUser.getIdToken); |
| 128 | + if (serverAppAuth) { |
| 129 | + expect(serverAppAuth.currentUser).to.equal(serverAuthUser); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + await new Promise(resolve => { |
| 135 | + setTimeout(resolve, signInWaitDuration); |
| 136 | + }); |
| 137 | + |
| 138 | + expect(numberServerLogins).to.equal(1); |
| 139 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 140 | + if (serverAppAuth.currentUser) { |
| 141 | + const idToken = await serverAppAuth.currentUser.getIdToken( |
| 142 | + /*forceRefresh=*/ false |
| 143 | + ); |
| 144 | + expect(idToken).to.not.be.null; |
| 145 | + await expect(serverAppAuth.currentUser.getIdToken(/*forceRefresh=*/ true)) |
| 146 | + .to.be.rejected; |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it('signs in with email crednetial user', async () => { |
| 151 | + if (isBrowser()) { |
| 152 | + return; |
| 153 | + } |
| 154 | + const email = randomEmail(); |
| 155 | + const password = 'password'; |
| 156 | + const userCred = await createUserWithEmailAndPassword( |
| 157 | + auth, |
| 158 | + email, |
| 159 | + password |
| 160 | + ); |
| 161 | + const user = userCred.user; |
| 162 | + expect(auth.currentUser).to.eq(userCred.user); |
| 163 | + expect(userCred.operationType).to.eq(OperationType.SIGN_IN); |
| 164 | + |
| 165 | + const additionalUserInfo = getAdditionalUserInfo(userCred)!; |
| 166 | + expect(additionalUserInfo.isNewUser).to.be.true; |
| 167 | + expect(additionalUserInfo.providerId).to.eq('password'); |
| 168 | + expect(user.isAnonymous).to.be.false; |
| 169 | + expect(user.email).to.equal(email); |
| 170 | + |
| 171 | + const authIdToken = await user.getIdToken(); |
| 172 | + const firebaseServerAppSettings = { authIdToken }; |
| 173 | + |
| 174 | + const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings); |
| 175 | + serverAppAuth = getAuth(serverApp); |
| 176 | + let numberServerLogins = 0; |
| 177 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 178 | + if (serverAuthUser) { |
| 179 | + numberServerLogins++; |
| 180 | + expect(serverAppAuth).to.not.be.null; |
| 181 | + if (serverAppAuth) { |
| 182 | + expect(serverAppAuth.currentUser).to.equal(serverAuthUser); |
| 183 | + } |
| 184 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 185 | + expect(serverAuthUser.refreshToken).to.be.empty; |
| 186 | + expect(user.isAnonymous).to.be.equal(serverAuthUser.isAnonymous); |
| 187 | + expect(user.emailVerified).to.be.equal(serverAuthUser.emailVerified); |
| 188 | + expect(user.providerData.length).to.eq( |
| 189 | + serverAuthUser.providerData.length |
| 190 | + ); |
| 191 | + expect(user.email).to.equal(serverAuthUser.email); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + await new Promise(resolve => { |
| 196 | + setTimeout(resolve, signInWaitDuration); |
| 197 | + }); |
| 198 | + |
| 199 | + expect(numberServerLogins).to.equal(1); |
| 200 | + }); |
| 201 | + |
| 202 | + it('can reload user', async () => { |
| 203 | + if (isBrowser()) { |
| 204 | + return; |
| 205 | + } |
| 206 | + const userCred = await signInAnonymously(auth); |
| 207 | + expect(auth.currentUser).to.eq(userCred.user); |
| 208 | + |
| 209 | + const user = userCred.user; |
| 210 | + expect(user).to.equal(auth.currentUser); |
| 211 | + expect(user.uid).to.be.a('string'); |
| 212 | + |
| 213 | + const authIdToken = await user.getIdToken(); |
| 214 | + const firebaseServerAppSettings = { authIdToken }; |
| 215 | + |
| 216 | + const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings); |
| 217 | + serverAppAuth = getAuth(serverApp); |
| 218 | + let numberServerLogins = 0; |
| 219 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 220 | + if (serverAuthUser) { |
| 221 | + numberServerLogins++; |
| 222 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 223 | + expect(serverAppAuth).to.not.be.null; |
| 224 | + if (serverAppAuth) { |
| 225 | + expect(serverAppAuth.currentUser).to.equal(serverAuthUser); |
| 226 | + } |
| 227 | + } |
| 228 | + }); |
| 229 | + |
| 230 | + await new Promise(resolve => { |
| 231 | + setTimeout(resolve, signInWaitDuration); |
| 232 | + }); |
| 233 | + |
| 234 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 235 | + if (serverAppAuth.currentUser) { |
| 236 | + await serverAppAuth.currentUser.reload(); |
| 237 | + } |
| 238 | + expect(numberServerLogins).to.equal(1); |
| 239 | + }); |
| 240 | + |
| 241 | + it('can update server based user profile', async () => { |
| 242 | + if (isBrowser()) { |
| 243 | + return; |
| 244 | + } |
| 245 | + const userCred = await signInAnonymously(auth); |
| 246 | + expect(auth.currentUser).to.eq(userCred.user); |
| 247 | + |
| 248 | + const user = userCred.user; |
| 249 | + expect(user).to.equal(auth.currentUser); |
| 250 | + expect(user.uid).to.be.a('string'); |
| 251 | + expect(user.displayName).to.be.null; |
| 252 | + |
| 253 | + const authIdToken = await user.getIdToken(); |
| 254 | + const firebaseServerAppSettings = { authIdToken }; |
| 255 | + |
| 256 | + const serverApp = initializeServerApp(auth.app, firebaseServerAppSettings); |
| 257 | + serverAppAuth = getAuth(serverApp); |
| 258 | + let numberServerLogins = 0; |
| 259 | + const newDisplayName = 'newName'; |
| 260 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 261 | + if (serverAuthUser) { |
| 262 | + numberServerLogins++; |
| 263 | + expect(serverAppAuth).to.not.be.null; |
| 264 | + if (serverAppAuth) { |
| 265 | + expect(serverAppAuth.currentUser).to.equal(serverAuthUser); |
| 266 | + } |
| 267 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 268 | + expect(user.displayName).to.be.null; |
| 269 | + void updateProfile(serverAuthUser, { |
| 270 | + displayName: newDisplayName |
| 271 | + }); |
| 272 | + } |
| 273 | + }); |
| 274 | + |
| 275 | + await new Promise(resolve => { |
| 276 | + setTimeout(resolve, signInWaitDuration); |
| 277 | + }); |
| 278 | + |
| 279 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 280 | + |
| 281 | + if (serverAppAuth.currentUser) { |
| 282 | + await serverAppAuth.currentUser.reload(); |
| 283 | + } |
| 284 | + |
| 285 | + expect(numberServerLogins).to.equal(1); |
| 286 | + expect(serverAppAuth).to.not.be.null; |
| 287 | + if (serverAppAuth) { |
| 288 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 289 | + expect(serverAppAuth.currentUser?.displayName).to.not.be.null; |
| 290 | + expect(serverAppAuth.currentUser?.displayName).to.equal(newDisplayName); |
| 291 | + } |
| 292 | + }); |
| 293 | + |
| 294 | + it('can sign out of main auth and still use server auth', async () => { |
| 295 | + if (isBrowser()) { |
| 296 | + return; |
| 297 | + } |
| 298 | + const userCred = await signInAnonymously(auth); |
| 299 | + expect(auth.currentUser).to.eq(userCred.user); |
| 300 | + |
| 301 | + const user = userCred.user; |
| 302 | + expect(user).to.equal(auth.currentUser); |
| 303 | + expect(user.uid).to.be.a('string'); |
| 304 | + expect(user.displayName).to.be.null; |
| 305 | + |
| 306 | + const authIdToken = await user.getIdToken(); |
| 307 | + const firebaseServerAppSettings = { authIdToken }; |
| 308 | + |
| 309 | + const serverApp = initializeServerApp( |
| 310 | + auth.app.options, |
| 311 | + firebaseServerAppSettings |
| 312 | + ); |
| 313 | + serverAppAuth = getAuth(serverApp); |
| 314 | + let numberServerLogins = 0; |
| 315 | + onAuthStateChanged(serverAppAuth, serverAuthUser => { |
| 316 | + if (serverAuthUser) { |
| 317 | + numberServerLogins++; |
| 318 | + expect(serverAppAuth).to.not.be.null; |
| 319 | + expect(user.uid).to.be.equal(serverAuthUser.uid); |
| 320 | + expect(user.displayName).to.be.null; |
| 321 | + if (serverAppAuth) { |
| 322 | + expect(serverAppAuth.currentUser).to.equal(serverAuthUser); |
| 323 | + } |
| 324 | + } |
| 325 | + }); |
| 326 | + |
| 327 | + await signOut(auth); |
| 328 | + await new Promise(resolve => { |
| 329 | + setTimeout(resolve, signInWaitDuration); |
| 330 | + }); |
| 331 | + |
| 332 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 333 | + |
| 334 | + if (serverAppAuth.currentUser) { |
| 335 | + await serverAppAuth.currentUser.reload(); |
| 336 | + } |
| 337 | + |
| 338 | + expect(numberServerLogins).to.equal(1); |
| 339 | + expect(serverAppAuth).to.not.be.null; |
| 340 | + if (serverAppAuth) { |
| 341 | + expect(serverAppAuth.currentUser).to.not.be.null; |
| 342 | + } |
| 343 | + }); |
| 344 | +}); |
0 commit comments