| 1 |
###################################################################### |
| 2 |
# |
| 3 |
# File: test/unit/_utils/test_uri.py |
| 4 |
# |
| 5 |
# Copyright 2023 Backblaze Inc. All Rights Reserved. |
| 6 |
# |
| 7 |
# License https://www.backblaze.com/using_b2_code.html |
| 8 |
# |
| 9 |
###################################################################### |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from b2._internal._utils.uri import B2URI, B2FileIdURI, parse_uri |
| 15 |
|
| 16 |
|
| 17 |
class TestB2URI: |
| 18 |
def test__str__(self): |
| 19 |
uri = B2URI(bucket_name="testbucket", path="path/to/file") |
| 20 |
assert str(uri) == "b2://testbucket/path/to/file" |
| 21 |
|
| 22 |
@pytest.mark.parametrize( |
| 23 |
"path, expected", |
| 24 |
[ |
| 25 |
("", True), |
| 26 |
("path/", True), |
| 27 |
("path/subpath", None), |
| 28 |
], |
| 29 |
) |
| 30 |
def test_is_dir(self, path, expected): |
| 31 |
assert B2URI("bucket", path).is_dir() is expected |
| 32 |
|
| 33 |
def test__bucket_uris_are_normalized(self): |
| 34 |
alternatives = [ |
| 35 |
B2URI("bucket"), |
| 36 |
B2URI("bucket", ""), |
| 37 |
] |
| 38 |
assert len(set(alternatives)) == 1 |
| 39 |
assert {str(uri) for uri in alternatives} == {"b2://bucket/"} # normalized |
| 40 |
|
| 41 |
@pytest.mark.parametrize( |
| 42 |
"path, expected_uri_str", |
| 43 |
[ |
| 44 |
("", "b2://bucket/"), |
| 45 |
("path/", "b2://bucket/path/"), |
| 46 |
("path/subpath", "b2://bucket/path/subpath"), |
| 47 |
], |
| 48 |
) |
| 49 |
def test__normalization(self, path, expected_uri_str): |
| 50 |
assert str(B2URI("bucket", path)) == expected_uri_str |
| 51 |
assert str(B2URI("bucket", path)) == str(B2URI("bucket", path)) # normalized |
| 52 |
|
| 53 |
|
| 54 |
def test_b2fileuri_str(): |
| 55 |
uri = B2FileIdURI(file_id="file123") |
| 56 |
assert str(uri) == "b2id://file123" |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
"uri,expected", |
| 61 |
[ |
| 62 |
("some/local/path", Path("some/local/path")), |
| 63 |
("./some/local/path", Path("some/local/path")), |
| 64 |
("b2://bucket", B2URI(bucket_name="bucket")), |
| 65 |
("b2://bucket/", B2URI(bucket_name="bucket")), |
| 66 |
("b2://bucket/path/to/dir/", B2URI(bucket_name="bucket", path="path/to/dir/")), |
| 67 |
("b2id://file123", B2FileIdURI(file_id="file123")), |
| 68 |
("b2://bucket/wild[card]", B2URI(bucket_name="bucket", path="wild[card]")), |
| 69 |
("b2://bucket/wild?card", B2URI(bucket_name="bucket", path="wild?card")), |
| 70 |
("b2://bucket/special#char", B2URI(bucket_name="bucket", path="special#char")), |
| 71 |
], |
| 72 |
) |
| 73 |
def test_parse_uri(uri, expected): |
| 74 |
assert parse_uri(uri) == expected |
| 75 |
|
| 76 |
|
| 77 |
def test_parse_uri__allow_all_buckets(): |
| 78 |
assert parse_uri("b2://", allow_all_buckets=True) == B2URI("") |
| 79 |
with pytest.raises(ValueError) as exc_info: |
| 80 |
parse_uri("b2:///", allow_all_buckets=True) |
| 81 |
assert "Invalid B2 URI: all buckets URI doesn't allow non-empty path, but '/' was provided" == str( |
| 82 |
exc_info.value |
| 83 |
) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.parametrize( |
| 87 |
"uri, expected_exception_message", |
| 88 |
[ |
| 89 |
("", "URI cannot be empty"), |
| 90 |
# Test cases for invalid B2 URIs (missing netloc part) |
| 91 |
("b2://", "Invalid B2 URI: 'b2://'"), |
| 92 |
("b2id://", "Invalid B2 URI: 'b2id://'"), |
| 93 |
# Test cases for B2 URIs with credentials |
| 94 |
( |
| 95 |
"b2://user@password:bucket/path", |
| 96 |
"Invalid B2 URI: credentials passed using `user@password:` syntax is not supported in URI", |
| 97 |
), |
| 98 |
( |
| 99 |
"b2id://user@password:file123", |
| 100 |
"Invalid B2 URI: credentials passed using `user@password:` syntax is not supported in URI", |
| 101 |
), |
| 102 |
# Test cases for unsupported URI schemes |
| 103 |
("unknown://bucket/path", "Unsupported URI scheme: 'unknown'"), |
| 104 |
], |
| 105 |
) |
| 106 |
def test_parse_uri_exceptions(uri, expected_exception_message): |
| 107 |
with pytest.raises(ValueError) as exc_info: |
| 108 |
parse_uri(uri) |
| 109 |
assert expected_exception_message in str(exc_info.value) |