Skip to content

Commit 8ecc592

Browse files
committed
test(EtsyProvider): Update tests accordingly to review suggestions and code changes in Etsy Provider
1 parent 0596516 commit 8ecc592

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

test/AspNet.Security.OAuth.Providers.Tests/Etsy/EtsyAuthenticationOptionsTests.cs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,89 +24,83 @@ public static void Validate_Does_Not_Throw_If_ClientSecret_Is_Not_Provided(strin
2424
options.Validate();
2525
}
2626

27-
[Theory]
28-
[InlineData(true, false)]
29-
[InlineData(false, false)]
30-
[InlineData(false, true)]
31-
public static void Validate_Throws_If_SaveTokens_Or_Pkce_Is_Disabled(bool usePkce, bool saveTokens)
27+
[Fact]
28+
public static void Validate_Does_Throw_If_Scope_Does_Not_Contain_Scope_shop_r()
3229
{
3330
// Arrange
3431
var options = new EtsyAuthenticationOptions()
3532
{
3633
ClientId = "my-client-id",
3734
ClientSecret = "my-client-secret",
38-
SaveTokens = saveTokens,
39-
UsePkce = usePkce,
4035
};
36+
options.Scope.Clear();
37+
options.Scope.Add(EtsyAuthenticationConstants.Scopes.EmailRead);
4138

42-
// Act and Assert
43-
_ = Assert.Throws<ArgumentException>(options.Validate);
39+
// Act
40+
_ = Assert.Throws<ArgumentOutOfRangeException>(options.Validate);
4441
}
4542

4643
[Fact]
47-
public static void Validate_Throws_If_Scope_Is_Empty()
44+
public static void Validate_Does_Not_Throw_When_IncludeDetailedUserInfo_Is_False_And_Contains_Scope_email_r()
4845
{
4946
// Arrange
5047
var options = new EtsyAuthenticationOptions()
5148
{
5249
ClientId = "my-client-id",
5350
ClientSecret = "my-client-secret",
54-
Scope = { },
51+
IncludeDetailedUserInfo = false,
5552
};
5653

57-
// Act and Assert
58-
_ = Assert.Throws<ArgumentOutOfRangeException>(options.Validate);
54+
// Adding email scope should be harmless when IncludeDetailedUserInfo is false
55+
options.Scope.Add(EtsyAuthenticationConstants.Scopes.EmailRead);
56+
57+
// Act (no Assert)
58+
options.Validate();
5959
}
6060

6161
[Fact]
62-
public static void Validate_Throws_If_Scope_Does_Not_Contain_Scope_shop_r()
62+
public static void Validate_Throws_If_AuthorizationEndpoint_Is_Null()
6363
{
6464
// Arrange
6565
var options = new EtsyAuthenticationOptions()
6666
{
67+
AuthorizationEndpoint = null!,
6768
ClientId = "my-client-id",
6869
ClientSecret = "my-client-secret",
6970
};
70-
options.Scope.Clear();
71-
options.Scope.Add(ClaimTypes.Email);
7271

7372
// Act and Assert
74-
_ = Assert.Throws<ArgumentOutOfRangeException>(options.Validate);
73+
_ = Assert.Throws<ArgumentNullException>(nameof(options.AuthorizationEndpoint), options.Validate);
7574
}
7675

7776
[Fact]
78-
public static void Validate_Throws_If_IncludeDetailedUserInfo_Is_True_But_Does_Not_Contain_Scope_email_r()
77+
public static void Validate_Throws_If_TokenEndpoint_Is_Null()
7978
{
8079
// Arrange
8180
var options = new EtsyAuthenticationOptions()
8281
{
8382
ClientId = "my-client-id",
8483
ClientSecret = "my-client-secret",
85-
IncludeDetailedUserInfo = true,
84+
TokenEndpoint = null!,
8685
};
8786

88-
// Not Adding email scope, shop scope is already added by default
89-
9087
// Act and Assert
91-
_ = Assert.Throws<ArgumentOutOfRangeException>(options.Validate);
88+
_ = Assert.Throws<ArgumentNullException>(nameof(options.TokenEndpoint), options.Validate);
9289
}
9390

9491
[Fact]
95-
public static void Validate_Does_Not_Throw_When_IncludeDetailedUserInfo_Is_False_And_Contains_Scope_email_r()
92+
public static void Validate_Throws_If_UserInformationEndpoint_Is_Null()
9693
{
9794
// Arrange
9895
var options = new EtsyAuthenticationOptions()
9996
{
10097
ClientId = "my-client-id",
10198
ClientSecret = "my-client-secret",
102-
IncludeDetailedUserInfo = false,
99+
TokenEndpoint = null!,
103100
};
104101

105-
// Adding email scope
106-
options.Scope.Add(ClaimTypes.Email);
107-
108-
// Act (no Assert)
109-
options.Validate();
102+
// Act and Assert
103+
_ = Assert.Throws<ArgumentNullException>(nameof(options.UserInformationEndpoint), options.Validate);
110104
}
111105

