diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs index 84a37494..0ab884d9 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs @@ -1200,7 +1200,7 @@ public async Task UpdateUserRemoveAttributes(TestConfig config) [Theory] [MemberData(nameof(TestConfigs))] - public async Task UpdateUserRemoveProviders(TestConfig config) + public async Task UpdateUserRemovePhoneProviderAutomatically(TestConfig config) { var handler = new MockMessageHandler() { @@ -1227,6 +1227,44 @@ public async Task UpdateUserRemoveProviders(TestConfig config) request["deleteProvider"]); } + [Theory] + [MemberData(nameof(TestConfigs))] + public async Task UpdateUserRemoveProviders(TestConfig config) + { + const string user = @"{ + ""localId"": ""user1"", + ""providerUserInfo"": [{ + ""rawId"": ""googleuid"", + ""providerId"": ""google.com"" + }] + }"; + + var handler = new MockMessageHandler() + { + Response = new List() { CreateUserResponse, config.GetUserResponse(user) }, + }; + + var auth = config.CreateAuth(handler); + + var userRecord = await auth.UpdateUserAsync(new UserRecordArgs() + { + Uid = "user1", + ProvidersToDelete = new[] { "google.com" }, + }); + + Assert.Equal("user1", userRecord.Uid); + Assert.Equal(config.TenantId, userRecord.TenantId); + Assert.Equal(2, handler.Requests.Count); + config.AssertRequest("accounts:update", handler.Requests[0]); + config.AssertRequest("accounts:lookup", handler.Requests[1]); + var request = NewtonsoftJsonSerializer.Instance.Deserialize(handler.Requests[0].Body); + Assert.Equal(2, request.Count); + Assert.Equal("user1", request["localId"]); + Assert.Equal( + new JArray() { "google.com" }, + request["deleteProvider"]); + } + [Theory] [MemberData(nameof(TestConfigs))] public async Task UpdateUserSetCustomClaims(TestConfig config) diff --git a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs index 433deb1b..1b1fc9ec 100644 --- a/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs +++ b/FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using FirebaseAdmin.Auth.Jwt; @@ -89,6 +90,11 @@ public bool Disabled set => this.disabled = value; } + /// + /// Gets or sets the list of providers the user account should have deleted. + /// + public IEnumerable ProvidersToDelete { get; set; } + /// /// Gets or sets the password of the user. /// @@ -361,6 +367,14 @@ internal UpdateUserRequest(UserRecordArgs args) this.PhoneNumber = CheckPhoneNumber(phoneNumber); } } + + if (args.ProvidersToDelete != null) + { + foreach (var providerToDelete in args.ProvidersToDelete) + { + this.AddDeleteProvider(providerToDelete); + } + } } [JsonProperty("customAttributes")] @@ -411,12 +425,12 @@ private void AddDeleteAttribute(string attribute) private void AddDeleteProvider(string provider) { - if (this.DeleteProvider == null) + this.DeleteProvider ??= new List(); + + if (!this.DeleteProvider.Contains(provider)) { - this.DeleteProvider = new List(); + this.DeleteProvider.Add(provider); } - - this.DeleteProvider.Add(provider); } }