Skip to content

Commit

Permalink
Add generic get/set metadata methods on UserBase (#646)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Prüfer <[email protected]>
Co-authored-by: frederikprijck <[email protected]>
  • Loading branch information
3 people authored Aug 23, 2023
1 parent 4c2b21e commit 369ab1f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 5 deletions.
54 changes: 53 additions & 1 deletion src/Auth0.ManagementApi/Models/UserBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace Auth0.ManagementApi.Models
{
Expand Down Expand Up @@ -58,7 +60,7 @@ public abstract class UserBase
[JsonProperty("username")]
public string UserName { get; set; }

/// <summary>
/// <summary>
/// The Nickname of the user.
/// </summary>
[JsonProperty("nickname")]
Expand Down Expand Up @@ -102,5 +104,55 @@ public abstract class UserBase
/// </summary>
[JsonProperty("blocked")]
public bool? Blocked { get; set; }

/// <summary>
/// Returns <see cref="AppMetadata"/> as specific type.
/// </summary>
/// <typeparam name="T">Type to be returned.</typeparam>
/// <returns>An instance of T.</returns>
public T GetAppMetadata<T>() where T : class
{
if (AppMetadata is JObject jObject)
{
return jObject.ToObject<T>();
}

return null;
}

/// <summary>
/// Returns <see cref="UserMetadata"/> as specific type.
/// </summary>
/// <typeparam name="T">Type to be returned.</typeparam>
/// <returns>An instance of T.</returns>
public T GetUserMetadata<T>() where T : class
{
if (UserMetadata is JObject jObject)
{
return jObject.ToObject<T>();
}

return null;
}

/// <summary>
/// Set given appMetadata as <see cref="AppMetadata"/>.
/// </summary>
/// <typeparam name="T">Metadata type.</typeparam>
/// <param name="appMetadata">Metadata to set.</param>
public void SetAppMetadata<T>(T appMetadata) where T : class
{
AppMetadata = JObject.FromObject(appMetadata);
}

/// <summary>
/// Set given userMetadata as <see cref="UserMetadata"/>.
/// </summary>
/// <typeparam name="T">Metadata type.</typeparam>
/// <param name="userMetadata">Metadata to set.</param>
public void SetUserMetadata<T>(T userMetadata) where T : class
{
UserMetadata = JObject.FromObject(userMetadata);
}
}
}
87 changes: 83 additions & 4 deletions tests/Auth0.ManagementApi.IntegrationTests/UsersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override async Task DisposeAsync()
await ManagementTestBaseUtils.CleanupAsync(ApiClient, entry.Key, entry.Value);
}

ApiClient.Dispose();
ApiClient?.Dispose();
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ public async Task Test_when_paging_not_specified_does_not_include_totals()
{
// Act
var users = await fixture.ApiClient.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo());

// Assert
Assert.Null(users.Paging);
}
Expand All @@ -231,7 +231,7 @@ public async Task Test_paging_does_not_include_totals()
{
// Act
var users = await fixture.ApiClient.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo(0, 50, false));

// Assert
Assert.Null(users.Paging);
}
Expand All @@ -241,7 +241,7 @@ public async Task Test_paging_includes_totals()
{
// Act
var users = await fixture.ApiClient.Users.GetAllAsync(new GetUsersRequest(), new PaginationInfo(0, 50, true));

// Assert
Assert.NotNull(users.Paging);
}
Expand Down Expand Up @@ -306,6 +306,79 @@ public async Task Can_update_user_metadata()

}


[Fact]
public async Task Can_read_user_metadata_as_type()
{
// Add a new user with metadata
var customAppMetadata = new CustomMetadata
{
Amount = 42,
Name = "A name"
};
var customUserMetadat = new CustomMetadata
{
Amount = 43,
Name = "One off"
};
var newUserRequest = new UserCreateRequest
{
Connection = fixture.Connection.Name,
Email = $"{Guid.NewGuid():N}{TestingConstants.UserEmailDomain}",
EmailVerified = true,
Password = Password
};
newUserRequest.SetAppMetadata(customAppMetadata);
newUserRequest.SetUserMetadata(customUserMetadat);

var newUserResponse = await fixture.ApiClient.Users.CreateAsync(newUserRequest);
fixture.TrackIdentifier(CleanUpType.Users, newUserResponse.UserId);
// read metadata
var appMetadata = newUserResponse.GetAppMetadata<CustomMetadata>();
var userMetadata = newUserResponse.GetUserMetadata<CustomMetadata>();

Assert.NotNull(appMetadata);
Assert.Equal(appMetadata.Amount, customAppMetadata.Amount);
Assert.Equal(appMetadata.Name, customAppMetadata.Name);

Assert.NotNull(userMetadata);
Assert.Equal(userMetadata.Amount, customUserMetadat.Amount);
Assert.Equal(userMetadata.Name, customUserMetadat.Name);

// Do some updating
var updateUserRequest = new UserUpdateRequest();
customAppMetadata = new CustomMetadata
{
Amount = 1,
Name = "Another name"
};
customUserMetadat = new CustomMetadata
{
Amount = 44,
Name = "Two off"
};
updateUserRequest.SetAppMetadata(customAppMetadata);
updateUserRequest.SetUserMetadata(customUserMetadat);
await fixture.ApiClient.Users.UpdateAsync(newUserResponse.UserId, updateUserRequest);

// Get the user to ensure the metadata was set
var user = await fixture.ApiClient.Users.GetAsync(newUserResponse.UserId);
appMetadata = user.GetAppMetadata<CustomMetadata>();
userMetadata = user.GetUserMetadata<CustomMetadata>();

Assert.NotNull(appMetadata);
Assert.Equal(appMetadata.Amount, customAppMetadata.Amount);
Assert.Equal(appMetadata.Name, customAppMetadata.Name);

Assert.NotNull(userMetadata);
Assert.Equal(userMetadata.Amount, customUserMetadat.Amount);
Assert.Equal(userMetadata.Name, customUserMetadat.Name);

// Delete the user
await fixture.ApiClient.Users.DeleteAsync(user.UserId);
fixture.UnTrackIdentifier(CleanUpType.Users, newUserResponse.UserId);
}

[Fact]
public async Task Test_logs_deserialization_without_totals()
{
Expand Down Expand Up @@ -585,5 +658,11 @@ public async Task Test_authentication_methods_crud()
var allAuthenticationMethods3 = await fixture.ApiClient.Users.GetAuthenticationMethodsAsync(fixture.User.UserId);
allAuthenticationMethods3.Count.Should().Be(0);
}

private class CustomMetadata
{
public string Name { get; set; }
public int Amount { get; set; }
}
}
}

0 comments on commit 369ab1f

Please sign in to comment.