Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
GH-78: Add tests for TraktDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Oct 5, 2017
1 parent 4d5e533 commit 9875cc9
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,24 @@ public class TraktDevice : ITraktDevice
[JsonIgnore]
public bool IsExpiredUnused => CreatedAt.AddSeconds(ExpiresInSeconds) <= DateTime.UtcNow;

public override string ToString() => IsValid ? DeviceCode : "no valid device code";
public override string ToString()
{
string value = !string.IsNullOrEmpty(DeviceCode) ? $"{DeviceCode}" : "no valid device code";
value += IsExpiredUnused ? " (expired unused)" : $" (valid until {CreatedAt.AddSeconds(ExpiresInSeconds)})";
return value;
}

public bool Equals(ITraktDevice other)
{
if (other == null
|| other.IsExpiredUnused != IsExpiredUnused
|| other.IsValid != IsValid
|| other.IntervalInSeconds != IntervalInSeconds
|| ((TraktDevice)other).CreatedAt != CreatedAt
|| other.DeviceCode != DeviceCode
|| other.UserCode != UserCode
|| other.VerificationUrl != VerificationUrl)
{
return false;
}

return true;
return other != null
&& other.IsExpiredUnused == IsExpiredUnused
&& other.IsValid == IsValid
&& other.ExpiresInSeconds == ExpiresInSeconds
&& other.IntervalInSeconds == IntervalInSeconds
&& other.IntervalInMilliseconds == IntervalInMilliseconds
&& other.DeviceCode == DeviceCode
&& other.UserCode == UserCode
&& other.VerificationUrl == VerificationUrl;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ internal static class JsonFactoryContainer
{
private static readonly Dictionary<Type, object> s_readerFactories = new Dictionary<Type, object>();

internal static IObjectJsonReader<TReturnType> CreateObjectReader<TReturnType>()
public static IObjectJsonReader<TReturnType> CreateObjectReader<TReturnType>()
{
IJsonReaderFactory<TReturnType> factory = GetReaderFactory<TReturnType>();
Debug.Assert(factory != null, $"factory for {nameof(TReturnType)} should not be null");
return factory.CreateObjectReader();
}

internal static IArrayJsonReader<TReturnType> CreateArrayReader<TReturnType>()
public static IArrayJsonReader<TReturnType> CreateArrayReader<TReturnType>()
{
IJsonReaderFactory<TReturnType> factory = GetReaderFactory<TReturnType>();
Debug.Assert(factory != null, $"factory for {nameof(TReturnType)} should not be null");
return factory.CreateArrayReader();
}

internal static IJsonReaderFactory<TReturnType> GetReaderFactory<TReturnType>()
public static IJsonReaderFactory<TReturnType> GetReaderFactory<TReturnType>()
{
Type type = typeof(TReturnType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TraktAuthorization_Tests
private static readonly ulong s_createdAtTimestamp = CalculateTimestamp(s_createdAt);

[Fact]
public void Test_TraktAuthorization_Implements_ITraktCastAndCrew_Interface()
public void Test_TraktAuthorization_Implements_ITraktAuthorization_Interface()
{
typeof(TraktAuthorization).GetInterfaces().Should().Contain(typeof(ITraktAuthorization));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
namespace TraktApiSharp.Tests.Objects.Authentication.Implementations
{
using FluentAssertions;
using System;
using System.Threading.Tasks;
using Traits;
using TraktApiSharp.Objects.Authentication;
using TraktApiSharp.Objects.Authentication.Implementations;
using TraktApiSharp.Objects.Authentication.JsonReader;
using Xunit;

[Category("Objects.Authentication.Implementations")]
public class TraktDevice_Tests
{
[Fact]
public void Test_TraktDevice_Implements_ITraktDevice_Interface()
{
typeof(TraktDevice).GetInterfaces().Should().Contain(typeof(ITraktDevice));
}

[Fact]
public void Test_TraktDevice_Default_Constructor()
{
var traktDevice = new TraktDevice();

traktDevice.DeviceCode.Should().BeNull();
traktDevice.UserCode.Should().BeNull();
traktDevice.VerificationUrl.Should().BeNull();
traktDevice.ExpiresInSeconds.Should().Be(0U);
traktDevice.IntervalInSeconds.Should().Be(0U);
traktDevice.IntervalInMilliseconds.Should().Be(0U);
traktDevice.IsValid.Should().BeFalse();
traktDevice.IsExpiredUnused.Should().BeTrue();
traktDevice.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, 1000);
}

[Fact]
public async Task Test_TraktDevice_From_Json()
{
var jsonReader = new DeviceObjectJsonReader();
TraktDevice traktDevice = await jsonReader.ReadObjectAsync(JSON);

traktDevice.Should().NotBeNull();
traktDevice.DeviceCode.Should().Be("194e624d66681668632a29292d72b87cb02d4e231d428d3136e596af9e5f942f");
traktDevice.UserCode.Should().Be("0F843E93");
traktDevice.VerificationUrl.Should().Be("https://trakt.tv/activate");
traktDevice.ExpiresInSeconds.Should().Be(600U);
traktDevice.IntervalInSeconds.Should().Be(5U);
traktDevice.IntervalInMilliseconds.Should().Be(5000U);
traktDevice.IsValid.Should().BeTrue();
traktDevice.IsExpiredUnused.Should().BeFalse();
traktDevice.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, 1000);
}

[Fact]
public void Test_TraktDevice_IsValid()
{
var traktDevice = new TraktDevice();

traktDevice.IsValid.Should().BeFalse();

traktDevice.DeviceCode = "deviceCode";
traktDevice.IsValid.Should().BeFalse();

traktDevice.UserCode = "userCode";
traktDevice.IsValid.Should().BeFalse();

traktDevice.VerificationUrl = "verificationUrl";
traktDevice.IsValid.Should().BeFalse();

traktDevice.IntervalInSeconds = 1;
traktDevice.IsValid.Should().BeFalse();

traktDevice.ExpiresInSeconds = 600;
traktDevice.IsExpiredUnused.Should().BeFalse();
traktDevice.IsValid.Should().BeTrue();
}

[Fact]
public void Test_TraktDevice_IsExpiredUnused()
{
var traktDevice = new TraktDevice();

traktDevice.IsExpiredUnused.Should().BeTrue();

traktDevice.ExpiresInSeconds = 600;
traktDevice.IsExpiredUnused.Should().BeFalse();
}

[Fact]
public void Test_TraktDevice_ToString()
{
var traktDevice = new TraktDevice();

traktDevice.ToString().Should().Be("no valid device code (expired unused)");

traktDevice.DeviceCode = "deviceCode";
traktDevice.ToString().Should().Be($"{traktDevice.DeviceCode} (expired unused)");

traktDevice.ExpiresInSeconds = 600;
traktDevice.IsExpiredUnused.Should().BeFalse();
traktDevice.ToString().Should().Be($"{traktDevice.DeviceCode} (valid until {traktDevice.CreatedAt.AddSeconds(traktDevice.ExpiresInSeconds)})");
}

[Fact]
public async Task Test_TraktDevice_Equals()
{
var traktDevice1 = new TraktDevice();
var traktDevice2 = new TraktDevice();

traktDevice1.Equals(traktDevice2).Should().BeTrue();

var jsonReader = new DeviceObjectJsonReader();
TraktDevice traktDeviceFromJson = await jsonReader.ReadObjectAsync(JSON);

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice2 = CopyTraktDevice(traktDeviceFromJson);

traktDevice2.Equals(traktDevice1).Should().BeTrue();

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice1.DeviceCode = null;
traktDevice2.Equals(traktDevice1).Should().BeFalse();

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice1.UserCode = null;
traktDevice2.Equals(traktDevice1).Should().BeFalse();

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice1.VerificationUrl = null;
traktDevice2.Equals(traktDevice1).Should().BeFalse();

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice1.ExpiresInSeconds = 0;
traktDevice2.Equals(traktDevice1).Should().BeFalse();

traktDevice1 = CopyTraktDevice(traktDeviceFromJson);
traktDevice1.IntervalInSeconds = 0;
traktDevice2.Equals(traktDevice1).Should().BeFalse();
}

private static TraktDevice CopyTraktDevice(ITraktDevice traktDevice)
{
return new TraktDevice
{
DeviceCode = traktDevice.DeviceCode,
UserCode = traktDevice.UserCode,
VerificationUrl = traktDevice.VerificationUrl,
ExpiresInSeconds = traktDevice.ExpiresInSeconds,
IntervalInSeconds = traktDevice.IntervalInSeconds
};
}

private const string JSON =
@"{
""device_code"": ""194e624d66681668632a29292d72b87cb02d4e231d428d3136e596af9e5f942f"",
""user_code"": ""0F843E93"",
""verification_url"": ""https://trakt.tv/activate"",
""expires_in"": 600,
""interval"": 5
}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using FluentAssertions;
using System.Linq;
using System.Reflection;
using Traits;
using TraktApiSharp.Objects.JsonReader;
using Xunit;
Expand All @@ -12,23 +13,29 @@ public class JsonFactoryContainer_Tests
[Fact]
public void Test_JsonFactoryContainer_Has_CreateObjectReader_Method()
{
var methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "CreateObjectReader");
MethodInfo methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "CreateObjectReader");

methodInfo.Should().NotBeNull();
methodInfo.GetParameters().Should().BeEmpty();
methodInfo.IsGenericMethod.Should().BeTrue();
}

[Fact]
public void Test_JsonFactoryContainer_Has_CreateArrayReader_Method()
{
var methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "CreateArrayReader");
MethodInfo methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "CreateArrayReader");

methodInfo.Should().NotBeNull();
methodInfo.GetParameters().Should().BeEmpty();
methodInfo.IsGenericMethod.Should().BeTrue();
}

[Fact]
public void Test_JsonFactoryContainer_Has_GetReaderFactory_Method()
{
var methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "GetReaderFactory");
MethodInfo methodInfo = typeof(JsonFactoryContainer).GetMethods().FirstOrDefault(m => m.Name == "GetReaderFactory");

methodInfo.Should().NotBeNull();
methodInfo.GetParameters().Should().BeEmpty();
methodInfo.IsGenericMethod.Should().BeTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Modules\TraktNetworksModule\TraktNetworksModule_Tests.cs" />
<Compile Include="Modules\TraktNetworksModule\TraktNetworksModule_Tests_Json_Data.cs" />
<Compile Include="Objects\Authentication\Implementations\TraktAuthorization_Tests.cs" />
<Compile Include="Objects\Authentication\Implementations\TraktDevice_Tests.cs" />
<Compile Include="Objects\Authentication\Interfaces\ITraktAuthorization_Tests.cs" />
<Compile Include="Objects\Authentication\Interfaces\ITraktDevice_Tests.cs" />
<Compile Include="Objects\Authentication\JsonReader\AuthorizationObjectJsonReader\AuthorizationObjectJsonReader_Json_String_Tests.cs" />
Expand Down

0 comments on commit 9875cc9

Please sign in to comment.