112106
[Fact]
@@ -121,6 +115,7 @@ public static void Validate_Throws_If_CallbackPath_Is_Null()
121115
};
122116

123117
// Act and Assert
124-
_ = Assert.Throws<ArgumentNullException>(nameof(EtsyAuthenticationOptions.CallbackPath), options.Validate);
118+
var ex = Assert.Throws<ArgumentException>(options.Validate);
119+
ex.ParamName.ShouldBe(nameof(options.CallbackPath));
125120
}
126121
}

test/AspNet.Security.OAuth.Providers.Tests/Etsy/EtsyTests.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
using AspNet.Security.OAuth.Etsy;
8+
using static AspNet.Security.OAuth.Etsy.EtsyAuthenticationConstants;
89

910
namespace AspNet.Security.OAuth.Providers.Tests.Etsy;
1011

@@ -23,16 +24,8 @@ protected internal override void RegisterAuthentication(AuthenticationBuilder bu
2324
}
2425

2526
[Theory]
26-
[InlineData(ClaimTypes.NameIdentifier, "789012")]
27-
[InlineData(ClaimTypes.Email, "test@example.com")]
28-
[InlineData(ClaimTypes.GivenName, "Test")]
29-
[InlineData(ClaimTypes.Surname, "User")]
30-
[InlineData("urn:etsy:user_id", "123456")]
31-
[InlineData("urn:etsy:shop_id", "789012")]
32-
[InlineData("urn:etsy:primary_email", "test@example.com")]
33-
[InlineData("urn:etsy:first_name", "Test")]
34-
[InlineData("urn:etsy:last_name", "User")]
35-
[InlineData("urn:etsy:image_url", "https://i.etsystatic.com/test/test_75x75.jpg")]
27+
[InlineData(ClaimTypes.NameIdentifier, "123456")]
28+
[InlineData("shop_id", "789012")]
3629
public async Task Can_Sign_In_Using_Etsy(string claimType, string claimValue)
3730
=> await AuthenticateUserAndAssertClaimValue(claimType, claimValue);
3831

@@ -48,24 +41,28 @@ public async Task Does_Not_Include_Detailed_Claims_When_IncludeDetailedUserInfo_
4841
var claims = await AuthenticateUserAsync(server);
4942

5043
// Assert basic claims are present
51-
claims.ShouldContainKey("urn:etsy:user_id");
52-
claims.ShouldContainKey("urn:etsy:shop_id");
44+
claims.ShouldContainKey(ClaimTypes.NameIdentifier);
45+
claims.ShouldContainKey(Claims.ShopId);
5346

5447
// Detailed claims should be absent when flag is false
5548
claims.Keys.ShouldNotContain(ClaimTypes.Email);
5649
claims.Keys.ShouldNotContain(ClaimTypes.GivenName);
5750
claims.Keys.ShouldNotContain(ClaimTypes.Surname);
58-
claims.Keys.ShouldNotContain("urn:etsy:primary_email");
59-
claims.Keys.ShouldNotContain("urn:etsy:first_name");
60-
claims.Keys.ShouldNotContain("urn:etsy:last_name");
61-
claims.Keys.ShouldNotContain("urn:etsy:image_url");
51+
claims.Keys.ShouldNotContain(Claims.ImageUrl);
6252
}
6353

6454
[Fact]
6555
public async Task Includes_Detailed_Claims_When_IncludeDetailedUserInfo_Is_True()
6656
{
67-
// Arrange: explicitly enable detailed user info enrichment (default may already be true, set explicitly for clarity)
68-
void ConfigureServices(IServiceCollection services) => services.PostConfigureAll<EtsyAuthenticationOptions>(o => o.IncludeDetailedUserInfo = true);
57+
// Arrange: enable detailed user info, configure claims to map.
58+
// Note: email_r will be auto-added by the provider's post-configure step.
59+
void ConfigureServices(IServiceCollection services) => services.PostConfigureAll<EtsyAuthenticationOptions>(o =>
60+
{
61+
o.IncludeDetailedUserInfo = true;
62+
63+
// User to include image claim
64+
o.ClaimActions.MapImageClaim();
65+
});
6966

7067
using var server = CreateTestServer(ConfigureServices);
7168

@@ -76,9 +73,6 @@ public async Task Includes_Detailed_Claims_When_IncludeDetailedUserInfo_Is_True(
7673
claims.ShouldContainKey(ClaimTypes.Email);
7774
claims.ShouldContainKey(ClaimTypes.GivenName);
7875
claims.ShouldContainKey(ClaimTypes.Surname);
79-
claims.ShouldContainKey("urn:etsy:primary_email");
80-
claims.ShouldContainKey("urn:etsy:first_name");
81-
claims.ShouldContainKey("urn:etsy:last_name");
82-
claims.ShouldContainKey("urn:etsy:image_url");
76+
claims.ShouldContainKey(Claims.ImageUrl);
8377
}
8478
}

0 commit comments

Comments
 (0)