Skip to content

Commit abd33c3

Browse files
author
Dominik Földi
committed
Add unit tests for UserManager and SetCustomUserClaims
1 parent 52cf9c0 commit abd33c3

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseAuthTest.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
using System.Threading;
2222
using System.Threading.Tasks;
2323
using Xunit;
24-
using FirebaseAdmin.Auth;
25-
using Google.Apis.Auth;
2624
using Google.Apis.Auth.OAuth2;
2725

2826
[assembly: CollectionBehavior(DisableTestParallelization = true)]
@@ -173,6 +171,30 @@ private static void VerifyCustomToken(string token, string uid, Dictionary<strin
173171
Assert.True(verified);
174172
}
175173

174+
[Fact]
175+
public async Task SetCustomUserClaimsInvalidCredential()
176+
{
177+
FirebaseApp.Create(new AppOptions() { Credential = mockCredential, ProjectId = "project1" });
178+
var customClaims = new Dictionary<string, object>()
179+
{
180+
{"admin", true}
181+
};
182+
await Assert.ThrowsAsync<FirebaseException>(
183+
async () => await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync("user1", customClaims));
184+
}
185+
186+
[Fact]
187+
public async Task SetCustomUserClaimsNoProjectId()
188+
{
189+
FirebaseApp.Create(new AppOptions() { Credential = mockCredential });
190+
var customClaims = new Dictionary<string, object>()
191+
{
192+
{"admin", true}
193+
};
194+
await Assert.ThrowsAsync<ArgumentException>(
195+
async () => await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync("user1", customClaims));
196+
}
197+
176198
public void Dispose()
177199
{
178200
FirebaseApp.DeleteAll();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2019, 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 Xunit;
18+
using Google.Apis.Auth.OAuth2;
19+
20+
namespace FirebaseAdmin.Auth.Tests
21+
{
22+
public class FirebaseUserManagerTest
23+
{
24+
private static readonly GoogleCredential mockCredential =
25+
GoogleCredential.FromAccessToken("test-token");
26+
private const string mockProjectId = "project1";
27+
28+
[Fact]
29+
public void InvalidUid()
30+
{
31+
var app = FirebaseApp.Create(new AppOptions() { Credential = mockCredential, ProjectId = mockProjectId });
32+
var userManager = FirebaseUserManager.Create(app);
33+
var customClaims = new Dictionary<string, object>()
34+
{
35+
{"admin", true}
36+
};
37+
38+
Assert.Throws<ArgumentException>(() => new UserRecord(null));
39+
Assert.Throws<ArgumentException>(() => new UserRecord(""));
40+
Assert.Throws<ArgumentException>(() => new UserRecord(new string('a', 129)));
41+
}
42+
43+
[Fact]
44+
public void ReservedClaims()
45+
{
46+
foreach (var key in FirebaseTokenFactory.ReservedClaims)
47+
{
48+
var customClaims = new Dictionary<string, object>(){
49+
{key, "value"},
50+
};
51+
Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = customClaims});
52+
}
53+
}
54+
55+
[Fact]
56+
public void EmptyClaims()
57+
{
58+
var emptyClaims = new Dictionary<string, object>(){
59+
{"", "value"},
60+
};
61+
Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = emptyClaims });
62+
}
63+
64+
[Fact]
65+
public void TooLargeClaimsPayload()
66+
{
67+
var customClaims = new Dictionary<string, object>();
68+
69+
for(var i = 0; i < 100; ++i)
70+
{
71+
customClaims.Add($"claim{i}", $"value{i}");
72+
}
73+
74+
Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = customClaims });
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)