|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2022 Google LLC | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package com.google.firebase.appcheck; | 
|  | 18 | + | 
|  | 19 | +import static org.junit.Assert.assertEquals; | 
|  | 20 | +import static org.junit.Assert.assertNotNull; | 
|  | 21 | +import static org.junit.Assert.assertSame; | 
|  | 22 | +import static org.junit.Assert.assertTrue; | 
|  | 23 | +import static org.junit.Assert.fail; | 
|  | 24 | + | 
|  | 25 | +import com.google.firebase.FirebaseApp; | 
|  | 26 | +import com.google.firebase.FirebaseOptions; | 
|  | 27 | +import com.google.firebase.TestOnlyImplFirebaseTrampolines; | 
|  | 28 | +import com.google.firebase.auth.MockGoogleCredentials; | 
|  | 29 | +import org.junit.After; | 
|  | 30 | +import org.junit.Test; | 
|  | 31 | + | 
|  | 32 | +public class FirebaseAppCheckTest { | 
|  | 33 | + | 
|  | 34 | +  private static final FirebaseOptions TEST_OPTIONS = FirebaseOptions.builder() | 
|  | 35 | +          .setCredentials(new MockGoogleCredentials("test-token")) | 
|  | 36 | +          .setProjectId("test-project") | 
|  | 37 | +          .build(); | 
|  | 38 | + | 
|  | 39 | +  @After | 
|  | 40 | +  public void tearDown() { | 
|  | 41 | +    TestOnlyImplFirebaseTrampolines.clearInstancesForTest(); | 
|  | 42 | +  } | 
|  | 43 | + | 
|  | 44 | +  @Test | 
|  | 45 | +  public void testGetInstance() { | 
|  | 46 | +    FirebaseApp.initializeApp(TEST_OPTIONS); | 
|  | 47 | + | 
|  | 48 | +    FirebaseAppCheck appCheck = FirebaseAppCheck.getInstance(); | 
|  | 49 | + | 
|  | 50 | +    assertSame(appCheck, FirebaseAppCheck.getInstance()); | 
|  | 51 | +  } | 
|  | 52 | + | 
|  | 53 | +  @Test | 
|  | 54 | +  public void testGetInstanceByApp() { | 
|  | 55 | +    FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | 
|  | 56 | + | 
|  | 57 | +    FirebaseAppCheck appCheck = FirebaseAppCheck.getInstance(app); | 
|  | 58 | + | 
|  | 59 | +    assertSame(appCheck, FirebaseAppCheck.getInstance(app)); | 
|  | 60 | +  } | 
|  | 61 | + | 
|  | 62 | +  @Test | 
|  | 63 | +  public void testDefaultAppCheckClient() { | 
|  | 64 | +    FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | 
|  | 65 | +    FirebaseAppCheck appCheck = FirebaseAppCheck.getInstance(app); | 
|  | 66 | + | 
|  | 67 | +    FirebaseAppCheckClient client = appCheck.getAppCheckClient(); | 
|  | 68 | + | 
|  | 69 | +    assertTrue(client instanceof FirebaseAppCheckClientImpl); | 
|  | 70 | +    assertSame(client, appCheck.getAppCheckClient()); | 
|  | 71 | +  } | 
|  | 72 | + | 
|  | 73 | +  @Test | 
|  | 74 | +  public void testAppDelete() { | 
|  | 75 | +    FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | 
|  | 76 | +    FirebaseAppCheck appCheck = FirebaseAppCheck.getInstance(app); | 
|  | 77 | +    assertNotNull(appCheck); | 
|  | 78 | + | 
|  | 79 | +    app.delete(); | 
|  | 80 | + | 
|  | 81 | +    try { | 
|  | 82 | +      FirebaseAppCheck.getInstance(app); | 
|  | 83 | +      fail("No error thrown when getting app check instance after deleting app"); | 
|  | 84 | +    } catch (IllegalStateException expected) { | 
|  | 85 | +      // expected | 
|  | 86 | +    } | 
|  | 87 | +  } | 
|  | 88 | + | 
|  | 89 | +  @Test | 
|  | 90 | +  public void testAppCheckClientWithoutProjectId() { | 
|  | 91 | +    FirebaseOptions options = FirebaseOptions.builder() | 
|  | 92 | +            .setCredentials(new MockGoogleCredentials("test-token")) | 
|  | 93 | +            .build(); | 
|  | 94 | +    FirebaseApp.initializeApp(options); | 
|  | 95 | + | 
|  | 96 | +    try { | 
|  | 97 | +      FirebaseAppCheck.getInstance(); | 
|  | 98 | +      fail("No error thrown for missing project ID"); | 
|  | 99 | +    } catch (IllegalArgumentException expected) { | 
|  | 100 | +      String message = "Project ID is required to access App Check service. Use a service " | 
|  | 101 | +              + "account credential or set the project ID explicitly via FirebaseOptions. " | 
|  | 102 | +              + "Alternatively you can also set the project ID via the GOOGLE_CLOUD_PROJECT " | 
|  | 103 | +              + "environment variable."; | 
|  | 104 | +      assertEquals(message, expected.getMessage()); | 
|  | 105 | +    } | 
|  | 106 | +  } | 
|  | 107 | +} | 
0 commit comments