1"""Test cases for the RestrictedURI class in atpasser.uri module.""" 2 3import pytest 4from atpasser.uri import RestrictedURI 5from atpasser.uri.exceptions import InvalidRestrictedURIError, InvalidURIError 6 7 8class TestRestrictedURI: 9 """Test cases for the RestrictedURI class.""" 10 11 def test_valid_restricted_uri_with_did_collection_and_rkey(self): 12 """Test creating a RestrictedURI with a valid DID, collection, and rkey.""" 13 uri_str = ( 14 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 15 ) 16 uri = RestrictedURI(uri_str) 17 18 assert str(uri) == uri_str 19 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 20 assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] 21 assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" 22 assert uri.collection is not None 23 assert str(uri.collection) == "app.bsky.feed.post" 24 assert uri.rkey is not None 25 assert str(uri.rkey) == "3jwdwj2ctlk26" 26 27 def test_valid_restricted_uri_with_handle_collection_and_rkey(self): 28 """Test creating a RestrictedURI with a valid handle, collection, and rkey.""" 29 uri_str = "at://bnewbold.bsky.team/app.bsky.feed.post/3jwdwj2ctlk26" 30 uri = RestrictedURI(uri_str) 31 32 assert str(uri) == uri_str 33 assert uri.authorityAsText == "bnewbold.bsky.team" 34 assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] 35 assert uri.collection is not None 36 assert str(uri.collection) == "app.bsky.feed.post" 37 assert uri.rkey is not None 38 assert str(uri.rkey) == "3jwdwj2ctlk26" 39 40 def test_valid_restricted_uri_with_collection_only(self): 41 """Test creating a RestrictedURI with only a collection.""" 42 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" 43 uri = RestrictedURI(uri_str) 44 45 assert str(uri) == uri_str 46 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 47 assert uri.path == ["app.bsky.feed.post"] 48 assert uri.collection is not None 49 assert str(uri.collection) == "app.bsky.feed.post" 50 assert uri.rkey is None 51 52 def test_valid_restricted_uri_with_authority_only(self): 53 """Test creating a RestrictedURI with only an authority.""" 54 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur" 55 uri = RestrictedURI(uri_str) 56 57 assert str(uri) == uri_str 58 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 59 assert uri.path == [] 60 assert uri.collection is None 61 assert uri.rkey is None 62 63 def test_invalid_restricted_uri_with_query(self): 64 """Test that a RestrictedURI with query parameters raises InvalidRestrictedURIError.""" 65 uri_str = ( 66 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post?param1=value1" 67 ) 68 69 with pytest.raises( 70 InvalidRestrictedURIError, match="query parameters not supported" 71 ): 72 RestrictedURI(uri_str) 73 74 def test_invalid_restricted_uri_with_fragment(self): 75 """Test that a RestrictedURI with a fragment raises InvalidRestrictedURIError.""" 76 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26#$.some.json.path" 77 78 with pytest.raises(InvalidRestrictedURIError, match="fragments not supported"): 79 RestrictedURI(uri_str) 80 81 def test_invalid_restricted_uri_with_invalid_authority(self): 82 """Test that a RestrictedURI with invalid authority raises InvalidRestrictedURIError.""" 83 uri_str = "at://invalid_authority/app.bsky.feed.post/3jwdwj2ctlk26" 84 85 with pytest.raises(InvalidRestrictedURIError, match="invalid authority"): 86 RestrictedURI(uri_str) 87 88 def test_invalid_restricted_uri_too_many_path_segments(self): 89 """Test that a RestrictedURI with too many path segments raises InvalidRestrictedURIError.""" 90 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26/extra" 91 92 with pytest.raises(InvalidRestrictedURIError, match="too many path segments"): 93 RestrictedURI(uri_str) 94 95 def test_invalid_restricted_uri_base_uri_validation_failure(self): 96 """Test that a RestrictedURI with invalid base URI raises InvalidURIError.""" 97 uri_str = "https://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" 98 99 with pytest.raises(InvalidURIError, match="invalid format"): 100 RestrictedURI(uri_str) 101 102 def test_restricted_uri_equality(self): 103 """Test RestrictedURI equality comparison.""" 104 uri_str = ( 105 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 106 ) 107 uri1 = RestrictedURI(uri_str) 108 uri2 = RestrictedURI(uri_str) 109 110 assert uri1 == uri2 111 assert uri1 != "not a uri object" 112 113 def test_restricted_uri_string_representation(self): 114 """Test RestrictedURI string representation.""" 115 uri_str = ( 116 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 117 ) 118 uri = RestrictedURI(uri_str) 119 120 assert str(uri) == uri_str