Skip to content

Commit

Permalink
set cookie on login, dont assume all tokens are the same header
Browse files Browse the repository at this point in the history
  • Loading branch information
devhl-labs committed Nov 29, 2023
1 parent 3ff3f15 commit 06ad019
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 231 deletions.
2 changes: 1 addition & 1 deletion docs/apis/DeveloperApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ No authorization required
### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | Successful response | - |
| **200** | Successful response | * Set-Cookie - <br> |
| **400** | Client provided incorrect parameters for the request. | - |
| **403** | Access denied, either because of missing/incorrect credentials or used API token does not grant access to the requested resource. | - |
| **404** | Resource was not found. | - |
Expand Down
10 changes: 7 additions & 3 deletions src/CocApi.Rest.Test/Apis/ApiTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ public ApiTestsBase(string[] args)
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureCocApi((context, services, options) =>
{
string apiKeyTokenValue = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken = new(apiKeyTokenValue, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);
string apiKeyTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, "JWT", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);

string apiKeyTokenValue2 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, "cookieAuth", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
});
}
}
78 changes: 43 additions & 35 deletions src/CocApi.Rest.Test/Apis/DependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,53 @@ namespace CocApi.Rest.Test.Apis
/// </summary>
public class DependencyInjectionTest
{
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureCocApi((context, services, options) =>
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder([]).ConfigureCocApi((context, services, options) =>
{
ApiKeyToken apiKeyToken = new ApiKeyToken($"<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);

ApiKeyToken apiKeyToken1 = new($"<token>", "JWT", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);

ApiKeyToken apiKeyToken2 = new($"<token>", "cookieAuth", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
})
.Build();

private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureCocApi((context, services, options) =>
Host.CreateDefaultBuilder([]).ConfigureCocApi((context, services, options) =>
{
ApiKeyToken apiKeyToken = new ApiKeyToken($"<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);

ApiKeyToken apiKeyToken1 = new($"<token>", "JWT", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);

ApiKeyToken apiKeyToken2 = new($"<token>", "cookieAuth", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
options.AddCocApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS));
})
.Build();

private readonly IHost _hostUsingAddWithoutAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureServices((host, services) =>
Host.CreateDefaultBuilder([]).ConfigureServices((host, services) =>
{
services.AddCocApi(options =>
{
ApiKeyToken apiKeyToken = new ApiKeyToken($"<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);

ApiKeyToken apiKeyToken1 = new($"<token>", "JWT", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);

ApiKeyToken apiKeyToken2 = new($"<token>", "cookieAuth", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
});
})
.Build();

private readonly IHost _hostUsingAddWithAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureServices((host, services) =>
Host.CreateDefaultBuilder([]).ConfigureServices((host, services) =>
{
services.AddCocApi(options =>
{
ApiKeyToken apiKeyToken = new ApiKeyToken($"<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);

ApiKeyToken apiKeyToken1 = new($"<token>", "JWT", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);

ApiKeyToken apiKeyToken2 = new($"<token>", "cookieAuth", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
options.AddCocApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS));
});
})
Expand All @@ -76,22 +84,22 @@ public void ConfigureApiWithAClientTest()
{
var clansApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IClansApi>();
Assert.True(clansApi.HttpClient.BaseAddress != null);

var developerApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IDeveloperApi>();
Assert.True(developerApi.HttpClient.BaseAddress != null);

var goldpassApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IGoldpassApi>();
Assert.True(goldpassApi.HttpClient.BaseAddress != null);

var labelsApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<ILabelsApi>();
Assert.True(labelsApi.HttpClient.BaseAddress != null);

var leaguesApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<ILeaguesApi>();
Assert.True(leaguesApi.HttpClient.BaseAddress != null);

var locationsApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<ILocationsApi>();
Assert.True(locationsApi.HttpClient.BaseAddress != null);

var playersApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IPlayersApi>();
Assert.True(playersApi.HttpClient.BaseAddress != null);
}
Expand All @@ -104,22 +112,22 @@ public void ConfigureApiWithoutAClientTest()
{
var clansApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IClansApi>();
Assert.True(clansApi.HttpClient.BaseAddress != null);

var developerApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IDeveloperApi>();
Assert.True(developerApi.HttpClient.BaseAddress != null);

var goldpassApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IGoldpassApi>();
Assert.True(goldpassApi.HttpClient.BaseAddress != null);

var labelsApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<ILabelsApi>();
Assert.True(labelsApi.HttpClient.BaseAddress != null);

var leaguesApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<ILeaguesApi>();
Assert.True(leaguesApi.HttpClient.BaseAddress != null);

var locationsApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<ILocationsApi>();
Assert.True(locationsApi.HttpClient.BaseAddress != null);

var playersApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IPlayersApi>();
Assert.True(playersApi.HttpClient.BaseAddress != null);
}
Expand Down Expand Up @@ -160,22 +168,22 @@ public void AddApiWithoutAClientTest()
{
var clansApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IClansApi>();
Assert.True(clansApi.HttpClient.BaseAddress != null);

var developerApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IDeveloperApi>();
Assert.True(developerApi.HttpClient.BaseAddress != null);

var goldpassApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IGoldpassApi>();
Assert.True(goldpassApi.HttpClient.BaseAddress != null);

var labelsApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<ILabelsApi>();
Assert.True(labelsApi.HttpClient.BaseAddress != null);

var leaguesApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<ILeaguesApi>();
Assert.True(leaguesApi.HttpClient.BaseAddress != null);

var locationsApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<ILocationsApi>();
Assert.True(locationsApi.HttpClient.BaseAddress != null);

var playersApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IPlayersApi>();
Assert.True(playersApi.HttpClient.BaseAddress != null);
}
Expand Down
12 changes: 6 additions & 6 deletions src/CocApi.Rest.Test/Models/KeyInstanceStatusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ public void CodeTest()
}

/// <summary>
/// Test the property 'Message'
/// Test the property 'Detail'
/// </summary>
[Fact]
public void MessageTest()
public void DetailTest()
{
// TODO unit test for the property 'Message'
// TODO unit test for the property 'Detail'
}

/// <summary>
/// Test the property 'Detail'
/// Test the property 'Message'
/// </summary>
[Fact]
public void DetailTest()
public void MessageTest()
{
// TODO unit test for the property 'Detail'
// TODO unit test for the property 'Message'
}
}
}
18 changes: 9 additions & 9 deletions src/CocApi.Rest.Test/Models/WarClanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,6 @@ public void DestructionPercentageTest()
// TODO unit test for the property 'DestructionPercentage'
}

/// <summary>
/// Test the property 'ExpEarned'
/// </summary>
[Fact]
public void ExpEarnedTest()
{
// TODO unit test for the property 'ExpEarned'
}

/// <summary>
/// Test the property 'Members'
/// </summary>
Expand Down Expand Up @@ -133,5 +124,14 @@ public void TagTest()
{
// TODO unit test for the property 'Tag'
}

/// <summary>
/// Test the property 'ExpEarned'
/// </summary>
[Fact]
public void ExpEarnedTest()
{
// TODO unit test for the property 'ExpEarned'
}
}
}
56 changes: 24 additions & 32 deletions src/CocApi.Rest/Apis/ClansApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,10 +1003,9 @@ public async Task<IFetchCapitalRaidSeasonsApiResponse> FetchCapitalRaidSeasonsAs
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -1480,10 +1479,9 @@ public async Task<IFetchClanApiResponse> FetchClanAsync(string clanTag, System.T
uriBuilderLocalVar.Path = uriBuilderLocalVar.Path.Replace("%7BclanTag%7D", Uri.EscapeDataString(clanTag.ToString()));

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -1996,10 +1994,9 @@ public async Task<IFetchClanMembersApiResponse> FetchClanMembersAsync(string cla
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -2486,10 +2483,9 @@ public async Task<IFetchClanWarLeagueGroupApiResponse> FetchClanWarLeagueGroupAs
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -2976,10 +2972,9 @@ public async Task<IFetchClanWarLeagueWarApiResponse> FetchClanWarLeagueWarAsync(
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -3492,10 +3487,9 @@ public async Task<IFetchClanWarLogApiResponse> FetchClanWarLogAsync(string clanT
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -3982,10 +3976,9 @@ public async Task<IFetchCurrentWarApiResponse> FetchCurrentWarAsync(string clanT
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down Expand Up @@ -4571,10 +4564,9 @@ public async Task<ISearchClansApiResponse> SearchClansAsync(Option<int> location
uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString();

List<TokenBase> tokenBaseLocalVars = new List<TokenBase>();
ApiKeyToken apiKeyTokenLocalVar;
apiKeyTokenLocalVar = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar);
apiKeyTokenLocalVar.UseInHeader(httpRequestMessageLocalVar, "authorization");
ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("authorization", cancellationToken).ConfigureAwait(false);
tokenBaseLocalVars.Add(apiKeyTokenLocalVar1);
apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar);

httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri;

Expand Down
Loading

0 comments on commit 06ad019

Please sign in to comment.