@@ -1608,3 +1608,192 @@ def test_list_upgrades_with_options():
16081608 mock_call_brew .assert_called_once_with (
16091609 "outdated" , "--json=v2" , "--greedy" , "--fetch-HEAD"
16101610 )
1611+
1612+
1613+ # 'list_trusted' function tests
1614+
1615+
1616+ def test_list_trusted ():
1617+ """
1618+ Tests that list_trusted returns all trusted items as a dict.
1619+ """
1620+ expected = {
1621+ "taps" : ["thirdparty/foo" ],
1622+ "formulae" : ["thirdparty/foo/bar" ],
1623+ "casks" : [],
1624+ "commands" : [],
1625+ }
1626+ mock_call_brew = MagicMock (
1627+ return_value = {
1628+ "retcode" : 0 ,
1629+ "stdout" : '{"taps": ["thirdparty/foo"], "formulae": ["thirdparty/foo/bar"], "casks": [], "commands": []}' ,
1630+ "stderr" : "" ,
1631+ }
1632+ )
1633+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1634+ assert mac_brew .list_trusted () == expected
1635+ mock_call_brew .assert_called_once_with ("trust" , "--json=v1" )
1636+
1637+
1638+ def test_list_trusted_by_type ():
1639+ """
1640+ Tests that list_trusted with a type returns a list of trusted items.
1641+ """
1642+ mock_call_brew = MagicMock (
1643+ return_value = {
1644+ "retcode" : 0 ,
1645+ "stdout" : '["thirdparty/foo"]' ,
1646+ "stderr" : "" ,
1647+ }
1648+ )
1649+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1650+ assert mac_brew .list_trusted (type = "tap" ) == ["thirdparty/foo" ]
1651+ mock_call_brew .assert_called_once_with ("trust" , "--json=v1" , "--tap" )
1652+
1653+
1654+ def test_list_trusted_invalid_type ():
1655+ """
1656+ Tests that list_trusted raises SaltInvocationError for an invalid type.
1657+ """
1658+ with pytest .raises (
1659+ salt .exceptions .SaltInvocationError , match = "Invalid type 'invalid'"
1660+ ):
1661+ mac_brew .list_trusted (type = "invalid" )
1662+
1663+
1664+ # 'trust' function tests
1665+
1666+
1667+ def test_trust ():
1668+ """
1669+ Tests successfully trusting a tap.
1670+ """
1671+ mock_call_brew = MagicMock (return_value = {"retcode" : 0 , "stdout" : "" , "stderr" : "" })
1672+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1673+ assert mac_brew .trust ("thirdparty/foo" ) is True
1674+ mock_call_brew .assert_called_once_with ("trust" , "thirdparty/foo" )
1675+
1676+
1677+ def test_trust_with_type ():
1678+ """
1679+ Tests trusting an item with an explicit type flag.
1680+ """
1681+ mock_call_brew = MagicMock (return_value = {"retcode" : 0 , "stdout" : "" , "stderr" : "" })
1682+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1683+ assert mac_brew .trust ("thirdparty/foo" , type = "tap" ) is True
1684+ mock_call_brew .assert_called_once_with ("trust" , "--tap" , "thirdparty/foo" )
1685+
1686+
1687+ def test_trust_failure ():
1688+ """
1689+ Tests that trust returns False when brew trust fails.
1690+ """
1691+ mock_call_brew = MagicMock (side_effect = CommandExecutionError ("brew failed" ))
1692+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1693+ assert mac_brew .trust ("thirdparty/foo" ) is False
1694+
1695+
1696+ def test_trust_invalid_type ():
1697+ """
1698+ Tests that trust raises SaltInvocationError for an invalid type.
1699+ """
1700+ with pytest .raises (
1701+ salt .exceptions .SaltInvocationError , match = "Invalid type 'invalid'"
1702+ ):
1703+ mac_brew .trust ("thirdparty/foo" , type = "invalid" )
1704+
1705+
1706+ # 'untrust' function tests
1707+
1708+
1709+ def test_untrust ():
1710+ """
1711+ Tests successfully untrusting a tap.
1712+ """
1713+ mock_call_brew = MagicMock (return_value = {"retcode" : 0 , "stdout" : "" , "stderr" : "" })
1714+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1715+ assert mac_brew .untrust ("thirdparty/foo" ) is True
1716+ mock_call_brew .assert_called_once_with ("untrust" , "thirdparty/foo" )
1717+
1718+
1719+ def test_untrust_with_type ():
1720+ """
1721+ Tests untrusting an item with an explicit type flag.
1722+ """
1723+ mock_call_brew = MagicMock (return_value = {"retcode" : 0 , "stdout" : "" , "stderr" : "" })
1724+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1725+ assert mac_brew .untrust ("thirdparty/foo/bar" , type = "formula" ) is True
1726+ mock_call_brew .assert_called_once_with (
1727+ "untrust" , "--formula" , "thirdparty/foo/bar"
1728+ )
1729+
1730+
1731+ def test_untrust_failure ():
1732+ """
1733+ Tests that untrust returns False when brew untrust fails.
1734+ """
1735+ mock_call_brew = MagicMock (side_effect = CommandExecutionError ("brew failed" ))
1736+ with patch ("salt.modules.mac_brew_pkg._call_brew" , mock_call_brew ):
1737+ assert mac_brew .untrust ("thirdparty/foo" ) is False
1738+
1739+
1740+ def test_untrust_invalid_type ():
1741+ """
1742+ Tests that untrust raises SaltInvocationError for an invalid type.
1743+ """
1744+ with pytest .raises (
1745+ salt .exceptions .SaltInvocationError , match = "Invalid type 'invalid'"
1746+ ):
1747+ mac_brew .untrust ("thirdparty/foo" , type = "invalid" )
1748+
1749+
1750+ # 'is_trusted' function tests
1751+
1752+
1753+ def test_is_trusted_found ():
1754+ """
1755+ Tests is_trusted returns True when the item is in the trusted list.
1756+ """
1757+ trusted = {
1758+ "taps" : ["thirdparty/foo" ],
1759+ "formulae" : [],
1760+ "casks" : [],
1761+ "commands" : [],
1762+ }
1763+ with patch ("salt.modules.mac_brew_pkg.list_trusted" , return_value = trusted ):
1764+ assert mac_brew .is_trusted ("thirdparty/foo" ) is True
1765+
1766+
1767+ def test_is_trusted_not_found ():
1768+ """
1769+ Tests is_trusted returns False when the item is not trusted.
1770+ """
1771+ trusted = {
1772+ "taps" : [],
1773+ "formulae" : [],
1774+ "casks" : [],
1775+ "commands" : [],
1776+ }
1777+ with patch ("salt.modules.mac_brew_pkg.list_trusted" , return_value = trusted ):
1778+ assert mac_brew .is_trusted ("thirdparty/foo" ) is False
1779+
1780+
1781+ def test_is_trusted_with_type ():
1782+ """
1783+ Tests is_trusted with a type filter delegates to list_trusted with that type.
1784+ """
1785+ with patch (
1786+ "salt.modules.mac_brew_pkg.list_trusted" , return_value = ["thirdparty/foo" ]
1787+ ) as mock_list :
1788+ assert mac_brew .is_trusted ("thirdparty/foo" , type = "tap" ) is True
1789+ mock_list .assert_called_once_with (type = "tap" )
1790+
1791+
1792+ def test_is_trusted_with_type_not_found ():
1793+ """
1794+ Tests is_trusted with a type filter returns False when not in list.
1795+ """
1796+ with patch (
1797+ "salt.modules.mac_brew_pkg.list_trusted" , return_value = ["other/tap" ]
1798+ ):
1799+ assert mac_brew .is_trusted ("thirdparty/foo" , type = "tap" ) is False
0 commit comments