# Path: kano_apps/AppManage.py
# def download_app(app_id_or_slug):
#     data = query_for_app(app_id_or_slug)
# 
#     # download the icon
#     icon_file_type = data['icon_url'].split(".")[-1]
#     icon_path = '/tmp/{}.{}'.format(app_id_or_slug, icon_file_type)
#     rv, err = download_url(data['icon_url'], icon_path)
#     if not rv:
#         msg = _("Unable to download the application ({})").format(err)
#         raise AppDownloadError(msg)
# 
#     # Check if the app is going to run well on the hardware
#     if 'min_performance_score' in data and \
#        has_min_performance(data['min_performance_score']):
# 
#         msg = _(
#             "{} won't be downloaded "
#             "because the hardware is not performant enough"
#         ).format(data['title'])
#         raise AppDownloadError(msg)
# 
#     # Cleanup the JSON file
#     data['icon'] = data['slug']
#     del data['icon_url']
#     data['time_installed'] = int(time.time())
#     data['categories'] = map(lambda c: c.lower(), data['categories'])
#     data['removable'] = True
# 
#     # FIXME: This should all be done in the API
#     if 'priority' not in data:
#         data['priority'] = get_prio(data['slug'])
# 
#     if data['slug'] == 'powerup':
#         data['title'] = 'Make Light'
# 
#     # write out the data
#     data_path = '/tmp/{}.app'.format(app_id_or_slug)
#     with open(data_path, 'w') as f:
#         f.write(json.dumps(data))
# 
#     return [data_path, icon_path]
# 
# class AppDownloadError(Exception):
#     pass


import json
import pytest
from mock import patch
from kano_apps.AppManage import download_app, AppDownloadError
from kano_i18n.init import install
# coding: utf-8
# test_AppManage.py
#
# Copyright (C) 2016, 2015 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Tests for AppManage




@patch('kano_apps.AppManage.query_for_app')
@patch('kano_apps.AppManage.download_url')
@patch('kano_apps.AppManage.has_min_performance')
def test_download_app_error(
     mock_has_min_performance, mock_download_url, mock_query_for_app
):
    mock_query_for_app.return_value = json.loads(
        '{ "icon_url": "foo.bar", "min_performance_score": 1, "title": "¡Hola!" }'
    )
    mock_has_min_performance.return_value = True
    mock_download_url.return_value = (True, None)

    install('kano-apps')

    with pytest.raises(AppDownloadError) as excinfo:

# Path: kano_apps/AppManage.py
# def download_app(app_id_or_slug):
#     data = query_for_app(app_id_or_slug)
# 
#     # download the icon
#     icon_file_type = data['icon_url'].split(".")[-1]
#     icon_path = '/tmp/{}.{}'.format(app_id_or_slug, icon_file_type)
#     rv, err = download_url(data['icon_url'], icon_path)
#     if not rv:
#         msg = _("Unable to download the application ({})").format(err)
#         raise AppDownloadError(msg)
# 
#     # Check if the app is going to run well on the hardware
#     if 'min_performance_score' in data and \
#        has_min_performance(data['min_performance_score']):
# 
#         msg = _(
#             "{} won't be downloaded "
#             "because the hardware is not performant enough"
#         ).format(data['title'])
#         raise AppDownloadError(msg)
# 
#     # Cleanup the JSON file
#     data['icon'] = data['slug']
#     del data['icon_url']
#     data['time_installed'] = int(time.time())
#     data['categories'] = map(lambda c: c.lower(), data['categories'])
#     data['removable'] = True
# 
#     # FIXME: This should all be done in the API
#     if 'priority' not in data:
#         data['priority'] = get_prio(data['slug'])
# 
#     if data['slug'] == 'powerup':
#         data['title'] = 'Make Light'
# 
#     # write out the data
#     data_path = '/tmp/{}.app'.format(app_id_or_slug)
#     with open(data_path, 'w') as f:
#         f.write(json.dumps(data))
# 
#     return [data_path, icon_path]
# 
# class AppDownloadError(Exception):
#     pass


# Path: tests/kano_apps/test_AppManage.py
import json
import pytest
from mock import patch
from kano_apps.AppManage import download_app, AppDownloadError
from kano_i18n.init import install

# coding: utf-8
# test_AppManage.py
#
# Copyright (C) 2016, 2015 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Tests for AppManage




@patch('kano_apps.AppManage.query_for_app')
@patch('kano_apps.AppManage.download_url')
@patch('kano_apps.AppManage.has_min_performance')
def test_download_app_error(
     mock_has_min_performance, mock_download_url, mock_query_for_app
):
    mock_query_for_app.return_value = json.loads(
        '{ "icon_url": "foo.bar", "min_performance_score": 1, "title": "¡Hola!" }'
    )
    mock_has_min_performance.return_value = True
    mock_download_url.return_value = (True, None)

    install('kano-apps')

    with pytest.raises(AppDownloadError) as excinfo:

        download_app('foo')