Skip to content
Merged
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
14 changes: 10 additions & 4 deletions src/Auth0.ManagementApi/Clients/JobsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Task<JobError> GetErrorDetailsAsync(string id, CancellationToken cancella
Connection.GetAsync<string>(
BuildUri($"jobs/{EncodePath(id)}/errors"), DefaultHeaders, cancellationToken: cancellationToken).Result;

if (string.IsNullOrEmpty(rawResponse)) return null;
if (string.IsNullOrEmpty(rawResponse)) return Task.FromResult<JobError>(null!);;

try
{
Expand All @@ -128,10 +128,16 @@ public Task<JobError> GetErrorDetailsAsync(string id, CancellationToken cancella
JobImportErrorDetails = jobImportErrorDetails
});
}

// Empty array means no errors
if (jobImportErrorDetails != null)
{
return Task.FromResult<JobError>(null!);
}
}
catch (JsonSerializationException ex)
catch (JsonSerializationException)
{
// ignoring the exception to try to serialize to JobErrorDetails.
// ignoring the exception to try to deserialize to JobErrorDetails.
}

var jobErrorDetails = JsonConvert.DeserializeObject<JobErrorDetails>(rawResponse);
Expand All @@ -143,6 +149,6 @@ public Task<JobError> GetErrorDetailsAsync(string id, CancellationToken cancella
});
}

return null;
return Task.FromResult<JobError>(null!);
}
}
99 changes: 99 additions & 0 deletions tests/Auth0.Core.UnitTests/JobsClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Auth0.ManagementApi.Clients;
using Auth0.ManagementApi.Models;
using FluentAssertions;
using Moq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi;
using Newtonsoft.Json;
using Xunit;

namespace Auth0.Core.UnitTests;

public class JobsClientTests
{
private static JobsClient CreateClient(string responseJson)
{
var mockConnection = new Mock<IManagementConnection>();
mockConnection
.Setup(c => c.GetAsync<string>(
It.IsAny<Uri>(),
It.IsAny<IDictionary<string, string>>(),
It.IsAny<JsonConverter[]>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(responseJson);

return new JobsClient(
mockConnection.Object,
new Uri("https://test.auth0.com"),
new Dictionary<string, string>());
}

[Fact]
public async Task GetErrorDetailsAsync_Returns_Null_When_Response_Is_Empty_Array()
{
var client = CreateClient("[]");

var result = await client.GetErrorDetailsAsync("job_123");

result.Should().BeNull();
}

[Fact]
public async Task GetErrorDetailsAsync_Returns_Null_When_Response_Is_Null_Or_Empty()
{
var client = CreateClient(null);

var result = await client.GetErrorDetailsAsync("job_123");

result.Should().BeNull();
}

[Fact]
public async Task GetErrorDetailsAsync_Returns_JobImportErrorDetails_When_Response_Is_Array_With_Errors()
{
var json = """
[
{
"user": { "email": "john@example.com" },
"errors": [
{ "code": "INVALID_FORMAT", "message": "Invalid email", "path": "email" }
]
}
]
""";
var client = CreateClient(json);

var result = await client.GetErrorDetailsAsync("job_123");

result.Should().NotBeNull();
result!.JobImportErrorDetails.Should().NotBeNull();
result.JobImportErrorDetails!.Length.Should().Be(1);
result.JobImportErrorDetails[0].Errors![0].Code.Should().Be("INVALID_FORMAT");
result.JobErrorDetails.Should().BeNull();
}

[Fact]
public async Task GetErrorDetailsAsync_Returns_JobErrorDetails_When_Response_Is_Object()
{
var json = """
{
"status": "failed",
"type": "users_import",
"id": "job_abc",
"status_details": "Failed to parse users file JSON when importing users."
}
""";
var client = CreateClient(json);

var result = await client.GetErrorDetailsAsync("job_123");

result.Should().NotBeNull();
result!.JobErrorDetails.Should().NotBeNull();
result.JobErrorDetails!.StatusDetails.Should().Be("Failed to parse users file JSON when importing users.");
result.JobImportErrorDetails.Should().BeNull();
}
}
Loading