File: test_logicals.py

package info (click to toggle)
python-proton-vpn-api-core 0.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 6,582; makefile: 8
file content (143 lines) | stat: -rw-r--r-- 4,858 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
133
134
135
136
137
138
139
140
141
142
143
"""
Copyright (c) 2023 Proton AG

This file is part of Proton VPN.

Proton VPN is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Proton VPN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ProtonVPN.  If not, see <https://www.gnu.org/licenses/>.
"""
from proton.vpn.session.servers import LogicalServer, ServerFeatureEnum
from proton.vpn.session.servers.logicals import sort_servers_alphabetically_by_country_and_server_name, ServerList


def test_server_list_get_fastest():
    api_response = {
        "Code": 1000,
        "LogicalServers": [
            {
                "ID": 1,
                "Name": "JP#10",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "Score": 15.0,  # AR#9 has better score (lower is better)
                "Tier": 2,
                "ExitCountry": "JP",
            },
            {
                "ID": 2,
                "Name": "AR#11",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "Score": 1.0,  # Even though it has a better score than CH#9,
                "Tier": 3,     # it's not in the user tier (2).
                "ExitCountry": "AR",
            },
            {
                "ID": 3,
                "Name": "AR#9",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "Score": 10.0,  # Fastest server in the  user tier (2)
                "Tier": 2,
                "ExitCountry": "AR",
            },
            {
                "ID": 4,
                "Name": "CH#18-TOR",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "Score": 7.0,                       # Even though it has a better score than AR#9,
                "Features": ServerFeatureEnum.TOR,  # TOR servers should be ignored.
                "Tier": 2,
                "ExitCountry": "CH",
            },
            {
                "ID": 5,
                "Name": "CH-US#1",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "Score": 8.0,                               # Even though it has a better score than AR#9,
                "Features": ServerFeatureEnum.SECURE_CORE,  # secure core servers should be ignored.
                "Tier": 2,
                "ExitCountry": "CH",
            },
            {
                "ID": 6,
                "Name": "JP#1",
                "Score": 9.0,  # Even though it has a better score than AR#9,
                "Status": 0,   # this server is not enabled.
                "Servers": [{"Status": 0}],
                "Tier": 2,
                "ExitCountry": "JP",
            },
        ]
    }

    server_list = ServerList(
        user_tier=2,
        logicals=[LogicalServer(ls) for ls in api_response["LogicalServers"]]
    )

    fastest = server_list.get_fastest()
    assert fastest.name == "AR#9"


def test_sort_servers_alphabetically_by_country_and_server_name():
    api_response = {
        "Code": 1000,
        "LogicalServers": [
            {
                "ID": 2,
                "Name": "AR#10",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "ExitCountry": "AR",
            },
            {
                "ID": 1,
                "Name": "JP-FREE#10",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "ExitCountry": "JP",
            },
            {
                "ID": 3,
                "Name": "AR#9",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "ExitCountry": "AR",
            },
            {
                "ID": 5,
                "Name": "Random Name",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "ExitCountry": "JP",
            },
            {
                "ID": 4,
                "Name": "JP#9",
                "Status": 1,
                "Servers": [{"Status": 1}],
                "ExitCountry": "JP",

            },
        ]
    }

    logicals = [LogicalServer(server_dict) for server_dict in api_response["LogicalServers"]]
    logicals.sort(key=sort_servers_alphabetically_by_country_and_server_name)

    expected_server_name_order = ["AR#9", "AR#10", "JP#9", "JP-FREE#10", "Random Name"]
    actual_server_name_order = [server.name for server in logicals]
    assert actual_server_name_order == expected_server_name_order