From 4d7b1288d1e7ea60f52a5053a040bd2115816aca Mon Sep 17 00:00:00 2001 From: Quentame Date: Wed, 29 May 2024 08:32:29 +0200 Subject: [PATCH] Fix epic_games_store mystery game URL (#118314) --- .../components/epic_games_store/helper.py | 2 +- tests/components/epic_games_store/const.py | 4 + .../fixtures/free_games_mystery_special.json | 541 ++++++++++++++++++ .../epic_games_store/test_helper.py | 107 +++- 4 files changed, 634 insertions(+), 20 deletions(-) create mode 100644 tests/components/epic_games_store/fixtures/free_games_mystery_special.json diff --git a/homeassistant/components/epic_games_store/helper.py b/homeassistant/components/epic_games_store/helper.py index 6cd55eaaf22..0eb6f0b0049 100644 --- a/homeassistant/components/epic_games_store/helper.py +++ b/homeassistant/components/epic_games_store/helper.py @@ -65,7 +65,7 @@ def get_game_url(raw_game_data: dict[str, Any], language: str) -> str: url_slug = raw_game_data["catalogNs"]["mappings"][0]["pageSlug"] if not url_slug: - url_slug = raw_game_data["urlSlug"] + url_slug = raw_game_data["productSlug"] return f"https://store.epicgames.com/{language}/{url_bundle_or_product}/{url_slug}" diff --git a/tests/components/epic_games_store/const.py b/tests/components/epic_games_store/const.py index dcd82c7e03e..f9c8b5dd581 100644 --- a/tests/components/epic_games_store/const.py +++ b/tests/components/epic_games_store/const.py @@ -23,3 +23,7 @@ DATA_FREE_GAMES_ONE = load_json_object_fixture("free_games_one.json", DOMAIN) DATA_FREE_GAMES_CHRISTMAS_SPECIAL = load_json_object_fixture( "free_games_christmas_special.json", DOMAIN ) + +DATA_FREE_GAMES_MYSTERY_SPECIAL = load_json_object_fixture( + "free_games_mystery_special.json", DOMAIN +) diff --git a/tests/components/epic_games_store/fixtures/free_games_mystery_special.json b/tests/components/epic_games_store/fixtures/free_games_mystery_special.json new file mode 100644 index 00000000000..5456e091a6b --- /dev/null +++ b/tests/components/epic_games_store/fixtures/free_games_mystery_special.json @@ -0,0 +1,541 @@ +{ + "data": { + "Catalog": { + "searchStore": { + "elements": [ + { + "title": "Lost Castle: The Old Ones Awaken", + "id": "4a88d0dc64114b20b67339c74543f859", + "namespace": "ab29925a0a9a49598adba45d108ceb3e", + "description": "Les Chasseurs de tr\u00e9sor ont creus\u00e9 trop profond\u00e9ment sous Castle Harwood, et les voil\u00e0 dans des lieux qui n\u2019auraient jamais d\u00fb sortir de l\u2019oubli.", + "effectiveDate": "2024-02-08T16:00:00.000Z", + "offerType": "ADD_ON", + "expiryDate": null, + "viewableDate": "2024-02-01T16:00:00.000Z", + "status": "ACTIVE", + "isCodeRedemptionOnly": false, + "keyImages": [ + { + "type": "OfferImageWide", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-r390n.png" + }, + { + "type": "OfferImageTall", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-1qvy6.jpg" + }, + { + "type": "Thumbnail", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-1qvy6.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-5fr2h.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-tl3jh.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-ooqww.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-y89ep.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-sagu3.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-1309n.jpg" + }, + { + "type": "featuredMedia", + "url": "https://cdn1.epicgames.com/spt-assets/a6d76157ad884f2c9aa470b30da9e2ff/lost-castle-1mwvz.jpg" + } + ], + "seller": { + "id": "o-ze7grkplqlrzc92lepkjv4xpaj7gn8", + "name": "Another Indie Studio Limited" + }, + "productSlug": null, + "urlSlug": "lost-castle-the-old-ones-awaken", + "url": null, + "items": [ + { + "id": "30f2fedfe5af4e9d96e151696f372a70", + "namespace": "ab29925a0a9a49598adba45d108ceb3e" + } + ], + "customAttributes": [ + { + "key": "isManuallySetRefundableType", + "value": "true" + }, + { + "key": "autoGeneratedPrice", + "value": "false" + }, + { + "key": "isManuallySetViewableDate", + "value": "true" + }, + { + "key": "isManuallySetPCReleaseDate", + "value": "false" + }, + { + "key": "isBlockchainUsed", + "value": "false" + } + ], + "categories": [ + { + "path": "addons" + }, + { + "path": "freegames" + }, + { + "path": "addons/durable" + } + ], + "tags": [ + { + "id": "1264" + }, + { + "id": "1265" + }, + { + "id": "1367" + }, + { + "id": "1370" + }, + { + "id": "1083" + }, + { + "id": "9547" + }, + { + "id": "35244" + }, + { + "id": "9549" + } + ], + "catalogNs": { + "mappings": [ + { + "pageSlug": "lost-castle-abb2e2", + "pageType": "productHome" + } + ] + }, + "offerMappings": [ + { + "pageSlug": "lost-castle-lost-castle-the-old-ones-awaken-db1545", + "pageType": "offer" + } + ], + "price": { + "totalPrice": { + "discountPrice": 359, + "originalPrice": 359, + "voucherDiscount": 0, + "discount": 0, + "currencyCode": "EUR", + "currencyInfo": { + "decimals": 2 + }, + "fmtPrice": { + "originalPrice": "3,59\u00a0\u20ac", + "discountPrice": "3,59\u00a0\u20ac", + "intermediatePrice": "3,59\u00a0\u20ac" + } + }, + "lineOffers": [ + { + "appliedRules": [] + } + ] + }, + "promotions": { + "promotionalOffers": [], + "upcomingPromotionalOffers": [ + { + "promotionalOffers": [ + { + "startDate": "2024-06-13T15:00:00.000Z", + "endDate": "2024-06-27T15:00:00.000Z", + "discountSetting": { + "discountType": "PERCENTAGE", + "discountPercentage": 50 + } + } + ] + } + ] + } + }, + { + "title": "LISA: Definitive Edition", + "id": "944b5b5d646d46bc92bc33edfe983d26", + "namespace": "ca3a9d16d131478c97fd56c138a6511a", + "description": "Explorez Olathe et d\u00e9couvrez ses terribles secrets avec LISA: Definitive Edition, qui contient le jeu de r\u00f4le narratif d'origine LISA: The Painful et sa suite, LISA: The Joyful.", + "effectiveDate": "2024-05-21T16:00:00.000Z", + "offerType": "BUNDLE", + "expiryDate": null, + "viewableDate": "2024-05-21T16:00:00.000Z", + "status": "ACTIVE", + "isCodeRedemptionOnly": false, + "keyImages": [ + { + "type": "OfferImageTall", + "url": "https://cdn1.epicgames.com/offer/ca3a9d16d131478c97fd56c138a6511a/EGS_LISATheDefinitiveEdition_DingalingProductions_Bundles_S2_1200x1600-4a9b4fc6e06e8aff136c1a3cf18292ae" + }, + { + "type": "OfferImageWide", + "url": "https://cdn1.epicgames.com/offer/ca3a9d16d131478c97fd56c138a6511a/EGS_LISATheDefinitiveEdition_DingalingProductions_Bundles_S1_2560x1440-55b66eb2046507e58eac435c21331bd5" + }, + { + "type": "Thumbnail", + "url": "https://cdn1.epicgames.com/offer/ca3a9d16d131478c97fd56c138a6511a/EGS_LISATheDefinitiveEdition_DingalingProductions_Bundles_S2_1200x1600-4a9b4fc6e06e8aff136c1a3cf18292ae" + } + ], + "seller": { + "id": "o-256f2bc2a35049a39ceae0f57d01bb", + "name": "Serenity Forge" + }, + "productSlug": "lisa-the-definitive-edition", + "urlSlug": "lisa-the-definitive-edition", + "url": null, + "items": [ + { + "id": "2cde880361534ed4bafd0a9bb502c543", + "namespace": "2052c58b9f64498386cbbbc85df90bbf" + }, + { + "id": "a7729179144d41ec9e0a7e1c09ad2f35", + "namespace": "87de7c0aad7944899fb6d2b05e13b108" + } + ], + "customAttributes": [ + { + "key": "com.epicgames.app.productSlug", + "value": "lisa-the-definitive-edition" + } + ], + "categories": [ + { + "path": "bundles" + }, + { + "path": "freegames" + }, + { + "path": "games" + }, + { + "path": "bundles/games" + }, + { + "path": "applications" + } + ], + "tags": [ + { + "id": "1367" + }, + { + "id": "1370" + }, + { + "id": "9547" + }, + { + "id": "1117" + }, + { + "id": "9549" + }, + { + "id": "1263" + } + ], + "catalogNs": { + "mappings": null + }, + "offerMappings": null, + "price": { + "totalPrice": { + "discountPrice": 2419, + "originalPrice": 2419, + "voucherDiscount": 0, + "discount": 0, + "currencyCode": "EUR", + "currencyInfo": { + "decimals": 2 + }, + "fmtPrice": { + "originalPrice": "24,19\u00a0\u20ac", + "discountPrice": "24,19\u00a0\u20ac", + "intermediatePrice": "24,19\u00a0\u20ac" + } + }, + "lineOffers": [ + { + "appliedRules": [] + } + ] + }, + "promotions": null + }, + { + "title": "Farming Simulator 22", + "id": "da9df253a7d04f6e8ba9ed175fe73d68", + "namespace": "d5241c76f178492ea1540fce45616757", + "description": "The new Farming Simulator is incoming!", + "effectiveDate": "2024-05-23T15:00:00.000Z", + "offerType": "OTHERS", + "expiryDate": null, + "viewableDate": "2024-05-16T14:25:00.000Z", + "status": "ACTIVE", + "isCodeRedemptionOnly": true, + "keyImages": [ + { + "type": "DieselStoreFrontWide", + "url": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/c93edfff-e8d3-4c0d-855b-03f44f1d9cd3_2560x1440-79fcb25480b4c1faf67a97207b97b7e2_2560x1440-79fcb25480b4c1faf67a97207b97b7e2" + }, + { + "type": "OfferImageWide", + "url": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/c93edfff-e8d3-4c0d-855b-03f44f1d9cd3_2560x1440-79fcb25480b4c1faf67a97207b97b7e2_2560x1440-79fcb25480b4c1faf67a97207b97b7e2" + }, + { + "type": "VaultClosed", + "url": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/EN-mega-sale-vault-16x9-asset_1920x1080-a27cf3919dde320a72936374a1d47813" + } + ], + "seller": { + "id": "o-ufmrk5furrrxgsp5tdngefzt5rxdcn", + "name": "Epic Dev Test Account" + }, + "productSlug": "farming-simulator-22", + "urlSlug": "mystery-game-02", + "url": null, + "items": [ + { + "id": "8341d7c7e4534db7848cc428aa4cbe5a", + "namespace": "d5241c76f178492ea1540fce45616757" + } + ], + "customAttributes": [ + { + "key": "com.epicgames.app.freegames.vault.close", + "value": "[]" + }, + { + "key": "com.epicgames.app.blacklist", + "value": "[]" + }, + { + "key": "com.epicgames.app.freegames.vault.slug", + "value": "sales-and-specials/mega-sale-2024" + }, + { + "key": "com.epicgames.app.freegames.vault.open", + "value": "[]" + }, + { + "key": "com.epicgames.app.productSlug", + "value": "farming-simulator-22" + } + ], + "categories": [ + { + "path": "freegames/vaulted" + }, + { + "path": "freegames" + }, + { + "path": "games" + }, + { + "path": "applications" + } + ], + "tags": [], + "catalogNs": { + "mappings": [] + }, + "offerMappings": [], + "price": { + "totalPrice": { + "discountPrice": 0, + "originalPrice": 0, + "voucherDiscount": 0, + "discount": 0, + "currencyCode": "EUR", + "currencyInfo": { + "decimals": 2 + }, + "fmtPrice": { + "originalPrice": "0", + "discountPrice": "0", + "intermediatePrice": "0" + } + }, + "lineOffers": [ + { + "appliedRules": [] + } + ] + }, + "promotions": { + "promotionalOffers": [ + { + "promotionalOffers": [ + { + "startDate": "2024-05-23T15:00:00.000Z", + "endDate": "2024-05-30T15:00:00.000Z", + "discountSetting": { + "discountType": "PERCENTAGE", + "discountPercentage": 0 + } + } + ] + } + ], + "upcomingPromotionalOffers": [] + } + }, + { + "title": "Mystery Game 3", + "id": "7a872a4be7ce438082f331cfe6c26b79", + "namespace": "d5241c76f178492ea1540fce45616757", + "description": "Mystery Game 3", + "effectiveDate": "2024-05-30T15:00:00.000Z", + "offerType": "OTHERS", + "expiryDate": null, + "viewableDate": "2024-05-23T14:25:00.000Z", + "status": "ACTIVE", + "isCodeRedemptionOnly": true, + "keyImages": [ + { + "type": "DieselStoreFrontWide", + "url": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/EN-mega-sale-vault-16x9-asset_1920x1080-a27cf3919dde320a72936374a1d47813_1920x1080-a27cf3919dde320a72936374a1d47813" + }, + { + "type": "VaultClosed", + "url": "https://cdn1.epicgames.com/offer/d5241c76f178492ea1540fce45616757/EN-mega-sale-vault-16x9-asset_1920x1080-a27cf3919dde320a72936374a1d47813" + } + ], + "seller": { + "id": "o-ufmrk5furrrxgsp5tdngefzt5rxdcn", + "name": "Epic Dev Test Account" + }, + "productSlug": "[]", + "urlSlug": "mystery-game-03", + "url": null, + "items": [ + { + "id": "8341d7c7e4534db7848cc428aa4cbe5a", + "namespace": "d5241c76f178492ea1540fce45616757" + } + ], + "customAttributes": [ + { + "key": "com.epicgames.app.freegames.vault.close", + "value": "[]" + }, + { + "key": "com.epicgames.app.blacklist", + "value": "[]" + }, + { + "key": "com.epicgames.app.freegames.vault.slug", + "value": "sales-and-specials/mega-sale" + }, + { + "key": "com.epicgames.app.freegames.vault.open", + "value": "[]" + }, + { + "key": "com.epicgames.app.productSlug", + "value": "[]" + } + ], + "categories": [ + { + "path": "freegames/vaulted" + }, + { + "path": "freegames" + }, + { + "path": "games" + }, + { + "path": "applications" + } + ], + "tags": [], + "catalogNs": { + "mappings": [] + }, + "offerMappings": [], + "price": { + "totalPrice": { + "discountPrice": 0, + "originalPrice": 0, + "voucherDiscount": 0, + "discount": 0, + "currencyCode": "EUR", + "currencyInfo": { + "decimals": 2 + }, + "fmtPrice": { + "originalPrice": "0", + "discountPrice": "0", + "intermediatePrice": "0" + } + }, + "lineOffers": [ + { + "appliedRules": [] + } + ] + }, + "promotions": { + "promotionalOffers": [], + "upcomingPromotionalOffers": [ + { + "promotionalOffers": [ + { + "startDate": "2024-05-30T15:00:00.000Z", + "endDate": "2024-06-06T15:00:00.000Z", + "discountSetting": { + "discountType": "PERCENTAGE", + "discountPercentage": 0 + } + } + ] + } + ] + } + } + ], + "paging": { + "count": 1000, + "total": 4 + } + } + } + }, + "extensions": {} +} diff --git a/tests/components/epic_games_store/test_helper.py b/tests/components/epic_games_store/test_helper.py index 155ccb7d211..1ca6884642e 100644 --- a/tests/components/epic_games_store/test_helper.py +++ b/tests/components/epic_games_store/test_helper.py @@ -10,16 +10,73 @@ from homeassistant.components.epic_games_store.helper import ( is_free_game, ) -from .const import DATA_ERROR_ATTRIBUTE_NOT_FOUND, DATA_FREE_GAMES_ONE +from .const import ( + DATA_ERROR_ATTRIBUTE_NOT_FOUND, + DATA_FREE_GAMES_MYSTERY_SPECIAL, + DATA_FREE_GAMES_ONE, +) -FREE_GAMES_API = DATA_FREE_GAMES_ONE["data"]["Catalog"]["searchStore"]["elements"] -FREE_GAME = FREE_GAMES_API[2] -NOT_FREE_GAME = FREE_GAMES_API[0] +GAMES_TO_TEST_FREE_OR_DISCOUNT = [ + { + "raw_game_data": DATA_FREE_GAMES_ONE["data"]["Catalog"]["searchStore"][ + "elements" + ][2], + "expected_result": True, + }, + { + "raw_game_data": DATA_FREE_GAMES_ONE["data"]["Catalog"]["searchStore"][ + "elements" + ][0], + "expected_result": False, + }, + { + "raw_game_data": DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"][ + "searchStore" + ]["elements"][1], + "expected_result": False, + }, + { + "raw_game_data": DATA_FREE_GAMES_MYSTERY_SPECIAL["data"]["Catalog"][ + "searchStore" + ]["elements"][2], + "expected_result": True, + }, +] + + +GAMES_TO_TEST_URL = [ + { + "raw_game_data": DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"][ + "searchStore" + ]["elements"][1], + "expected_result": "/p/destiny-2--bungie-30th-anniversary-pack", + }, + { + "raw_game_data": DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"][ + "searchStore" + ]["elements"][4], + "expected_result": "/bundles/qube-ultimate-bundle", + }, + { + "raw_game_data": DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"][ + "searchStore" + ]["elements"][5], + "expected_result": "/p/payday-2-c66369", + }, + { + "raw_game_data": DATA_FREE_GAMES_MYSTERY_SPECIAL["data"]["Catalog"][ + "searchStore" + ]["elements"][2], + "expected_result": "/p/farming-simulator-22", + }, +] def test_format_game_data() -> None: """Test game data format.""" - game_data = format_game_data(FREE_GAME, "fr") + game_data = format_game_data( + GAMES_TO_TEST_FREE_OR_DISCOUNT[0]["raw_game_data"], "fr" + ) assert game_data assert game_data["title"] assert game_data["description"] @@ -38,22 +95,20 @@ def test_format_game_data() -> None: ("raw_game_data", "expected_result"), [ ( - DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"]["searchStore"][ - "elements" - ][1], - "/p/destiny-2--bungie-30th-anniversary-pack", + GAMES_TO_TEST_URL[0]["raw_game_data"], + GAMES_TO_TEST_URL[0]["expected_result"], ), ( - DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"]["searchStore"][ - "elements" - ][4], - "/bundles/qube-ultimate-bundle", + GAMES_TO_TEST_URL[1]["raw_game_data"], + GAMES_TO_TEST_URL[1]["expected_result"], ), ( - DATA_ERROR_ATTRIBUTE_NOT_FOUND["data"]["Catalog"]["searchStore"][ - "elements" - ][5], - "/p/mystery-game-7", + GAMES_TO_TEST_URL[2]["raw_game_data"], + GAMES_TO_TEST_URL[2]["expected_result"], + ), + ( + GAMES_TO_TEST_URL[3]["raw_game_data"], + GAMES_TO_TEST_URL[3]["expected_result"], ), ], ) @@ -65,8 +120,22 @@ def test_get_game_url(raw_game_data: dict[str, Any], expected_result: bool) -> N @pytest.mark.parametrize( ("raw_game_data", "expected_result"), [ - (FREE_GAME, True), - (NOT_FREE_GAME, False), + ( + GAMES_TO_TEST_FREE_OR_DISCOUNT[0]["raw_game_data"], + GAMES_TO_TEST_FREE_OR_DISCOUNT[0]["expected_result"], + ), + ( + GAMES_TO_TEST_FREE_OR_DISCOUNT[1]["raw_game_data"], + GAMES_TO_TEST_FREE_OR_DISCOUNT[1]["expected_result"], + ), + ( + GAMES_TO_TEST_FREE_OR_DISCOUNT[2]["raw_game_data"], + GAMES_TO_TEST_FREE_OR_DISCOUNT[2]["expected_result"], + ), + ( + GAMES_TO_TEST_FREE_OR_DISCOUNT[3]["raw_game_data"], + GAMES_TO_TEST_FREE_OR_DISCOUNT[3]["expected_result"], + ), ], ) def test_is_free_game(raw_game_data: dict[str, Any], expected_result: bool) -> None: