33
44import com .faforever .api .AbstractIntegrationTest ;
55import com .faforever .api .data .domain .GroupPermission ;
6+ import com .faforever .api .data .domain .LinkedServiceType ;
67import com .faforever .api .data .domain .User ;
78import com .faforever .api .email .EmailSender ;
89import com .faforever .api .error .ErrorCode ;
2425import static org .hamcrest .MatcherAssert .assertThat ;
2526import static org .hamcrest .Matchers .is ;
2627import static org .hamcrest .Matchers .not ;
27- import static org .hamcrest .Matchers .nullValue ;
2828import static org .mockito .ArgumentMatchers .any ;
2929import static org .mockito .ArgumentMatchers .anyString ;
3030import static org .mockito .ArgumentMatchers .eq ;
@@ -61,6 +61,9 @@ public class UsersControllerTest extends AbstractIntegrationTest {
6161 @ Autowired
6262 private UserRepository userRepository ;
6363
64+ @ Autowired
65+ private AccountLinkRepository accountLinkRepository ;
66+
6467 @ Test
6568 public void registerWithSuccess () throws Exception {
6669 MultiValueMap <String , String > params = new HttpHeaders ();
@@ -292,9 +295,10 @@ public void buildSteamLinkUrl() throws Exception {
292295
293296 @ Test
294297 public void linkToSteam () throws Exception {
295- assertThat (userRepository .getById (1 ).getSteamId (), nullValue ());
296-
297298 String steamId = "12345" ;
299+
300+ assertThat (accountLinkRepository .findOneByServiceIdAndServiceType (steamId , LinkedServiceType .STEAM ).isEmpty (), is (true ));
301+
298302 String callbackUrl = "http://faforever.com" ;
299303 String token = fafTokenService .createToken (
300304 FafTokenType .LINK_TO_STEAM ,
@@ -312,15 +316,14 @@ public void linkToSteam() throws Exception {
312316 .andExpect (status ().isFound ())
313317 .andExpect (redirectedUrl (callbackUrl ));
314318
315- assertThat (userRepository . getById ( 1 ). getSteamId () , is (steamId ));
319+ assertThat (accountLinkRepository . findOneByServiceIdAndServiceType ( steamId , LinkedServiceType . STEAM ). get (). getUser (). getId () , is (1 ));
316320 }
317321
318322 @ Test
319323 public void linkToSteamAlreadyLinkedAccount () throws Exception {
320324 String steamId = "1234" ;
321- assertThat (userRepository .getById (1 ).getSteamId (), nullValue ());
322- User userThatOwnsSteamId = userRepository .getById (2 );
323- assertThat (userThatOwnsSteamId .getSteamId (), is (steamId ));
325+ User userThatOwnsSteamId = accountLinkRepository .findOneByServiceIdAndServiceType (steamId , LinkedServiceType .STEAM ).get ().getUser ();
326+ assertThat (userThatOwnsSteamId .getId (), is (2 ));
324327
325328 String callbackUrl = "http://faforever.com" ;
326329 String token = fafTokenService .createToken (
@@ -340,7 +343,7 @@ public void linkToSteamAlreadyLinkedAccount() throws Exception {
340343 .andExpect (redirectedUrlPattern (callbackUrl + "?errors=*" + ErrorCode .STEAM_ID_ALREADY_LINKED .getCode () + "*" + userThatOwnsSteamId .getLogin () + "*" ));
341344 //We expect and error with code STEAM_ID_ALREADY_LINKED and that the error message contains the user that this steam account was linked to already which is MODERATOR with id 2
342345
343- assertThat (userRepository .getById (1 ). getSteamId ( ), nullValue ( ));
346+ assertThat (accountLinkRepository . existsByUserAndServiceType ( userRepository .getReferenceById (1 ), LinkedServiceType . STEAM ), is ( false ));
344347 }
345348
346349 @ Test
@@ -508,7 +511,7 @@ public void linkToGogWithoutOAuthScopeFails() throws Exception {
508511 }
509512
510513 @ Test
511- public void linkToGogWithoutSuccess () throws Exception {
514+ public void linkToGogSuccess () throws Exception {
512515 MultiValueMap <String , String > params = new HttpHeaders ();
513516 params .add ("gogUsername" , "someUsername" );
514517
@@ -519,5 +522,23 @@ public void linkToGogWithoutSuccess() throws Exception {
519522 .with (getOAuthTokenForUserId (USERID_USER , OAuthScope ._WRITE_ACCOUNT_DATA ))
520523 .params (params ))
521524 .andExpect (status ().isOk ());
525+
526+ assertThat (accountLinkRepository .existsByUserAndServiceType (userRepository .getReferenceById (1 ), LinkedServiceType .GOG ), is (true ));
527+ }
528+
529+ @ Test
530+ public void linkToGogAlreadyLinked () throws Exception {
531+ MultiValueMap <String , String > params = new HttpHeaders ();
532+ params .add ("gogUsername" , "username" );
533+
534+ when (gogService .buildGogToken (any ())).thenReturn ("theToken" );
535+
536+ mockMvc .perform (
537+ post ("/users/linkToGog" )
538+ .with (getOAuthTokenForUserId (USERID_USER , OAuthScope ._WRITE_ACCOUNT_DATA ))
539+ .params (params ))
540+ .andExpect (status ().isUnprocessableEntity ());
541+
542+ assertThat (accountLinkRepository .existsByUserAndServiceType (userRepository .getReferenceById (1 ), LinkedServiceType .GOG ), is (false ));
522543 }
523544}
0 commit comments