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 TraktAuthorization
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Oct 5, 2017
1 parent 71927e5 commit 3e8e781
Show file tree
Hide file tree
Showing 4 changed files with 580 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/// </summary>
public class TraktAuthorization : ITraktAuthorization
{
private const uint DEFAULT_EXPIRES_IN_SECONDS = 7776000;

/// <summary>Gets or sets the access token.</summary>
[JsonProperty(PropertyName = "access_token")]
public string AccessToken { get; set; }
Expand Down Expand Up @@ -57,7 +59,7 @@ public class TraktAuthorization : ITraktAuthorization
/// </para>
/// </summary>
[JsonIgnore]
public bool IsExpired => !IsValid || (IgnoreExpiration ? false : CreatedAt.AddSeconds(ExpiresInSeconds) <= DateTime.UtcNow);
public bool IsExpired => !IsValid || (!IgnoreExpiration && CreatedAt.AddSeconds(ExpiresInSeconds) <= DateTime.UtcNow);

/// <summary>
/// Returns, whether this authorization information is valid.
Expand All @@ -79,40 +81,49 @@ public class TraktAuthorization : ITraktAuthorization

/// <summary>Returns the UTC DateTime, when this authorization information was created.</summary>
[JsonIgnore]
public DateTime CreatedAt
=> CreatedAtTimestamp > 0 ? new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(CreatedAtTimestamp) : default(DateTime);
public DateTime CreatedAt => CreatedAtTimestamp > 0 ? new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(CreatedAtTimestamp) : default(DateTime);

[JsonIgnore]
internal bool IgnoreExpiration { get; set; }

public override string ToString()
{
string value = IsValid ? $"{AccessToken}" : "no valid access token";
value += IsExpired ? " (expired)" : $" (valid until {CreatedAt.AddSeconds(ExpiresInSeconds)})";
return value;
}

public bool Equals(ITraktAuthorization other)
=> other != null
&& other.IsValid == IsValid
&& other.IsExpired == IsExpired
&& other.IsRefreshPossible == IsRefreshPossible
&& other.ExpiresInSeconds == ExpiresInSeconds
&& other.CreatedAtTimestamp == CreatedAtTimestamp
&& other.CreatedAt == CreatedAt
&& other.AccessToken == AccessToken
&& other.RefreshToken == RefreshToken
&& other.Scope == Scope
&& other.TokenType == TokenType;

/// <summary>Creates a new <see cref="TraktAuthorization" /> instance with the given values.</summary>
/// <param name="accessToken">The access token for the new <see cref="TraktAuthorization" /> instance.</param>
/// <param name="refreshToken">The optional refresh token for the new <see cref="TraktAuthorization" /> instance.</param>
/// <returns>A new <see cref="TraktAuthorization" /> instance with the given values.</returns>
public static TraktAuthorization CreateWith(string accessToken, string refreshToken = null)
=> new TraktAuthorization
{
Scope = TraktAccessScope.Public,
TokenType = TraktAccessTokenType.Bearer,
AccessToken = accessToken ?? string.Empty,
RefreshToken = refreshToken ?? string.Empty,
IgnoreExpiration = true
};
{
TraktAuthorization traktAuthorization = CreateWith(DateTime.UtcNow, accessToken, refreshToken);
traktAuthorization.IgnoreExpiration = true;
return traktAuthorization;
}

/// <summary>Creates a new <see cref="TraktAuthorization" /> instance with the given values.</summary>
/// <param name="expiresInSeconds">The seconds, after which the given access token will expire.</param>
/// <param name="accessToken">The access token for the new <see cref="TraktAuthorization" /> instance.</param>
/// <param name="refreshToken">The optional refresh token for the new <see cref="TraktAuthorization" /> instance.</param>
/// <returns>A new <see cref="TraktAuthorization" /> instance with the given values.</returns>
public static TraktAuthorization CreateWith(uint expiresInSeconds, string accessToken, string refreshToken = null)
=> new TraktAuthorization
{
ExpiresInSeconds = expiresInSeconds,
Scope = TraktAccessScope.Public,
TokenType = TraktAccessTokenType.Bearer,
AccessToken = accessToken ?? string.Empty,
RefreshToken = refreshToken ?? string.Empty
};
=> CreateWith(DateTime.UtcNow, expiresInSeconds, accessToken, refreshToken);

/// <summary>
/// Creates a new <see cref="TraktAuthorization" /> instance with the given values.
Expand All @@ -124,7 +135,11 @@ public static TraktAuthorization CreateWith(uint expiresInSeconds, string access
/// <param name="refreshToken">The optional refresh token for the new <see cref="TraktAuthorization" /> instance.</param>
/// <returns>A new <see cref="TraktAuthorization" /> instance with the given values.</returns>
public static TraktAuthorization CreateWith(DateTime createdAt, string accessToken, string refreshToken = null)
=> CreateWith(createdAt, 7776000, accessToken, refreshToken);
{
TraktAuthorization traktAuthorization = CreateWith(createdAt, DEFAULT_EXPIRES_IN_SECONDS, accessToken, refreshToken);
traktAuthorization.IgnoreExpiration = true;
return traktAuthorization;
}

/// <summary>Creates a new <see cref="TraktAuthorization" /> instance with the given values.</summary>
/// <param name="createdAt">The datetime, when the given access token was created. Will be converted to UTC datetime.</param>
Expand All @@ -136,39 +151,23 @@ public static TraktAuthorization CreateWith(DateTime createdAt, uint expiresInSe
string accessToken, string refreshToken = null)
=> new TraktAuthorization
{
CreatedAtTimestamp = (ulong)createdAt.ToUniversalTime().Ticks,
ExpiresInSeconds = expiresInSeconds,
AccessToken = accessToken ?? string.Empty,
RefreshToken = refreshToken ?? string.Empty,
Scope = TraktAccessScope.Public,
TokenType = TraktAccessTokenType.Bearer,
AccessToken = accessToken ?? string.Empty,
RefreshToken = refreshToken ?? string.Empty
ExpiresInSeconds = expiresInSeconds,
CreatedAtTimestamp = CalculateTimestamp(createdAt)
};

public override string ToString()
private static ulong CalculateTimestamp(DateTime createdAt)
{
var validUntil = CreatedAt.AddSeconds(ExpiresInSeconds);
var strIsExpired = IsExpired ? "(expired)" : $"(valid until {validUntil})";
return IsValid ? $"{AccessToken} {strIsExpired}" : $"no valid access token {strIsExpired}";
}

public bool Equals(ITraktAuthorization other)
{
if (other == null || other.IsValid != IsValid
|| other.IsExpired != IsExpired
|| other.IsRefreshPossible != IsRefreshPossible
|| ((TraktAuthorization)other).IgnoreExpiration != IgnoreExpiration
|| other.ExpiresInSeconds != ExpiresInSeconds
|| other.CreatedAtTimestamp != CreatedAtTimestamp
|| other.CreatedAt != CreatedAt
|| other.AccessToken != AccessToken
|| other.RefreshToken != RefreshToken
|| other.Scope != Scope
|| other.TokenType != TokenType)
{
return false;
}

return true;
const long ticksPerMilliseconds = TimeSpan.TicksPerMillisecond;
var origin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long originMilliseconds = origin.Ticks / ticksPerMilliseconds;
DateTime utcCreatedAt = createdAt.ToUniversalTime();
long utcCreatedAtMilliseconds = utcCreatedAt.Ticks / ticksPerMilliseconds;
long differenceInMilliseconds = utcCreatedAtMilliseconds - originMilliseconds;
return (ulong) (differenceInMilliseconds / 1000);
}
}
}
Loading

0 comments on commit 3e8e781

Please sign in to comment.