forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceFlow.cpp
More file actions
184 lines (156 loc) · 7.28 KB
/
Copy pathSourceFlow.cpp
File metadata and controls
184 lines (156 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WorkflowCommon.h"
#include "TestHooks.h"
#include "TestSettings.h"
#include <Commands/SourceCommand.h>
#include <Workflows/PromptFlow.h>
#include <Workflows/SourceFlow.h>
using namespace TestCommon;
using namespace AppInstaller::CLI;
using namespace AppInstaller::CLI::Workflow;
using namespace AppInstaller::Repository;
using namespace AppInstaller::Settings;
void OverrideForSourceAddWithAgreements(TestContext& context, bool isAddExpected = true)
{
context.Override({ EnsureRunningAsAdmin, [](TestContext&)
{
} });
if (isAddExpected)
{
context.Override({ AddSource, [](TestContext&)
{
} });
}
context.Override({ CreateSourceForSourceAdd, [](TestContext& context)
{
auto testSource = std::make_shared<TestSource>();
testSource->Information.SourceAgreementsIdentifier = "AgreementsIdentifier";
testSource->Information.SourceAgreements.emplace_back("Agreement Label", "Agreement Text", "https://test");
testSource->Information.RequiredPackageMatchFields.emplace_back("Market");
testSource->Information.RequiredQueryParameters.emplace_back("Market");
context << Workflow::HandleSourceAgreements(Source{ testSource });
} });
}
TEST_CASE("SourceAddFlow_Agreement", "[SourceAddFlow][workflow]")
{
std::ostringstream sourceAddOutput;
TestContext context{ sourceAddOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
OverrideForSourceAddWithAgreements(context);
context.Args.AddArg(Execution::Args::Type::SourceName, "TestSource"sv);
context.Args.AddArg(Execution::Args::Type::SourceType, "Microsoft.Test"sv);
context.Args.AddArg(Execution::Args::Type::SourceArg, "TestArg"sv);
context.Args.AddArg(Execution::Args::Type::AcceptSourceAgreements);
SourceAddCommand sourceAdd({});
sourceAdd.Execute(context);
INFO(sourceAddOutput.str());
// Verify agreements are shown
REQUIRE(sourceAddOutput.str().find("Agreement Label") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("Agreement Text") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("https://test") != std::string::npos);
REQUIRE(sourceAddOutput.str().find(Resource::LocString(Resource::String::SourceAgreementsMarketMessage).get()) != std::string::npos);
// Verify Installer is called.
REQUIRE(context.GetTerminationHR() == S_OK);
}
TEST_CASE("SourceAddFlow_Agreement_Prompt_Yes", "[SourceAddFlow][workflow]")
{
// Accept the agreements by saying "Yes" at the prompt
std::istringstream sourceAddInput{ "y" };
std::ostringstream sourceAddOutput;
TestContext context{ sourceAddOutput, sourceAddInput };
auto previousThreadGlobals = context.SetForCurrentThread();
OverrideForSourceAddWithAgreements(context);
context.Args.AddArg(Execution::Args::Type::SourceName, "TestSource"sv);
context.Args.AddArg(Execution::Args::Type::SourceType, "Microsoft.Test"sv);
context.Args.AddArg(Execution::Args::Type::SourceArg, "TestArg"sv);
SourceAddCommand sourceAdd({});
sourceAdd.Execute(context);
INFO(sourceAddOutput.str());
// Verify agreements are shown
REQUIRE(sourceAddOutput.str().find("Agreement Label") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("Agreement Text") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("https://test") != std::string::npos);
REQUIRE(sourceAddOutput.str().find(Resource::LocString(Resource::String::SourceAgreementsMarketMessage).get()) != std::string::npos);
// Verify Installer is called.
REQUIRE(context.GetTerminationHR() == S_OK);
}
TEST_CASE("SourceAddFlow_Agreement_Prompt_No", "[SourceAddFlow][workflow]")
{
// Accept the agreements by saying "No" at the prompt
std::istringstream sourceAddInput{ "n" };
std::ostringstream sourceAddOutput;
TestContext context{ sourceAddOutput, sourceAddInput };
auto previousThreadGlobals = context.SetForCurrentThread();
OverrideForSourceAddWithAgreements(context, false);
context.Args.AddArg(Execution::Args::Type::SourceName, "TestSource"sv);
context.Args.AddArg(Execution::Args::Type::SourceType, "Microsoft.Test"sv);
context.Args.AddArg(Execution::Args::Type::SourceArg, "TestArg"sv);
SourceAddCommand sourceAdd({});
sourceAdd.Execute(context);
INFO(sourceAddOutput.str());
// Verify agreements are shown
REQUIRE(sourceAddOutput.str().find("Agreement Label") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("Agreement Text") != std::string::npos);
REQUIRE(sourceAddOutput.str().find("https://test") != std::string::npos);
REQUIRE(sourceAddOutput.str().find(Resource::LocString(Resource::String::SourceAgreementsMarketMessage).get()) != std::string::npos);
// Verify Installer is called.
REQUIRE(context.GetTerminationHR() == APPINSTALLER_CLI_ERROR_SOURCE_AGREEMENTS_NOT_ACCEPTED);
}
TEST_CASE("OpenSource_WithCustomHeader", "[OpenSource][CustomHeader]")
{
SetSetting(Stream::UserSources, R"(Sources:)"sv);
TestHook_ClearSourceFactoryOverrides();
SourceDetails details;
details.Name = "restsource";
details.Type = "Microsoft.Rest";
details.Arg = "thisIsTheArg";
details.Data = "thisIsTheData";
std::string customHeader = "Test custom header in Open source Flow";
bool receivedCustomHeader = false;
TestSourceFactory factory{
[&](const SourceDetails& sd, std::optional<std::string> header)
{
receivedCustomHeader = header.value() == customHeader;
return std::shared_ptr<ISource>(new TestSource(sd));
} };
TestHook_SetSourceFactoryOverride(details.Type, factory);
TestProgress progress;
AddSource(details, progress);
std::ostringstream output;
TestContext context{ output, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Args.AddArg(Execution::Args::Type::Query, "TestQuery"sv);
context.Args.AddArg(Execution::Args::Type::CustomHeader, customHeader);
context.Args.AddArg(Execution::Args::Type::Source, details.Name);
AppInstaller::CLI::Workflow::OpenSource()(context);
REQUIRE(receivedCustomHeader);
}
TEST_CASE("SourceResetFlow_ByNameResetsTombstonedDefaultSource", "[SourceResetFlow][workflow]")
{
SetSetting(Stream::UserSources, R"(
Sources:
- Name: winget
Type: ""
Arg: ""
Data: ""
IsTombstone: true
)"sv);
std::ostringstream output;
TestContext context{ output, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();
context.Override({ EnsureRunningAsAdmin, [](TestContext&) {} });
context.Args.AddArg(Execution::Args::Type::SourceName, "winget"sv);
SourceResetCommand sourceReset({});
sourceReset.Execute(context);
INFO(output.str());
REQUIRE(context.GetTerminationHR() == S_OK);
auto sources = Source::GetCurrentSources();
auto winget = std::find_if(
sources.begin(),
sources.end(),
[](const SourceDetails& sd) { return sd.Name == "winget"; });
REQUIRE(winget != sources.end());
REQUIRE(winget->Origin == SourceOrigin::Default);
}