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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public abstract class AbstractFirebaseAuthTest<T>
HandleCodeInApp = false,
};

private static readonly ActionCodeSettings InvalidEmailLinkSettingsWithCustomDomain = new ActionCodeSettings()
{
Url = ContinueUrl,
HandleCodeInApp = true,
LinkDomain = "cool.link.domain",
};

private readonly AbstractAuthFixture<T> fixture;
private readonly TemporaryUserBuilder userBuilder;

Expand Down Expand Up @@ -658,6 +665,18 @@ public async Task SignInWithEmailLink()
Assert.True(user.EmailVerified);
}

[Fact]
public async Task AuthErrorCodeParse()
{
var user = await this.userBuilder.CreateRandomUserAsync();

var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
() => this.Auth.GeneratePasswordResetLinkAsync(
user.Email, InvalidEmailLinkSettingsWithCustomDomain));
Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode);
Assert.Equal(AuthErrorCode.InvalidHostingLinkDomain, exception.AuthErrorCode);
}

private async Task<FirebaseToken> AssertValidIdTokenAsync(
string idToken, bool checkRevoked = false)
{
Expand Down
25 changes: 25 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Auth/AuthErrorHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ public void KnownErrorCodeWithDetails(
Assert.EndsWith($" ({code}): Some details.", error.Message);
}

[Theory]
[MemberData(nameof(AuthErrorCodes))]
public void KnownErrorCodeWithDetailsAndWhiteSpace(
string code, ErrorCode expectedCode, AuthErrorCode expectedAuthCode)
{
var json = $@"{{
""error"": {{
""message"": ""{code} : Some details. "",
}}
}}";
var resp = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.ServiceUnavailable,
Content = new StringContent(json, Encoding.UTF8, "application/json"),
};

var error = AuthErrorHandler.Instance.HandleHttpErrorResponse(resp, json);

Assert.Equal(expectedCode, error.ErrorCode);
Assert.Equal(expectedAuthCode, error.AuthErrorCode);
Assert.Same(resp, error.HttpResponse);
Assert.Null(error.InnerException);
Assert.EndsWith($" ({code}): Some details.", error.Message);
}

[Fact]
public void UnknownErrorCode()
{
Expand Down
5 changes: 3 additions & 2 deletions FirebaseAdmin/FirebaseAdmin/Auth/AuthErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ private sealed class AuthError

/// <summary>
/// Gets the Firebase Auth error code extracted from the response. Returns empty string
/// if the error code cannot be determined.
/// if the error code cannot be determined. These error messages take the form
/// <c>{"error": {"message": "CODE : OPTIONAL DETAILS"}}</c>.
/// </summary>
internal string Code
{
Expand All @@ -225,7 +226,7 @@ internal string Code
var separator = this.GetSeparator();
if (separator != -1)
{
return this.Message.Substring(0, separator);
return this.Message.Substring(0, separator).Trim();
}

return this.Message ?? string.Empty;
Expand Down