mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[auth] Added tests and completed QgsAuthenticationWidget
This commit is contained in:
parent
458213590d
commit
242696c1d5
@ -21,14 +21,81 @@ class QgsAuthenticationWidget : QWidget
|
||||
%End
|
||||
public:
|
||||
|
||||
explicit QgsAuthenticationWidget( QWidget *parent /TransferThis/ = 0, const QString &dataprovider = QString() );
|
||||
explicit QgsAuthenticationWidget( QWidget *parent /TransferThis/ = 0,
|
||||
const QString &configId = QString(),
|
||||
const QString &username = QString(),
|
||||
const QString &password = QString(),
|
||||
const QString &dataprovider = QString() );
|
||||
%Docstring
|
||||
Create a dialog for setting an associated authentication config, either
|
||||
from existing configs, or creating/removing them from auth database
|
||||
\param parent Parent widget
|
||||
\param configId authentication configuration id
|
||||
\param username
|
||||
\param password
|
||||
\param dataprovider The key of the calling layer provider, if applicable
|
||||
%End
|
||||
|
||||
void setWarningText( const QString &warningText );
|
||||
%Docstring
|
||||
setWarningText set the text of the warning label
|
||||
\param warningText the text of the warning label
|
||||
%End
|
||||
|
||||
void setBasicText( const QString &basicText );
|
||||
%Docstring
|
||||
setBasicText set the text of the warning label
|
||||
\param warningText the text of the basic tab label
|
||||
%End
|
||||
|
||||
const QString username( ) const;
|
||||
%Docstring
|
||||
username
|
||||
:return: basic authentication username
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
const QString password( ) const;
|
||||
%Docstring
|
||||
password
|
||||
:return: basic authentication password
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
const QString configId( ) const;
|
||||
%Docstring
|
||||
configId
|
||||
:return: authentication configuration id
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
int currentTabIndex( ) const;
|
||||
%Docstring
|
||||
currentTabIndex, mainly useful for unit tests
|
||||
:return: active tab index
|
||||
:rtype: int
|
||||
%End
|
||||
|
||||
bool btnConvertToEncryptedIsEnabled( ) const;
|
||||
%Docstring
|
||||
convertButtonEnabled, mainly useful for unit tests
|
||||
:return: true if the convert button is enabled
|
||||
:rtype: bool
|
||||
%End
|
||||
|
||||
public slots:
|
||||
|
||||
bool on_btnConvertToEncrypted_clicked( );
|
||||
%Docstring
|
||||
on_btnConvertToEncrypted_clicked create a Basic authentication configuration from
|
||||
username and password specified in the Basic tab
|
||||
:return: return true on success
|
||||
:rtype: bool
|
||||
%End
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
/************************************************************************
|
||||
|
@ -14,10 +14,106 @@
|
||||
* *
|
||||
***************************************************************************/
|
||||
#include "qgsauthenticationwidget.h"
|
||||
#include "qgsauthmanager.h"
|
||||
#include "qgsauthconfig.h"
|
||||
|
||||
QgsAuthenticationWidget::QgsAuthenticationWidget( QWidget *parent, const QString &dataprovider )
|
||||
#include <QDateTime>
|
||||
|
||||
QgsAuthenticationWidget::QgsAuthenticationWidget( QWidget *parent,
|
||||
const QString &configId,
|
||||
const QString &username,
|
||||
const QString &password,
|
||||
const QString &dataprovider )
|
||||
: QWidget( parent )
|
||||
{
|
||||
setupUi( new QgsAuthConfigSelect( this, dataprovider ) );
|
||||
mAuthConfigSelect->hide();
|
||||
setupUi( this );
|
||||
txtPassword->setText( password );
|
||||
txtUserName->setText( username );
|
||||
if ( ! dataprovider.isEmpty( ) )
|
||||
{
|
||||
mAuthConfigSelect->setDataProviderKey( dataprovider );
|
||||
}
|
||||
if ( ! configId.isEmpty( ) )
|
||||
{
|
||||
mAuthConfigSelect->setConfigId( configId );
|
||||
tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
|
||||
}
|
||||
else if ( !( username.isEmpty() && password.isEmpty( ) ) )
|
||||
{
|
||||
tabAuth->setCurrentIndex( tabAuth->indexOf( tabBasic ) );
|
||||
}
|
||||
setConvertBtnState();
|
||||
}
|
||||
|
||||
void QgsAuthenticationWidget::setWarningText( const QString &warningText )
|
||||
{
|
||||
lblWarning->setText( warningText );
|
||||
}
|
||||
|
||||
void QgsAuthenticationWidget::setBasicText( const QString &basicText )
|
||||
{
|
||||
lblBasic->setText( basicText );
|
||||
}
|
||||
|
||||
const QString QgsAuthenticationWidget::username() const
|
||||
{
|
||||
return txtUserName->text();
|
||||
}
|
||||
|
||||
const QString QgsAuthenticationWidget::password() const
|
||||
{
|
||||
return txtPassword->text();
|
||||
}
|
||||
|
||||
const QString QgsAuthenticationWidget::configId() const
|
||||
{
|
||||
return mAuthConfigSelect->configId();
|
||||
}
|
||||
|
||||
int QgsAuthenticationWidget::currentTabIndex() const
|
||||
{
|
||||
return tabAuth->currentIndex( );
|
||||
}
|
||||
|
||||
bool QgsAuthenticationWidget::btnConvertToEncryptedIsEnabled() const
|
||||
{
|
||||
return btnConvertToEncrypted->isEnabled( );
|
||||
}
|
||||
|
||||
bool QgsAuthenticationWidget::on_btnConvertToEncrypted_clicked()
|
||||
{
|
||||
tabAuth->setCurrentIndex( tabAuth->indexOf( tabConfigurations ) );
|
||||
QgsAuthMethodConfig config( QStringLiteral( "Basic" ) );
|
||||
config.setName( tr( "Converted config %1" ).arg( QDateTime::currentDateTime().toString( ) ) );
|
||||
config.setConfig( QStringLiteral( "username" ), txtUserName->text() );
|
||||
config.setConfig( QStringLiteral( "password" ), txtPassword->text() );
|
||||
if ( ! QgsAuthManager::instance()->storeAuthenticationConfig( config ) )
|
||||
{
|
||||
mAuthConfigSelect->showMessage( tr( "Couldn't create a Basic authentication configuration!" ) );
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
txtUserName->setText( QString( ) );
|
||||
txtPassword->setText( QString( ) );
|
||||
mAuthConfigSelect->setConfigId( config.id( ) );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void QgsAuthenticationWidget::on_txtUserName_textChanged( const QString &text )
|
||||
{
|
||||
Q_UNUSED( text );
|
||||
setConvertBtnState();
|
||||
}
|
||||
|
||||
void QgsAuthenticationWidget::on_txtPassword_textChanged( const QString &text )
|
||||
{
|
||||
Q_UNUSED( text );
|
||||
setConvertBtnState();
|
||||
}
|
||||
|
||||
void QgsAuthenticationWidget::setConvertBtnState()
|
||||
{
|
||||
btnConvertToEncrypted->setEnabled( ! txtUserName->text().isEmpty() || ! txtPassword->text().isEmpty() );
|
||||
}
|
||||
|
@ -38,9 +38,85 @@ class GUI_EXPORT QgsAuthenticationWidget : public QWidget, private Ui::QgsAuthen
|
||||
* Create a dialog for setting an associated authentication config, either
|
||||
* from existing configs, or creating/removing them from auth database
|
||||
* \param parent Parent widget
|
||||
* \param configId authentication configuration id
|
||||
* \param username
|
||||
* \param password
|
||||
* \param dataprovider The key of the calling layer provider, if applicable
|
||||
*/
|
||||
explicit QgsAuthenticationWidget( QWidget *parent SIP_TRANSFERTHIS = 0, const QString &dataprovider = QString() );
|
||||
explicit QgsAuthenticationWidget( QWidget *parent SIP_TRANSFERTHIS = 0,
|
||||
const QString &configId = QString(),
|
||||
const QString &username = QString(),
|
||||
const QString &password = QString(),
|
||||
const QString &dataprovider = QString() );
|
||||
|
||||
/**
|
||||
* \brief setWarningText set the text of the warning label
|
||||
* \param warningText the text of the warning label
|
||||
*/
|
||||
void setWarningText( const QString &warningText );
|
||||
|
||||
/**
|
||||
* \brief setBasicText set the text of the warning label
|
||||
* \param warningText the text of the basic tab label
|
||||
*/
|
||||
void setBasicText( const QString &basicText );
|
||||
|
||||
/**
|
||||
* \brief username
|
||||
* \return basic authentication username
|
||||
*/
|
||||
const QString username( ) const;
|
||||
|
||||
/**
|
||||
* \brief password
|
||||
* \return basic authentication password
|
||||
*/
|
||||
const QString password( ) const;
|
||||
|
||||
/**
|
||||
* \brief configId
|
||||
* \return authentication configuration id
|
||||
*/
|
||||
const QString configId( ) const;
|
||||
|
||||
/**
|
||||
* \brief currentTabIndex, mainly useful for unit tests
|
||||
* \return active tab index
|
||||
*/
|
||||
int currentTabIndex( ) const;
|
||||
|
||||
/**
|
||||
* \brief convertButtonEnabled, mainly useful for unit tests
|
||||
* \return true if the convert button is enabled
|
||||
*/
|
||||
bool btnConvertToEncryptedIsEnabled( ) const;
|
||||
|
||||
public slots:
|
||||
|
||||
/**
|
||||
* \brief on_btnConvertToEncrypted_clicked create a Basic authentication configuration from
|
||||
* username and password specified in the Basic tab
|
||||
* \return return true on success
|
||||
*/
|
||||
bool on_btnConvertToEncrypted_clicked( );
|
||||
|
||||
/**
|
||||
* \brief on_txtUserName_textChanged set convert button state
|
||||
* \param Not available in Python bindings
|
||||
*/
|
||||
void on_txtUserName_textChanged( const QString &text ) SIP_SKIP;
|
||||
|
||||
/**
|
||||
* \brief on_txtPassword_textChanged set convert button state
|
||||
* \param text
|
||||
* \note Not available in Python bindings
|
||||
*/
|
||||
void on_txtPassword_textChanged( const QString &text ) SIP_SKIP;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void setConvertBtnState( );
|
||||
|
||||
};
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tabConfigurations">
|
||||
<attribute name="title">
|
||||
@ -48,8 +48,11 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QgsAuthConfigSelect" name="mAuthConfigSelect">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
@ -93,9 +96,9 @@
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<widget class="QLabel" name="lblWarning">
|
||||
<property name="text">
|
||||
<string><html><head/><body><p><span style=" font-weight:600; color:#ff0000;">Note: credentials are stored unencrypted (in clear text) in the project file.</span></p></body></html></string>
|
||||
<string><html><head/><body><p><span style=" font-weight:600; color:#ff0000;">Warning: credentials are stored unencrypted (in clear text) in the project file!</span></p></body></html></string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
@ -165,9 +168,9 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<widget class="QPushButton" name="btnConvertToEncrypted">
|
||||
<property name="text">
|
||||
<string>Convert to encrypted</string>
|
||||
<string>Convert to configuration</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@ -192,6 +195,13 @@
|
||||
<header>qgsauthconfigselect.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<tabstops>
|
||||
<tabstop>tabAuth</tabstop>
|
||||
<tabstop>mAuthConfigSelect</tabstop>
|
||||
<tabstop>txtUserName</tabstop>
|
||||
<tabstop>txtPassword</tabstop>
|
||||
<tabstop>btnConvertToEncrypted</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
@ -69,14 +69,75 @@ class TestAuthenticationWidget(unittest.TestCase):
|
||||
"""Run after each test."""
|
||||
pass
|
||||
|
||||
def testWidget(self):
|
||||
def testWidgetNoArgs(self):
|
||||
"""
|
||||
Test the widget
|
||||
Test the widget with no args
|
||||
"""
|
||||
w = QgsAuthenticationWidget()
|
||||
w.show()
|
||||
from IPython import embed
|
||||
embed()
|
||||
self.assertEqual(w.username(), '')
|
||||
self.assertEqual(w.password(), '')
|
||||
self.assertEqual(w.configId(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 0)
|
||||
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
|
||||
|
||||
def testWidgetConfigId(self):
|
||||
"""
|
||||
Test the widget with configId
|
||||
"""
|
||||
w = QgsAuthenticationWidget(None, self.auth_config.id())
|
||||
self.assertEqual(w.username(), '')
|
||||
self.assertEqual(w.password(), '')
|
||||
self.assertEqual(w.configId(), self.auth_config.id())
|
||||
self.assertEqual(w.currentTabIndex(), 0)
|
||||
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
|
||||
|
||||
def testWidgetUsername(self):
|
||||
"""
|
||||
Test the widget with username only
|
||||
"""
|
||||
w = QgsAuthenticationWidget(None, None, 'username')
|
||||
self.assertEqual(w.username(), 'username')
|
||||
self.assertEqual(w.password(), '')
|
||||
self.assertEqual(w.configId(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 1)
|
||||
|
||||
def testWidgetPassword(self):
|
||||
"""
|
||||
Test the widget with password only
|
||||
"""
|
||||
w = QgsAuthenticationWidget(None, None, None, 'password')
|
||||
self.assertEqual(w.username(), '')
|
||||
self.assertEqual(w.password(), 'password')
|
||||
self.assertEqual(w.configId(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 1)
|
||||
|
||||
def testWidgetUsernameAndPassword(self):
|
||||
"""
|
||||
Test the widget with username and password
|
||||
"""
|
||||
w = QgsAuthenticationWidget(None, None, 'username', 'password')
|
||||
self.assertEqual(w.username(), 'username')
|
||||
self.assertEqual(w.password(), 'password')
|
||||
self.assertEqual(w.configId(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 1)
|
||||
self.assertTrue(w.btnConvertToEncryptedIsEnabled())
|
||||
|
||||
def testConvertToEncrypted(self):
|
||||
"""
|
||||
Test the widget to encrypted conversion
|
||||
"""
|
||||
w = QgsAuthenticationWidget(None, None, 'username', 'password')
|
||||
self.assertEqual(w.username(), 'username')
|
||||
self.assertEqual(w.password(), 'password')
|
||||
self.assertEqual(w.configId(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 1)
|
||||
self.assertTrue(w.btnConvertToEncryptedIsEnabled())
|
||||
self.assertTrue(w.on_btnConvertToEncrypted_clicked())
|
||||
self.assertNotEqual(w.configId(), '')
|
||||
self.assertEqual(w.username(), '')
|
||||
self.assertEqual(w.password(), '')
|
||||
self.assertEqual(w.currentTabIndex(), 0)
|
||||
self.assertFalse(w.btnConvertToEncryptedIsEnabled())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
x
Reference in New Issue
Block a user