|
| 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