Skip to content

Commit 799077e

Browse files
Fix/1180-Add validation to prevent URLs in org and repo fields improve test targeting
1 parent b148ead commit 799077e

File tree

6 files changed

+242
-2
lines changed

6 files changed

+242
-2
lines changed

src/OctoshiftCLI.Tests/gei/Commands/GenerateScript/GenerateScriptCommandArgsTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,52 @@ public void UseGithubStorage_And_Aws_Bucket_Name_Throws()
107107
.ThrowExactly<OctoshiftCliException>()
108108
.WithMessage("*--use-github-storage flag was provided with an AWS S3 Bucket name*");
109109
}
110+
[Fact]
111+
public void Throws_If_Url_Passed_In_GithubSourceOrg()
112+
{
113+
var args = new GenerateScriptCommandArgs
114+
{
115+
GithubSourceOrg = "https://github.com/foo",
116+
GithubTargetOrg = "bar",
117+
GhesApiUrl = "https://github.contoso.com"
118+
};
119+
120+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
121+
.Should()
122+
.ThrowExactly<OctoshiftCliException>()
123+
.WithMessage("*GithubSourceOrg should be an org name, not a URL*");
124+
}
125+
126+
[Fact]
127+
public void Throws_If_Url_Passed_In_GithubTargetOrg()
128+
{
129+
var args = new GenerateScriptCommandArgs
130+
{
131+
GithubSourceOrg = "foo",
132+
GithubTargetOrg = "https://github.com/bar",
133+
GhesApiUrl = "https://github.contoso.com"
134+
};
135+
136+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
137+
.Should()
138+
.ThrowExactly<OctoshiftCliException>()
139+
.WithMessage("*GithubTargetOrg should be an org name, not a URL*");
140+
}
141+
142+
[Fact]
143+
public void Throws_If_Url_Passed_In_Both_Source_And_Target_Org()
144+
{
145+
var args = new GenerateScriptCommandArgs
146+
{
147+
GithubSourceOrg = "https://github.com/foo",
148+
GithubTargetOrg = "https://github.com/bar",
149+
GhesApiUrl = "https://github.contoso.com"
150+
};
151+
152+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
153+
.Should()
154+
.ThrowExactly<OctoshiftCliException>()
155+
.WithMessage("GithubSourceOrg should be an org name, not a URL.");
156+
}
110157
}
111158
}

src/OctoshiftCLI.Tests/gei/Commands/MigrateOrg/MigrateOrgCommandArgsTests.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,70 @@ public void Source_Pat_Defaults_To_Target_Pat()
3030

3131
args.GithubSourcePat.Should().Be(TARGET_PAT);
3232
}
33+
[Fact]
34+
public void Throws_If_Url_Passed_In_GithubSourceOrg()
35+
{
36+
var args = new MigrateOrgCommandArgs
37+
{
38+
GithubSourceOrg = "https://github.com/foo",
39+
GithubTargetOrg = TARGET_ORG,
40+
GithubTargetEnterprise = TARGET_ENTERPRISE,
41+
GithubTargetPat = TARGET_PAT,
42+
};
43+
44+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
45+
.Should()
46+
.ThrowExactly<OctoshiftCliException>()
47+
.WithMessage("*GithubSourceOrg should be an org name, not a URL*");
48+
}
49+
50+
[Fact]
51+
public void Throws_If_Url_Passed_In_GithubTargetOrg()
52+
{
53+
var args = new MigrateOrgCommandArgs
54+
{
55+
GithubSourceOrg = SOURCE_ORG,
56+
GithubTargetOrg = "https://github.com/foo",
57+
GithubTargetEnterprise = TARGET_ENTERPRISE,
58+
GithubTargetPat = TARGET_PAT,
59+
};
60+
61+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
62+
.Should()
63+
.ThrowExactly<OctoshiftCliException>()
64+
.WithMessage("*GithubTargetOrg should be an org name, not a URL*");
65+
}
66+
67+
[Fact]
68+
public void Throws_If_Url_Passed_In_Both_Source_And_Target_Org()
69+
{
70+
var args = new MigrateOrgCommandArgs
71+
{
72+
GithubSourceOrg = "https://github.com/foo",
73+
GithubTargetOrg = "https://github.com/bar",
74+
GithubTargetEnterprise = TARGET_ENTERPRISE,
75+
GithubTargetPat = TARGET_PAT,
76+
};
77+
78+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
79+
.Should()
80+
.ThrowExactly<OctoshiftCliException>()
81+
.WithMessage("GithubSourceOrg should be an org name, not a URL.");
82+
}
83+
[Fact]
84+
public void Throws_If_Url_Passed_In_GithubTargetEnterprise()
85+
{
86+
var args = new MigrateOrgCommandArgs
87+
{
88+
GithubSourceOrg = SOURCE_ORG,
89+
GithubTargetOrg = TARGET_ORG,
90+
GithubTargetEnterprise = "https://github.com/foo",
91+
GithubTargetPat = TARGET_PAT,
92+
};
93+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
94+
.Should()
95+
.ThrowExactly<OctoshiftCliException>()
96+
.WithMessage("*GithubTargetEnterprise should be an enterprise name, not a URL*");
97+
}
3398
}
3499
}

