Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import types
 
import pytest
from core.utils.common import int_from_request
from core.utils.exceptions import InvalidUploadUrlError, LabelStudioAPIException
from core.utils.io import validate_upload_url
from core.utils.params import bool_from_request
from rest_framework.exceptions import ValidationError
 
 
@pytest.mark.parametrize(
    'param, result',
    [
        ('True', True),
        ('Yes', True),
        ('1', True),
        ('False', False),
        ('no', False),
        ('0', False),
        ('test', None),
        (None, False),
    ],
)
@pytest.mark.django_db
def test_core_bool_from_request(param, result):
    params = {'test': param} if param is not None else {}
 
    # incorrect param should call exception
    if result is None:
        error = False
        try:
            bool_from_request(params, 'test', 0)
        except:  # noqa: E722
            error = True
 
        assert error
 
    # everything ok
    else:
        assert bool_from_request(params, 'test', 0) == result
 
 
@pytest.mark.parametrize('param, result', [('', None), ('0', 0), ('1', 1), ('10', 10), ('test', None), (None, None)])
@pytest.mark.django_db
def test_core_int_from_request(param, result):
    params = {'test': param}
 
    # incorrect param should call exception
    if result is None:
        error = False
        try:
            int_from_request(params, 'test', 0)
        except ValidationError:
            error = True
 
        assert error
 
    # everything ok
    else:
        assert int_from_request(params, 'test', 0) == result
 
 
@pytest.mark.django_db
def test_user_info(business_client):
    from label_studio.server import _create_user, _get_user_info
 
    user_data = _get_user_info(business_client.admin.email)
    assert 'token' in user_data
 
    user_data = _get_user_info(None)
    assert user_data is None
 
    class DummyArgs:
        username = 'tester@x.com'
        password = 'passwdx'
        user_token = 'token12345'
 
    args = DummyArgs()
    _create_user(args, {})
    user_data = _get_user_info('tester@x.com')
    assert user_data['token'] == 'token12345'
 
    args.user_token, args.username = '123', 'tester2@x.com'
    user = _create_user(args, {})
    assert user is not None
 
 
@pytest.mark.parametrize(
    'command_line, result',
    [
        (['label-studio', 'user', '--username', 'test@test.com', '--password', '12345678'], None),
    ],
)
@pytest.mark.django_db
def test_main(mocker, command_line, result):
    from server import main
 
    mocker.patch('sys.argv', command_line)
    output = main()
 
    assert output == result
 
 
def test_string_is_url():
    from label_studio.core.utils.common import string_is_url
 
    assert string_is_url('http://test.com') is True
    assert string_is_url('https://test.com') is True
    assert string_is_url('xyz') is False
 
 
def test_get_client_ip():
    from label_studio.core.utils.common import get_client_ip
 
    ip = get_client_ip(types.SimpleNamespace(META={'HTTP_X_FORWARDED_FOR': '127.0.0.1'}))
    assert ip == '127.0.0.1'
 
    ip = get_client_ip(types.SimpleNamespace(META={'REMOTE_ADDR': '127.0.0.2'}))
    assert ip == '127.0.0.2'
 
 
def test_timestamp_now():
    from label_studio.core.utils.common import timestamp_now
 
    t = timestamp_now()
    assert t is not None
 
 
def test_start_browser():
    from label_studio.core.utils.common import start_browser
 
    assert start_browser('http://localhost:8080', True) is None
    assert start_browser('http://localhost:8080', False) is None
 
 
@pytest.mark.parametrize(
    'url, block_local_urls, raises_exc',
    [
        ('http://0.0.0.0', True, InvalidUploadUrlError),
        ('http://0.0.0.0', False, None),
        ('https://0.0.0.0', True, InvalidUploadUrlError),
        ('https://0.0.0.0', False, None),
        # Non-http[s] schemes
        ('ftp://example.org', True, InvalidUploadUrlError),
        ('ftp://example.org', False, InvalidUploadUrlError),
        ('FILE:///etc/passwd', True, InvalidUploadUrlError),
        ('file:///etc/passwd', False, InvalidUploadUrlError),
        # Start and end of 127.0.0.0/8
        ('https://127.0.0.0', True, InvalidUploadUrlError),
        ('https://127.255.255.255', True, InvalidUploadUrlError),
        # Start and end of 10.0.0.0/8
        ('http://10.0.0.0', True, InvalidUploadUrlError),
        ('https://10.255.255.255', True, InvalidUploadUrlError),
        # Start and end of 172.16.0.0/12
        ('https://172.16.0.0', True, InvalidUploadUrlError),
        ('https://172.31.255.255', True, InvalidUploadUrlError),
        # Start and end of 192.168.0.0/16
        ('https://192.168.0.0', True, InvalidUploadUrlError),
        ('https://192.168.255.255', True, InvalidUploadUrlError),
        # Valid external IPs
        ('https://4.4.4.4', True, None),
        ('https://8.8.8.8', True, None),
        ('http://8.8.8.8', False, None),
        # Valid external websites
        ('https://example.org', True, None),
        ('http://example.org', False, None),
        # Space prepended to otherwise valid external IP
        (' http://8.8.8.8', False, InvalidUploadUrlError),
        # Host that doesn't resolve
        ('http://example', False, LabelStudioAPIException),
        ('http://example', True, LabelStudioAPIException),
        # localhost
        ('http://localhost', True, InvalidUploadUrlError),
        ('http://localhost', False, None),
    ],
)
@pytest.mark.django_db
def test_core_validate_upload_url(url, block_local_urls, raises_exc):
 
    if raises_exc is None:
        assert validate_upload_url(url, block_local_urls=block_local_urls) is None
        return
 
    with pytest.raises(raises_exc):
        validate_upload_url(url, block_local_urls=block_local_urls)