@@ -1345,3 +1345,175 @@ def test_latest_no_change_windows():
13451345 with patch .dict (pkg .__salt__ , salt_dict ):
13461346 ret = pkg .latest (pkg_name )
13471347 assert ret .get ("result" , False ) is True
1348+
1349+
1350+ # 'trusted' state tests
1351+
1352+
1353+ def test_trusted_not_available ():
1354+ """
1355+ Test pkg.trusted when pkg.trust is not available for the package manager.
1356+ """
1357+ with patch .dict (pkg .__salt__ , {}):
1358+ ret = pkg .trusted ("cdalvaro/tap" )
1359+ assert ret ["result" ] is False
1360+ assert "not available" in ret ["comment" ]
1361+ assert ret ["changes" ] == {}
1362+
1363+
1364+ def test_trusted_already_trusted ():
1365+ """
1366+ Test pkg.trusted when the item is already trusted.
1367+ """
1368+ with patch .dict (
1369+ pkg .__salt__ ,
1370+ {
1371+ "pkg.trust" : MagicMock (return_value = True ),
1372+ "pkg.is_trusted" : MagicMock (return_value = True ),
1373+ },
1374+ ):
1375+ ret = pkg .trusted ("cdalvaro/tap" , type = "tap" )
1376+ assert ret ["result" ] is True
1377+ assert "already trusted" in ret ["comment" ]
1378+ assert ret ["changes" ] == {}
1379+
1380+
1381+ def test_trusted_test_mode ():
1382+ """
1383+ Test pkg.trusted in test mode when the item would be trusted.
1384+ """
1385+ with patch .dict (
1386+ pkg .__salt__ ,
1387+ {
1388+ "pkg.trust" : MagicMock (return_value = True ),
1389+ "pkg.is_trusted" : MagicMock (return_value = False ),
1390+ },
1391+ ), patch .dict (pkg .__opts__ , {"test" : True }):
1392+ ret = pkg .trusted ("cdalvaro/tap" )
1393+ assert ret ["result" ] is None
1394+ assert "would be trusted" in ret ["comment" ]
1395+ assert ret ["changes" ] == {}
1396+
1397+
1398+ def test_trusted_success ():
1399+ """
1400+ Test pkg.trusted successfully trusts an item.
1401+ """
1402+ trust_mock = MagicMock (return_value = True )
1403+ with patch .dict (
1404+ pkg .__salt__ ,
1405+ {
1406+ "pkg.trust" : trust_mock ,
1407+ "pkg.is_trusted" : MagicMock (return_value = False ),
1408+ },
1409+ ):
1410+ ret = pkg .trusted ("cdalvaro/tap" , type = "tap" )
1411+ assert ret ["result" ] is True
1412+ assert ret ["changes" ] == {
1413+ "cdalvaro/tap" : {"old" : "untrusted" , "new" : "trusted" }
1414+ }
1415+ assert "now trusted" in ret ["comment" ]
1416+ trust_mock .assert_called_once_with ("cdalvaro/tap" , type = "tap" )
1417+
1418+
1419+ def test_trusted_failure ():
1420+ """
1421+ Test pkg.trusted when the brew trust command fails.
1422+ """
1423+ with patch .dict (
1424+ pkg .__salt__ ,
1425+ {
1426+ "pkg.trust" : MagicMock (return_value = False ),
1427+ "pkg.is_trusted" : MagicMock (return_value = False ),
1428+ },
1429+ ):
1430+ ret = pkg .trusted ("cdalvaro/tap" )
1431+ assert ret ["result" ] is False
1432+ assert "Failed to trust" in ret ["comment" ]
1433+ assert ret ["changes" ] == {}
1434+
1435+
1436+ # 'untrusted' state tests
1437+
1438+
1439+ def test_untrusted_not_available ():
1440+ """
1441+ Test pkg.untrusted when pkg.untrust is not available for the package manager.
1442+ """
1443+ with patch .dict (pkg .__salt__ , {}):
1444+ ret = pkg .untrusted ("cdalvaro/tap" )
1445+ assert ret ["result" ] is False
1446+ assert "not available" in ret ["comment" ]
1447+ assert ret ["changes" ] == {}
1448+
1449+
1450+ def test_untrusted_already_not_trusted ():
1451+ """
1452+ Test pkg.untrusted when the item is already not trusted.
1453+ """
1454+ with patch .dict (
1455+ pkg .__salt__ ,
1456+ {
1457+ "pkg.untrust" : MagicMock (return_value = True ),
1458+ "pkg.is_trusted" : MagicMock (return_value = False ),
1459+ },
1460+ ):
1461+ ret = pkg .untrusted ("cdalvaro/tap" , type = "tap" )
1462+ assert ret ["result" ] is True
1463+ assert "already not trusted" in ret ["comment" ]
1464+ assert ret ["changes" ] == {}
1465+
1466+
1467+ def test_untrusted_test_mode ():
1468+ """
1469+ Test pkg.untrusted in test mode when the item would be untrusted.
1470+ """
1471+ with patch .dict (
1472+ pkg .__salt__ ,
1473+ {
1474+ "pkg.untrust" : MagicMock (return_value = True ),
1475+ "pkg.is_trusted" : MagicMock (return_value = True ),
1476+ },
1477+ ), patch .dict (pkg .__opts__ , {"test" : True }):
1478+ ret = pkg .untrusted ("cdalvaro/tap" )
1479+ assert ret ["result" ] is None
1480+ assert "would be untrusted" in ret ["comment" ]
1481+ assert ret ["changes" ] == {}
1482+
1483+
1484+ def test_untrusted_success ():
1485+ """
1486+ Test pkg.untrusted successfully untrusts an item.
1487+ """
1488+ untrust_mock = MagicMock (return_value = True )
1489+ with patch .dict (
1490+ pkg .__salt__ ,
1491+ {
1492+ "pkg.untrust" : untrust_mock ,
1493+ "pkg.is_trusted" : MagicMock (return_value = True ),
1494+ },
1495+ ):
1496+ ret = pkg .untrusted ("cdalvaro/tap" , type = "tap" )
1497+ assert ret ["result" ] is True
1498+ assert ret ["changes" ] == {
1499+ "cdalvaro/tap" : {"old" : "trusted" , "new" : "untrusted" }
1500+ }
1501+ assert "no longer trusted" in ret ["comment" ]
1502+ untrust_mock .assert_called_once_with ("cdalvaro/tap" , type = "tap" )
1503+
1504+
1505+ def test_untrusted_failure ():
1506+ """
1507+ Test pkg.untrusted when the brew untrust command fails.
1508+ """
1509+ with patch .dict (
1510+ pkg .__salt__ ,
1511+ {
1512+ "pkg.untrust" : MagicMock (return_value = False ),
1513+ "pkg.is_trusted" : MagicMock (return_value = True ),
1514+ },
1515+ ):
1516+ ret = pkg .untrusted ("cdalvaro/tap" )
1517+ assert ret ["result" ] is False
1518+ assert "Failed to untrust" in ret ["comment" ]
1519+ assert ret ["changes" ] == {}
0 commit comments