Skip to content

Commit

Permalink
Allow developer to pass in HttpClient (#190)
Browse files Browse the repository at this point in the history
* Allow developer to pass in HttpClient

* Clean up tests

* Fix StyleCop build errors
  • Loading branch information
nbarbettini authored Jun 13, 2018
1 parent 64771cb commit e155419
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 211 deletions.
86 changes: 0 additions & 86 deletions src/Okta.Sdk.IntegrationTests/LocalServerFixture.cs

This file was deleted.

24 changes: 6 additions & 18 deletions src/Okta.Sdk.IntegrationTests/OktaClientShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ namespace Okta.Sdk.IntegrationTests
{
public class OktaClientShould
{
private static IOktaClient CreateClient()
{
// Client settings are expected to be in environment variables on the test machine.
return new OktaClient();
}

[Fact]
public void ThrowForNullOrgUrl()
{
IOktaClient client;

Assert.Throws<ArgumentNullException>(() =>
{
client = new OktaClient(new OktaClientConfiguration
var client = new OktaClient(new OktaClientConfiguration
{
OrgUrl = string.Empty,
Token = "foobar",
Expand All @@ -37,11 +29,9 @@ public void ThrowForNullOrgUrl()
[Fact]
public void ThrowForInvalidOrgUrl()
{
IOktaClient client;

Assert.Throws<ArgumentException>(() =>
{
client = new OktaClient(new OktaClientConfiguration
var client = new OktaClient(new OktaClientConfiguration
{
// Must start with https://
OrgUrl = "http://insecure.dev",
Expand All @@ -53,11 +43,9 @@ public void ThrowForInvalidOrgUrl()
[Fact]
public void ThrowForNullToken()
{
IOktaClient client;

Assert.Throws<ArgumentNullException>(() =>
{
client = new OktaClient(new OktaClientConfiguration
var client = new OktaClient(new OktaClientConfiguration
{
OrgUrl = "https://dev-12345.oktapreview.com",
Token = string.Empty,
Expand All @@ -68,7 +56,7 @@ public void ThrowForNullToken()
[Fact]
public async Task ThrowForArbitraryRequestUrl()
{
var client = CreateClient();
var client = TestClient.Create();

await Assert.ThrowsAsync<InvalidOperationException>(() =>
{
Expand All @@ -80,7 +68,7 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
[Fact]
public async Task ThrowApiExceptionForInvalidToken()
{
var client = new OktaClient(new OktaClientConfiguration
var client = TestClient.Create(new OktaClientConfiguration
{
Token = "abcd1234",
});
Expand All @@ -91,7 +79,7 @@ public async Task ThrowApiExceptionForInvalidToken()
}
catch (OktaApiException apiException)
{
apiException.Message.Should().Be("Invalid token provided (400, E0000011)");
apiException.Message.Should().StartWith("Invalid token provided");

apiException.Error.Should().NotBeNull();
apiException.Error.ErrorCode.Should().Be("E0000011");
Expand Down
27 changes: 4 additions & 23 deletions src/Okta.Sdk.IntegrationTests/ScenarioGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,19 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
// </copyright>

using System;
using Okta.Sdk.Configuration;
using Xunit;

namespace Okta.Sdk.IntegrationTests
{
/// <summary>
/// Base class for groups of integration tests that optionally are run against a local test server.
/// Base class for groups of integration tests.
/// </summary>
public abstract class ScenarioGroup
{
private readonly Lazy<OktaClient> _defaultClient;

public ScenarioGroup()
{
_defaultClient = new Lazy<OktaClient>(() => new OktaClient());
}

protected IOktaClient GetClient(string scenarioName)
{
if (!TestConfiguration.UseLocalServer)
{
return _defaultClient.Value;
}

var configuredOrgUrl = _defaultClient.Value.Configuration.OrgUrl;
var orgUrlWithScenarioName = $"{configuredOrgUrl}/{scenarioName}";
// scenarioName is reserved for future use, if we add
// mocked server responses to our testing strategy.

return new OktaClient(new OktaClientConfiguration
{
OrgUrl = orgUrlWithScenarioName,
});
return TestClient.Create();
}
}
}
2 changes: 1 addition & 1 deletion src/Okta.Sdk.IntegrationTests/ScenariosCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Okta.Sdk.IntegrationTests
/// This collection is used group all integration tests that can run against a single shared instance of a local test server.
/// </summary>
[CollectionDefinition(nameof(ScenariosCollection))]
public sealed class ScenariosCollection : ICollectionFixture<LocalServerFixture>
public sealed class ScenariosCollection
{
// Intentionally left blank. This class only serves as an anchor for CollectionDefinition.
}
Expand Down
19 changes: 19 additions & 0 deletions src/Okta.Sdk.IntegrationTests/TestClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <copyright file="TestClient.cs" company="Okta, Inc">
// Copyright (c) 2014 - present Okta, Inc. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
// </copyright>

using Okta.Sdk.Configuration;

namespace Okta.Sdk.IntegrationTests
{
public static class TestClient
{
public static IOktaClient Create(OktaClientConfiguration configuration = null)
{
// Configuration is expected to be stored in environment variables on the test machine.
// A few tests pass in a configuration object, but this is just to override and test specific things.
return new OktaClient(configuration);
}
}
}
2 changes: 1 addition & 1 deletion src/Okta.Sdk.UnitTests/Okta.Sdk.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.3.0" />
<PackageReference Include="FluentAssertions" Version="5.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="NSubstitute" Version="3.1.0" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
Expand Down
54 changes: 31 additions & 23 deletions src/Okta.Sdk.UnitTests/OktaClientShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Okta.Sdk.Internal;
using Xunit;

namespace Okta.Sdk.UnitTests
Expand All @@ -18,23 +18,20 @@ public class OktaClientShould
[Fact]
public async Task GetCollection()
{
// Arrange
var testItems = new[]
{
new TestResource { Foo = "foo1" },
new TestResource { Foo = "foo2", Bar = true },
new TestResource { Foo = "foo3", Bar = false },
};
var mockRequestExecutor = new MockedCollectionRequestExecutor<TestResource>(pageSize: 2, items: testItems);
var dataStore = new DefaultDataStore(
mockRequestExecutor,
new DefaultSerializer(),
new ResourceFactory(null, NullLogger.Instance),
NullLogger.Instance);

var client = new TestableOktaClient(dataStore);
var client = new TestableOktaClient(mockRequestExecutor);

// Act
var items = await client.GetCollection<TestResource>("https://stuff").ToArray();

// Assert
items.Count().Should().Be(3);
items.ElementAt(0).Foo.Should().Be("foo1");
items.ElementAt(0).Bar.Should().BeNull();
Expand All @@ -58,13 +55,7 @@ public async Task ThrowApiExceptionFor4xx()
""errorCauses"": []
}";
var mockRequestExecutor = new MockedStringRequestExecutor(rawErrorResponse, 400);
var dataStore = new DefaultDataStore(
mockRequestExecutor,
new DefaultSerializer(),
new ResourceFactory(null, NullLogger.Instance),
NullLogger.Instance);

var client = new TestableOktaClient(dataStore);
var client = new TestableOktaClient(mockRequestExecutor);

try
{
Expand Down Expand Up @@ -100,13 +91,7 @@ public async Task IncludeErrorCausesInApiException()
]
}";
var mockRequestExecutor = new MockedStringRequestExecutor(rawErrorResponse, 500);
var dataStore = new DefaultDataStore(
mockRequestExecutor,
new DefaultSerializer(),
new ResourceFactory(null, NullLogger.Instance),
NullLogger.Instance);

var client = new TestableOktaClient(dataStore);
var client = new TestableOktaClient(mockRequestExecutor);

try
{
Expand All @@ -122,6 +107,29 @@ public async Task IncludeErrorCausesInApiException()
causes.Should().HaveCount(2);
causes.First().ErrorSummary.Should().Be("Password requirements were not met.");
causes.Last().ErrorSummary.Should().Be("Another bad thing that happened");

}
}

[Fact]
public void UsePassedHttpClient()
{
var testableClient = new TestableHttpClient();
var client = new OktaClient(
TestableOktaClient.DefaultFakeConfiguration,
testableClient);

Func<Task> act = async () => await client.Users.GetUserAsync("foobar");

act.Should().Throw<NotImplementedException>()
.WithMessage("Used the client!");
}

public class TestableHttpClient : System.Net.Http.HttpClient
{
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
throw new NotImplementedException("Used the client!");
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/Okta.Sdk.UnitTests/TestableOktaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
// </copyright>

using Microsoft.Extensions.Logging.Abstractions;
using Okta.Sdk.Configuration;
using Okta.Sdk.Internal;

namespace Okta.Sdk.UnitTests
{
public class TestableOktaClient : OktaClient
{
private static readonly OktaClientConfiguration DefaultFakeConfiguration = new OktaClientConfiguration
public static readonly OktaClientConfiguration DefaultFakeConfiguration = new OktaClientConfiguration
{
OrgUrl = "https://fake.example.com",
Token = "foobar",
};

public TestableOktaClient(IDataStore dataStore, OktaClientConfiguration configuration = null, RequestContext requestContext = null)
: base(dataStore, configuration ?? DefaultFakeConfiguration, requestContext ?? new RequestContext())
//public TestableOktaClient(IDataStore dataStore, OktaClientConfiguration configuration = null, RequestContext requestContext = null)
// : base(dataStore, configuration ?? DefaultFakeConfiguration, requestContext ?? new RequestContext())
//{
//}

public TestableOktaClient(IRequestExecutor requestExecutor)
: base(
new DefaultDataStore(requestExecutor, new DefaultSerializer(), new ResourceFactory(null, null), NullLogger.Instance),
DefaultFakeConfiguration,
new RequestContext())
{
}
}
Expand Down
Loading

0 comments on commit e155419

Please sign in to comment.