-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from okta/lr-fix-638-null-response-after-rate…
…-limit Fix "Null response after hitting rate limit" issue
- Loading branch information
Showing
8 changed files
with
206 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Okta.Sdk.Api; | ||
using Okta.Sdk.Client; | ||
using Okta.Sdk.Model; | ||
using Okta.Sdk.UnitTest.Internal; | ||
using WireMock.RequestBuilders; | ||
using WireMock.ResponseBuilders; | ||
using WireMock.Server; | ||
using Xunit; | ||
|
||
namespace Okta.Sdk.UnitTest.Api | ||
{ | ||
public class UsersApiTests | ||
{ | ||
private readonly WireMockServer _server; | ||
|
||
|
||
public UsersApiTests() | ||
{ | ||
_server = WireMockServer.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_server.Stop(); | ||
} | ||
|
||
private void CreateStubReturningRateLimitResponse() | ||
{ | ||
var dateHeader = new DateTimeOffset(DateTime.Now); | ||
var resetTime = dateHeader.AddSeconds(1).ToUnixTimeSeconds(); | ||
|
||
_server.Given( | ||
Request.Create().WithPath("/api/v1/users/foo") | ||
).AtPriority(1) | ||
.RespondWith( | ||
Response.Create() | ||
.WithStatusCode(429) | ||
.WithHeader("Content-Type", "text/json") | ||
.WithHeader("Date", dateHeader.ToString()) | ||
.WithHeader("x-rate-limit-reset", resetTime.ToString()) | ||
.WithHeader(DefaultRetryStrategy.XOktaRequestId, "foo") | ||
.WithBody(@"{ | ||
""errorCode"": ""E0000047"", | ||
""errorSummary"": ""API call exceeded rate limit due to too many requests."", | ||
""errorLink"": ""E0000047"", | ||
""errorId"": ""sample0eww_TKcLhu8FoK7qM4"", | ||
""errorCauses"": [] | ||
}") | ||
|
||
); | ||
} | ||
|
||
private void CreateStubReturningOAuthTokenResponse() | ||
{ | ||
var tokenResponse = | ||
@"{""access_token"" : ""eyJhbGciOiJSUzI1NiJ9.eyJ2ZXIiOjEsImlzcyI6Imh0dHA6Ly9yYWluLm9rdGExLmNvbToxODAyIiwiaWF0IjoxNDQ5NjI0MDI2LCJleHAiOjE0NDk2Mjc2MjYsImp0aSI6IlVmU0lURzZCVVNfdHA3N21BTjJxIiwic2NvcGVzIjpbIm9wZW5pZCIsImVtYWlsIl0sImNsaWVudF9pZCI6InVBYXVub2ZXa2FESnh1a0NGZUJ4IiwidXNlcl9pZCI6IjAwdWlkNEJ4WHc2STZUVjRtMGczIn0.HaBu5oQxdVCIvea88HPgr2O5evqZlCT4UXH4UKhJnZ5px-ArNRqwhxXWhHJisslswjPpMkx1IgrudQIjzGYbtLFjrrg2ueiU5-YfmKuJuD6O2yPWGTsV7X6i7ABT6P-t8PRz_RNbk-U1GXWIEkNnEWbPqYDAm_Ofh7iW0Y8WDA5ez1jbtMvd-oXMvJLctRiACrTMLJQ2e5HkbUFxgXQ_rFPNHJbNSUBDLqdi2rg_ND64DLRlXRY7hupNsvWGo0gF4WEUk8IZeaLjKw8UoIs-ETEwJlAMcvkhoVVOsN5dPAaEKvbyvPC1hUGXb4uuThlwdD3ECJrtwgKqLqcWonNtiw"", | ||
""token_type"" : ""Bearer"", | ||
""expires_in"" : 3600, | ||
""scope"" : ""openid email"", | ||
""refresh_token"" : ""a9VpZDRCeFh3Nkk2VdY"", | ||
""id_token"" : ""eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIwMHVpZDRCeFh3Nkk2VFY0bTBnMyIsImVtYWlsIjoid2VibWFzdGVyQGNsb3VkaXR1ZG | ||
UubmV0IiwiZW1haWxfdmVyaWZpZWQiOnRydWUsInZlciI6MSwiaXNzIjoiaHR0cDovL3JhaW4ub2t0YTEuY29tOjE4MDIiLCJsb | ||
2dpbiI6ImFkbWluaXN0cmF0b3IxQGNsb3VkaXR1ZGUubmV0IiwiYXVkIjoidUFhdW5vZldrYURKeHVrQ0ZlQngiLCJpYXQiOjE0 | ||
NDk2MjQwMjYsImV4cCI6MTQ0OTYyNzYyNiwiYW1yIjpbInB3ZCJdLCJqdGkiOiI0ZUFXSk9DTUIzU1g4WGV3RGZWUiIsImF1dGh | ||
fdGltZSI6MTQ0OTYyNDAyNiwiYXRfaGFzaCI6ImNwcUtmZFFBNWVIODkxRmY1b0pyX1EifQ.Btw6bUbZhRa89DsBb8KmL9rfhku | ||
--_mbNC2pgC8yu8obJnwO12nFBepui9KzbpJhGM91PqJwi_AylE6rp-ehamfnUAO4JL14PkemF45Pn3u_6KKwxJnxcWxLvMuuis | ||
nvIs7NScKpOAab6ayZU0VL8W6XAijQmnYTtMWQfSuaaR8rYOaWHrffh3OypvDdrQuYacbkT0csxdrayXfBG3UF5-ZAlhfch1fhF | ||
T3yZFdWwzkSDc0BGygfiFyNhCezfyT454wbciSZgrA9ROeHkfPCaX7KCFO8GgQEkGRoQntFBNjluFhNLJIUkEFovEDlfuB4tv_M | ||
8BM75celdy3jkpOurg"" | ||
}"; | ||
_server.Given( | ||
Request.Create().WithPath("/oauth2/v1/token*") | ||
).AtPriority(1) | ||
.RespondWith( | ||
Response.Create() | ||
.WithStatusCode(200) | ||
.WithHeader("Content-Type", "text/json") | ||
.WithBody(tokenResponse.Replace("\r|\n", string.Empty)) | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task PartialUpdateUserAsyncThrowOnRateLimitErrorAfterMaxWithApiToken() | ||
{ | ||
var configuration = new Configuration | ||
{ | ||
OktaDomain = _server.Url!, | ||
Token = "foo", | ||
DisableOktaDomainCheck = true, | ||
MaxRetries = 1, | ||
|
||
}; | ||
|
||
var userApi = new UserApi(configuration); | ||
|
||
CreateStubReturningRateLimitResponse(); | ||
|
||
var exception = await Assert.ThrowsAsync<ApiException>(async () => await userApi.PartialUpdateUserAsync("foo", | ||
new UpdateUserRequest | ||
{ | ||
Profile = new UserProfile | ||
{ | ||
Email = "[email protected]", | ||
} | ||
})); | ||
|
||
exception.Should().NotBeNull(); | ||
exception.ErrorCode.Should().Be(429); | ||
} | ||
|
||
[Fact] | ||
public async Task PartialUpdateUserAsyncThrowOnRateLimitErrorAfterMaxRetriesWithOAuthToken() | ||
{ | ||
var jsonPrivateKey = @"{ | ||
""p"":""2-8pgwYv9jrkM2KsbnQmnJZnr69Rsj95M20I1zx5HhM3tgjGSa7d_dELPRkp9Usy8UGISt7eUHpYOVl529irHwbXevuId1Q804aQ_AtNJwpbRY48rw2T8LdtyVSaEyoFMCa8PJwtzZYzKJCKAe5eoXvW5zxB65RaIct0igYcoIs"", | ||
""kty"":""RSA"", | ||
""q"":""slkNUY_SCIn95ip7HoPs_IIiNoKOGeesIV1gacyoAycly1vBPMhtar9gwx51nN1tCMVGlSOk583eRJe2omAbqkIEYm1jSWtMdJKQSOJvx812xbF1afMgJDlJ6iRIlcnWEYhNNMCK5s_UR5zE0Mc5jktxDFeiEatriyu6o9hQix8"", | ||
""d"":""LIpJTKCi9hPTiuUU954hayd3lXNwTVS6Fdny2iUj6iZ22eRp1V_UswECuMy5B-8lWbp1Gu_eASvhElSCB26m3UgHRVy8LP6Lmvm9VlJuZ5NtOK5D0R-gzFLINGdQH1PehzEc44js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H6LGffYCth157w-K4AZwXw74Ybo5NOux3XpIpKRbFTwvBLp1Q"" | ||
}"; | ||
|
||
var configuration = new Configuration(); | ||
configuration.Scopes = new HashSet<string> { "okta.user.read" }; | ||
configuration.ClientId = "foo"; | ||
configuration.PrivateKey = new JsonWebKeyConfiguration(jsonPrivateKey); | ||
configuration.AuthorizationMode = AuthorizationMode.PrivateKey; | ||
configuration.OktaDomain = _server.Url!; | ||
configuration.MaxRetries = 1; | ||
configuration.DisableOktaDomainCheck = true; | ||
|
||
var userApi = new UserApi(configuration); | ||
|
||
CreateStubReturningOAuthTokenResponse(); | ||
CreateStubReturningRateLimitResponse(); | ||
|
||
var exception = await Assert.ThrowsAsync<ApiException>(async () => await userApi.PartialUpdateUserAsync("foo", | ||
new UpdateUserRequest | ||
{ | ||
Profile = new UserProfile | ||
{ | ||
Email = "[email protected]", | ||
} | ||
})); | ||
|
||
exception.Should().NotBeNull(); | ||
exception.ErrorCode.Should().Be(429); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters