-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
154 additions
and
0 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
Src/Plugins/Tests.Security/Validators/SEC101_017.NpmAuthorTokenValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.PatternMatcher.Sdk; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.PatternMatcher.Plugins.Security.Validators | ||
{ | ||
public class NpmAuthorTokenValidatorTests | ||
{ | ||
[Fact] | ||
public void NpmAuthorTokenValidator_MockHttpTests() | ||
{ | ||
var testCases = new[] | ||
{ | ||
new | ||
{ | ||
Title = "Testing Unauthorized StatusCode", | ||
HttpStatusCode = HttpStatusCode.Unauthorized, | ||
HttpContent = (HttpContent)null, | ||
ExpectedValidationState = ValidationState.Unauthorized, | ||
ExpectedMessage = string.Empty | ||
}, | ||
new | ||
{ | ||
Title = "Testing NotFound StatusCode", | ||
HttpStatusCode = HttpStatusCode.NotFound, | ||
HttpContent = (HttpContent)null, | ||
ExpectedValidationState = ValidationState.Unknown, | ||
ExpectedMessage = "An unexpected HTTP response code was received: 'NotFound'." | ||
}, | ||
new | ||
{ | ||
Title = "Testing Valid credentials - empty content", | ||
HttpStatusCode = HttpStatusCode.OK, | ||
HttpContent = new StringContent(string.Empty).As<HttpContent>(), | ||
ExpectedValidationState = ValidationState.Authorized, | ||
ExpectedMessage = string.Empty | ||
}, | ||
new | ||
{ | ||
Title = "Testing Valid credentials - readonly", | ||
HttpStatusCode = HttpStatusCode.OK, | ||
HttpContent = new StringContent(@" | ||
{ | ||
""objects"": [ | ||
{ | ||
""token"": ""abc123"", | ||
""key"": ""some long key"", | ||
""cidr_whitelist"": null, | ||
""readonly"": true, | ||
""automation"": false, | ||
""created"": ""2020-12-23T15:35:05.255Z"", | ||
""updated"": ""2020-12-23T15:35:05.255Z"" | ||
} | ||
], | ||
""total"": 1, | ||
""urls"": {} | ||
} | ||
", Encoding.UTF8, "application/json").As<HttpContent>(), | ||
ExpectedValidationState = ValidationState.Authorized, | ||
ExpectedMessage = "The token has 'read' permissions." | ||
}, | ||
new | ||
{ | ||
Title = "Testing Valid credentials - automation", | ||
HttpStatusCode = HttpStatusCode.OK, | ||
HttpContent = new StringContent(@" | ||
{ | ||
""objects"": [ | ||
{ | ||
""token"": ""abc123"", | ||
""key"": ""some long key"", | ||
""cidr_whitelist"": null, | ||
""readonly"": false, | ||
""automation"": true, | ||
""created"": ""2020-12-23T15:35:05.255Z"", | ||
""updated"": ""2020-12-23T15:35:05.255Z"" | ||
} | ||
], | ||
""total"": 1, | ||
""urls"": {} | ||
} | ||
", Encoding.UTF8, "application/json").As<HttpContent>(), | ||
ExpectedValidationState = ValidationState.Authorized, | ||
ExpectedMessage = "The token has 'automation' permissions." | ||
}, | ||
new | ||
{ | ||
Title = "Testing Valid credentials - publish", | ||
HttpStatusCode = HttpStatusCode.OK, | ||
HttpContent = new StringContent(@" | ||
{ | ||
""objects"": [ | ||
{ | ||
""token"": ""abc123"", | ||
""key"": ""some long key"", | ||
""cidr_whitelist"": null, | ||
""readonly"": false, | ||
""automation"": false, | ||
""created"": ""2020-12-23T15:35:05.255Z"", | ||
""updated"": ""2020-12-23T15:35:05.255Z"" | ||
} | ||
], | ||
""total"": 1, | ||
""urls"": {} | ||
} | ||
", Encoding.UTF8, "application/json").As<HttpContent>(), | ||
ExpectedValidationState = ValidationState.Authorized, | ||
ExpectedMessage = "The token has 'publish' permissions." | ||
}, | ||
}; | ||
|
||
const string fingerprintText = "[secret=abc123]"; | ||
|
||
var sb = new StringBuilder(); | ||
foreach (var testCase in testCases) | ||
{ | ||
string message = string.Empty; | ||
ResultLevelKind resultLevelKind = default; | ||
var fingerprint = new Fingerprint(fingerprintText); | ||
var keyValuePairs = new Dictionary<string, string>(); | ||
|
||
MockHelper.ResetStaticInstance<NpmAuthorTokenValidator>(); | ||
using var httpClient = new HttpClient(MockHelper.MockHttpMessageHandler(testCase.HttpStatusCode, testCase.HttpContent)); | ||
NpmAuthorTokenValidator.Instance.SetHttpClient(httpClient); | ||
|
||
ValidationState currentState = NpmAuthorTokenValidator.IsValidDynamic(ref fingerprint, | ||
ref message, | ||
keyValuePairs, | ||
ref resultLevelKind); | ||
if (currentState != testCase.ExpectedValidationState) | ||
{ | ||
sb.AppendLine($"The test case '{testCase.Title}' was expecting '{testCase.ExpectedValidationState}' but found '{currentState}'."); | ||
} | ||
|
||
if (!message.Equals(testCase.ExpectedMessage)) | ||
{ | ||
sb.AppendLine($"The test case '{testCase.Title}' was expecting '{testCase.ExpectedMessage}' but found '{message}'."); | ||
} | ||
} | ||
|
||
sb.Length.Should().Be(0, sb.ToString()); | ||
} | ||
} | ||
} |