Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add WechatTests
  • Loading branch information
saber-wang committed May 20, 2019
commit 7891b2540b58d9a17b869919f633bfb9f8a44635
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<ItemGroup>
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.*\*.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="Wechat\bundle.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="$(AspNetCoreVersion)" />
<PackageReference Include="JustEat.HttpClientInterception" Version="$(JustEatHttpClientInterceptionVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
string state = queryString["state"];

queryString.Clear();

var builder = new UriBuilder(location);
// Retain the parameters of redirect_uri
queryString = HttpUtility.ParseQueryString(builder.Query);
queryString.Add("code", "a6ed8e7f-471f-44f1-903b-65946475f351");
queryString.Add("state", state);

var builder = new UriBuilder(location)
{
Query = queryString.ToString(),
};
builder.Query = queryString.ToString();

var redirectRequest = new HttpRequestMessage(request.Method, builder.Uri);

Expand Down
57 changes: 57 additions & 0 deletions test/AspNet.Security.OAuth.Providers.Tests/Wechat/WechatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
* for more information concerning the license and the contributors participating to this project.
*/

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;

namespace AspNet.Security.OAuth.Weixin
{
public class WechatTests : OAuthTests<WeixinAuthenticationOptions>
{
public WechatTests(ITestOutputHelper outputHelper)
{
OutputHelper = outputHelper;
}

public override string DefaultScheme => WeixinAuthenticationDefaults.AuthenticationScheme;

protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
{
builder.AddWeixin(options =>
{
ConfigureDefaults(builder, options);
options.AuthorizationEndpoint = "https://open.weixin.qq.com/connect/oauth2/authorize";
});
}

[Theory]
[InlineData(ClaimTypes.NameIdentifier, "my-id")]
[InlineData(ClaimTypes.Name, "John Smith")]
[InlineData(ClaimTypes.Gender, "Male")]
[InlineData(ClaimTypes.Country, "CN")]
[InlineData("urn:weixin:city", "Beijing")]
[InlineData("urn:weixin:headimgurl", "https://weixin.qq.local/image.png")]
[InlineData("urn:weixin:openid", "my-open-id")]
[InlineData("urn:weixin:privilege", "a,b,c")]
[InlineData("urn:weixin:province", "Hebei")]
public async Task Can_Sign_In_Using_Wechat(string claimType, string claimValue)
{
// Arrange
using (var server = CreateTestServer())
{
// Act
var claims = await AuthenticateUserAsync(server);

// Assert
AssertClaim(claims, claimType, claimValue);
}
}
}
}
35 changes: 35 additions & 0 deletions test/AspNet.Security.OAuth.Providers.Tests/Wechat/bundle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "https://raw.githubusercontent.com/justeat/httpclient-interception/master/src/HttpClientInterception/Bundles/http-request-bundle-schema.json",
"items": [
{
"uri": "https://api.weixin.qq.com/sns/oauth2/access_token?appid=my-client-id&secret=my-client-secret&code=a6ed8e7f-471f-44f1-903b-65946475f351&grant_type=authorization_code",
"contentFormat": "json",
"contentJson": {
"access_token": "secret-access-token",
"token_type": "access",
"refresh_token": "secret-refresh-token",
"expires_in": "300",
"openid": "my-open-id"
}
},
{
"uri": "https://api.weixin.qq.com/sns/userinfo?access_token=secret-access-token&openid=my-open-id",
"contentFormat": "json",
"contentJson": {
"unionid": "my-id",
"nickname": "John Smith",
"sex": "Male",
"country": "CN",
"openid": "my-open-id",
"province": "Hebei",
"city": "Beijing",
"headimgurl": "https://weixin.qq.local/image.png",
"privilege": [
"a",
"b",
"c"
]
}
}
]
}