public void EnsureAuthHeaderThrowsForNullClientSecret() { var cmd = new GetAuthTokenCommand(); cmd.ClientId = "id"; cmd.ConstructAuthHeader(); }
public void EnsureAuthHeaderReturnsValueForValidClientIdAndSecret() { var cmd = new GetAuthTokenCommand(); cmd.ClientId = "id"; cmd.ClientSecret = "secret"; Assert.IsNotNullOrEmpty(cmd.ConstructAuthHeader(), "Expected an auth header"); }
/// <summary> /// Initialises the OAuthUserFlow class /// </summary> /// <param name="clientId">The client id.</param> /// <param name="clientSecret">The client secret.</param> /// <param name="secureBaseApiUri">The secure base API URI.</param> /// <param name="command">The command.</param> public OAuthUserFlow(string clientId, string clientSecret, string secureBaseApiUri, GetAuthTokenCommand command) { this._clientId = clientId; this._clientSecret = clientSecret; this._secureBaseApiUri = secureBaseApiUri; this._tokenCommand = command; this.TokenResponse = null; }
public void EnsurePropertiesPersist() { const string TestValue = "value"; var cmd = new GetAuthTokenCommand(); cmd.ClientId = TestValue; Assert.AreEqual(TestValue, cmd.ClientId, "Expected value to persist"); cmd.ClientSecret = TestValue; Assert.AreEqual(TestValue, cmd.ClientSecret, "Expected value to persist"); }
public void EnsureKeysSortedByConvertParamsToString() { var request = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("scope", Scope.ReadUserPlayHistory.AsStringParam()), new KeyValuePair <string, string>("grant_type", GetAuthTokenCommand.GrantTypeAuthorizationCode) }; var cmd = new GetAuthTokenCommand(); var stringForm = cmd.ConvertParamsToString(request, false); Assert.AreEqual("grant_type=authorization_code&scope=read_userplayhistory", stringForm, "Expected params to be re-sorted alphabetically"); }
public async Task EnsureExceptionGivenForBadApiCredentials() { var cmd = new GetAuthTokenCommand() { RequestHandler = new MockApiRequestHandler(FakeResponse.Unauthorized()), OAuth2 = new OAuth2(null), AuthorizationCode = "code", ClientId = "test", ClientSecret = "test", ClientSettings = new MockMusicClientSettings(null, "test", null) }; var t = await cmd.ExecuteAsync(null); Assert.IsNull(t.Result, "Expected no result"); Assert.IsNotNull(t.Error, "Expected an error"); Assert.AreNotEqual(typeof(InvalidApiCredentialsException), t.Error.GetType()); }
public void EnsureConvertParamsToStringRespectsForHeaderParam() { var request = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", GetAuthTokenCommand.GrantTypeAuthorizationCode), new KeyValuePair <string, string>("scope", (Scope.ReadUserPlayHistory | Scope.ReadUserTasteProfile).AsStringParam()) }; var cmd = new GetAuthTokenCommand(); var bodyParams = cmd.ConvertParamsToString(request, false); var headerParams = cmd.ConvertParamsToString(request, true); Assert.IsTrue(bodyParams.IndexOf("+") > -1, "Expected + encoded spaces"); Assert.IsTrue(bodyParams.IndexOf("%20") == -1, "Expected + encoded spaces"); Assert.IsTrue(headerParams.IndexOf("%20") > -1, "Expected %20 encoded spaces"); Assert.IsTrue(headerParams.IndexOf("+") == -1, "Expected %20 encoded spaces"); }
public async Task EnsureResponseParsedForValidRefreshTokenRequest() { var cmd = new GetAuthTokenCommand() { RequestHandler = new MockApiRequestHandler(FakeResponse.Success(Resources.token_response, MusicClientCommand.ContentTypeJson)), OAuth2 = new OAuth2(null), RefreshToken = "token", ClientId = "test", ClientSecret = "test", ClientSettings = new MockMusicClientSettings("test", "gb", null) }; var t = await cmd.ExecuteAsync(null); Assert.IsNotNull(t.Result, "Expected a result"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected an access token"); Assert.IsTrue(t.Result.ExpiresIn > 0, "Expected expires > 0"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected a refresh token"); Assert.IsNotNull(t.Result.UserId, "Expected a user id"); Assert.IsNotNullOrEmpty(t.Result.Territory, "Expected territory"); }
public void EnsureStockPropertiesAreCorrect() { var cmd = new GetAuthTokenCommand(); Assert.AreEqual(MusicClientCommand.DefaultBaseApiUri, cmd.BaseApiUri, "Expected the right value"); Assert.AreEqual(MusicClientCommand.ContentTypeFormPost, cmd.ContentType, "Expected the right value"); Assert.AreEqual(false, cmd.RequiresCountryCode, "Expected the right value"); Assert.AreEqual(true, cmd.RequiresEmptyQuerystring, "Expected the right value"); Assert.AreEqual(HttpMethod.Post, cmd.HttpMethod, "Expected the right value"); StringBuilder sb = new StringBuilder(); cmd.AppendUriPath(sb); Assert.AreEqual("token/", sb.ToString(), "Expected the right value"); // Ensure right Grant Type is returned... cmd.AuthorizationCode = "code"; Assert.AreEqual(GetAuthTokenCommand.GrantTypeAuthorizationCode, cmd.GrantType, "Expected the right value"); cmd.AuthorizationCode = null; cmd.RefreshToken = "token"; Assert.AreEqual(GetAuthTokenCommand.GrantTypeRefreshToken, cmd.GrantType, "Expected the right value"); }
private void GetAuthToken() { ServerAPI serverAPI = Service.ServerAPI; if (this.useRealAuthentication) { string requestToken = ServerStartupTask.GenerateRequestToken(PlayerPrefs.GetString("prefPlayerId"), PlayerPrefs.GetString("prefPlayerSecret")); this.getAuthTokenCommand = new GetAuthTokenCommand(new GetAuthTokenRequest { PlayerId = PlayerPrefs.GetString("prefPlayerId"), RequestToken = requestToken }); this.getAuthTokenCommand.AddSuccessCallback(new AbstractCommand <GetAuthTokenRequest, GetAuthTokenResponse> .OnSuccessCallback(this.OnGetAuthTokenComplete)); serverAPI.Async(this.getAuthTokenCommand); } else { this.InitializeCurrentPlayer(); string playerId = Service.CurrentPlayer.PlayerId; serverAPI.SetAuth(playerId); this.CompleteTask(); } }
public void EnsureStockPropertiesAreCorrect() { var cmd = new GetAuthTokenCommand(); Assert.AreEqual(MusicClientCommand.DefaultSecureBaseApiUri, cmd.BaseApiUri, "Expected the right value"); Assert.AreEqual(MusicClientCommand.ContentTypeFormPost, cmd.ContentType, "Expected the right value"); Assert.AreEqual(false, cmd.RequiresCountryCode, "Expected the right value"); Assert.AreEqual(true, cmd.RequiresEmptyQuerystring, "Expected the right value"); Assert.AreEqual(HttpMethod.Post, cmd.HttpMethod, "Expected the right value"); StringBuilder sb = new StringBuilder(); cmd.AppendUriPath(sb); Assert.AreEqual("token/", sb.ToString(), "Expected the right value"); // Ensure right Grant Type is returned... cmd.AuthorizationCode = "code"; Assert.AreEqual(GetAuthTokenCommand.GrantTypeAuthorizationCode, cmd.GrantType, "Expected the right value"); cmd.AuthorizationCode = null; cmd.RefreshToken = "token"; Assert.AreEqual(GetAuthTokenCommand.GrantTypeRefreshToken, cmd.GrantType, "Expected the right value"); }
public void EnsureResponseParsedForValidAuthCodeRequest() { var cmd = new GetAuthTokenCommand() { RequestHandler = new MockApiRequestHandler(FakeResponse.Success(Resources.token_response, MusicClientCommand.ContentTypeJson)), OAuth2 = new OAuth2(null), AuthorizationCode = "code", ClientId = "test", ClientSecret = "test", ClientSettings = new MockMusicClientSettings("test", "gb", null) }; var task = cmd.InvokeAsync(); task.Wait(); Response <TokenResponse> t = task.Result; Assert.IsNotNull(t.Result, "Expected a result"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected an access token"); Assert.IsTrue(t.Result.ExpiresIn > 0, "Expected expires > 0"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected a refresh token"); Assert.IsNotNull(t.Result.UserId, "Expected a user id"); Assert.IsNotNullOrEmpty(t.Result.Territory, "Expected territory"); }
/// <summary> /// Initialises the OAuthUserFlow class /// </summary> /// <param name="clientId">The client id.</param> /// <param name="clientSecret">The client secret.</param> /// <param name="command">The command.</param> public OAuthUserFlow(string clientId, string clientSecret, GetAuthTokenCommand command) { this._clientId = clientId; this._clientSecret = clientSecret; this._tokenCommand = command; }
public void EnsureResponseParsedForValidAuthCodeRequest() { var cmd = new GetAuthTokenCommand() { RequestHandler = new MockApiRequestHandler(FakeResponse.Success(Resources.token_response, MusicClientCommand.ContentTypeJson)), OAuth2 = new OAuth2(null), AuthorizationCode = "code", ClientId = "test", ClientSecret = "test", ClientSettings = new MockMusicClientSettings("test", "gb", null) }; var task = cmd.InvokeAsync(); task.Wait(); Response<TokenResponse> t = task.Result; Assert.IsNotNull(t.Result, "Expected a result"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected an access token"); Assert.IsTrue(t.Result.ExpiresIn > 0, "Expected expires > 0"); Assert.IsNotNullOrEmpty(t.Result.AccessToken, "Expected a refresh token"); Assert.IsNotNull(t.Result.UserId, "Expected a user id"); Assert.IsNotNullOrEmpty(t.Result.Territory, "Expected territory"); }
public void EnsureConvertParamsToStringRespectsForHeaderParam() { var request = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", GetAuthTokenCommand.GrantTypeAuthorizationCode), new KeyValuePair<string, string>("scope", Scope.ReadUserPlayHistory.AsStringParam()) }; var cmd = new GetAuthTokenCommand(); var bodyParams = cmd.ConvertParamsToString(request, false); var headerParams = cmd.ConvertParamsToString(request, true); Assert.IsTrue(bodyParams.IndexOf("+") > -1, "Expected + encoded spaces"); Assert.IsTrue(bodyParams.IndexOf("%20") == -1, "Expected + encoded spaces"); Assert.IsTrue(headerParams.IndexOf("%20") > -1, "Expected %20 encoded spaces"); Assert.IsTrue(headerParams.IndexOf("+") == -1, "Expected %20 encoded spaces"); }
public void EnsureGrantTypeThrowsForNullAuthCodeAndRefreshToken() { var cmd = new GetAuthTokenCommand(); string grantType = cmd.GrantType; }
public void EnsureKeysSortedByConvertParamsToString() { var request = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("scope", Scope.ReadUserPlayHistory.AsStringParam()), new KeyValuePair<string, string>("grant_type", GetAuthTokenCommand.GrantTypeAuthorizationCode) }; var cmd = new GetAuthTokenCommand(); var stringForm = cmd.ConvertParamsToString(request, false); Assert.AreEqual("grant_type=authorization_code&scope=read_userplayhistory", stringForm, "Expected params to be re-sorted alphabetically"); }