File: TestAccount.py

package info (click to toggle)
cura 5.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 122,888 kB
  • sloc: python: 44,572; sh: 81; xml: 32; makefile: 16
file content (132 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from unittest.mock import MagicMock, patch

import pytest

from cura.API import Account
from cura.API.Account import SyncState
from cura.OAuth2.Models import UserProfile


@pytest.fixture()
def user_profile():
    result = UserProfile()
    result.username = "username!"
    result.profile_image_url = "profile_image_url!"
    result.user_id = "user_id!"
    return result


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(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(False)

    # 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


def test_initialize():
    account = Account(MagicMock())
    mocked_auth_service = MagicMock()
    account._authorization_service = mocked_auth_service

    account.initialize()
    mocked_auth_service.loadAuthDataFromPreferences.assert_called_once_with()


def test_logout():
    account = Account(MagicMock())
    mocked_auth_service = MagicMock()
    account._authorization_service = mocked_auth_service

    account.logout()
    mocked_auth_service.deleteAuthData.assert_not_called()  # We weren't logged in, so nothing should happen
    assert not account.isLoggedIn

    # Pretend the stage changed
    account._onLoginStateChanged(True)
    assert account.isLoggedIn

    account.logout()
    mocked_auth_service.deleteAuthData.assert_called_once_with()

@patch("UM.Application.Application.getInstance")
def test_errorLoginState(application):
    account = Account(MagicMock())
    mocked_auth_service = MagicMock()
    account._authorization_service = mocked_auth_service
    account.loginStateChanged = MagicMock()

    account._onLoginStateChanged(True, "BLARG!")
    # Even though we said that the login worked, it had an error message, so the login failed.
    account.loginStateChanged.emit.assert_called_with(False)

    account._onLoginStateChanged(True)
    account._onLoginStateChanged(False, "OMGZOMG!")
    account.loginStateChanged.emit.assert_called_with(False)

def test_sync_success():
    account = Account(MagicMock())

    service1 = "test_service1"
    service2 = "test_service2"

    account.setSyncState(service1, SyncState.SYNCING)
    assert account.syncState == SyncState.SYNCING

    account.setSyncState(service2, SyncState.SYNCING)
    assert account.syncState == SyncState.SYNCING

    account.setSyncState(service1, SyncState.SUCCESS)
    # service2 still syncing
    assert account.syncState == SyncState.SYNCING

    account.setSyncState(service2, SyncState.SUCCESS)
    assert account.syncState == SyncState.SUCCESS


def test_sync_update_action():
    account = Account(MagicMock())

    service1 = "test_service1"

    mockUpdateCallback = MagicMock()

    account.setSyncState(service1, SyncState.SYNCING)
    assert account.syncState == SyncState.SYNCING

    account.setUpdatePackagesAction(mockUpdateCallback)
    account.onUpdatePackagesClicked()
    mockUpdateCallback.assert_called_once_with()
    account.setSyncState(service1, SyncState.SUCCESS)

    account.sync()  # starting a new sync resets the update action to None

    account.setSyncState(service1, SyncState.SYNCING)
    assert account.syncState == SyncState.SYNCING

    account.onUpdatePackagesClicked()  # Should not be connected to an action anymore
    mockUpdateCallback.assert_called_once_with()  # No additional calls
    assert account.updatePackagesEnabled is False
    account.setSyncState(service1, SyncState.SUCCESS)