public TraktAuthenticationModule_Tests() { MockDevice = new TraktDevice { DeviceCode = MOCK_DEVICE_CODE, UserCode = MOCK_DEVICE_USER_CODE, VerificationUrl = DEVICE_VERIFICATION_URL, ExpiresInSeconds = DEVICE_EXPIRES_IN_SECONDS, IntervalInSeconds = DEVICE_INTERVAL_IN_SECONDS }; MockAuthorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = TestConstants.MOCK_ACCESS_TOKEN, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = TestConstants.MOCK_REFRESH_TOKEN, Scope = TraktAccessScope.Public }; TraktClientId = TestConstants.TRAKT_CLIENT_ID; TraktClientSecret = TestConstants.TRAKT_CLIENT_SECRET; TraktRedirectUri = TestConstants.DEFAULT_REDIRECT_URI; MockAuthorizationPostContent = $"{{ \"code\": \"{MOCK_AUTH_CODE}\", \"client_id\": \"{TraktClientId}\", " + $"\"client_secret\": \"{TraktClientSecret}\", \"redirect_uri\": " + $"\"{TraktRedirectUri}\", \"grant_type\": \"authorization_code\" }}"; MockAuthorizationRefreshPostContent = $"{{ \"refresh_token\": \"{TestConstants.MOCK_REFRESH_TOKEN}\", \"client_id\": \"{TraktClientId}\", " + $"\"client_secret\": \"{TraktClientSecret}\", \"redirect_uri\": " + $"\"{TraktRedirectUri}\", \"grant_type\": \"refresh_token\" }}"; MockAuthorizationRevokePostContent = $"{{ \"token\": \"{TestConstants.MOCK_ACCESS_TOKEN}\", \"client_id\": \"{TraktClientId}\"," + $" \"client_secret\": \"{TraktClientSecret}\" }}"; MockAuthorizationPollingPostContent = $"{{ \"code\": \"{MOCK_DEVICE_CODE}\", \"client_id\": \"{TraktClientId}\", \"client_secret\": \"{TraktClientSecret}\" }}"; MockAuthorizationError = new TraktError { Error = "invalid_grant", Description = "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client." }; MockAuthorizationErrorMessage = $"error on retrieving oauth access token\nerror: {MockAuthorizationError.Error}\n" + $"description: {MockAuthorizationError.Description}"; MockAuthorizationRefreshErrorMessage = $"error on refreshing oauth access token\nerror: {MockAuthorizationError.Error}\n" + $"description: {MockAuthorizationError.Description}"; }
public void Test_TraktAuthorization_CreateWith_ExpiresIn_And_AccessToken_And_RefreshToken() { DateTime createdAtUtcNow = DateTime.UtcNow; ulong createdAtUtcNowTimestamp = TestUtility.CalculateTimestamp(createdAtUtcNow); TraktAuthorization traktAuthorization = TraktAuthorization.CreateWith(EXPIRES_IN_SECONDS, ACCESS_TOKEN, REFRESH_TOKEN); traktAuthorization.Should().NotBeNull(); traktAuthorization.AccessToken.Should().Be(ACCESS_TOKEN); traktAuthorization.RefreshToken.Should().Be(REFRESH_TOKEN); traktAuthorization.Scope.Should().Be(TraktAccessScope.Public); traktAuthorization.TokenType.Should().Be(TraktAccessTokenType.Bearer); traktAuthorization.ExpiresInSeconds.Should().Be(EXPIRES_IN_SECONDS); traktAuthorization.CreatedAtTimestamp.Should().BeInRange(createdAtUtcNowTimestamp - 2, createdAtUtcNowTimestamp + 2); traktAuthorization.CreatedAt.Should().BeCloseTo(createdAtUtcNow, 1000); traktAuthorization.IgnoreExpiration.Should().BeFalse(); traktAuthorization.IsExpired.Should().BeFalse(); traktAuthorization.IsValid.Should().BeTrue(); traktAuthorization.IsRefreshPossible.Should().BeTrue(); }
public async Task Test_TraktAuthenticationModule_CheckIfAuthorizationIsExpiredOrWasRevoked_With_Authorization_Failed_Expired() { TraktClient client = TestUtility.GetOAuthMockClient(CHECK_ACCESS_TOKEN_URI, SYNC_LAST_ACTIVITIES_JSON); ITraktAuthorization authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = TestConstants.MOCK_ACCESS_TOKEN, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 0, RefreshToken = TestConstants.MOCK_REFRESH_TOKEN, Scope = TraktAccessScope.Public }; Pair <bool, TraktResponse <ITraktAuthorization> > response = await client.Authentication.CheckIfAuthorizationIsExpiredOrWasRevokedAsync(authorization); response.Should().NotBeNull(); response.First.Should().BeTrue(); response.Second.Should().NotBeNull(); response.Second.IsSuccess.Should().BeFalse(); response.Second.HasValue.Should().BeFalse(); }
public void Test_TraktAuthenticationModule_RevokeAuthorization_ArgumentExceptions() { TraktClient client = TestUtility.GetAuthenticationMockClient(); client.Authorization = null; Func <Task <TraktNoContentResponse> > act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = null, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = TestConstants.MOCK_REFRESH_TOKEN, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = string.Empty, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = TestConstants.MOCK_REFRESH_TOKEN, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = "mock access token", TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = TestConstants.MOCK_REFRESH_TOKEN, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.Authorization = MockAuthorization; client.ClientId = null; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.ClientId = string.Empty; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.ClientId = "client id"; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.ClientId = TraktClientId; client.ClientSecret = null; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.ClientSecret = string.Empty; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); client.ClientSecret = "client secret"; act = () => client.Authentication.RevokeAuthorizationAsync(); act.Should().Throw <ArgumentException>(); }
public async Task Test_TraktAuthenticationModule_RefreshAuthorization_ArgumentExceptions() { TraktClient client = TestUtility.GetAuthenticationMockClient(); client.Authorization = null; Func <Task <TraktResponse <ITraktAuthorization> > > act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = TestConstants.MOCK_ACCESS_TOKEN, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = null, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = TestConstants.MOCK_ACCESS_TOKEN, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = string.Empty, Scope = TraktAccessScope.Public }; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authorization = new TraktAuthorization { CreatedAtTimestamp = TestUtility.CalculateTimestamp(TestConstants.CREATED_AT), AccessToken = TestConstants.MOCK_ACCESS_TOKEN, TokenType = TraktAccessTokenType.Bearer, ExpiresInSeconds = 7200, RefreshToken = "mock refresh token", Scope = TraktAccessScope.Public }; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authorization = MockAuthorization; client.ClientId = null; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientId = string.Empty; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientId = "client id"; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientId = TraktClientId; client.ClientSecret = null; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientSecret = string.Empty; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientSecret = "client secret"; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.ClientSecret = TraktClientSecret; client.Authentication.RedirectUri = null; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authentication.RedirectUri = string.Empty; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); client.Authentication.RedirectUri = "redirect uri"; act = () => client.Authentication.RefreshAuthorizationAsync(); await act.Should().ThrowAsync <ArgumentException>(); }