diff options
Diffstat (limited to 'BackEnd/Timeline.Tests')
5 files changed, 186 insertions, 0 deletions
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs new file mode 100644 index 00000000..e6ae178f --- /dev/null +++ b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs @@ -0,0 +1,87 @@ +using FluentAssertions;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class BookmarkTimelineTest : IntegratedTestBase
+ {
+ [Fact]
+ public async Task AuthTest()
+ {
+ using var client = await CreateDefaultClient();
+
+ await client.TestPutAssertUnauthorizedAsync("bookmarks/@user1");
+ await client.TestDeleteAssertUnauthorizedAsync("bookmarks/@user1");
+ await client.TestPostAssertUnauthorizedAsync("bookmarkop/move", new HttpBookmarkTimelineMoveRequest { Timeline = "aaa", NewPosition = 1 });
+ }
+
+ [Fact]
+ public async Task InvalidModel()
+ {
+ using var client = await CreateClientAsUser();
+
+ await client.TestPutAssertInvalidModelAsync("bookmarks/!!!");
+ await client.TestDeleteAssertInvalidModelAsync("bookmarks/!!!");
+ await client.TestPostAssertInvalidModelAsync("bookmarkop/move", new HttpBookmarkTimelineMoveRequest { Timeline = null!, NewPosition = 1 });
+ await client.TestPostAssertInvalidModelAsync("bookmarkop/move", new HttpBookmarkTimelineMoveRequest { Timeline = "!!!", NewPosition = 1 });
+ await client.TestPostAssertInvalidModelAsync("bookmarkop/move", new HttpBookmarkTimelineMoveRequest { Timeline = "aaa", NewPosition = null });
+ }
+
+ [Fact]
+ public async Task ShouldWork()
+ {
+ using var client = await CreateClientAsUser();
+ await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t1" });
+
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().BeEmpty();
+ }
+
+ await client.TestPutAsync("bookmarks/@user1");
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().HaveCount(1);
+ h[0].Name.Should().Be("@user1");
+ }
+
+ await client.TestPutAsync("bookmarks/t1");
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().HaveCount(2);
+ h[0].Name.Should().Be("@user1");
+ h[1].Name.Should().Be("t1");
+ }
+
+ await client.TestPostAsync("bookmarkop/move", new HttpHighlightTimelineMoveRequest { Timeline = "@user1", NewPosition = 2 });
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().HaveCount(2);
+ h[0].Name.Should().Be("t1");
+ h[1].Name.Should().Be("@user1");
+ }
+
+ await client.TestDeleteAsync("bookmarks/@user1");
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().HaveCount(1);
+ h[0].Name.Should().Be("t1");
+ }
+
+ await client.TestDeleteAsync("bookmarks/t1");
+
+ {
+ var h = await client.TestGetAsync<List<HttpTimeline>>("bookmarks");
+ h.Should().BeEmpty();
+ }
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs index d4b4d55d..63f40a1e 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs @@ -28,6 +28,7 @@ namespace Timeline.Tests.IntegratedTests await client.TestPutAssertInvalidModelAsync("highlights/!!!");
await client.TestDeleteAssertInvalidModelAsync("highlights/!!!");
await client.TestPostAssertInvalidModelAsync("highlightop/move", new HttpHighlightTimelineMoveRequest { Timeline = null!, NewPosition = 1 });
+ await client.TestPostAssertInvalidModelAsync("highlightop/move", new HttpHighlightTimelineMoveRequest { Timeline = "!!!", NewPosition = 1 });
await client.TestPostAssertInvalidModelAsync("highlightop/move", new HttpHighlightTimelineMoveRequest { Timeline = "aaa", NewPosition = null });
}
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index ec517362..b219f092 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -192,6 +192,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Patch, url, jsonBody, errorCode, headerSetup);
}
+ public static async Task TestPutAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null)
+ {
+ await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Put, url, jsonBody, errorCode, headerSetup);
+ }
+
public static async Task TestDeleteAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null)
{
await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup);
diff --git a/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs new file mode 100644 index 00000000..1b8bff63 --- /dev/null +++ b/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs @@ -0,0 +1,89 @@ +using FluentAssertions;
+using Microsoft.Extensions.Logging.Abstractions;
+using System.Threading.Tasks;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.Services
+{
+ public class BookmarkTimelineServiceTest : DatabaseBasedTest
+ {
+ private BookmarkTimelineService _service = default!;
+ private UserService _userService = default!;
+ private TimelineService _timelineService = default!;
+
+ protected override void OnDatabaseCreated()
+ {
+ var clock = new TestClock();
+ _userService = new UserService(NullLogger<UserService>.Instance, Database, new PasswordService(), new UserPermissionService(Database), clock);
+ _timelineService = new TimelineService(Database, _userService, clock);
+ _service = new BookmarkTimelineService(Database, _userService, _timelineService);
+ }
+
+ [Fact]
+ public async Task Should_Work()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+
+ {
+ var b = await _service.GetBookmarks(userId);
+ b.Should().BeEmpty();
+ }
+
+ await _timelineService.CreateTimeline("tl", userId);
+ await _service.AddBookmark(userId, "tl");
+
+ {
+ var b = await _service.GetBookmarks(userId);
+ b.Should().HaveCount(1).And.BeEquivalentTo(await _timelineService.GetTimeline("tl"));
+ }
+ }
+
+ [Fact]
+ public async Task NewOne_Should_BeAtLast()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+ await _timelineService.CreateTimeline("t1", userId);
+ await _service.AddBookmark(userId, "t1");
+
+ await _timelineService.CreateTimeline("t2", userId);
+ await _service.AddBookmark(userId, "t2");
+
+ var b = await _service.GetBookmarks(userId);
+
+ b.Should().HaveCount(2);
+ b[0].Name.Should().Be("t1");
+ b[1].Name.Should().Be("t2");
+ }
+
+ [Fact]
+ public async Task Multiple_Should_Work()
+ {
+ var userId = await _userService.GetUserIdByUsername("user");
+
+ // make timeline id not same as entity id.
+ await _timelineService.CreateTimeline("t0", userId);
+
+ await _timelineService.CreateTimeline("t1", userId);
+ await _service.AddBookmark(userId, "t1");
+
+ await _timelineService.CreateTimeline("t2", userId);
+ await _service.AddBookmark(userId, "t2");
+
+ await _timelineService.CreateTimeline("t3", userId);
+ await _service.AddBookmark(userId, "t3");
+
+ await _service.MoveBookmark(userId, "t3", 2);
+ (await _service.GetBookmarks(userId))[1].Name.Should().Be("t3");
+
+ await _service.MoveBookmark(userId, "t1", 3);
+ (await _service.GetBookmarks(userId))[2].Name.Should().Be("t1");
+
+ await _service.RemoveBookmark(userId, "t2");
+ await _service.RemoveBookmark(userId, "t1");
+ await _service.RemoveBookmark(userId, "t3");
+ (await _service.GetBookmarks(userId)).Should().BeEmpty();
+ }
+ }
+}
diff --git a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs index dca070c6..f48404a9 100644 --- a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs +++ b/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs @@ -68,6 +68,10 @@ namespace Timeline.Tests.Services public async Task Multiple_Should_Work()
{
var userId = await _userService.GetUserIdByUsername("user");
+
+ // make timeline id not same as entity id.
+ await _timelineService.CreateTimeline("t0", userId);
+
await _timelineService.CreateTimeline("t1", userId);
await _service.AddHighlightTimeline("t1", userId);
|