diff --git a/cura/API/Account.py b/cura/API/Account.py index f87f613382..7f15ad7efb 100644 --- a/cura/API/Account.py +++ b/cura/API/Account.py @@ -90,23 +90,23 @@ class Account(QObject): self.loginStateChanged.emit(logged_in) @pyqtSlot() - def login(self) -> None: - if self._logged_in: - # Nothing to do, user already logged in. - return - self._authorization_service.startAuthorizationFlow() - - @pyqtSlot() - def loginWithForcedLogout(self) -> None: + @pyqtSlot(bool) + def login(self, force_logout_before_login: bool = False) -> None: """ - Forces a logout from Cura and then initiates the authorization flow with the force_browser_logout variable - as true, to sync the accounts in Cura and in the browser. + Initializes the login process. If the user is logged in already and force_logout_before_login is true, Cura will + logout from the account before initiating the authorization flow. If the user is logged in and + force_logout_before_login is false, the function will return, as there is nothing to do. + :param force_logout_before_login: Optional boolean parameter :return: None """ if self._logged_in: - self.logout() - self._authorization_service.startAuthorizationFlow(True) + if force_logout_before_login: + self.logout() + else: + # Nothing to do, user already logged in. + return + self._authorization_service.startAuthorizationFlow(force_logout_before_login) @pyqtProperty(str, notify=loginStateChanged) def userName(self): diff --git a/resources/qml/WelcomePages/AddCloudPrintersView.qml b/resources/qml/WelcomePages/AddCloudPrintersView.qml index 511328b0c8..ccaaa6908f 100644 --- a/resources/qml/WelcomePages/AddCloudPrintersView.qml +++ b/resources/qml/WelcomePages/AddCloudPrintersView.qml @@ -93,7 +93,7 @@ Item color: UM.Theme.getColor("text_link") MouseArea { anchors.fill: parent; - onClicked: Cura.API.account.loginWithForcedLogout() + onClicked: Cura.API.account.login(true) hoverEnabled: true onEntered: { diff --git a/tests/API/TestAccount.py b/tests/API/TestAccount.py index 1b23947cc0..09091ba7e0 100644 --- a/tests/API/TestAccount.py +++ b/tests/API/TestAccount.py @@ -19,35 +19,23 @@ def test_login(): account = Account(MagicMock()) mocked_auth_service = MagicMock() account._authorization_service = mocked_auth_service + account.logout = MagicMock() account.login() - mocked_auth_service.startAuthorizationFlow.assert_called_once_with() + mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False) # Fake a successful login account._onLoginStateChanged(True) # Attempting to log in again shouldn't change anything. account.login() - mocked_auth_service.startAuthorizationFlow.assert_called_once_with() + mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False) - -def test_loginWithForcedLogout(): - account = Account(MagicMock()) - mocked_auth_service = MagicMock() - account._authorization_service = mocked_auth_service - account.logout = MagicMock() - - # Fake a successful login - account._onLoginStateChanged(True) - account.loginWithForcedLogout() - # Make sure logout is called once - account.logout.assert_called_once_with() - mocked_auth_service.startAuthorizationFlow.assert_called_once_with(True) - - account._onLoginStateChanged(False) - account.loginWithForcedLogout() - # If we are not logged in previously, logout shouldn't be called again + # Attempting to log in with force_logout_before_login as True should call the logout before calling the + # startAuthorizationFlow(True). + account.login(force_logout_before_login=True) account.logout.assert_called_once_with() + mocked_auth_service.startAuthorizationFlow.assert_called_with(True) assert mocked_auth_service.startAuthorizationFlow.call_count == 2