winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

WorkflowGroupPolicy.cpp (6654B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "TestCommon.h"
      5 #include "TestSettings.h"
      6 #include "AppInstallerErrors.h"
      7 #include "Commands/InstallCommand.h"
      8 #include "Commands/RootCommand.h"
      9 #include "Commands/ShowCommand.h"
     10 #include "Commands/UpgradeCommand.h"
     11 #include "Commands/ValidateCommand.h"
     12 
     13 using namespace TestCommon;
     14 using namespace AppInstaller::CLI;
     15 using namespace AppInstaller::Settings;
     16 using namespace std::string_view_literals;
     17 
     18 
     19 TEST_CASE("GroupPolicy_WinGet", "[groupPolicy]")
     20 {
     21     GroupPolicyTestOverride policies;
     22     policies.SetState(TogglePolicy::Policy::WinGet, PolicyState::Disabled);
     23 
     24     SECTION("Install is blocked")
     25     {
     26         std::ostringstream output;
     27         Execution::Context context{ output, std::cin };
     28         context.Args.AddArg(Execution::Args::Type::Query, "Fake.Package"sv);
     29         InstallCommand installCommand({});
     30 
     31         REQUIRE_POLICY_EXCEPTION(
     32             installCommand.Execute(context),
     33             TogglePolicy::Policy::WinGet);
     34     }
     35     SECTION("Info is not blocked")
     36     {
     37         std::ostringstream output;
     38         Execution::Context context{ output, std::cin };
     39         context.Args.AddArg(Execution::Args::Type::Info);
     40         RootCommand rootCommand({});
     41 
     42         rootCommand.Execute(context);
     43 
     44         REQUIRE_FALSE(context.IsTerminated());
     45     }
     46 }
     47 
     48 TEST_CASE("GroupPolicy_SettingsCommand", "[groupPolicy]")
     49 {
     50     GroupPolicyTestOverride policies;
     51     policies.SetState(TogglePolicy::Policy::Settings, PolicyState::Disabled);
     52 
     53     Invocation inv{ std::vector<std::string>{ "settings" } };
     54     RootCommand rootCommand;
     55     REQUIRE_THROWS(rootCommand.FindSubCommand(inv));
     56 }
     57 
     58 TEST_CASE("GroupPolicy_LocalManifests", "[groupPolicy]")
     59 {
     60     GroupPolicyTestOverride policies;
     61     policies.SetState(TogglePolicy::Policy::LocalManifestFiles, PolicyState::Disabled);
     62 
     63     SECTION("Blocked on install")
     64     {
     65         Execution::Args args;
     66         args.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
     67         InstallCommand installCommand({});
     68         REQUIRE_THROWS(installCommand.ValidateArguments(args));
     69     }
     70     SECTION("Blocked on upgrade")
     71     {
     72         Execution::Args args;
     73         args.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
     74         UpgradeCommand upgradeCommand({});
     75         REQUIRE_THROWS(upgradeCommand.ValidateArguments(args));
     76     }
     77     SECTION("Allowed on show")
     78     {
     79         Execution::Args args;
     80         args.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
     81         ShowCommand showCommand({});
     82         REQUIRE_NOTHROW(showCommand.ValidateArguments(args));
     83     }
     84     SECTION("Allowed on validate")
     85     {
     86         Execution::Args args;
     87         args.AddArg(Execution::Args::Type::ValidateManifest, TestDataFile("InstallFlowTest_Exe.yaml").GetPath().u8string());
     88         ValidateCommand validateCommand({});
     89         REQUIRE_NOTHROW(validateCommand.ValidateArguments(args));
     90     }
     91 }
     92 
     93 TEST_CASE("GroupPolicy_Info", "[groupPolicy]")
     94 {
     95     GroupPolicyTestOverride policies;
     96 
     97     std::ostringstream output;
     98     Execution::Context context{ output, std::cin };
     99     context.Args.AddArg(Execution::Args::Type::Info);
    100     RootCommand rootCommand({});
    101 
    102     SECTION("Does not list not configured")
    103     {
    104         rootCommand.Execute(context);
    105         INFO(output.str());
    106 
    107         REQUIRE_FALSE(context.IsTerminated());
    108         REQUIRE(output.str().find("Group Policy") == std::string::npos);
    109     }
    110     SECTION("Shows enabled policies")
    111     {
    112         policies.SetState(TogglePolicy::Policy::HashOverride, PolicyState::Enabled);
    113 
    114         rootCommand.Execute(context);
    115         INFO(output.str());
    116 
    117         REQUIRE_FALSE(context.IsTerminated());
    118         REQUIRE(output.str().find("Group Policy") != std::string::npos);
    119         REQUIRE(output.str().find("Hash Override Enabled") != std::string::npos);
    120     }
    121     SECTION("Shows disabled policies")
    122     {
    123         policies.SetState(TogglePolicy::Policy::LocalManifestFiles, PolicyState::Disabled);
    124 
    125         rootCommand.Execute(context);
    126         INFO(output.str());
    127 
    128         REQUIRE_FALSE(context.IsTerminated());
    129         REQUIRE(output.str().find("Group Policy") != std::string::npos);
    130         REQUIRE(output.str().find("Local Manifest Files Disabled") != std::string::npos);
    131     }
    132     SECTION("Shows auto update interval")
    133     {
    134         policies.SetValue<ValuePolicy::SourceAutoUpdateIntervalInMinutes>(60);
    135 
    136         rootCommand.Execute(context);
    137         INFO(output.str());
    138 
    139         REQUIRE_FALSE(context.IsTerminated());
    140         REQUIRE(output.str().find("Group Policy") != std::string::npos);
    141         REQUIRE(output.str().find("Source Auto Update Interval In Minutes 60") != std::string::npos);
    142     }
    143     SECTION("Shows additional sources list")
    144     {
    145         SourceFromPolicy source;
    146         source.Name = "policy-source";
    147         source.Type = "Test.Type";
    148         source.Arg = "test-arg";
    149         policies.SetState(TogglePolicy::Policy::AdditionalSources, PolicyState::Enabled);
    150         policies.SetValue<ValuePolicy::AdditionalSources>({ source });
    151 
    152         rootCommand.Execute(context);
    153         INFO(output.str());
    154 
    155         REQUIRE_FALSE(context.IsTerminated());
    156         REQUIRE(output.str().find("Group Policy") != std::string::npos);
    157         REQUIRE(output.str().find("Sources Enabled") != std::string::npos);
    158         REQUIRE(output.str().find("Additional source") != std::string::npos);
    159         REQUIRE(output.str().find(source.Name) != std::string::npos);
    160         REQUIRE(output.str().find(source.Type) != std::string::npos);
    161         REQUIRE(output.str().find(source.Arg) != std::string::npos);
    162     }
    163     SECTION("Shows allowed sources list")
    164     {
    165         SourceFromPolicy source;
    166         source.Name = "allowed-source";
    167         source.Type = "Test.Type";
    168         source.Arg = "test-arg";
    169         policies.SetState(TogglePolicy::Policy::AllowedSources, PolicyState::Enabled);
    170         policies.SetValue<ValuePolicy::AllowedSources>({ source });
    171 
    172         rootCommand.Execute(context);
    173         INFO(output.str());
    174 
    175         REQUIRE_FALSE(context.IsTerminated());
    176         REQUIRE(output.str().find("Group Policy") != std::string::npos);
    177         REQUIRE(output.str().find("Allowed Sources Enabled") != std::string::npos);
    178         REQUIRE(output.str().find("Allowed source") != std::string::npos);
    179         REQUIRE(output.str().find(source.Name) != std::string::npos);
    180         REQUIRE(output.str().find(source.Type) != std::string::npos);
    181         REQUIRE(output.str().find(source.Arg) != std::string::npos);
    182     }
    183 }