src/OctoshiftCLI.Tests/gei/Commands/MigrateRepo/MigrateRepoCommandArgsTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,90 @@ public void MetadataArchiveUrl_With_MetadataArchivePath_Throws()
308308
.ThrowExactly<OctoshiftCliException>()
309309
.WithMessage("*--metadata-archive-url and --metadata-archive-path may not be used together*");
310310
}
311+
[Fact]
312+
public void It_Throws_Error_When_Url_Provided_In_GithubSourceOrg()
313+
{
314+
var args = new MigrateRepoCommandArgs
315+
{
316+
SourceRepo = SOURCE_REPO,
317+
GithubSourceOrg = "https://github.com/foo",
318+
GithubTargetOrg = TARGET_ORG,
319+
TargetRepo = TARGET_REPO,
320+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
321+
MetadataArchivePath = METADATA_ARCHIVE_PATH
322+
};
323+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
324+
.Should()
325+
.ThrowExactly<OctoshiftCliException>()
326+
.WithMessage("*GithubSourceOrg should be an org name, not a URL*");
327+
}
328+
[Fact]
329+
public void It_Throws_Error_When_Url_Provided_In_GithubTargetOrg()
330+
{
331+
var args = new MigrateRepoCommandArgs
332+
{
333+
SourceRepo = SOURCE_REPO,
334+
GithubSourceOrg = SOURCE_ORG,
335+
GithubTargetOrg = "https://github.com/bar",
336+
TargetRepo = TARGET_REPO,
337+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
338+
MetadataArchivePath = METADATA_ARCHIVE_PATH
339+
};
340+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
341+
.Should()
342+
.ThrowExactly<OctoshiftCliException>()
343+
.WithMessage("*GithubTargetOrg should be an org name, not a URL*");
344+
}
345+
[Fact]
346+
public void It_Throws_Error_When_Url_Provided_In_Both_Source_And_Target_Org()
347+
{
348+
var args = new MigrateRepoCommandArgs
349+
{
350+
SourceRepo = SOURCE_REPO,
351+
GithubSourceOrg = "https://github.com/foo",
352+
GithubTargetOrg = "https://github.com/bar",
353+
TargetRepo = TARGET_REPO,
354+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
355+
MetadataArchivePath = METADATA_ARCHIVE_PATH
356+
};
357+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
358+
.Should()
359+
.ThrowExactly<OctoshiftCliException>()
360+
.WithMessage("*GithubSourceOrg should be an org name, not a URL*");
361+
}
362+
[Fact]
363+
public void It_Throws_Error_When_Url_Provided_In_SourceRepo()
364+
{
365+
var args = new MigrateRepoCommandArgs
366+
{
367+
SourceRepo = "https://github.com/foo",
368+
GithubSourceOrg = SOURCE_ORG,
369+
GithubTargetOrg = TARGET_ORG,
370+
TargetRepo = TARGET_REPO,
371+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
372+
MetadataArchivePath = METADATA_ARCHIVE_PATH
373+
};
374+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
375+
.Should()
376+
.ThrowExactly<OctoshiftCliException>()
377+
.WithMessage("*SourceRepo should be a repo name, not a URL*");
378+
}
379+
[Fact]
380+
public void It_Throws_Error_When_Url_Provided_In_TargetRepo()
381+
{
382+
var args = new MigrateRepoCommandArgs
383+
{
384+
SourceRepo = SOURCE_REPO,
385+
GithubSourceOrg = SOURCE_ORG,
386+
GithubTargetOrg = TARGET_ORG,
387+
TargetRepo = "https://github.com/bar",
388+
MetadataArchiveUrl = METADATA_ARCHIVE_URL,
389+
MetadataArchivePath = METADATA_ARCHIVE_PATH
390+
};
391+
FluentActions.Invoking(() => args.Validate(_mockOctoLogger.Object))
392+
.Should()
393+
.ThrowExactly<OctoshiftCliException>()
394+
.WithMessage("*TargetRepo should be a repo name, not a URL*");
395+
}
311396
}
312397
}

