1+ using System . Net ;
2+ using System . Net . Http . Json ;
3+ using ManagedCode . Orleans . Identity . Tests . Cluster ;
4+ using ManagedCode . Orleans . Identity . Tests . Constants ;
5+ using Shouldly ;
6+ using Xunit ;
7+ using Xunit . Abstractions ;
8+
9+ namespace ManagedCode . Orleans . Identity . Tests . Cookies ;
10+
11+ [ Collection ( nameof ( TestClusterApplication ) ) ]
12+ public class CookieAuthTests ( TestClusterApplication testApp , ITestOutputHelper outputHelper )
13+ : IClassFixture < TestClusterApplication >
14+ {
15+ private readonly ITestOutputHelper _outputHelper = outputHelper ;
16+
17+ #region Cookie Authentication - Basic Tests
18+
19+ [ Fact ]
20+ public async Task CookieAuth_WhenUserAuthenticated_ShouldAccessProtectedEndpoint ( )
21+ {
22+ // Arrange
23+ var client = testApp . CreateClient ( ) ;
24+ await LoginWithCookie ( client , "testuser" ) ;
25+
26+ // Act
27+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_DEFAULT_ROUTE ) ;
28+ var content = await response . Content . ReadAsStringAsync ( ) ;
29+
30+ // Assert
31+ response . IsSuccessStatusCode . ShouldBeTrue ( ) ;
32+ content . ShouldContain ( "Hello, testuser!" ) ;
33+ }
34+
35+ [ Fact ]
36+ public async Task CookieAuth_WhenUserNotAuthenticated_ShouldReturnUnauthorized ( )
37+ {
38+ // Arrange
39+ var client = testApp . CreateClient ( ) ;
40+
41+ // Act
42+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_DEFAULT_ROUTE ) ;
43+
44+ // Assert
45+ response . StatusCode . ShouldBe ( HttpStatusCode . Unauthorized ) ;
46+ }
47+
48+ #endregion
49+
50+ #region Cookie Authentication - Role-based Tests
51+
52+ [ Fact ]
53+ public async Task CookieAuth_WhenUserIsAdmin_ShouldAccessAdminEndpoint ( )
54+ {
55+ // Arrange
56+ var client = testApp . CreateClient ( ) ;
57+ await LoginWithCookie ( client , "admin" ) ;
58+
59+ // Act
60+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_BAN ) ;
61+ var content = await response . Content . ReadAsStringAsync ( ) ;
62+
63+ // Assert
64+ response . IsSuccessStatusCode . ShouldBeTrue ( ) ;
65+ content . ShouldContain ( "User admin is banned" ) ;
66+ }
67+
68+ [ Fact ]
69+ public async Task CookieAuth_WhenUserIsNotAdmin_ShouldReturnForbidden ( )
70+ {
71+ // Arrange
72+ var client = testApp . CreateClient ( ) ;
73+ await LoginWithCookie ( client , "user" ) ;
74+
75+ // Act
76+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_BAN ) ;
77+
78+ // Assert
79+ response . StatusCode . ShouldBe ( HttpStatusCode . Forbidden ) ;
80+ }
81+
82+ [ Fact ]
83+ public async Task CookieAuth_WhenUserIsModerator_ShouldAccessModeratorEndpoint ( )
84+ {
85+ // Arrange
86+ var client = testApp . CreateClient ( ) ;
87+ await LoginWithCookie ( client , "moderator" ) ;
88+
89+ // Act
90+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_MODIFY ) ;
91+ var content = await response . Content . ReadAsStringAsync ( ) ;
92+
93+ // Assert
94+ response . IsSuccessStatusCode . ShouldBeTrue ( ) ;
95+ content . ShouldContain ( "User moderator has been modified" ) ;
96+ }
97+
98+ [ Fact ]
99+ public async Task CookieAuth_WhenUserIsNotModerator_ShouldReturnForbidden ( )
100+ {
101+ // Arrange
102+ var client = testApp . CreateClient ( ) ;
103+ await LoginWithCookie ( client , "user" ) ;
104+
105+ // Act
106+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_MODIFY ) ;
107+
108+ // Assert
109+ response . StatusCode . ShouldBe ( HttpStatusCode . Forbidden ) ;
110+ }
111+
112+ #endregion
113+
114+ #region Cookie Authentication - Public Endpoint Tests
115+
116+ [ Fact ]
117+ public async Task CookieAuth_WhenUserNotAuthenticated_ShouldAccessPublicEndpoint ( )
118+ {
119+ // Arrange
120+ var client = testApp . CreateClient ( ) ;
121+
122+ // Act
123+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_PUBLIC_INFO ) ;
124+ var content = await response . Content . ReadAsStringAsync ( ) ;
125+
126+ // Assert
127+ response . IsSuccessStatusCode . ShouldBeTrue ( ) ;
128+ content . ShouldContain ( "This is public information" ) ;
129+ }
130+
131+ #endregion
132+
133+ #region Cookie Authentication - Logout Tests
134+
135+ [ Fact ]
136+ public async Task CookieAuth_WhenUserLogsOut_ShouldNotAccessProtectedEndpoint ( )
137+ {
138+ // Arrange
139+ var client = testApp . CreateClient ( ) ;
140+ await LoginWithCookie ( client , "testuser" ) ;
141+
142+ // Verify user is authenticated
143+ var authResponse = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_DEFAULT_ROUTE ) ;
144+ authResponse . IsSuccessStatusCode . ShouldBeTrue ( ) ;
145+
146+ // Act - Logout
147+ var logoutResponse = await client . PostAsync ( "/auth/logout" , null ) ;
148+ logoutResponse . IsSuccessStatusCode . ShouldBeTrue ( ) ;
149+
150+ // Act - Try to access protected endpoint
151+ var response = await client . GetAsync ( TestControllerRoutes . USER_CONTROLLER_DEFAULT_ROUTE ) ;
152+
153+ // Assert
154+ response . StatusCode . ShouldBe ( HttpStatusCode . Unauthorized ) ;
155+ }
156+
157+ #endregion
158+
159+ #region Helper Methods
160+
161+ private async Task LoginWithCookie ( HttpClient client , string username )
162+ {
163+ var loginRequest = new LoginRequest { Username = username } ;
164+ var response = await client . PostAsJsonAsync ( "/auth/login-cookie" , loginRequest ) ;
165+
166+ response . IsSuccessStatusCode . ShouldBeTrue ( ) ;
167+ }
168+
169+ #endregion
170+ }
171+
172+ public class LoginRequest
173+ {
174+ public string Username { get ; set ; } = string . Empty ;
175+ }
0 commit comments