-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_utils.py
153 lines (130 loc) · 4.46 KB
/
test_utils.py
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
import sys
from typing import Any
import pytest
from supabase_functions.utils import (
BASE64URL_REGEX,
FunctionRegion,
SyncClient,
is_http_url,
is_valid_jwt,
is_valid_str_arg,
)
def test_function_region_values():
assert FunctionRegion.Any.value == "any"
assert FunctionRegion.ApNortheast1.value == "ap-northeast-1"
assert FunctionRegion.ApNortheast2.value == "ap-northeast-2"
assert FunctionRegion.ApSouth1.value == "ap-south-1"
assert FunctionRegion.ApSoutheast1.value == "ap-southeast-1"
assert FunctionRegion.ApSoutheast2.value == "ap-southeast-2"
assert FunctionRegion.CaCentral1.value == "ca-central-1"
assert FunctionRegion.EuCentral1.value == "eu-central-1"
assert FunctionRegion.EuWest1.value == "eu-west-1"
assert FunctionRegion.EuWest2.value == "eu-west-2"
assert FunctionRegion.EuWest3.value == "eu-west-3"
assert FunctionRegion.SaEast1.value == "sa-east-1"
assert FunctionRegion.UsEast1.value == "us-east-1"
assert FunctionRegion.UsWest1.value == "us-west-1"
assert FunctionRegion.UsWest2.value == "us-west-2"
def test_sync_client():
client = SyncClient()
# Verify that aclose method exists and calls close
assert hasattr(client, "aclose")
assert callable(client.aclose)
client.aclose() # Should not raise any exception
@pytest.mark.parametrize(
"test_input,expected",
[
("valid_string", True),
(" spaced_string ", True),
("", False),
(" ", False),
(None, False),
(123, False),
([], False),
({}, False),
],
)
def test_is_valid_str_arg(test_input: Any, expected: bool):
assert is_valid_str_arg(test_input) == expected
@pytest.mark.parametrize(
"test_input,expected",
[
("https://example.com", True),
("http://localhost", True),
("http://127.0.0.1:8000", True),
("https://api.supabase.com", True),
("ftp://example.com", False),
("ws://example.com", False),
("not-a-url", False),
("", False),
],
)
def test_is_http_url(https://github.com/test_input:%20str,%20expected:%20bool):
assert is_http_url(https://github.com/test_input) == expected
@pytest.mark.parametrize(
"test_input,expected",
[
# Valid JWTs
(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
True,
),
(
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
True,
),
# JWT with whitespace
(
" eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U ",
True,
),
# Invalid inputs
("", False),
("not.a.jwt", False),
("invalid.jwt.format.extra.dots", False),
("Bearer ", False),
("Bearer invalid", False),
# Invalid types
(None, False),
(123, False),
([], False),
({}, False),
# Invalid base64url format
("[email protected]", False),
("header.pay!load.signature", False),
],
)
def test_is_valid_jwt(test_input: Any, expected: bool):
assert is_valid_jwt(test_input) == expected
def test_base64url_regex():
import re
pattern = re.compile(BASE64URL_REGEX, re.IGNORECASE)
# Valid base64url strings
assert pattern.match("abcd")
assert pattern.match("1234")
assert pattern.match("abc")
assert pattern.match("12")
assert pattern.match("ab")
assert pattern.match("ABCD")
assert pattern.match("ABC")
assert pattern.match("AB")
assert pattern.match("a-b_")
# Invalid base64url strings
assert not pattern.match("a") # too short
assert not pattern.match("abcde") # invalid length
assert not pattern.match("abc!") # invalid character
assert not pattern.match("abc@") # invalid character
@pytest.mark.skipif(
sys.version_info < (3, 11),
reason="StrEnum import test only relevant for Python 3.11+",
)
def test_strenum_import_python_311_plus():
from enum import StrEnum as BuiltinStrEnum
assert isinstance(FunctionRegion.Any, BuiltinStrEnum)
@pytest.mark.skipif(
sys.version_info >= (3, 11),
reason="strenum import test only relevant for Python < 3.11",
)
def test_strenum_import_python_310_and_below():
from strenum import StrEnum as ExternalStrEnum
assert isinstance(FunctionRegion.Any, ExternalStrEnum)