Make login parametrized with a force_logout boolean

Instead of using a separate function to force logging out before
login, login now has a boolean parameter that instructs it to logout
before loging in again, if the user is alread logged in. It then
starts the authorization with a force browser logout first.

CURA-7427
This commit is contained in:
Kostas Karmas 2020-05-12 16:54:41 +02:00
parent 898ca852f0
commit eac4d3e463
3 changed files with 20 additions and 32 deletions

View file

@ -90,23 +90,23 @@ class Account(QObject):
self.loginStateChanged.emit(logged_in) self.loginStateChanged.emit(logged_in)
@pyqtSlot() @pyqtSlot()
def login(self) -> None: @pyqtSlot(bool)
if self._logged_in: def login(self, force_logout_before_login: bool = False) -> None:
# Nothing to do, user already logged in.
return
self._authorization_service.startAuthorizationFlow()
@pyqtSlot()
def loginWithForcedLogout(self) -> None:
""" """
Forces a logout from Cura and then initiates the authorization flow with the force_browser_logout variable Initializes the login process. If the user is logged in already and force_logout_before_login is true, Cura will
as true, to sync the accounts in Cura and in the browser. 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 :return: None
""" """
if self._logged_in: if self._logged_in:
self.logout() if force_logout_before_login:
self._authorization_service.startAuthorizationFlow(True) self.logout()
else:
# Nothing to do, user already logged in.
return
self._authorization_service.startAuthorizationFlow(force_logout_before_login)
@pyqtProperty(str, notify=loginStateChanged) @pyqtProperty(str, notify=loginStateChanged)
def userName(self): def userName(self):

View file

@ -93,7 +93,7 @@ Item
color: UM.Theme.getColor("text_link") color: UM.Theme.getColor("text_link")
MouseArea { MouseArea {
anchors.fill: parent; anchors.fill: parent;
onClicked: Cura.API.account.loginWithForcedLogout() onClicked: Cura.API.account.login(true)
hoverEnabled: true hoverEnabled: true
onEntered: onEntered:
{ {

View file

@ -19,35 +19,23 @@ def test_login():
account = Account(MagicMock()) account = Account(MagicMock())
mocked_auth_service = MagicMock() mocked_auth_service = MagicMock()
account._authorization_service = mocked_auth_service account._authorization_service = mocked_auth_service
account.logout = MagicMock()
account.login() account.login()
mocked_auth_service.startAuthorizationFlow.assert_called_once_with() mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
# Fake a successful login # Fake a successful login
account._onLoginStateChanged(True) account._onLoginStateChanged(True)
# Attempting to log in again shouldn't change anything. # Attempting to log in again shouldn't change anything.
account.login() account.login()
mocked_auth_service.startAuthorizationFlow.assert_called_once_with() mocked_auth_service.startAuthorizationFlow.assert_called_once_with(False)
# Attempting to log in with force_logout_before_login as True should call the logout before calling the
def test_loginWithForcedLogout(): # startAuthorizationFlow(True).
account = Account(MagicMock()) account.login(force_logout_before_login=True)
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
account.logout.assert_called_once_with() account.logout.assert_called_once_with()
mocked_auth_service.startAuthorizationFlow.assert_called_with(True)
assert mocked_auth_service.startAuthorizationFlow.call_count == 2 assert mocked_auth_service.startAuthorizationFlow.call_count == 2