示例#1
0
    def test_register_key_failed(
        self,
        mock_installed,
        mock_input,
        mock_getpass,
        mock_check_output,
        mock_login,
        mock_get_account_information,
        mock_register_key,
    ):
        mock_installed.return_value = True
        mock_check_output.side_effect = mock_snap_output
        mock_get_account_information.return_value = {"account_id": "abcd"}
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        response.status_code = 500
        response.reason = "Internal Server Error"
        mock_register_key.side_effect = storeapi.errors.StoreKeyRegistrationError(
            response
        )

        raised = self.assertRaises(
            storeapi.errors.StoreKeyRegistrationError,
            self.run_command,
            ["register-key", "default"],
        )

        self.assertThat(
            str(raised), Equals("Key registration failed: 500 Internal Server Error")
        )
示例#2
0
    def test_register_key_account_info_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        response.status_code = 500
        response.reason = "Internal Server Error"
        self.fake_store_account_info.mock.side_effect = (
            storeapi.errors.StoreAccountInformationError(response)
        )

        # Fake the login check
        self.useFixture(
            fixtures.MockPatch("snapcraft_legacy._store.login", return_value=True)
        )

        raised = self.assertRaises(
            storeapi.errors.StoreAccountInformationError,
            self.run_command,
            ["register-key", "default"],
        )

        self.assertThat(
            str(raised),
            Equals(
                "Error fetching account information from store: "
                "500 Internal Server Error"
            ),
        )
