Example #1
0
 def test_get_no_confinement(self):
     channel_map = {
         "arch": {
             "track": [{
                 "risk": "stable",
                 "confinement": "strict"
             }]
         }
     }
     no_version = logic.extract_info_channel_map(channel_map, "track",
                                                 "edge")["confinement"]
     self.assertEqual(no_version, None)
Example #2
0
 def test_get_no_version(self):
     channel_map = {
         "arch": {
             "track": [{
                 "risk": "stable",
                 "version": "10"
             }]
         }
     }
     no_version = logic.extract_info_channel_map(channel_map, "track",
                                                 "edge")["version"]
     self.assertEqual(no_version, None)
Example #3
0
    def test_get_confinement(self):
        channel_map = {
            "arch": {
                "track": [
                    {
                        "risk": "edge",
                        "confinement": "classic"
                    },
                    {
                        "risk": "stable",
                        "confinement": "strict"
                    },
                ]
            }
        }
        classic_result = logic.extract_info_channel_map(
            channel_map, "track", "edge")["confinement"]
        self.assertEqual(classic_result, "classic")

        strict_result = logic.extract_info_channel_map(channel_map, "track",
                                                       "stable")["confinement"]
        self.assertEqual(strict_result, "strict")
Example #4
0
    def test_get_version(self):
        channel_map = {
            "arch": {
                "track": [
                    {
                        "risk": "edge",
                        "version": "12"
                    },
                    {
                        "risk": "stable",
                        "version": "10"
                    },
                ]
            }
        }
        edge_version = logic.extract_info_channel_map(channel_map, "track",
                                                      "edge")["version"]
        self.assertEqual(edge_version, "12")

        stable_version = logic.extract_info_channel_map(
            channel_map, "track", "stable")["version"]
        self.assertEqual(stable_version, "10")
Example #5
0
    def _get_context_snap_details(snap_name):
        try:
            details = api.get_item_details(snap_name, api_version=2)
        except StoreApiTimeoutError as api_timeout_error:
            flask.abort(504, str(api_timeout_error))
        except StoreApiResponseDecodeError as api_response_decode_error:
            flask.abort(502, str(api_response_decode_error))
        except StoreApiResponseErrorList as api_response_error_list:
            if api_response_error_list.status_code == 404:
                flask.abort(404, "No snap named {}".format(snap_name))
            else:
                if api_response_error_list.errors:
                    error_messages = ", ".join(
                        api_response_error_list.errors.key())
                else:
                    error_messages = "An error occurred."
                flask.abort(502, error_messages)
        except StoreApiResponseError as api_response_error:
            flask.abort(502, str(api_response_error))
        except StoreApiCircuitBreaker:
            flask.abort(503)
        except (StoreApiError, ApiError) as api_error:
            flask.abort(502, str(api_error))

        # When removing all the channel maps of an existing snap the API,
        # responds that the snaps still exists with data.
        # Return a 404 if not channel maps, to avoid having a error.
        # For example: mir-kiosk-browser
        if not details.get("channel-map"):
            flask.abort(404, "No snap named {}".format(snap_name))

        formatted_description = parse_markdown_description(
            details["snap"]["description"])

        channel_maps_list = logic.convert_channel_maps(
            details.get("channel-map"))

        latest_channel = logic.get_last_updated_version(
            details.get("channel-map"))

        default_track = (details.get("default-track")
                         if details.get("default-track") else "latest")

        lowest_risk_available = logic.get_lowest_available_risk(
            channel_maps_list, default_track)

        extracted_info = logic.extract_info_channel_map(
            channel_maps_list, default_track, lowest_risk_available)

        last_updated = latest_channel["channel"]["released-at"]
        binary_filesize = latest_channel["download"]["size"]

        # filter out banner and banner-icon images from screenshots
        screenshots = logic.filter_screenshots(details["snap"]["media"])

        icons = logic.get_icon(details["snap"]["media"])

        publisher_info = helpers.get_yaml(
            "{}{}.yaml".format(
                flask.current_app.config["CONTENT_DIRECTORY"]
                ["PUBLISHER_PAGES"],
                details["snap"]["publisher"]["username"],
            ),
            typ="safe",
        )

        publisher_snaps = helpers.get_yaml(
            "{}{}-snaps.yaml".format(
                flask.current_app.config["CONTENT_DIRECTORY"]
                ["PUBLISHER_PAGES"],
                details["snap"]["publisher"]["username"],
            ),
            typ="safe",
        )

        publisher_featured_snaps = None

        if publisher_info:
            publisher_featured_snaps = publisher_info.get("featured_snaps")
            publisher_snaps = logic.get_n_random_snaps(
                publisher_snaps["snaps"], 4)

        video = logic.get_video(details["snap"]["media"])

        is_users_snap = False
        if authentication.is_authenticated(flask.session):
            if (flask.session.get("publisher").get("nickname")
                    == details["snap"]["publisher"]["username"]
                ) or ("user_shared_snaps" in flask.session
                      and snap_name in flask.session.get("user_shared_snaps")):
                is_users_snap = True

        # build list of categories of a snap
        categories = logic.get_snap_categories(details["snap"]["categories"])

        developer = logic.get_snap_developer(details["name"])

        context = {
            "snap-id": details.get("snap-id"),
            # Data direct from details API
            "snap_title": details["snap"]["title"],
            "package_name": details["name"],
            "categories": categories,
            "icon_url": icons[0] if icons else None,
            "version": extracted_info["version"],
            "license": details["snap"]["license"],
            "publisher": details["snap"]["publisher"]["display-name"],
            "username": details["snap"]["publisher"]["username"],
            "screenshots": screenshots,
            "video": video,
            "publisher_snaps": publisher_snaps,
            "publisher_featured_snaps": publisher_featured_snaps,
            "has_publisher_page": publisher_info is not None,
            "prices": details["snap"]["prices"],
            "contact": details["snap"].get("contact"),
            "website": details["snap"].get("website"),
            "summary": details["snap"]["summary"],
            "description": formatted_description,
            "channel_map": channel_maps_list,
            "has_stable": logic.has_stable(channel_maps_list),
            "developer_validation": details["snap"]["publisher"]["validation"],
            "default_track": default_track,
            "lowest_risk_available": lowest_risk_available,
            "confinement": extracted_info["confinement"],
            "trending": details["snap"]["trending"],
            # Transformed API data
            "filesize": humanize.naturalsize(binary_filesize),
            "last_updated": logic.convert_date(last_updated),
            "last_updated_raw": last_updated,
            "is_users_snap": is_users_snap,
            "unlisted": details["snap"]["unlisted"],
            "developer": developer,
            # TODO: This is horrible and hacky
            "appliances": {
                "adguard-home": "adguard",
                "mosquitto": "mosquitto",
                "nextcloud": "nextcloud",
                "plexmediaserver": "plex",
                "openhab": "openhab",
            },
        }

        return context