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
142 changes: 142 additions & 0 deletions Src/Support/Google.Apis.Auth.Tests/OAuth2/GoogleCredentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,148 @@ public async Task UniverseDomain_DifferentCustomInRequestAndCredential()
Assert.Null(request.Headers.Authorization?.Parameter);
}

[Theory]
[MemberData(nameof(CredentialFactory_Success_Data))]
public void FromFile_WithType(string json, string credentialType, Type expectedType)
{
var tempFile = Path.GetTempFileName();
try
{
File.WriteAllText(tempFile, json);
var credential = CredentialFactory.FromFile(tempFile, credentialType);
Assert.IsType(expectedType, credential.UnderlyingCredential);
}
finally
{
File.Delete(tempFile);
}
}

[Theory]
[MemberData(nameof(CredentialFactory_Failure_Data))]
public void FromFile_WithType_Failure(string json, string credentialType, string expectedMessage)
{
var tempFile = Path.GetTempFileName();
try
{
File.WriteAllText(tempFile, json);
var ex = Assert.Throws<InvalidOperationException>(() => CredentialFactory.FromFile(tempFile, credentialType));
Assert.Equal(expectedMessage, ex.Message);
}
finally
{
File.Delete(tempFile);
}
}

[Theory]
[MemberData(nameof(CredentialFactory_Success_Data))]
public void FromJson_WithType(string json, string credentialType, Type expectedType)
{
var credential = CredentialFactory.FromJson(json, credentialType);
Assert.IsType(expectedType, credential.UnderlyingCredential);
}

[Theory]
[MemberData(nameof(CredentialFactory_Failure_Data))]
public void FromJson_WithType_Failure(string json, string credentialType, string expectedMessage)
{
var ex = Assert.Throws<InvalidOperationException>(() => CredentialFactory.FromJson(json, credentialType));
Assert.Equal(expectedMessage, ex.Message);
}

[Theory]
[MemberData(nameof(CredentialFactory_Success_Data))]
public async Task FromFileAsync_WithType(string json, string credentialType, Type expectedType)
{
var tempFile = Path.GetTempFileName();
try
{
File.WriteAllText(tempFile, json);
var credential = await CredentialFactory.FromFileAsync(tempFile, credentialType, CancellationToken.None);
Assert.IsType(expectedType, credential.UnderlyingCredential);
}
finally
{
File.Delete(tempFile);
}
}

[Theory]
[MemberData(nameof(CredentialFactory_Failure_Data))]
public async Task FromFileAsync_WithType_Failure(string json, string credentialType, string expectedMessage)
{
var tempFile = Path.GetTempFileName();
try
{
File.WriteAllText(tempFile, json);
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => CredentialFactory.FromFileAsync(tempFile, credentialType, CancellationToken.None));
Assert.Equal(expectedMessage, ex.Message);
}
finally
{
File.Delete(tempFile);
}
}

public static TheoryData<string, string, Type> CredentialFactory_Success_Data() =>
new TheoryData<string, string, Type>
{
{ FakeUserCredentialFileContents, JsonCredentialParameters.AuthorizedUserCredentialType, typeof(UserCredential) },
{ FakeServiceAccountCredentialFileContents, JsonCredentialParameters.ServiceAccountCredentialType, typeof(ServiceAccountCredential) }
};

public static TheoryData<string, string, string> CredentialFactory_Failure_Data() =>
new TheoryData<string, string, string>
{
{ FakeUserCredentialFileContents, JsonCredentialParameters.ServiceAccountCredentialType, "The credential type authorized_user is not compatible with the requested type service_account" },
{ FakeUserCredentialFileContents, "invalid_type", "The credential type authorized_user is not compatible with the requested type invalid_type" },
{ "invalid_json", "any_type", "Error deserializing JSON credential data." }
};

[Fact]
public void FromStream_WrapsDeserializationException_WithBadJson()
{
string malformedUserCredentialFileContents = FakeUserCredentialFileContents.Replace("}","");
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(malformedUserCredentialFileContents));
var ex = Assert.Throws<InvalidOperationException>(() => CredentialFactory.FromStream<UserCredential>(stream));
Assert.Equal("Error deserializing JSON credential data.", ex.Message);
Assert.IsType<Newtonsoft.Json.JsonSerializationException>(ex.InnerException);
}

[Fact]
public void FromStream_WrapsDeserializationException()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try one with a bad JSON also, you don't need to write the bad JSON to a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the additional test.

{
var stream = new MockStream(() => throw new Exception("Underlying exception"));
var ex = Assert.Throws<InvalidOperationException>(() => CredentialFactory.FromStream<UserCredential>(stream));
Assert.Equal("Error deserializing JSON credential data.", ex.Message);
Assert.IsType<Exception>(ex.InnerException);
Assert.Equal("Underlying exception", ex.InnerException.Message);
}

private class MockStream : Stream
{
private readonly Action _onRead;

public MockStream(Action onRead) => _onRead = onRead;

public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }

public override void Flush() => throw new NotSupportedException();
public override int Read(byte[] buffer, int offset, int count)
{
_onRead();
return 0;
}
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}

/// <summary>
/// Fake implementation of <see cref="IAuthorizationCodeFlow"/> which only supports fetching the
/// clock and the access method.
Expand Down
Loading