src/gei/Commands/GenerateScript/GenerateScriptCommandArgs.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public class GenerateScriptCommandArgs : CommandArgs
2828

2929
public override void Validate(OctoLogger log)
3030
{
31+
if (GithubSourceOrg.HasValue() && Uri.IsWellFormedUriString(GithubSourceOrg, UriKind.Absolute))
32+
{
33+
throw new OctoshiftCliException("GithubSourceOrg should be an org name, not a URL.");
34+
}
35+
36+
if (GithubTargetOrg.HasValue() && Uri.IsWellFormedUriString(GithubTargetOrg, UriKind.Absolute))
37+
{
38+
throw new OctoshiftCliException("GithubTargetOrg should be an org name, not a URL.");
39+
}
3140
if (AwsBucketName.HasValue())
3241
{
3342
if (GhesApiUrl.IsNullOrWhiteSpace())

src/gei/Commands/MigrateOrg/MigrateOrgCommandArgs.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OctoshiftCLI.Commands;
1+
using System;
2+
using OctoshiftCLI.Commands;
23
using OctoshiftCLI.Extensions;
34
using OctoshiftCLI.Services;
45

@@ -19,6 +20,19 @@ public class MigrateOrgCommandArgs : CommandArgs
1920

2021
public override void Validate(OctoLogger log)
2122
{
23+
if (GithubSourceOrg.HasValue() && Uri.IsWellFormedUriString(GithubSourceOrg, UriKind.Absolute))
24+
{
25+
throw new OctoshiftCliException("GithubSourceOrg should be an org name, not a URL.");
26+
}
27+
28+
if (GithubTargetOrg.HasValue() && Uri.IsWellFormedUriString(GithubTargetOrg, UriKind.Absolute))
29+
{
30+
throw new OctoshiftCliException("GithubTargetOrg should be an org name, not a URL.");
31+
}
32+
if(GithubTargetEnterprise.HasValue() && Uri.IsWellFormedUriString(GithubTargetEnterprise, UriKind.Absolute))
33+
{
34+
throw new OctoshiftCliException("GithubTargetEnterprise should be an enterprise name, not a URL.");
35+
}
2236
if (GithubTargetPat.HasValue() && GithubSourcePat.IsNullOrWhiteSpace())
2337
{
2438
GithubSourcePat = GithubTargetPat;

src/gei/Commands/MigrateRepo/MigrateRepoCommandArgs.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OctoshiftCLI.Commands;
1+
using System;
2+
using OctoshiftCLI.Commands;
23
using OctoshiftCLI.Extensions;
34
using OctoshiftCLI.Services;
45

@@ -44,6 +45,25 @@ public override void Validate(OctoLogger log)
4445
DefaultSourcePat(log);
4546
DefaultTargetRepo(log);
4647

48+
if (!string.IsNullOrWhiteSpace(GithubSourceOrg) && Uri.IsWellFormedUriString(GithubSourceOrg, UriKind.Absolute))
49+
{
50+
throw new OctoshiftCliException("GithubSourceOrg should be an org name, not a URL.");
51+
}
52+
53+
if (!string.IsNullOrWhiteSpace(GithubTargetOrg) && Uri.IsWellFormedUriString(GithubTargetOrg, UriKind.Absolute))
54+
{
55+
throw new OctoshiftCliException("GithubTargetOrg should be an org name, not a URL.");
56+
}
57+
58+
if (!string.IsNullOrWhiteSpace(SourceRepo) && Uri.IsWellFormedUriString(SourceRepo, UriKind.Absolute))
59+
{
60+
throw new OctoshiftCliException("SourceRepo should be a repo name, not a URL.");
61+
}
62+
63+
if (!string.IsNullOrWhiteSpace(TargetRepo) && Uri.IsWellFormedUriString(TargetRepo, UriKind.Absolute))
64+
{
65+
throw new OctoshiftCliException("TargetRepo should be a repo name, not a URL.");
66+
}
4767
if (GitArchiveUrl.HasValue() && GitArchivePath.HasValue())
4868
{
4969
throw new OctoshiftCliException("The options --git-archive-url and --git-archive-path may not be used together");

0 commit comments

Comments
 (0)