示例#3
0
    def test_registration_cancelled(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        self.fake_store_register.mock.side_effect = storeapi.errors.StoreRegistrationError(
            "test-snap", response)

        result = self.run_command(["register", "test-snap"], input="n\n")

        self.assertThat(result.exit_code, Equals(0))
        self.assertThat(
            result.output,
            Contains("Thank you! 'test-snap' will remain available"))
示例#4
0
    def test_registration_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        with mock.patch.object(storeapi.SCAClient,
                               'register') as mock_register:
            mock_register.side_effect = storeapi.errors.StoreRegistrationError(
                'test-snap', response)
            result = self.run_command(['register', 'test-snap'], input='y\n')

        self.assertThat(result.exit_code, Equals(1))
        self.assertThat(result.output, Contains('Registering test-snap'))
        self.assertThat(result.output, Contains('Registration failed'))
示例#5
0
    def test_registration_cancelled(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        with mock.patch.object(
                storeapi._sca_client.SCAClient, 'register') as mock_register:
            mock_register.side_effect = storeapi.errors.StoreRegistrationError(
                'test-snap', response)
            result = self.run_command(['register', 'test-snap'], input='n\n')

        self.assertThat(result.exit_code, Equals(0))
        self.assertThat(result.output, Contains(
            "Thank you! 'test-snap' will remain available"))
示例#6
0
    def test_registration_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        with mock.patch.object(
                storeapi._sca_client.SCAClient, 'register') as mock_register:
            mock_register.side_effect = storeapi.errors.StoreRegistrationError(
                'test-snap', response)
            raised = self.assertRaises(
                storeapi.errors.StoreRegistrationError,
                self.run_command, ['register', 'test-snap'], input='y\n')

        self.assertThat(str(raised), Equals('Registration failed.'))
示例#7
0
    def test_failed_login_with_store_account_info_error(self, mock_login):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        response.status_code = 500
        response.reason = 'Internal Server Error'
        mock_login.side_effect = storeapi.errors.StoreAccountInformationError(
            response)

        main(['login'])

        self.assertEqual(
            storeapi.constants.ACCOUNT_INFORMATION_ERROR + '\nLogin failed.\n',
            self.fake_logger.output)
    def test_registration_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        with mock.patch.object(storeapi.SCAClient,
                               'register') as mock_register:
            mock_register.side_effect = storeapi.errors.StoreRegistrationError(
                'test-snap', response)
            with self.assertRaises(SystemExit) as raised:
                main(['register', 'test-snap'])

        self.assertEqual(1, raised.exception.code)
        self.assertEqual('Registering test-snap.\n'
                         'Registration failed.\n', self.fake_logger.output)
示例#9
0
    def test_registration_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        self.fake_store_register.mock.side_effect = storeapi.errors.StoreRegistrationError(
            "test-snap", response)

        raised = self.assertRaises(
            storeapi.errors.StoreRegistrationError,
            self.run_command,
            ["register", "test-snap"],
            input="y\n",
        )

        self.assertThat(str(raised), Equals("Registration failed."))
示例#10
0
    def test_failed_login_with_store_account_info_error(self, mock_login):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        response.status_code = 500
        response.reason = 'Internal Server Error'
        mock_login.side_effect = storeapi.errors.StoreAccountInformationError(
            response)

        result = self.run_command(['login'])

        self.assertThat(result.exit_code, Equals(1))
        self.assertThat(result.output,
                        Contains(storeapi.constants.ACCOUNT_INFORMATION_ERROR))
        self.assertThat(result.output, Contains('Login failed.'))
示例#11
0
    def test_registration_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        with mock.patch.object(storeapi._sca_client.SCAClient,
                               "register") as mock_register:
            mock_register.side_effect = storeapi.errors.StoreRegistrationError(
                "test-snap", response)
            raised = self.assertRaises(
                storeapi.errors.StoreRegistrationError,
                self.run_command,
                ["register", "test-snap"],
                input="y\n",
            )

        self.assertThat(str(raised), Equals("Registration failed."))
示例#12
0
    def test_failed_login_with_store_account_info_error(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        response.status_code = 500
        response.reason = "Internal Server Error"
        self.fake_store_login.mock.side_effect = storeapi.errors.StoreAccountInformationError(
            response)

        result = self.run_command(["login"],
                                  input="[email protected]\nsecret\n\n")

        self.assertThat(result.exit_code, Equals(1))
        self.assertThat(result.output,
                        Contains(storeapi.constants.ACCOUNT_INFORMATION_ERROR))
        self.assertThat(result.output, Contains("Login failed."))
示例#13
0
    def test_failed_login_with_store_account_info_error(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        response.status_code = 500
        response.reason = "Internal Server Error"
        self.fake_store_login.mock.side_effect = storeapi.errors.StoreAccountInformationError(
            response)

        with pytest.raises(
                storeapi.errors.StoreAccountInformationError) as exc_info:
            self.run_command(["login"], input="[email protected]\nsecret\n\n")

        assert (
            str(exc_info.value) ==
            "Error fetching account information from store: 500 Internal Server Error"
        )
示例#14
0
    def test_register_key_failed(self):
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
        response.status_code = 500
        response.reason = "Internal Server Error"
        self.fake_store_register_key.mock.side_effect = storeapi.errors.StoreKeyRegistrationError(
            response)

        raised = self.assertRaises(
            storeapi.errors.StoreKeyRegistrationError,
            self.run_command,
            ["register-key", "default"],
            input="[email protected]\nsecret\n",
        )

        self.assertThat(
            str(raised),
            Equals("Key registration failed: 500 Internal Server Error"))
示例#15
0
    def test_register_key_failed(self, mock_installed, mock_input,
                                 mock_getpass, mock_check_output, mock_login,
                                 mock_get_account_information,
                                 mock_register_key):
        mock_installed.return_value = True
        mock_check_output.side_effect = mock_snap_output
        mock_get_account_information.return_value = {'account_id': 'abcd'}
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        response.status_code = 500
        response.reason = 'Internal Server Error'
        mock_register_key.side_effect = (
            storeapi.errors.StoreKeyRegistrationError(response))

        result = self.run_command(['register-key', 'default'])

        self.assertThat(result.exit_code, Equals(1))
        self.assertThat(
            result.output,
            Contains('Key registration failed: 500 Internal Server Error'))
    def test_register_key_failed(self, mock_installed, mock_input,
                                 mock_getpass, mock_check_output, mock_login,
                                 mock_get_account_information,
                                 mock_register_key):
        mock_installed.return_value = True
        mock_check_output.side_effect = mock_snap_output
        mock_get_account_information.return_value = {'account_id': 'abcd'}
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        response.status_code = 500
        response.reason = 'Internal Server Error'
        mock_register_key.side_effect = (
            storeapi.errors.StoreKeyRegistrationError(response))

        with self.assertRaises(SystemExit) as raised:
            main(['register-key', 'default'])

        self.assertEqual(1, raised.exception.code)
        self.assertIn('Key registration failed: 500 Internal Server Error\n',
                      self.fake_logger.output)
示例#17
0
    def test_register_key_account_info_failed(self, mock_installed, mock_input,
                                              mock_getpass, mock_check_output,
                                              mock_login,
                                              mock_get_account_information,
                                              mock_register_key, mock__login):
        mock_installed.return_value = True
        mock_check_output.side_effect = mock_snap_output
        mock__login.return_value = True
        response = mock.Mock()
        response.json.side_effect = JSONDecodeError('mock-fail', 'doc', 1)
        response.status_code = 500
        response.reason = 'Internal Server Error'
        mock_get_account_information.side_effect = (
            storeapi.errors.StoreAccountInformationError(response))

        raised = self.assertRaises(SystemExit, main,
                                   ['register-key', 'default'])

        self.assertEqual(1, raised.code)
        self.assertIn(
            'Error fetching account information from store: '
            '500 Internal Server Error\n', self.fake_logger.output)
示例#18
0
def JsJSONObject(state,
                 encoding,
                 strict,
                 scan_once,
                 object_hook,
                 object_pairs_hook,
                 memo=None,
                 _w=WHITESPACE.match,
                 _ws=WHITESPACE_STR):
    (s, end) = state
    # Backwards compatibility
    if memo is None:
        memo = {}
    memo_get = memo.setdefault
    pairs = []
    # Use a slice to prevent IndexError from being raised, the following
    # check will raise a more specific ValueError if the string is empty
    nextchar = s[end:end + 1]
    # Normally we expect nextchar == '"'
    is_js_mode = False
    if nextchar != '"':
        if nextchar in _ws:
            end = _w(s, end).end()
            nextchar = s[end:end + 1]
        # Trivial empty object
        if nextchar == '}':
            if object_pairs_hook is not None:
                result = object_pairs_hook(pairs)
                return result, end + 1
            pairs = {}
            if object_hook is not None:
                pairs = object_hook(pairs)
            return pairs, end + 1
        # digit without "
        if nextchar.isdigit():
            is_js_mode = True
            end -= 1
        elif nextchar != '"':
            raise JSONDecodeError(
                "Expecting property name enclosed in double quotes", s, end)
    end += 1
    while True:
        if is_js_mode:
            key, end = scan_digit(s, end)
            is_js_mode = False
        else:
            key, end = scanstring(s, end, encoding, strict)
        key = memo_get(key, key)

        # To skip some function call overhead we optimize the fast paths where
        # the JSON key separator is ": " or just ":".
        if s[end:end + 1] != ':':
            end = _w(s, end).end()
            if s[end:end + 1] != ':':
                raise JSONDecodeError("Expecting ':' delimiter", s, end)

        end += 1

        try:
            if s[end] in _ws:
                end += 1
                if s[end] in _ws:
                    end = _w(s, end + 1).end()
        except IndexError:
            pass

        value, end = scan_once(s, end)
        pairs.append((key, value))

        try:
            nextchar = s[end]
            if nextchar in _ws:
                end = _w(s, end + 1).end()
                nextchar = s[end]
        except IndexError:
            nextchar = ''
        end += 1

        if nextchar == '}':
            break
        elif nextchar != ',':
            raise JSONDecodeError("Expecting ',' delimiter or '}'", s, end - 1)

        try:
            nextchar = s[end]
            if nextchar in _ws:
                end += 1
                nextchar = s[end]
                if nextchar in _ws:
                    end = _w(s, end + 1).end()
                    nextchar = s[end]
        except IndexError:
            nextchar = ''

        end += 1
        if nextchar.isdigit():
            is_js_mode = True
            end -= 1
        elif nextchar != '"':
            raise JSONDecodeError(
                "Expecting property name enclosed in double quotes", s,
                end - 1)

    if object_pairs_hook is not None:
        result = object_pairs_hook(pairs)
        return result, end
    pairs = dict(pairs)
    if object_hook is not None:
        pairs = object_hook(pairs)
    return pairs, end