Skip to content

Commit

Permalink
sonarlcoud
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Sep 10, 2024
1 parent be35c3c commit 52b9e2f
Show file tree
Hide file tree
Showing 9 changed files with 801 additions and 763 deletions.
755 changes: 377 additions & 378 deletions starsky/starskytest/Controllers/UploadControllerTest.cs

Large diffs are not rendered by default.

530 changes: 268 additions & 262 deletions starsky/starskytest/FakeCreateAn/CreateAnImage.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@
using starsky.foundation.storage.Storage;
using starskytest.FakeMocks;

namespace starskytest.FakeCreateAn.CreateAnImageCorrupt
namespace starskytest.FakeCreateAn.CreateAnImageCorrupt;

public class CreateAnImageCorrupt
{
public class CreateAnImageCorrupt
public readonly ImmutableArray<byte> Bytes = Array.Empty<byte>().ToImmutableArray();

public CreateAnImageCorrupt()
{
public CreateAnImageCorrupt()
var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if ( string.IsNullOrEmpty(dirName) )
{
var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if ( string.IsNullOrEmpty(dirName) ) return;
var path = Path.Combine(dirName, "FakeCreateAn",
"CreateAnImageCorrupt", "corrupt.jpg");

Bytes = StreamToBytes(path).ToImmutableArray();
return;
}

private static byte[] StreamToBytes(string path)
{
var input = new StorageHostFullPathFilesystem(new FakeIWebLogger()).ReadStream(path);
using var ms = new MemoryStream();
input.CopyTo(ms);
input.Dispose();
return ms.ToArray();
}
var path = Path.Combine(dirName, "FakeCreateAn",
"CreateAnImageCorrupt", "corrupt.jpg");

public readonly ImmutableArray<byte> Bytes = Array.Empty<byte>().ToImmutableArray();
Bytes = StreamToBytes(path).ToImmutableArray();
}

private static byte[] StreamToBytes(string path)
{
var input = new StorageHostFullPathFilesystem(new FakeIWebLogger()).ReadStream(path);
using var ms = new MemoryStream();
input.CopyTo(ms);
input.Dispose();
return ms.ToArray();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public class CreateFakeStarskyWindowsExe
public CreateFakeStarskyWindowsExe()
{
var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if ( string.IsNullOrEmpty(dirName) ) return;
if ( string.IsNullOrEmpty(dirName) )
{
return;
}

var parentFolder = Path.Combine(dirName, "FakeCreateAn",
"CreateFakeStarskyExe");
var path = Path.Combine(parentFolder, "starsky.exe");
Expand Down
30 changes: 20 additions & 10 deletions starsky/starskytest/FakeMocks/AppSettingsReflection.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
using System.Reflection;
using starsky.foundation.platform.Models;

namespace starskytest.FakeMocks
namespace starskytest.FakeMocks;

public static class AppSettingsReflection
{
public static class AppSettingsReflection
public static void Modify(AppSettings inputObject, string methodGetName = "get_DatabaseType",
object? value = null)
{
public static void Modify(AppSettings inputObject, string methodGetName = "get_DatabaseType", object? value = null)
var type = typeof(AppSettings);
foreach ( var property in type.GetProperties(BindingFlags.Public
| BindingFlags.Instance |
BindingFlags.DeclaredOnly) )
{
var type = typeof(AppSettings);
foreach ( var property in type.GetProperties(BindingFlags.Public
| BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
var getMethod = property.GetGetMethod(false);
if ( getMethod?.GetBaseDefinition() != getMethod ) continue;
if ( methodGetName != getMethod?.Name ) continue;
property.SetValue(inputObject, value, null);
var getMethod = property.GetGetMethod(false);
if ( getMethod?.GetBaseDefinition() != getMethod )
{
continue;
}

if ( methodGetName != getMethod?.Name )
{
continue;
}

property.SetValue(inputObject, value, null);
}
}
}
15 changes: 8 additions & 7 deletions starsky/starskytest/FakeMocks/FakeUserManagerActiveUsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public int GetCurrentUserId(HttpContext httpContext)
public User? GetUser(string credentialTypeCode, string identifier)
{
if ( CurrentUser != null && !Users.Contains(CurrentUser!) )
{
Users.Add(CurrentUser!);
}

return Users.Find(p => p.Credentials?.Any(credential =>
credential.Identifier == identifier) == true);
Expand All @@ -147,7 +149,11 @@ public Task<ValidateResult> RemoveUser(string credentialTypeCode,

public User? Exist(string identifier)
{
if ( Credentials.Identifier == identifier ) return CurrentUser;
if ( Credentials.Identifier == identifier )
{
return CurrentUser;
}

return null;
}

Expand All @@ -171,13 +177,8 @@ public bool PreflightValidate(string userName, string password, string confirmPa
return password != "false";
}

public CredentialType? GetCachedCredentialType(string email)
public CredentialType GetCachedCredentialType(string? credentialTypeCode)
{
return new CredentialType { Code = "email" };
}

public void AddUserToCache(User user)
{
throw new NotImplementedException();
}
}
106 changes: 56 additions & 50 deletions starsky/starskytest/FakeMocks/FakeWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,78 @@
using System.Threading;
using System.Threading.Tasks;

namespace starskytest.FakeMocks
namespace starskytest.FakeMocks;

public class FakeWebSocket : WebSocket
{
public class FakeWebSocket : WebSocket
{
public List<string> FakeSendItems { get; set; } = new List<string>();

public override void Abort()
{
throw new NotImplementedException();
}
public List<string> FakeSendItems { get; set; } = new();

public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public List<WebSocketCloseStatus> FakeCloseOutputAsync { get; set; } = new();

public List<WebSocketCloseStatus> FakeCloseOutputAsync { get; set; } = new List<WebSocketCloseStatus>();

#pragma warning disable 1998
public async override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription,
#pragma warning restore 1998
CancellationToken cancellationToken)
{
FakeCloseOutputAsync.Add(closeStatus);
}
private int FakeReceiveAsyncCounter { get; set; }

public override void Dispose()
{
GC.SuppressFinalize(this);
}
public string ReceiveAsyncMessage { get; set; } = "message";

public WebSocketError ReceiveAsyncErrorType { get; set; } = WebSocketError.InvalidState;

private int FakeReceiveAsyncCounter { get; set; }
public override WebSocketCloseStatus? CloseStatus { get; }
public override string? CloseStatusDescription { get; }

public string ReceiveAsyncMessage { get; set; } = "message";
public override WebSocketState State { get; } = WebSocketState.None;
public override string? SubProtocol { get; }

public WebSocketError ReceiveAsyncErrorType { get; set; } = WebSocketError.InvalidState;
public override void Abort()
{
throw new NotImplementedException();
}

public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

#pragma warning disable 1998
public override async Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
public override async Task CloseOutputAsync(WebSocketCloseStatus closeStatus,
string? statusDescription,
#pragma warning restore 1998
{
FakeReceiveAsyncCounter++;
if ( FakeReceiveAsyncCounter <= 2 )
{
new ArraySegment<byte>(Encoding.ASCII.GetBytes(ReceiveAsyncMessage)).CopyTo(buffer);
return new WebSocketReceiveResult(buffer.Count, WebSocketMessageType.Text, true,
WebSocketCloseStatus.Empty, "");
}
if ( FakeReceiveAsyncCounter == 3 ) return new WebSocketReceiveResult(buffer.Count, WebSocketMessageType.Close, true);

throw new WebSocketException(ReceiveAsyncErrorType);
}
CancellationToken cancellationToken)
{
FakeCloseOutputAsync.Add(closeStatus);
}

public override void Dispose()
{
GC.SuppressFinalize(this);
}

#pragma warning disable 1998
public override async Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage,
public override async Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer,
CancellationToken cancellationToken)
#pragma warning restore 1998
CancellationToken cancellationToken)
{
FakeReceiveAsyncCounter++;
if ( FakeReceiveAsyncCounter <= 2 )
{
new ArraySegment<byte>(Encoding.ASCII.GetBytes(ReceiveAsyncMessage)).CopyTo(buffer);
return new WebSocketReceiveResult(buffer.Count, WebSocketMessageType.Text, true,
WebSocketCloseStatus.Empty, "");
}

if ( FakeReceiveAsyncCounter == 3 )
{
FakeSendItems.Add(System.Text.Encoding.Default.GetString(buffer));
return new WebSocketReceiveResult(buffer.Count, WebSocketMessageType.Close, true);
}

public override WebSocketCloseStatus? CloseStatus { get; }
public override string? CloseStatusDescription { get; }
throw new WebSocketException(ReceiveAsyncErrorType);
}

public override WebSocketState State { get; } = WebSocketState.None;
public override string? SubProtocol { get; }
#pragma warning disable 1998
public override async Task SendAsync(ArraySegment<byte> buffer,
WebSocketMessageType messageType, bool endOfMessage,
#pragma warning restore 1998
CancellationToken cancellationToken)
{
FakeSendItems.Add(Encoding.Default.GetString(buffer));
}
}
51 changes: 27 additions & 24 deletions starsky/starskytest/Helpers/AntiForgeryCookieTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@
using starsky.Helpers;
using starskytest.FakeMocks;

namespace starskytest.Helpers
namespace starskytest.Helpers;

[TestClass]
public sealed class AntiForgeryCookieTest
{
[TestClass]
public sealed class AntiForgeryCookieTest
private static string? GetCookieValueFromResponse(HttpResponse response, string cookieName)
{
private static string? GetCookieValueFromResponse(HttpResponse response, string cookieName)
foreach ( var headers in response.Headers )
{
foreach (var headers in response.Headers)
if ( headers.Key != "Set-Cookie" )
{
if (headers.Key != "Set-Cookie")
continue;
string? header = headers.Value;
if (header?.StartsWith($"{cookieName}=") == true)
{
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
return header.Substring(p1 + 1, p2 - p1 - 1);
}
continue;
}
return null;
}


[TestMethod]
public void AntiForgeryCookie_SetRequestToken()
{
var httpContext = new DefaultHttpContext();
new AntiForgeryCookie(new FakeAntiforgery()).SetAntiForgeryCookie(httpContext);
var requestToken = GetCookieValueFromResponse(httpContext.Response, "X-XSRF-TOKEN");
Assert.AreEqual("requestToken",requestToken);
string? header = headers.Value;
if ( header?.StartsWith($"{cookieName}=") == true )
{
var p1 = header.IndexOf('=');
var p2 = header.IndexOf(';');
return header.Substring(p1 + 1, p2 - p1 - 1);
}
}

return null;
}


[TestMethod]
public void AntiForgeryCookie_SetRequestToken()
{
var httpContext = new DefaultHttpContext();
new AntiForgeryCookie(new FakeAntiforgery()).SetAntiForgeryCookie(httpContext);
var requestToken = GetCookieValueFromResponse(httpContext.Response, "X-XSRF-TOKEN");
Assert.AreEqual("requestToken", requestToken);
}
}
Loading

0 comments on commit 52b9e2f

Please sign in to comment.