test: Add test infrastructure and basic model testing
This commit is contained in:
		
							
								
								
									
										92
									
								
								tests/test_models.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								tests/test_models.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from fediverse_blocklist_deploy import models
 | 
			
		||||
 | 
			
		||||
def test_empty_instance():
 | 
			
		||||
    with pytest.raises(KeyError):
 | 
			
		||||
        models.Instance = models.Instance({})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_minimal_init():
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    assert i.id == None
 | 
			
		||||
    assert i.domain == "abc.xyz"
 | 
			
		||||
    assert i.obfuscate == False
 | 
			
		||||
    assert i.reject_media == False
 | 
			
		||||
    assert i.reject_reports == False
 | 
			
		||||
    
 | 
			
		||||
def test_string_representation():
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    assert str(i) == "abc.xyz: suspend"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_status():
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    assert i.status_str() == "suspend\nReject reports: False\nReject media: False\nObfuscate: False"
 | 
			
		||||
 | 
			
		||||
def test_equality():
 | 
			
		||||
    a1: models.Instance = models.Instance({"domain": "a"})
 | 
			
		||||
    a2: models.Instance = models.Instance({"domain": "a"})
 | 
			
		||||
    b: models.Instance = models.Instance({"domain": "b"})
 | 
			
		||||
 | 
			
		||||
    assert a1 == a2
 | 
			
		||||
    assert a2 != b
 | 
			
		||||
 | 
			
		||||
def test_as_dict():
 | 
			
		||||
    test_data = {"domain": "abc.xyz", "severity": "suspend", "private_comment": "hidden", "public_comment": "", "obfuscate": True, "reject_media": False, "reject_reports": False}
 | 
			
		||||
    i: models.Instance = models.Instance(test_data)
 | 
			
		||||
    test_data.pop("private_comment")
 | 
			
		||||
    assert i.as_dict() == test_data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_as_dict_private():
 | 
			
		||||
    test_data = {"domain": "abc.xyz", "severity": "suspend", "private_comment": "hidden", "public_comment": "", "obfuscate": True, "reject_media": False, "reject_reports": False}
 | 
			
		||||
    i: models.Instance = models.Instance(test_data)
 | 
			
		||||
    assert i.as_dict(private=True) == test_data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_apply(requests_mock):
 | 
			
		||||
    requests_mock.post("https://server.org/api/v1/admin/domain_blocks", text="success")
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    i.apply("server.org", token="abcdef")
 | 
			
		||||
    assert requests_mock.called
 | 
			
		||||
 | 
			
		||||
def test_apply_with_id(requests_mock):
 | 
			
		||||
    requests_mock.put("https://server.org/api/v1/admin/domain_blocks/123", text="success")
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    i.apply("server.org", token="abcdef", block_id=123)
 | 
			
		||||
    assert requests_mock.called
 | 
			
		||||
 | 
			
		||||
def test_apply_error(requests_mock):
 | 
			
		||||
    requests_mock.post("https://server.org/api/v1/admin/domain_blocks", status_code=400)
 | 
			
		||||
    with pytest.raises(ConnectionError):
 | 
			
		||||
        i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
        i.apply("server.org", token="abcdef")
 | 
			
		||||
    assert requests_mock.called
 | 
			
		||||
 | 
			
		||||
def test_delete(requests_mock):
 | 
			
		||||
    requests_mock.delete("https://server.org/api/v1/admin/domain_blocks/123", text="success")
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    i.id = 123
 | 
			
		||||
    i.delete("server.org", token="abcdef")
 | 
			
		||||
    assert requests_mock.called
 | 
			
		||||
 | 
			
		||||
def test_delete_error(requests_mock):
 | 
			
		||||
    requests_mock.delete("https://server.org/api/v1/admin/domain_blocks/123", status_code=400)
 | 
			
		||||
    i: models.Instance = models.Instance({"domain": "abc.xyz"})
 | 
			
		||||
    i.id = 123
 | 
			
		||||
    with pytest.raises(ConnectionError):
 | 
			
		||||
        i.delete("server.org", token="abcdef")
 | 
			
		||||
    assert requests_mock.called
 | 
			
		||||
 | 
			
		||||
def test_diff_equal():
 | 
			
		||||
    a1: models.Instance = models.Instance({"domain": "a"})
 | 
			
		||||
    a2: models.Instance = models.Instance({"domain": "a"})
 | 
			
		||||
 | 
			
		||||
    assert models.Instance.list_diffs([a1], [a2]) == []
 | 
			
		||||
 | 
			
		||||
def test_diff_not_equal():
 | 
			
		||||
    a1: models.Instance = models.Instance({"domain": "a2"})
 | 
			
		||||
    a2: models.Instance = models.Instance({"domain": "a1"})
 | 
			
		||||
 | 
			
		||||
    assert models.Instance.list_diffs([a1], [a2]) == [{"local": a1, "remote": None}, {"local": None, "remote": a2}]
 | 
			
		||||
		Reference in New Issue
	
	Block a user