Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the DeleteProvider to the UpdateRecordArgs #327

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<string>() { 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<JObject>(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)
Expand Down
22 changes: 18 additions & 4 deletions FirebaseAdmin/FirebaseAdmin/Auth/UserRecordArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using FirebaseAdmin.Auth.Jwt;
Expand Down Expand Up @@ -89,6 +90,11 @@ public bool Disabled
set => this.disabled = value;
}

/// <summary>
/// Gets or sets the list of providers the user account should have deleted.
/// </summary>
public IEnumerable<string> ProvidersToDelete { get; set; }

/// <summary>
/// Gets or sets the password of the user.
/// </summary>
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -411,12 +425,12 @@ private void AddDeleteAttribute(string attribute)

private void AddDeleteProvider(string provider)
{
if (this.DeleteProvider == null)
this.DeleteProvider ??= new List<string>();

if (!this.DeleteProvider.Contains(provider))
{
this.DeleteProvider = new List<string>();
this.DeleteProvider.Add(provider);
}

this.DeleteProvider.Add(provider);
}
}

Expand Down