Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
822 changes: 822 additions & 0 deletions Notesnook.API.Tests/Controllers/MonographsControllerTests.cs

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions Notesnook.API.Tests/Helpers/ControllerTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Notesnook.API.Controllers;

namespace Notesnook.API.Tests.Helpers
{
public static class ControllerTestHelper
{
public static void SetupControllerContext(MonographsController controller, string? userId = null, string? jti = null)
{
var claims = new List<Claim>();

if (!string.IsNullOrEmpty(userId))
{
claims.Add(new Claim("sub", userId));
}

if (!string.IsNullOrEmpty(jti))
{
claims.Add(new Claim("jti", jti));
}

var identity = new ClaimsIdentity(claims, "Test");
var principal = new ClaimsPrincipal(identity);
controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = principal
}
};
}

public static void SetupUnauthenticatedControllerContext(MonographsController controller)
{
controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = new ClaimsPrincipal(new ClaimsIdentity())
}
};
}
}
}
31 changes: 31 additions & 0 deletions Notesnook.API.Tests/Notesnook.API.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Notesnook.API\Notesnook.API.csproj" />
<ProjectReference Include="..\Streetwriters.Data\Streetwriters.Data.csproj" />
<ProjectReference Include="..\Streetwriters.Common\Streetwriters.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions Notesnook.API.Tests/TestData/MonographTestData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using MongoDB.Bson;
using Notesnook.API.Models;

namespace Notesnook.API.Tests.TestData
{
public static class MonographTestData
{
public const string TestUserId = "test-user-123";
public const string TestDeviceId = "test-device-456";
public const string TestJtiToken = "test-jti-token";

public static Monograph CreateMonograph(
string? itemId = null,
string? userId = null,
bool deleted = false,
bool isExisting = false,
bool selfDestruct = false,
string? title = null)
{
return new Monograph
{
Id = ObjectId.GenerateNewId().ToString(),
ItemId = itemId ?? Guid.NewGuid().ToString(),
Title = title ?? "Test Monograph",
UserId = userId ?? TestUserId,
Content = "This is test content for the monograph.",
Deleted = deleted,
DatePublished = isExisting ? DateTimeOffset.UtcNow.AddDays(-1).ToUnixTimeMilliseconds() : 0,
SelfDestruct = selfDestruct,
EncryptedContent = null,
CompressedContent = [1, 2, 3, 4, 5],
Password = null
};
}

public static Monograph CreateEncryptedMonograph()
{
var monograph = CreateMonograph();
monograph.Content = null;
monograph.CompressedContent = null;
monograph.EncryptedContent = new EncryptedData
{
Cipher = "encrypted-test-content",
IV = "test-iv",
Salt = "test-salt"
};
return monograph;
}

public static Monograph CreateLargeEncryptedMonograph()
{
var monograph = CreateMonograph();
monograph.Content = null;
monograph.CompressedContent = null;
monograph.EncryptedContent = new EncryptedData
{
Cipher = new string('*', 16 * 1024 * 1024),
IV = "test-iv",
Salt = "test-salt"
};
return monograph;
}

public static IEnumerable<string> MonographIds()
{
return new List<string> { "id1", "id2", "id3" };
}
}
}
4 changes: 2 additions & 2 deletions Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SyncItemsRepositoryAccessor : ISyncItemsRepositoryAccessor
public SyncItemsRepository Vaults { get; }
public SyncItemsRepository Tags { get; }
public Repository<UserSettings> UsersSettings { get; }
public Repository<Monograph> Monographs { get; }
public IMonographRepository Monographs { get; }

public SyncItemsRepositoryAccessor(IDbContext dbContext,

Expand Down Expand Up @@ -71,7 +71,7 @@ public SyncItemsRepositoryAccessor(IDbContext dbContext,
[FromKeyedServices(Collections.TagsKey)]
IMongoCollection<SyncItem> tags,

Repository<UserSettings> usersSettings, Repository<Monograph> monographs)
Repository<UserSettings> usersSettings, IMonographRepository monographs)
{
UsersSettings = usersSettings;
Monographs = monographs;
Expand Down
Loading