|
| 1 | +// Copyright 2018, Google Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.IO; |
| 18 | +using System.Security.Cryptography; |
| 19 | +using System.Security.Cryptography.X509Certificates; |
| 20 | +using System.Text; |
| 21 | +using System.Threading; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using Xunit; |
| 24 | +using FirebaseAdmin.Auth; |
| 25 | +using Google.Apis.Auth; |
| 26 | +using Google.Apis.Auth.OAuth2; |
| 27 | + |
| 28 | +[assembly: CollectionBehavior(DisableTestParallelization = true)] |
| 29 | +namespace FirebaseAdmin.Auth.Tests |
| 30 | +{ |
| 31 | + public class FirebaseAuthTest: IDisposable |
| 32 | + { |
| 33 | + private static readonly GoogleCredential mockCredential = |
| 34 | + GoogleCredential.FromAccessToken("test-token"); |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void GetAuthWithoutApp() |
| 38 | + { |
| 39 | + Assert.Null(FirebaseAuth.DefaultInstance); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void GetDefaultAuth() |
| 44 | + { |
| 45 | + var app = FirebaseApp.Create(new AppOptions(){Credential = mockCredential}); |
| 46 | + FirebaseAuth auth = FirebaseAuth.DefaultInstance; |
| 47 | + Assert.Same(auth, FirebaseAuth.DefaultInstance); |
| 48 | + app.Delete(); |
| 49 | + Assert.Null(FirebaseAuth.DefaultInstance); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void GetAuth() |
| 54 | + { |
| 55 | + var app = FirebaseApp.Create(new AppOptions(){Credential = mockCredential}, "MyApp"); |
| 56 | + FirebaseAuth auth = FirebaseAuth.GetAuth(app); |
| 57 | + Assert.Same(auth, FirebaseAuth.GetAuth(app)); |
| 58 | + app.Delete(); |
| 59 | + Assert.Throws<InvalidOperationException>(() => FirebaseAuth.GetAuth(app)); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task VerifyIdTokenNoProjectId() |
| 64 | + { |
| 65 | + FirebaseApp.Create(new AppOptions(){Credential = mockCredential}); |
| 66 | + var idToken = await FirebaseTokenVerifierTest.CreateTestTokenAsync(); |
| 67 | + await Assert.ThrowsAsync<ArgumentException>( |
| 68 | + async () => await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken)); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task VerifyIdTokenCancel() |
| 73 | + { |
| 74 | + FirebaseApp.Create(new AppOptions() |
| 75 | + { |
| 76 | + Credential = mockCredential, |
| 77 | + ProjectId = "test-project", |
| 78 | + }); |
| 79 | + var canceller = new CancellationTokenSource(); |
| 80 | + canceller.Cancel(); |
| 81 | + var idToken = await FirebaseTokenVerifierTest.CreateTestTokenAsync(); |
| 82 | + await Assert.ThrowsAsync<OperationCanceledException>( |
| 83 | + async () => await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync( |
| 84 | + idToken, canceller.Token)); |
| 85 | + } |
| 86 | + |
| 87 | + public void Dispose() |
| 88 | + { |
| 89 | + FirebaseApp.DeleteAll(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments