Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gog linking breaks with new line after profile token #929

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/faforever/api/user/GogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -48,9 +49,10 @@ void verifyGogUsername(@NotNull String username) {
}

void verifyProfileToken(String gogUsername, User user, String targetToken) {
String profileStatus = getProfileStatus(gogUsername);
final String profileStatus = getProfileStatus(gogUsername);
final String profileStatusTrimmed = StringEscapeUtils.unescapeJava(profileStatus).trim();

if(profileStatus.length() > 100 || !Objects.equals(targetToken, profileStatus.trim())) {
if (profileStatusTrimmed.length() > 100 || !Objects.equals(targetToken, profileStatusTrimmed)) {
throw ApiException.of(ErrorCode.GOG_LINK_PROFILE_TOKEN_NOT_SET);
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/com/faforever/api/user/GogServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.faforever.api.user.GogService.GOG_FA_GAME_ID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -170,6 +171,67 @@ void verifyProfileTokenSuccess() {
instance.verifyProfileToken(GOG_USERNAME, user, "{{FAF:76365}}");
}

@Test
void verifyProfileTokenSuccessWhenDescriptionTrailingNewLine() {
when(fafApiProperties.getGog().getProfilePageUrl()).thenReturn(PROFILE_PAGE_URL);

when(restTemplate.getForObject(PROFILE_PAGE_URL, String.class))
.thenReturn("""
<html>
<head>
</head>
<body>
<script
src="https://profiles-static.gog.com/assets/vendor/anchorme_min.js?afebd1f7c7ea2891da546c75c2e25f33c0cbbff3">
</script>
<script
src="https://profiles-static.gog.com/assets/vendor/sanitize-html_min.js?afebd1f7c7ea2891da546c75c2e25f33c0cbbff3">
</script>
<script>
// define global namespace object
window.profilesData = window.profilesData || {};

window.profilesData.env = "prod";

window.profilesData.currency = "USD";
window.profilesData.langCode = "en-US";
window.profilesData.translations = {"activity_achievement_action_text_one":"earned 1 achievement in %productLink%","activity_achievement_action_text_few":"earned %achievementsCount% achievements in %productLink%","activity_achievement_action_text_many":"earned %achievementsCount% achievements in %productLink%","activity_achievement_action_text_other":"earned %achievementsCount% achievements in %productLink%","activity_achievements_milestone_action_text":"reached a milestone in %productLink%","activity_custom_post_action_text":"shared a thought","activity_first_time_played_milestone_action_text":"started playing %productLink%","activity_forum_topic_created_action_text":"started a topic in the %forumLink% forum","activity_playtime_milestone_action_text":"reached a milestone in %productLink%","activity_product_review_action_text":"reviewed %productLink%","user_status_online":"Online","user_status_offline":"Offline","filter_recent_playtime":"Recently played","filter_achievements":"Achievements earned","filter_total_playtime":"Total playtime","filter_abc":"Alphabetically","filter_rarity_desc":"Common first","filter_rarity_asc":"Legendary first","filter_original_order":"Developer's order","filter_by_name":"Username","filter_by_status":"Online first","filter_by_total_games":"Owned games","filter_by_total_achievements":"Achievements earned","filter_by_total_playtime":"Total playtime","filter_unlock_date":"Unlock date","filter_you":"(You)"};
window.profilesData.sortingOptions = {alphabetically: "filter_abc",
percent_of_unlocked_achievements: "filter_achievements",
total_playtime: "filter_total_playtime",
recent_playtime: "filter_recent_playtime",
user_unlock_date: "filter_unlock_date",
rarityDESC: "filter_rarity_desc",
rarityASC: "filter_rarity_asc",
original: "filter_original_order",
username: "filter_by_name",
status: "filter_by_status",
gamesCount: "filter_by_total_games",
achievementsCount: "filter_by_total_achievements" ,
playtime: "filter_by_total_playtime"
};

window.profilesData.currentUser = null;
window.profilesData.currentUserPreferences = null;
window.profilesData.profileUser = {"username":"Brutus5000","created_date":"","userId":"","avatar":"https:\\/\\/images.gog.com\\/67ec2f3bd8d73b975a8e8d8905da5211ccdf1ee4505f0a8e32df1a355df3652f.jpg","settings":{"allow_to_be_invited_by":"friend"},"stats":{"games_owned":666,"achievements":null,"hours_played":0},"background":{"id":"100","name":"Sea-bg","type":"predefined","src":"https:\\/\\/images.gog.com\\/b78feb5921e6a290638ba7e53927ec1db9dcb0686cb655b351c06fda0fd07990.jpg","background_dominant_color":[58,118,126]}};
window.profilesData.profileUserPreferences = {"bio":"{{FAF:76365}}\\n","privacy":{"profile":"public","games":"public","friends":"friends"}};
window.profilesData.profileUserFriends = [];
window.profilesData.currentUserFriends = [];
window.profilesData.recentGames = [];
window.profilesData.activities = [];
window.profilesData.recentAchievements = [];
window.profilesData.profileUserFriendInvites = null;
window.profilesData.serverNow = 1632687270;
window.profilesData.mode = "standard";
window.activeFeatures = {"menuMicroservice":true,"footerMicroservice":true,"new_footer_enabled":true};
</script>
</body>
</html>
""");

assertDoesNotThrow(() -> instance.verifyProfileToken(GOG_USERNAME, user, "{{FAF:76365}}"));
}

@Test
void verifyGameOwnershipSuccess() {
when(fafApiProperties.getGog().getGamesListUrl()).thenReturn(GAME_LIST_PAGE_URL);
Expand Down
Loading