def test_initialize_should_call_provided_url(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_url = "This is a URL"
        test_installer_api = InstallerAPI(test_url)
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data='{"version": 2}')
        test_installer_api.initialize()

        mock_urllib2.urlopen.assert_called_with(test_url)
    def test_initialize_should_return_an_error_if_setup_file_is_incorrect_version(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_installer_api = InstallerAPI('config_url')
        expected_result = (False, 10304, "Configuration version too new installer upgrade required")
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data='{"version": 2}')

        result = test_installer_api.initialize()

        self.assertEqual(expected_result, result)
    def test_initialize_should_return_a_setup_files_unavailable_if_cannot_be_parsed(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_installer_api = InstallerAPI('config_url')
        expected_result = (False, 10302, "Web data File Corrupt or damaged")
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data="empty")

        result = test_installer_api.initialize()

        self.assertEqual(expected_result, result)
    def test_initialize_should_return_an_error_if_setup_file_valid_but_empty(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_installer_api = InstallerAPI('config_url')
        expected_result = (False, 10303, "Config is not valid")
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data="[]")

        result = test_installer_api.initialize()

        self.assertEqual(expected_result, result)
    def test_initialize_should_return_a_tuple_and_error_when_internet_unavailable(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_installer_api = InstallerAPI('config_url')
        expected_result = (False, 10301, "Connection unavailable")
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=404)

        result = test_installer_api.initialize()

        self.assertEqual(expected_result, result)
 def test_initialize_if_file_config_exists_and_is_not_json(self, mock_urllib2, mock_exists):
     mock_exists.return_value = True
     expected_result = (False, 10402, "Install File Corrupt or Damaged")
     mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
     mock_open_file = mock_open(read_data="Not Json")
     with patch('installer_api.open', mock_open_file, create=True):
         mock_open_file.read = IOError("Mock Error")
         test_installer_api = InstallerAPI('config_url')
         result = test_installer_api.initialize()
         self.assertEquals(expected_result, result)
 def test_initialize_if_file_config_exists_and_is_inaccessable(self, mock_urllib2, mock_exists):
     mock_exists.return_value = True
     expected_result = (False, 10401, "Install File Inaccessable")
     mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
     mock_open_file = mock_open()
     with patch('installer_api.open', mock_open_file, create=True):
         mock_open_file.side_effect = IOError("Mock Error")
         test_installer_api = InstallerAPI('config_url')
         result = test_installer_api.initialize()
         self.assertEquals(expected_result, result)
    def test_get_item_responds_with_specific_application(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
        expected_app = Application.from_configs(self.get_sample_application_config())
        test_installer_api = InstallerAPI('config_url')

        result = test_installer_api.initialize()
        self.assertTrue(result[0])
        app = test_installer_api.get_item(expected_app.id)

        self.assertEquals(expected_app, app)
    def test_get_items_should_return_a_list_of_available_items(self, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        test_installer_api = InstallerAPI('config_url')
        sample = '{"version": 0, "applications":[%s]}' % json.dumps(self.get_sample_application_config())
        expected_result = [Application.from_configs(self.get_sample_application_config())]
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=sample)
        init_result = list(test_installer_api.initialize())
        self.assertTrue(init_result[0], init_result[2])

        result = test_installer_api.get_items()

        self.assertEqual(expected_result, result)
    def test_process_should_create_and_start_an_upgrader_for_item_if_install_true(self, mock_AsyncActionHandler, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
        expected_app = Application.from_configs(self.get_sample_application_config())
        test_installer_api = InstallerAPI('config_url')

        result = test_installer_api.initialize()
        self.assertTrue(result[0], result)
        test_installer_api.process(expected_app.id, 'base_folder', action='upgrade', status_callback='status_callback', complete_callback='complete_callback')

        mock_AsyncActionHandler.assert_called_with('upgrade', expected_app, 'base_folder', status_callback='status_callback', complete_callback='complete_callback')
        mock_AsyncActionHandler.return_value.start.assert_called_with()
    def test_process_should_raise_if_nothing_selected(self, mock_AsyncActionHandler, mock_urllib2, mock_exists):
        mock_exists.return_value = False
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
        expected_app = Application.from_configs(self.get_sample_application_config())
        test_installer_api = InstallerAPI('config_url')

        result = test_installer_api.initialize()
        self.assertTrue(result[0], result)

        with self.assertRaises(Exception):
            test_installer_api.process(expected_app.id, 'base_folder', action='wrong', status_callback='status_callback', complete_callback='complete_callback')

        mock_AsyncActionHandler.assert_not_called()
    def test_initialize_should_create_correct_application(self, mock_urllib2, mock_exists):
        mock_exists.return_value = True
        mock_urllib2.urlopen.return_value = self.make_mock_response(code=200, data=self.get_sample_web_config())
        mock_open_file = mock_open(read_data=json.dumps(self.get_sample_installed_config()))
        expected_app = Application.from_configs(self.get_sample_application_config(), self.get_sample_installed_config())

        with patch('installer_api.open', mock_open_file, create=True):
            test_installer_api = InstallerAPI('config_url')

            result = test_installer_api.initialize()
            self.assertTrue(result[0])
            apps = test_installer_api.get_items()

            self.assertEquals(1, len(apps))
            self.assertEquals(expected_app, apps[0])
Exemple #13
0
    parser.add_argument('-l', '--log',     dest='loglevel', action='store',      required=False, default="INFO", help="Enter the loglevel [DEBUG|INFO|WARNING|ERROR] default: WARNING")
    parser.add_argument('-t', '--console', dest='console',  action='store_true', required=False, help="Logs to console not file")
    parser.add_argument('-a', '--alternate-config', dest='alt_config', action='store', required=False, default=default_config_url, help="Alternate url for config file")
    args, unknown = parser.parse_known_args()

    ASADMIN = 'asadmin'
    if sys.argv[-1] != ASADMIN:
        script = os.path.abspath(sys.argv[0])
        params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
        shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
        sys.exit(0)

    setup_logging(args)
    logger = logging.getLogger('peashy')
    try:
        api = InstallerAPI(args.alt_config)
        result, code, message = api.initialize()
        logger.info('{} -- {} -- {}'.format(result, code, message))
        root = Tk()
        root.wm_title("Peachy Installer")
        root.resizable(width=FALSE, height=FALSE)
        root.geometry('{}x{}'.format(640, 400))
        if not result:
            tkMessageBox.showinfo("Something annoying has occured", message)
            if code == 10304:
                import webbrowser
                webbrowser.open('https://github.com/PeachyPrinter/peachyinstaller/releases', new=0, autoraise=True)
            sys.exit()
        i = InstallerUI(api, master=root)
        i.mainloop()
    except Exception as ex: