def test_upload_file_raises_exception_when_invalid_response(self):
        self.connection_mock.send.return_value = self._connection_response(
            'invalidJsonResponse')

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            with self.assertRaises(ConnectionError) as res:
                self.ftd_plugin.upload_file('/tmp/test.txt', '/files')

        assert 'Invalid JSON response' in str(res.exception)
    def test_download_file(self):
        self.connection_mock.send.return_value = self._connection_response(
            'File content')

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            self.ftd_plugin.download_file('/files/1', '/tmp/test.txt')

        open_mock.assert_called_once_with('/tmp/test.txt', 'wb')
        open_mock().write.assert_called_once_with(b'File content')
def test_systemid_with_requirements(capfd, mocker, patch_rhn):
    """Check 'msg' and 'changed' results"""

    mocker.patch.object(rhn_register.Rhn, 'enable')
    mock_isfile = mocker.patch('os.path.isfile', return_value=True)
    mocker.patch(
        'ansible_collections.misc.not_a_real_collection.plugins.modules.rhn_register.open',
        mock_open(read_data=SYSTEMID),
        create=True)
    rhn = rhn_register.Rhn()
    assert '123456789' == to_native(rhn.systemid)
 def test_validate_credentials_file(self):
     # TODO(supertom): Only dealing with p12 here, check the other states
     # of this function
     module = FakeModule()
     with mock.patch(
             'ansible_collections.misc.not_a_real_collection.plugins.module_utils.gcp.open',
             mock.mock_open(read_data='foobar'),
             create=True):
         # pem condition, warning is suppressed with the return_value
         credentials_file = '/foopath/pem.pem'
         with self.assertRaises(ValueError):
             _validate_credentials_file(module,
                                        credentials_file=credentials_file,
                                        require_valid_json=False,
                                        check_libcloud=False)
    def test_download_file_should_extract_filename_from_headers(self):
        filename = 'test_file.txt'
        response = mock.Mock()
        response.info.return_value = {
            'Content-Disposition': 'attachment; filename="%s"' % filename
        }
        dummy, response_data = self._connection_response('File content')
        self.connection_mock.send.return_value = response, response_data

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            self.ftd_plugin.download_file('/files/1', '/tmp/')

        open_mock.assert_called_once_with('/tmp/%s' % filename, 'wb')
        open_mock().write.assert_called_once_with(b'File content')
def test_systemid_requirements_missing(capfd, mocker, patch_rhn,
                                       import_libxml):
    """Check that missing dependencies are detected"""

    mocker.patch('os.path.isfile', return_value=True)
    mocker.patch(
        'ansible_collections.misc.not_a_real_collection.plugins.modules.rhn_register.open',
        mock_open(read_data=SYSTEMID),
        create=True)

    with pytest.raises(SystemExit):
        rhn_register.main()

    out, err = capfd.readouterr()
    results = json.loads(out)
    assert results['failed']
    assert 'Missing arguments' in results['msg']
    def test_upload_file(self):
        self.connection_mock.send.return_value = self._connection_response(
            {'id': '123'})

        open_mock = mock_open()
        with patch('%s.open' % BUILTINS_NAME, open_mock):
            resp = self.ftd_plugin.upload_file('/tmp/test.txt', '/files')

        assert {'id': '123'} == resp
        exp_headers = dict(BASE_HEADERS)
        exp_headers['Content-Length'] = len('--Encoded data--')
        exp_headers['Content-Type'] = 'multipart/form-data'
        self.connection_mock.send.assert_called_once_with(
            '/files',
            data='--Encoded data--',
            headers=exp_headers,
            method=HTTPMethod.POST)
        open_mock.assert_called_once_with('/tmp/test.txt', 'rb')