11package dev .findfirst .security .oauth2 ;
22
3+ import static org .junit .Assert .assertTrue ;
34import static org .junit .jupiter .api .Assertions .assertEquals ;
4- import static org .junit .jupiter .api .Assertions .assertNotNull ;
55import static org .mockito .Mockito .mock ;
66import static org .mockito .Mockito .when ;
77
8+ import java .util .Collection ;
9+ import java .util .HashMap ;
810import java .util .List ;
11+ import java .util .Map ;
912
13+ import dev .findfirst .security .jwt .JwtService ;
14+ import dev .findfirst .security .jwt .service .RefreshTokenService ;
15+ import dev .findfirst .security .jwt .service .TokenService ;
16+ import dev .findfirst .security .oauth2client .handlers .Oauth2LoginSuccessHandler ;
17+ import dev .findfirst .users .repository .UserRepo ;
18+
19+ import lombok .AllArgsConstructor ;
20+ import lombok .Getter ;
1021import org .junit .jupiter .api .DisplayName ;
1122import org .junit .jupiter .api .Test ;
1223import org .junit .jupiter .api .extension .ExtendWith ;
1526import org .mockito .junit .jupiter .MockitoExtension ;
1627import org .springframework .mock .web .MockHttpServletRequest ;
1728import org .springframework .mock .web .MockHttpServletResponse ;
29+ import org .springframework .security .core .GrantedAuthority ;
30+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
1831import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
32+ import org .springframework .security .oauth2 .core .user .DefaultOAuth2User ;
1933import org .springframework .security .oauth2 .core .user .OAuth2User ;
20-
21- import dev .findfirst .security .jwt .JwtService ;
22- import dev .findfirst .security .jwt .service .RefreshTokenService ;
23- import dev .findfirst .security .jwt .service .TokenService ;
24- import dev .findfirst .security .oauth2client .handlers .Oauth2LoginSuccessHandler ;
25- import dev .findfirst .security .userauth .models .RefreshToken ;
26- import dev .findfirst .users .model .user .User ;
27- import dev .findfirst .users .repository .UserRepo ;
28- import jakarta .servlet .http .Cookie ;
29- import lombok .AllArgsConstructor ;
30- import lombok .Getter ;
34+ import org .springframework .test .context .TestPropertySource ;
3135
3236/**
33- * Tests borrowed from:
37+ * _Found using github search._ Test borrowed from:
3438 * https://github.com/vadof/vplay-backend/blob/15e355f9f2283feb389e55e75aa4f620b62becea/user-service/src/test/java/com/vcasino/user/oauth2/OAuth2LoginSuccessHandlerTests.java
3539 */
3640@ ExtendWith (MockitoExtension .class )
3741class Oauth2LoginSuccessHandlerTest {
3842
39- private final int ID = 1111111111 ;
40- private final String EMAIL = "johndoe@gmail.com" ;
41- private final String NAME = "John Doe" ;
43+ private final int ID = 1111111111 ;
4244 private final String USERNAME = "johndoe" ;
4345
4446 @ Mock
@@ -56,74 +58,38 @@ class Oauth2LoginSuccessHandlerTest {
5658 @ InjectMocks
5759 private Oauth2LoginSuccessHandler oAuthHandler ;
5860
59- @ Test
61+ @ Test
6062 @ DisplayName ("Authenticate active Github user" )
6163 void authenticateGithubUser () throws Exception {
64+ oAuthHandler .setRedirectURL ("localhost" );
6265 authenticateUserByProvider ("github" );
6366 }
6467
6568
66- private User getUserMock (String provider , boolean active ) {
67- return new User (USERNAME , EMAIL , null , true );
68- }
69-
7069 private void authenticateUserByProvider (String provider ) throws Exception {
71- OAuth2AuthenticationToken oAuthToken = mockAuthentication (provider .toString ().toLowerCase (), ID ).getFirst ();
72-
73- User user = getUserMock (provider , true );
74-
75- Entry <List <Cookie >, String > cookiesAndUrl = mockAuthenticationCookies (user );
70+ OAuth2AuthenticationToken oAuthToken =
71+ mockAuthentication (provider .toString ().toLowerCase (), ID ).getFirst ();
7672
7773 MockHttpServletResponse response = new MockHttpServletResponse ();
7874 oAuthHandler .onAuthenticationSuccess (new MockHttpServletRequest (), response , oAuthToken );
7975
80- checkCookies (cookiesAndUrl .getFirst (), response );
81-
82- assertEquals (cookiesAndUrl .getSecond (), response .getRedirectedUrl ());
76+ assertTrue (response .getHeader ("Set-Cookie" ) != null );
77+ assertEquals ("localhost/account/login/oauth2" , response .getRedirectedUrl ());
8378 }
8479
85- private Entry <List <Cookie >, String > mockAuthenticationCookies (User user ) {
86- String jwtToken = "AAA-BBB-CCC" ;
87- var refreshToken = new RefreshToken (1l , null , "fasfsaf-asfsf" , null );
88-
89- when (ts .generateTokenFromUser (ID )).thenReturn (jwtToken );
90-
91- when (rs .createRefreshToken (user )).thenReturn (refreshToken );
92-
93- Cookie jwtCookie = new Cookie ("jwt" , "AAA-BBB-CCC" );
94- Cookie refreshCookie = new Cookie ("refresh" , refreshToken .getToken ());
95-
96- String expectedUrl = "%s/login/success?name=%s&username=%s&email=%s"
97- .formatted ("localhost" , user .getUsername (), user .getUsername (), user .getEmail ());
98-
99- return new Entry <>(List .of (jwtCookie , refreshCookie ), expectedUrl );
100- }
101-
102- private Entry <OAuth2AuthenticationToken , OAuth2User > mockAuthentication (String provider , int id , String email ) {
103- var entry = mockAuthentication (provider , id );
104- when (entry .getFirst ().getPrincipal ()).thenReturn (entry .getSecond ());
105- when (entry .getSecond ().getAttribute ("email" )).thenReturn (email );
106- return entry ;
107- }
10880
10981 private Entry <OAuth2AuthenticationToken , OAuth2User > mockAuthentication (String provider , int id ) {
11082 OAuth2AuthenticationToken oauthToken = mock (OAuth2AuthenticationToken .class );
11183 OAuth2User principal = mock (OAuth2User .class );
112- when (oauthToken .getAuthorizedClientRegistrationId ()).thenReturn (provider );
113- when (oauthToken .getName ()).thenReturn (id + "" );
84+ Map <String , Object > attributes = new HashMap <>();
85+ attributes .put ("name" , USERNAME );
86+ attributes .put ("userID" , ID );
87+ Collection <GrantedAuthority > authorities = List .of (new SimpleGrantedAuthority ("ROLE_USER" ));
88+ OAuth2User user = new DefaultOAuth2User (authorities , attributes , "name" );
89+ when (oauthToken .getPrincipal ()).thenReturn (user );
11490 return new Entry <>(oauthToken , principal );
11591 }
11692
117- private void checkCookies (List <Cookie > expectedCookies , MockHttpServletResponse response ) {
118- if (expectedCookies != null && !expectedCookies .isEmpty ()) {
119- for (Cookie expectedCookie : expectedCookies ) {
120- Cookie actualCookie = response .getCookie (expectedCookie .getName ());
121- assertNotNull (actualCookie );
122- assertEquals (expectedCookie .getValue (), actualCookie .getValue ());
123- }
124- }
125- }
126-
12793 @ Getter
12894 @ AllArgsConstructor
12995 static class Entry <F , S > {
0 commit comments