-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pavlo
committed
Jan 23, 2024
1 parent
7760b87
commit bc8360f
Showing
16 changed files
with
525 additions
and
432 deletions.
There are no files selected for viewing
111 changes: 74 additions & 37 deletions
111
FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAppCheckTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Reflection.Metadata.Ecma335; | ||
using System.Threading.Tasks; | ||
using FirebaseAdmin; | ||
using FirebaseAdmin.Auth; | ||
using FirebaseAdmin.Auth.Jwt; | ||
using FirebaseAdmin.Auth.Tests; | ||
using FirebaseAdmin.Check; | ||
using Google.Apis.Auth.OAuth2; | ||
using Moq; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace FirebaseAdmin.Tests | ||
{ | ||
public class FirebaseAppCheckTests : IDisposable | ||
{ | ||
[Fact] | ||
public async Task CreateTokenFromAppId() | ||
private readonly string appId = "1:1234:android:1234"; | ||
private FirebaseApp mockCredentialApp; | ||
|
||
public FirebaseAppCheckTests() | ||
{ | ||
string filePath = @"C:\path\to\your\file.txt"; | ||
string fileContent = File.ReadAllText(filePath); | ||
string[] appIds = fileContent.Split(','); | ||
foreach (string appId in appIds) | ||
var credential = GoogleCredential.FromFile("./resources/service_account.json"); | ||
var options = new AppOptions() | ||
{ | ||
var token = await FirebaseAppCheck.CreateToken(appId); | ||
Assert.IsType<string>(token.Token); | ||
Assert.NotNull(token.Token); | ||
Assert.IsType<int>(token.TtlMillis); | ||
Assert.Equal<int>(3600000, token.TtlMillis); | ||
} | ||
Credential = credential, | ||
}; | ||
this.mockCredentialApp = FirebaseApp.Create(options); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateTokenFromAppIdAndTtlMillis() | ||
public void CreateAppCheck() | ||
{ | ||
FirebaseAppCheck withoutAppIdCreate = FirebaseAppCheck.Create(this.mockCredentialApp); | ||
Assert.NotNull(withoutAppIdCreate); | ||
} | ||
|
||
[Fact] | ||
public async Task InvalidAppIdCreateToken() | ||
{ | ||
FirebaseAppCheck invalidAppIdCreate = FirebaseAppCheck.Create(this.mockCredentialApp); | ||
|
||
await Assert.ThrowsAsync<ArgumentException>(() => invalidAppIdCreate.CreateToken(appId: null)); | ||
await Assert.ThrowsAsync<ArgumentException>(() => invalidAppIdCreate.CreateToken(appId: string.Empty)); | ||
} | ||
|
||
[Fact] | ||
public void WithoutProjectIDCreate() | ||
{ | ||
string filePath = @"C:\path\to\your\file.txt"; | ||
string fileContent = File.ReadAllText(filePath); | ||
string[] appIds = fileContent.Split(','); | ||
foreach (string appId in appIds) | ||
// Project ID not set in the environment. | ||
Environment.SetEnvironmentVariable("GOOGLE_CLOUD_PROJECT", null); | ||
Environment.SetEnvironmentVariable("GCLOUD_PROJECT", null); | ||
|
||
var options = new AppOptions() | ||
{ | ||
AppCheckTokenOptions options = new (1800000); | ||
var token = await FirebaseAppCheck.CreateToken(appId, options); | ||
Assert.IsType<string>(token.Token); | ||
Assert.NotNull(token.Token); | ||
Assert.IsType<int>(token.TtlMillis); | ||
Assert.Equal<int>(1800000, token.TtlMillis); | ||
} | ||
Credential = GoogleCredential.FromAccessToken("token"), | ||
}; | ||
var app = FirebaseApp.Create(options, "1234"); | ||
|
||
Assert.Throws<ArgumentException>(() => FirebaseAppCheck.Create(app)); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateTokenFromAppId() | ||
{ | ||
FirebaseAppCheck createTokenFromAppId = new FirebaseAppCheck(this.mockCredentialApp); | ||
var token = await createTokenFromAppId.CreateToken(this.appId); | ||
Assert.IsType<string>(token.Token); | ||
Assert.NotNull(token.Token); | ||
Assert.IsType<int>(token.TtlMillis); | ||
Assert.Equal<int>(3600000, token.TtlMillis); | ||
} | ||
|
||
[Fact] | ||
public async Task InvalidAppIdCreate() | ||
public async Task CreateTokenFromAppIdAndTtlMillis() | ||
{ | ||
await Assert.ThrowsAsync<ArgumentException>(() => FirebaseAppCheck.CreateToken(appId: null)); | ||
await Assert.ThrowsAsync<ArgumentException>(() => FirebaseAppCheck.CreateToken(appId: string.Empty)); | ||
AppCheckTokenOptions options = new (1800000); | ||
FirebaseAppCheck createTokenFromAppIdAndTtlMillis = new FirebaseAppCheck(this.mockCredentialApp); | ||
|
||
var token = await createTokenFromAppIdAndTtlMillis.CreateToken(this.appId, options); | ||
Assert.IsType<string>(token.Token); | ||
Assert.NotNull(token.Token); | ||
Assert.IsType<int>(token.TtlMillis); | ||
Assert.Equal<int>(1800000, token.TtlMillis); | ||
} | ||
|
||
[Fact] | ||
public async Task DecodeVerifyToken() | ||
public async Task VerifyToken() | ||
{ | ||
string appId = "1234"; // '../resources/appid.txt' | ||
AppCheckToken validToken = await FirebaseAppCheck.CreateToken(appId); | ||
var verifiedToken = FirebaseAppCheck.Decode_and_verify(validToken.Token); | ||
/* Assert.Equal("explicit-project", verifiedToken);*/ | ||
FirebaseAppCheck verifyToken = new FirebaseAppCheck(this.mockCredentialApp); | ||
|
||
AppCheckToken validToken = await verifyToken.CreateToken(this.appId); | ||
AppCheckVerifyResponse verifiedToken = await verifyToken.VerifyToken(validToken.Token, null); | ||
Assert.Equal("explicit-project", verifiedToken.AppId); | ||
} | ||
|
||
[Fact] | ||
public async Task DecodeVerifyTokenInvaild() | ||
public async Task VerifyTokenInvaild() | ||
{ | ||
await Assert.ThrowsAsync<ArgumentException>(() => FirebaseAppCheck.Decode_and_verify(token: null)); | ||
await Assert.ThrowsAsync<ArgumentException>(() => FirebaseAppCheck.Decode_and_verify(token: string.Empty)); | ||
FirebaseAppCheck verifyTokenInvaild = new FirebaseAppCheck(this.mockCredentialApp); | ||
|
||
await Assert.ThrowsAsync<ArgumentException>(() => verifyTokenInvaild.VerifyToken(null)); | ||
await Assert.ThrowsAsync<ArgumentException>(() => verifyTokenInvaild.VerifyToken(string.Empty)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
FirebaseAppCheck.Delete(); | ||
FirebaseAppCheck.DeleteAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.