|
| 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 | +using FirebaseAdmin.Tests; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using System.Net; |
| 22 | + |
| 23 | +namespace FirebaseAdmin.Auth.Tests |
| 24 | +{ |
| 25 | + public class FirebaseUserManagerTest |
| 26 | + { |
| 27 | + private static readonly GoogleCredential mockCredential = |
| 28 | + GoogleCredential.FromAccessToken("test-token"); |
| 29 | + private const string mockProjectId = "project1"; |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void InvalidUidForUserRecord() |
| 33 | + { |
| 34 | + Assert.Throws<ArgumentException>(() => new UserRecord(null)); |
| 35 | + Assert.Throws<ArgumentException>(() => new UserRecord("")); |
| 36 | + Assert.Throws<ArgumentException>(() => new UserRecord(new string('a', 129))); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void ReservedClaims() |
| 41 | + { |
| 42 | + foreach (var key in FirebaseTokenFactory.ReservedClaims) |
| 43 | + { |
| 44 | + var customClaims = new Dictionary<string, object>(){ |
| 45 | + {key, "value"}, |
| 46 | + }; |
| 47 | + Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = customClaims}); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void EmptyClaims() |
| 53 | + { |
| 54 | + var emptyClaims = new Dictionary<string, object>(){ |
| 55 | + {"", "value"}, |
| 56 | + }; |
| 57 | + Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = emptyClaims }); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void TooLargeClaimsPayload() |
| 62 | + { |
| 63 | + var customClaims = new Dictionary<string, object>() |
| 64 | + { |
| 65 | + { "testClaim", new string('a', 1001) }, |
| 66 | + }; |
| 67 | + |
| 68 | + Assert.Throws<ArgumentException>(() => new UserRecord("user1") { CustomClaims = customClaims }); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task UpdateUser() |
| 73 | + { |
| 74 | + var handler = new MockMessageHandler() |
| 75 | + { |
| 76 | + Response = new UserRecord("user1") |
| 77 | + }; |
| 78 | + var factory = new MockHttpClientFactory(handler); |
| 79 | + var userManager = new FirebaseUserManager( |
| 80 | + new FirebaseUserManagerArgs |
| 81 | + { |
| 82 | + Credential = mockCredential, |
| 83 | + ProjectId = mockProjectId, |
| 84 | + ClientFactory = factory |
| 85 | + }); |
| 86 | + var customClaims = new Dictionary<string, object>(){ |
| 87 | + {"admin", true}, |
| 88 | + }; |
| 89 | + |
| 90 | + await userManager.UpdateUserAsync(new UserRecord("user1") { CustomClaims = customClaims }); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public async Task UpdateUserIncorrectResponseObject() |
| 95 | + { |
| 96 | + var handler = new MockMessageHandler() |
| 97 | + { |
| 98 | + Response = new object() |
| 99 | + }; |
| 100 | + var factory = new MockHttpClientFactory(handler); |
| 101 | + var userManager = new FirebaseUserManager( |
| 102 | + new FirebaseUserManagerArgs |
| 103 | + { |
| 104 | + Credential = mockCredential, |
| 105 | + ProjectId = mockProjectId, |
| 106 | + ClientFactory = factory |
| 107 | + }); |
| 108 | + var customClaims = new Dictionary<string, object>(){ |
| 109 | + {"admin", true}, |
| 110 | + }; |
| 111 | + |
| 112 | + await Assert.ThrowsAsync<FirebaseException>( |
| 113 | + async () => await userManager.UpdateUserAsync(new UserRecord("user1") { CustomClaims = customClaims })); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public async Task UpdateUserIncorrectResponseUid() |
| 118 | + { |
| 119 | + var handler = new MockMessageHandler() |
| 120 | + { |
| 121 | + Response = new UserRecord("testuser") |
| 122 | + }; |
| 123 | + var factory = new MockHttpClientFactory(handler); |
| 124 | + var userManager = new FirebaseUserManager( |
| 125 | + new FirebaseUserManagerArgs |
| 126 | + { |
| 127 | + Credential = mockCredential, |
| 128 | + ProjectId = mockProjectId, |
| 129 | + ClientFactory = factory |
| 130 | + }); |
| 131 | + var customClaims = new Dictionary<string, object>(){ |
| 132 | + {"admin", true}, |
| 133 | + }; |
| 134 | + |
| 135 | + await Assert.ThrowsAsync<FirebaseException>( |
| 136 | + async () => await userManager.UpdateUserAsync(new UserRecord("user1") { CustomClaims = customClaims })); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public async Task UpdateUserHttpError() |
| 141 | + { |
| 142 | + var handler = new MockMessageHandler() |
| 143 | + { |
| 144 | + StatusCode = HttpStatusCode.InternalServerError |
| 145 | + }; |
| 146 | + var factory = new MockHttpClientFactory(handler); |
| 147 | + var userManager = new FirebaseUserManager( |
| 148 | + new FirebaseUserManagerArgs |
| 149 | + { |
| 150 | + Credential = mockCredential, |
| 151 | + ProjectId = mockProjectId, |
| 152 | + ClientFactory = factory |
| 153 | + }); |
| 154 | + var customClaims = new Dictionary<string, object>(){ |
| 155 | + {"admin", true}, |
| 156 | + }; |
| 157 | + |
| 158 | + await Assert.ThrowsAsync<FirebaseException>( |
| 159 | + async () => await userManager.UpdateUserAsync(new UserRecord("user1") { CustomClaims = customClaims })); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments