class TestSignInForm(GuiTest): @patch( 'orangecontrib.bioinformatics.widgets.components.resolwe.get_credential_manager', return_value=CredentialManager('resolwe_credentials_test'), ) @patch('orangecontrib.bioinformatics.widgets.components.resolwe.connect') def test_dialog_success(self, mocked_connect, mocked_cm): widget = MockWidget() dialog = SignIn(widget) self.assertFalse(dialog.sign_in_btn.isEnabled()) dialog.sign_in() mocked_connect.assert_not_called() self.assertIsNone(dialog.resolwe_instance) dialog.username_line_edit.setText('foo') dialog.sign_in() mocked_connect.assert_not_called() dialog.password_line_edit.setText('bar') self.assertTrue(dialog.sign_in_btn.isEnabled()) dialog.sign_in() mocked_connect.assert_called_once() self.assertTrue(dialog.error_msg.isHidden()) self.assertIsNotNone(dialog.resolwe_instance) # cleanup cm = CredentialManager('resolwe_credentials_test') if cm.username: del cm.username if cm.password: del cm.password @patch( 'orangecontrib.bioinformatics.widgets.components.resolwe.get_credential_manager', return_value=CredentialManager('resolwe_credentials_test'), ) @patch('orangecontrib.bioinformatics.widgets.components.resolwe.connect', side_effect=ResolweAuthError()) def test_dialog_fail(self, mocked_connect, mocked_cm): widget = MockWidget() dialog = SignIn(widget) dialog.username_line_edit.setText('foo') dialog.password_line_edit.setText('bar') self.assertTrue(dialog.error_msg.isHidden()) dialog.sign_in() self.assertFalse(dialog.error_msg.isHidden()) mocked_connect.assert_called_once()
def test_dialog_success(self, mocked_connect, mocked_cm): widget = MockWidget() dialog = SignIn(widget) self.assertFalse(dialog.sign_in_btn.isEnabled()) dialog.sign_in() mocked_connect.assert_not_called() self.assertIsNone(dialog.resolwe_instance) dialog.username_line_edit.setText('foo') dialog.sign_in() mocked_connect.assert_not_called() dialog.password_line_edit.setText('bar') self.assertTrue(dialog.sign_in_btn.isEnabled()) dialog.sign_in() mocked_connect.assert_called_once() self.assertTrue(dialog.error_msg.isHidden()) self.assertIsNotNone(dialog.resolwe_instance) # cleanup cm = CredentialManager('resolwe_credentials_test') if cm.username: del cm.username if cm.password: del cm.password
def sign_out(self): # Use public credentials when user signs out self.res = connect(url=DEFAULT_URL) # Remove username and cm = CredentialManager(CREDENTIAL_MANAGER_SERVICE) del cm.username del cm.password
def get_credential_manager(server_type: str) -> CredentialManager: if server_type == RESOLWE_PLATFORM: service_name = resapi.CREDENTIAL_MANAGER_SERVICE elif server_type == GENESIS_PLATFORM: service_name = genapi.CREDENTIAL_MANAGER_SERVICE else: raise ValueError( 'Unexpected server type. Available options: resolwe or genesis.') return CredentialManager(service_name)
class EmailCredentialsDialog(OWWidget): name = "Pubmed Email" want_main_area = False resizing_enabled = False email_manager = CredentialManager('Email') email_input = '' class Error(OWWidget.Error): invalid_credentials = Msg('This email is invalid.') def __init__(self, parent): super().__init__() self.parent = parent self.api = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.email_edit = gui.lineEdit(self, self, 'email_input', controlWidth=400) form.addRow('Email:', self.email_edit) self.controlArea.layout().addLayout(form) self.submit_button = gui.button(self.controlArea, self, "OK", self.accept) self.load_credentials() def setVisible(self, visible): super().setVisible(visible) self.email_edit.setFocus() def load_credentials(self): self.email_edit.setText(self.email_manager.key) def save_credentials(self): self.email_manager.key = self.email_input def check_credentials(self): if validate_email(self.email_input): self.save_credentials() return True else: return False def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() valid = self.check_credentials() if valid: self.parent.sync_email(self.email_input) super().accept() else: self.Error.invalid_credentials()
def __init__(self, parent): super().__init__() self.cm_key = CredentialManager("Twitter Bearer Token") self.parent = parent box = gui.vBox(self.controlArea, "Bearer Token") self.key_edit = QPlainTextEdit() box.layout().addWidget(self.key_edit) self.submit_button = gui.button(self.buttonsArea, self, "OK", self.accept) self.load_credentials()
class APICredentialsDialog(OWWidget): name = "New York Times API key" want_main_area = False resizing_enabled = False cm_key = CredentialManager('NY Times API Key') key_input = '' class Error(OWWidget.Error): invalid_credentials = Msg( 'This credentials are invalid. ' 'Check the key and your internet connection.') def __init__(self, parent): super().__init__() self.parent = parent self.api = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.key_edit = gui.lineEdit(self, self, 'key_input', controlWidth=400) form.addRow('Key:', self.key_edit) self.controlArea.layout().addLayout(form) self.submit_button = gui.button(self.controlArea, self, "OK", self.accept) self.load_credentials() def load_credentials(self): self.key_edit.setText(self.cm_key.key) def save_credentials(self): self.cm_key.key = self.key_input def check_credentials(self): api = NYT(self.key_input) if api.api_key_valid(): self.save_credentials() else: api = None self.api = api def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() self.check_credentials() if self.api: self.parent.update_api(self.api) super().accept() elif not silent: self.Error.invalid_credentials()
class CredentialsDialog(OWWidget): name = 'The Guardian Credentials' want_main_area = False resizing_enabled = False cm_key = CredentialManager('The Guardian API Key') key_input = 'test' class Error(OWWidget.Error): invalid_credentials = Msg('These credentials are invalid.') def __init__(self, parent): super().__init__() self.parent = parent self.api = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.key_edit = gui.lineEdit(self, self, 'key_input', controlWidth=350) form.addRow('Key:', self.key_edit) self.controlArea.layout().addLayout(form) self.submit_button = gui.button(self.controlArea, self, 'OK', self.accept) self.load_credentials() def load_credentials(self): if self.cm_key.key: self.key_edit.setText(self.cm_key.key) def save_credentials(self): self.cm_key.key = self.key_input def check_credentials(self): api = TheGuardianCredentials(self.key_input) if api.valid: self.save_credentials() else: api = None self.api = api def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() self.check_credentials() if self.api: self.parent.update_api(self.api) super().accept() elif not silent: self.Error.invalid_credentials()
def test_dialog_fail(self, mocked_connect): widget = MockWidget() dialog = SignInForm(widget) dialog.username_line_edit.setText('foo') dialog.password_line_edit.setText('bar') self.assertTrue(dialog.error_msg.isHidden()) dialog.sign_in() self.assertFalse(dialog.error_msg.isHidden()) mocked_connect.assert_called_once() # cleanup cm = CredentialManager('resolwe_credentials_test') del cm.username del cm.password
def __init__(self, flags, *args, **kwargs): super().__init__(flags, *args, **kwargs) self.cm: CredentialManager = CredentialManager(CREDENTIAL_MANAGER_SERVICE) self.setWindowTitle('Sign in') self.setFixedSize(400, 250) self.server_cb_label = QLabel('Server *') self.server_cb = QComboBox(self) self.server_cb.addItems(RESOLWE_URLS) self.server_cb.setEditable(True) self.username_label = QLabel('Username *') self.username_line_edit = QLineEdit(self) self.username_line_edit.setPlaceholderText('Enter correct username') self.username_line_edit.returnPressed.connect(self.sign_in) self.username_line_edit.textChanged.connect(self.handle_sign_in_btn) self.password_label = QLabel('Password *') self.password_line_edit = QLineEdit(self) self.password_line_edit.setPlaceholderText('Enter correct password') self.password_line_edit.returnPressed.connect(self.sign_in) self.password_line_edit.textChanged.connect(self.handle_sign_in_btn) self.password_line_edit.setEchoMode(QLineEdit.Password) self.sign_in_btn = QPushButton('Sign in', self) self.sign_in_btn.setDisabled(True) self.sign_in_btn.clicked.connect(self.sign_in) self.error_msg = QLabel('Unable to log in with provided credentials.') self.error_msg.setStyleSheet('color:red') self.error_msg.hide() layout = QVBoxLayout(self) layout.addWidget(self.server_cb_label) layout.addWidget(self.server_cb) layout.addWidget(self.username_label) layout.addWidget(self.username_line_edit) layout.addWidget(self.password_label) layout.addWidget(self.password_line_edit) layout.addWidget(self.error_msg) layout.addStretch() layout.addWidget(self.sign_in_btn) self.resolwe_instance = None
def __init__(self, parent): super().__init__() self.cm_key = CredentialManager('NY Times API Key') self.parent = parent self.api = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.key_edit = gui.lineEdit(self, self, 'key_input', controlWidth=400) form.addRow('Key:', self.key_edit) self.controlArea.layout().addLayout(form) self.submit_button = gui.button(self.controlArea, self, "OK", self.accept) self.load_credentials()
class MockWidget(OWWidget): name = 'MockWidget' want_main_area = False filter_component = SettingProvider(CollapsibleFilterComponent) pagination_component = SettingProvider(PaginationComponent) pagination_availability = pyqtSignal(bool, bool) @patch( 'orangecontrib.bioinformatics.widgets.components.resolwe.get_credential_manager', return_value=CredentialManager('resolwe_credentials_test'), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.filter_component = CollapsibleFilterComponent( self, self.controlArea) self.pagination_component = PaginationComponent(self, self.controlArea) self.sign_in_dialog = SignIn(self)
def _credential_manager(self, host, port): return CredentialManager("SQL Table: {}:{}".format(host, port))
class CredentialsDialog(OWWidget): name = 'The Facebook Credentials' want_main_area = False resizing_enabled = False cm = CredentialManager('Facebook orange') app_id_input = '' app_secret_input = '' temp_token_input = '' token_input = '' class Error(OWWidget.Error): invalid_credentials = Msg('These credentials are invalid.') def __init__(self, parent): super().__init__() self.parent = parent self.api = None self.info = gui.widgetLabel(self.controlArea, 'There are two ways to connect. Either register a Facebook app or obtain a temporary access token. Both require a Facebook account.') self.info.setWordWrap(True); login_box = gui.hBox(self.controlArea) login_box.setMinimumWidth(300) app_login = gui.widgetBox(login_box, box='Option 1: App login') temp_login = gui.widgetBox(login_box, box = 'Option 2: Temporary access token') ## app login app_form = QFormLayout() app_form.setContentsMargins(5, 5, 5, 5) app_info = gui.widgetLabel(app_login, 'To obtain an App ID and secret, <a href=\"https://developers.facebook.com/?advanced_app_create=true\">register</a> a new app (no actual app required). If you already registered an app you need to <a href=\"https://developers.facebook.com/apps/\">visit your app page</a>. The information is on the app dashboard.') app_info.setWordWrap(True); app_info.setOpenExternalLinks(True) self.app_id_edit = gui.lineEdit(self, self, 'app_id_input', controlWidth=350) self.app_secret_edit = gui.lineEdit(self, self, 'app_secret_input', controlWidth=350) app_form.addRow('App ID:', self.app_id_edit) app_form.addRow('App secret:', self.app_secret_edit) app_login.layout().addLayout(app_form) self.submit_button = gui.button(app_login, self, 'Connect', self.app_accept) ## temp login temp_form = QFormLayout() temp_form.setContentsMargins(5, 5, 5, 5) temp_info = gui.widgetLabel(temp_login, 'To obtain a temporary (1 hour) access token, visit <a href=\"https://developers.facebook.com/tools/explorer">here</a>. Copy the token (the long line of gibberish) from the "Access Token:" box.') temp_info.setWordWrap(True); temp_info.setOpenExternalLinks(True) self.temp_token_edit = gui.lineEdit(self, self, 'temp_token_input', controlWidth=350) temp_form.addRow('Access Token:', self.temp_token_edit) temp_login.layout().addLayout(temp_form) self.submit_button = gui.button(temp_login, self, 'Connect', self.temp_accept) self.load_credentials() def load_credentials(self): if self.cm.token: self.token_input = self.cm.token if '|' in self.cm.token: token = self.cm.token.split('|') self.app_id_input = token[0] self.app_secret_input = token[1] else: self.temp_token_input = self.cm.token def save_credentials(self): self.cm.token = self.token_input def check_credentials(self, drop_token=True): self.credentials = FacebookCredentials(self.token_input) self.token_input = self.credentials.token self.save_credentials() if not self.credentials.valid: self.credentials = None def app_accept(self): self.token_input = self.app_id_input + '|' + self.app_secret_input self.accept() def temp_accept(self): self.token_input = self.temp_token_input self.accept() def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() self.check_credentials(drop_token = not silent) ## first time loading, use token from last session self.parent.update_api(self.credentials) if self.credentials: super().accept() elif not silent: self.Error.invalid_credentials()
def test_credential_manager(self): cm = CredentialManager('Orange') cm.key = 'Foo' self.assertEqual(cm.key, 'Foo') del cm.key self.assertEqual(cm.key, None)
class APICredentialsDialog(OWWidget): name = "Twitter API Credentials" want_main_area = False resizing_enabled = False cm_key = CredentialManager("Twitter API Key") cm_secret = CredentialManager("Twitter API Secret") key_input = "" secret_input = "" class Error(OWWidget.Error): invalid_credentials = Msg("This credentials are invalid.") def __init__(self, parent): super().__init__() self.parent = parent self.credentials = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.key_edit = gui.lineEdit(self, self, "key_input", controlWidth=400) form.addRow("Key:", self.key_edit) self.secret_edit = gui.lineEdit(self, self, "secret_input", controlWidth=400) form.addRow("Secret:", self.secret_edit) self.controlArea.layout().addLayout(form) self.submit_button = gui.button(self.controlArea, self, "OK", self.accept) self.load_credentials() def load_credentials(self): self.key_edit.setText(self.cm_key.key) self.secret_edit.setText(self.cm_secret.key) def save_credentials(self): self.cm_key.key = self.key_input self.cm_secret.key = self.secret_input def check_credentials(self): c = twitter.Credentials(self.key_input, self.secret_input) if self.credentials != c: if c.valid: self.save_credentials() else: c = None self.credentials = c def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() self.check_credentials() if self.credentials and self.credentials.valid: self.parent.update_api(self.credentials) super().accept() elif not silent: self.Error.invalid_credentials()
def setUp(self): self.cm = CredentialManager('Orange') self.cm.key = "Foo"
class CredentialsDialog(OWWidget): name = 'The AmCAT Credentials' want_main_area = False resizing_enabled = False cm = CredentialManager('AmCAT orange') host_input = 'https://amcat.nl' user_input = '' passwd_input = '' token_input = '' class Error(OWWidget.Error): invalid_credentials = Msg('These credentials are invalid.') def __init__(self, parent): super().__init__() self.parent = parent self.api = None form = QFormLayout() form.setContentsMargins(5, 5, 5, 5) self.host_edit = gui.lineEdit(self, self, 'host_input', controlWidth=150) self.user_edit = gui.lineEdit(self, self, 'user_input', controlWidth=150) self.passwd_edit = gui.lineEdit(self, self, 'passwd_input', controlWidth=150) self.passwd_edit.setEchoMode(QLineEdit.Password) tokenbox = gui.vBox(self) self.submit_button = gui.button(tokenbox, self, 'request new token', self.accept, width=100) self.token_edit = gui.lineEdit(tokenbox, self, 'token_input', controlWidth=200) form.addRow('Host:', self.host_edit) form.addRow('username:'******'password:'******'\n') def save_credentials(self): self.cm.token = '{}\n{}\n{}'.format(self.host_input, self.user_input, self.token_input) def check_credentials(self, drop_token=True): if drop_token: token = None else: token = self.token_input or None if token or self.passwd_input: try: api = AmcatAPI(self.host_input, self.user_input, self.passwd_input, token) if api.token is None: api = None except (APIError, HTTPError) as e: logging.exception("Error on getting credentials") api = None else: api = None self.passwd_input = '' self.token_input = api and api.token self.save_credentials() self.api = api def accept(self, silent=False): if not silent: self.Error.invalid_credentials.clear() self.check_credentials( drop_token=not silent ) ## first time loading, use token from last session self.parent.update_api( self.api ) ## always update parent, to enable the user break the token if self.api: super().accept() elif not silent: self.Error.invalid_credentials()
def setUp(self): self._ring = keyring.get_keyring() keyring.set_keyring(Keyring()) self.cm = CredentialManager('Orange')
def test_credential_manager(self): cm = CredentialManager("Orange") cm.key = "Foo" self.assertEqual(cm.key, "Foo") del cm.key self.assertEqual(cm.key, None)