1+ using FluentAssertions ;
2+ using Microsoft . AspNetCore . Mvc . Testing ;
3+ using System . Net ;
4+ using System . Net . Http . Json ;
5+
6+ namespace CodeBeam . UltimateAuth . Tests . Integration ;
7+
8+ public class LoginTests : IClassFixture < AuthServerFactory >
9+ {
10+ private readonly HttpClient _client ;
11+
12+ public LoginTests ( AuthServerFactory factory )
13+ {
14+ _client = factory . CreateClient ( new WebApplicationFactoryClientOptions
15+ {
16+ AllowAutoRedirect = false ,
17+ HandleCookies = false
18+ } ) ;
19+
20+ _client . DefaultRequestHeaders . Add ( "Origin" , "https://localhost:6130" ) ;
21+ _client . DefaultRequestHeaders . Add ( "X-UDID" , "test-device-1234567890123456" ) ;
22+ }
23+
24+ [ Fact ]
25+ public async Task Login_Should_Return_Cookie ( )
26+ {
27+ var response = await _client . PostAsJsonAsync ( "/auth/login" , new
28+ {
29+ identifier = "admin" ,
30+ secret = "admin"
31+ } ) ;
32+
33+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . Found ) ;
34+ response . Headers . Location . Should ( ) . NotBeNull ( ) ;
35+ response . Headers . TryGetValues ( "Set-Cookie" , out var cookies ) . Should ( ) . BeTrue ( ) ;
36+ cookies . Should ( ) . NotBeNull ( ) ;
37+ }
38+
39+ [ Fact ]
40+ public async Task Session_Lifecycle_Should_Work_Correctly ( )
41+ {
42+ var loginResponse1 = await _client . PostAsJsonAsync ( "/auth/login" , new
43+ {
44+ identifier = "admin" ,
45+ secret = "admin"
46+ } ) ;
47+
48+ loginResponse1 . StatusCode . Should ( ) . Be ( HttpStatusCode . Found ) ;
49+
50+ var cookie1 = loginResponse1 . Headers . GetValues ( "Set-Cookie" ) . FirstOrDefault ( ) ;
51+ cookie1 . Should ( ) . NotBeNull ( ) ;
52+
53+ _client . DefaultRequestHeaders . Add ( "Cookie" , cookie1 ! ) ;
54+
55+ var logoutResponse = await _client . PostAsync ( "/auth/logout" , null ) ;
56+ logoutResponse . StatusCode . Should ( ) . Be ( HttpStatusCode . Found ) ;
57+
58+ var logoutAgain = await _client . PostAsync ( "/auth/logout" , null ) ;
59+ logoutAgain . StatusCode . Should ( ) . BeOneOf ( HttpStatusCode . Unauthorized , HttpStatusCode . Found ) ;
60+
61+ _client . DefaultRequestHeaders . Remove ( "Cookie" ) ;
62+
63+ var loginResponse2 = await _client . PostAsJsonAsync ( "/auth/login" , new
64+ {
65+ identifier = "admin" ,
66+ secret = "admin"
67+ } ) ;
68+
69+ loginResponse2 . StatusCode . Should ( ) . Be ( HttpStatusCode . Found ) ;
70+ var cookie2 = loginResponse2 . Headers . GetValues ( "Set-Cookie" ) . FirstOrDefault ( ) ;
71+ cookie2 . Should ( ) . NotBeNull ( ) ;
72+ cookie2 . Should ( ) . NotBe ( cookie1 ) ;
73+ }
74+
75+ [ Fact ]
76+ public async Task Authenticated_User_Should_Access_Me_Endpoint ( )
77+ {
78+ var loginResponse = await _client . PostAsJsonAsync ( "/auth/login" , new
79+ {
80+ identifier = "admin" ,
81+ secret = "admin"
82+ } ) ;
83+
84+ var cookie = loginResponse . Headers . GetValues ( "Set-Cookie" ) . First ( ) ;
85+ _client . DefaultRequestHeaders . Add ( "Cookie" , cookie ) ;
86+ var response = await _client . PostAsync ( "/auth/me/get" , null ) ;
87+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
88+ }
89+
90+ [ Fact ]
91+ public async Task Anonymous_Should_Not_Access_Me ( )
92+ {
93+ var response = await _client . PostAsync ( "/auth/me/get" , null ) ;
94+ response . StatusCode . Should ( ) . Be ( HttpStatusCode . Unauthorized ) ;
95+ }
96+ }
0 commit comments