aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-07 16:23:20 +0800
committercrupest <crupest@outlook.com>2021-01-07 16:23:20 +0800
commitdf1ef1e21d8d889a2c9abd440039533c6a43818f (patch)
treec483a2fff48ad952e787e5af1bb541d939a09f19 /BackEnd/Timeline.Tests
parent9470631c67c4740982ff2d8a16cbbb86fdd34609 (diff)
downloadtimeline-df1ef1e21d8d889a2c9abd440039533c6a43818f.tar.gz
timeline-df1ef1e21d8d889a2c9abd440039533c6a43818f.tar.bz2
timeline-df1ef1e21d8d889a2c9abd440039533c6a43818f.zip
史诗级重构!
Diffstat (limited to 'BackEnd/Timeline.Tests')
-rw-r--r--BackEnd/Timeline.Tests/Helpers/TestDatabase.cs18
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs5
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs32
-rw-r--r--BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs102
-rw-r--r--BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs96
-rw-r--r--BackEnd/Timeline.Tests/Services/TimelinePostServiceTest.cs200
-rw-r--r--BackEnd/Timeline.Tests/Services/TimelineServiceTest.cs149
7 files changed, 27 insertions, 575 deletions
diff --git a/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs b/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs
index 00164835..7b9a992f 100644
--- a/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs
+++ b/BackEnd/Timeline.Tests/Helpers/TestDatabase.cs
@@ -33,16 +33,22 @@ namespace Timeline.Tests.Helpers
{
Key = JwtTokenGenerateHelper.GenerateKey()
});
+
await context.SaveChangesAsync();
- if (_createUser)
- {
- var passwordService = new PasswordService();
- var userService = new UserService(NullLogger<UserService>.Instance, context, passwordService, new UserPermissionService(context), new Clock());
+ var passwordService = new PasswordService();
+ var userService = new UserService(NullLogger<UserService>.Instance, context, passwordService, new Clock());
- var admin = await userService.CreateUser("admin", "adminpw");
- await userService.ModifyUser(admin.Id, new ModifyUserParams() { Nickname = "administrator" });
+ var admin = await userService.CreateUser("admin", "adminpw");
+ await userService.ModifyUser(admin.Id, new ModifyUserParams() { Nickname = "administrator" });
+ admin.Permissions.Add(new UserPermissionEntity { Permission = UserPermission.AllTimelineManagement.ToString() });
+ admin.Permissions.Add(new UserPermissionEntity { Permission = UserPermission.HighlightTimelineManagement.ToString() });
+ admin.Permissions.Add(new UserPermissionEntity { Permission = UserPermission.UserManagement.ToString() });
+ await context.SaveChangesAsync();
+
+ if (_createUser)
+ {
var user = await userService.CreateUser("user", "userpw");
await userService.ModifyUser(user.Id, new ModifyUserParams() { Nickname = "imuser" });
}
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
index 82aed24e..b700ada2 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
@@ -56,10 +56,7 @@ namespace Timeline.Tests.IntegratedTests
{
using var scope = TestApp.Host.Services.CreateScope();
- var users = new List<(string username, string password, string nickname)>()
- {
- ("admin", "adminpw", "administrator")
- };
+ var users = new List<(string username, string password, string nickname)>();
for (int i = 1; i <= TestUserCount; i++)
{
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
index 12dd2b8d..f0715082 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
@@ -21,11 +21,7 @@ namespace Timeline.Tests.IntegratedTests
{
public static HttpTimelinePostContent TextPostContent(string text)
{
- return new HttpTimelinePostContent
- {
- Type = "text",
- Text = text
- };
+ return new HttpTimelinePostContent("text", text, null, null);
}
public static HttpTimelinePostCreateRequest TextPostCreateRequest(string text, DateTime? time = null)
@@ -261,15 +257,15 @@ namespace Timeline.Tests.IntegratedTests
using var client = await CreateClientAsAdministrator();
await client.TestDeleteAssertInvalidModelAsync("timelines/!!!");
- await client.TestDeleteAsync("timelines/t2", true);
- await client.TestDeleteAsync("timelines/t2", false);
+ await client.TestDeleteAsync("timelines/t2");
+ await client.TestDeleteAssertErrorAsync("timelines/t2");
}
{
using var client = await CreateClientAs(1);
await client.TestDeleteAssertInvalidModelAsync("timelines/!!!");
- await client.TestDeleteAsync("timelines/t1", true);
+ await client.TestDeleteAsync("timelines/t1");
await client.TestDeleteAssertErrorAsync("timelines/t1");
}
}
@@ -355,13 +351,13 @@ namespace Timeline.Tests.IntegratedTests
}
await AssertEmptyMembers();
- await client.TestPutAssertErrorAsync($"timelines/{timelineName}/members/usernotexist", errorCode: ErrorCodes.TimelineController.MemberPut_NotExist);
+ await client.TestPutAssertErrorAsync($"timelines/{timelineName}/members/usernotexist", errorCode: ErrorCodes.UserCommon.NotExist);
await AssertEmptyMembers();
await client.PutTimelineMemberAsync(timelineName, "user2");
await AssertMembers(new List<HttpUser> { await client.GetUserAsync("user2") });
await client.DeleteTimelineMemberAsync(timelineName, "user2", true);
await AssertEmptyMembers();
- await client.DeleteTimelineMemberAsync(timelineName, "aaa", false);
+ await client.TestDeleteAssertErrorAsync($"timelines/{timelineName}/members/usernotexist", errorCode: ErrorCodes.UserCommon.NotExist);
await AssertEmptyMembers();
}
@@ -549,9 +545,9 @@ namespace Timeline.Tests.IntegratedTests
body.Should().BeEquivalentTo(createRes, createRes2);
}
{
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}", true);
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}", false);
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/30000", false);
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}");
+ await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{createRes.Id}");
+ await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/30000");
}
{
var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
@@ -651,8 +647,8 @@ namespace Timeline.Tests.IntegratedTests
}
await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data");
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}", true);
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}", false);
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}");
+ await client.TestDeleteAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}");
{
var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts");
@@ -736,7 +732,7 @@ namespace Timeline.Tests.IntegratedTests
await Task.Delay(1000);
}
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}", true);
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}");
{
var body = await client.TestGetAsync<List<HttpTimelinePost>>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }");
@@ -763,7 +759,7 @@ namespace Timeline.Tests.IntegratedTests
foreach (var id in new long[] { posts[0].Id, posts[2].Id })
{
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}", true);
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}");
}
{
@@ -791,7 +787,7 @@ namespace Timeline.Tests.IntegratedTests
await Task.Delay(1000);
}
- await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}", true);
+ await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}");
{
diff --git a/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs
deleted file mode 100644
index 849936ec..00000000
--- a/BackEnd/Timeline.Tests/Services/BookmarkTimelineServiceTest.cs
+++ /dev/null
@@ -1,102 +0,0 @@
-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();
- }
-
- [Fact]
- public async Task AddExist_Should_DoNothing()
- {
- var userId = await _userService.GetUserIdByUsername("user");
-
- await _timelineService.CreateTimeline("t", userId);
-
- await _service.AddBookmark(userId, "t");
- await _service.AddBookmark(userId, "t");
-
- (await _service.GetBookmarks(userId)).Should().HaveCount(1);
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs
deleted file mode 100644
index f48404a9..00000000
--- a/BackEnd/Timeline.Tests/Services/HighlightTimelineServiceTest.cs
+++ /dev/null
@@ -1,96 +0,0 @@
-using FluentAssertions;
-using Microsoft.Extensions.Logging.Abstractions;
-using System.Threading.Tasks;
-using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-using Xunit.Abstractions;
-
-namespace Timeline.Tests.Services
-{
- public class HighlightTimelineServiceTest : DatabaseBasedTest
- {
- private readonly TestClock _clock = new TestClock();
- private UserService _userService = default!;
- private TimelineService _timelineService = default!;
-
- private HighlightTimelineService _service = default!;
-
- public HighlightTimelineServiceTest(ITestOutputHelper testOutputHelper)
- : base(testOutputHelper)
- {
-
- }
-
- protected override void OnDatabaseCreated()
- {
- _userService = new UserService(NullLogger<UserService>.Instance, Database, new PasswordService(), new UserPermissionService(Database), _clock);
- _timelineService = new TimelineService(Database, _userService, _clock);
- _service = new HighlightTimelineService(Database, _userService, _timelineService, _clock);
- }
-
- [Fact]
- public async Task Should_Work()
- {
- {
- var ht = await _service.GetHighlightTimelines();
- ht.Should().BeEmpty();
- }
-
- var userId = await _userService.GetUserIdByUsername("user");
- await _timelineService.CreateTimeline("tl", userId);
- await _service.AddHighlightTimeline("tl", userId);
-
- {
- var ht = await _service.GetHighlightTimelines();
- ht.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.AddHighlightTimeline("t1", userId);
-
- await _timelineService.CreateTimeline("t2", userId);
- await _service.AddHighlightTimeline("t2", userId);
-
- var ht = await _service.GetHighlightTimelines();
-
- ht.Should().HaveCount(2);
- ht[0].Name.Should().Be("t1");
- ht[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.AddHighlightTimeline("t1", userId);
-
- await _timelineService.CreateTimeline("t2", userId);
- await _service.AddHighlightTimeline("t2", userId);
-
- await _timelineService.CreateTimeline("t3", userId);
- await _service.AddHighlightTimeline("t3", userId);
-
- await _service.MoveHighlightTimeline("t3", 2);
- (await _service.GetHighlightTimelines())[1].Name.Should().Be("t3");
-
- await _service.MoveHighlightTimeline("t1", 3);
- (await _service.GetHighlightTimelines())[2].Name.Should().Be("t1");
-
- await _service.RemoveHighlightTimeline("t2", userId);
- await _service.RemoveHighlightTimeline("t1", userId);
- await _service.RemoveHighlightTimeline("t3", userId);
- (await _service.GetHighlightTimelines()).Should().BeEmpty();
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Services/TimelinePostServiceTest.cs b/BackEnd/Timeline.Tests/Services/TimelinePostServiceTest.cs
deleted file mode 100644
index 7771ae0b..00000000
--- a/BackEnd/Timeline.Tests/Services/TimelinePostServiceTest.cs
+++ /dev/null
@@ -1,200 +0,0 @@
-using FluentAssertions;
-using Microsoft.Extensions.Logging.Abstractions;
-using System;
-using System.Linq;
-using System.Threading.Tasks;
-using Timeline.Models;
-using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-
-namespace Timeline.Tests.Services
-{
- public class TimelinePostServiceTest : DatabaseBasedTest
- {
- private readonly PasswordService _passwordService = new PasswordService();
-
- private readonly ETagGenerator _eTagGenerator = new ETagGenerator();
-
- private readonly ImageValidator _imageValidator = new ImageValidator();
-
- private readonly TestClock _clock = new TestClock();
-
- private DataManager _dataManager = default!;
-
- private UserPermissionService _userPermissionService = default!;
-
- private UserService _userService = default!;
-
- private TimelineService _timelineService = default!;
-
- private TimelinePostService _timelinePostService = default!;
-
- private UserDeleteService _userDeleteService = default!;
-
- protected override void OnDatabaseCreated()
- {
- _dataManager = new DataManager(Database, _eTagGenerator);
- _userPermissionService = new UserPermissionService(Database);
- _userService = new UserService(NullLogger<UserService>.Instance, Database, _passwordService, _userPermissionService, _clock);
- _timelineService = new TimelineService(Database, _userService, _clock);
- _timelinePostService = new TimelinePostService(NullLogger<TimelinePostService>.Instance, Database, _timelineService, _userService, _dataManager, _imageValidator, _clock);
- _userDeleteService = new UserDeleteService(NullLogger<UserDeleteService>.Instance, Database, _timelinePostService);
- }
-
- protected override void BeforeDatabaseDestroy()
- {
- _eTagGenerator.Dispose();
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task GetPosts_ModifiedSince(string timelineName)
- {
- _clock.ForwardCurrentTime();
-
- var userId = await _userService.GetUserIdByUsername("user");
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, userId);
-
- var postContentList = new string[] { "a", "b", "c", "d" };
-
- DateTime testPoint = new DateTime();
-
- foreach (var (content, index) in postContentList.Select((v, i) => (v, i)))
- {
- var t = _clock.ForwardCurrentTime();
- if (index == 1)
- testPoint = t;
- await _timelinePostService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var posts = await _timelinePostService.GetPosts(timelineName, testPoint);
- posts.Should().HaveCount(3)
- .And.Subject.Select(p => ((TextTimelinePostContent)p.Content!).Text).Should().Equal(postContentList.Skip(1));
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task GetPosts_IncludeDeleted(string timelineName)
- {
- var userId = await _userService.GetUserIdByUsername("user");
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, userId);
-
- var postContentList = new string[] { "a", "b", "c", "d" };
-
- foreach (var content in postContentList)
- {
- await _timelinePostService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var posts = await _timelinePostService.GetPosts(timelineName);
- posts.Should().HaveCount(4);
- posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count));
- posts.Select(p => ((TextTimelinePostContent)p.Content!).Text).Should().Equal(postContentList);
-
- foreach (var id in new long[] { posts[0].Id, posts[2].Id })
- {
- await _timelinePostService.DeletePost(timelineName, id);
- }
-
- posts = await _timelinePostService.GetPosts(timelineName);
- posts.Should().HaveCount(2);
- posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count));
- posts.Select(p => ((TextTimelinePostContent)p.Content!).Text).Should().Equal(new string[] { "b", "d" });
-
- posts = await _timelinePostService.GetPosts(timelineName, includeDeleted: true);
- posts.Should().HaveCount(4);
- posts.Select(p => p.Deleted).Should().Equal(new bool[] { true, false, true, false });
- posts.Where(p => !p.Deleted).Select(p => ((TextTimelinePostContent)p.Content!).Text).Should().Equal(new string[] { "b", "d" });
- }
-
- [Theory]
- [InlineData("@admin")]
- [InlineData("tl")]
- public async Task GetPosts_ModifiedSince_UsernameChange(string timelineName)
- {
- var time1 = _clock.ForwardCurrentTime();
-
- var userId = await _userService.GetUserIdByUsername("user");
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, userId);
-
- var postContentList = new string[] { "a", "b", "c", "d" };
-
- foreach (var (content, index) in postContentList.Select((v, i) => (v, i)))
- {
- await _timelinePostService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var time2 = _clock.ForwardCurrentTime();
-
- {
- var posts = await _timelinePostService.GetPosts(timelineName, time2);
- posts.Should().HaveCount(0);
- }
-
- {
- await _userService.ModifyUser(userId, new ModifyUserParams { Nickname = "haha" });
- var posts = await _timelinePostService.GetPosts(timelineName, time2);
- posts.Should().HaveCount(0);
- }
-
- {
- await _userService.ModifyUser(userId, new ModifyUserParams { Username = "haha" });
- var posts = await _timelinePostService.GetPosts(timelineName, time2);
- posts.Should().HaveCount(4);
- }
- }
-
- [Theory]
- [InlineData("@admin")]
- [InlineData("tl")]
- public async Task GetPosts_ModifiedSince_UserDelete(string timelineName)
- {
- var time1 = _clock.ForwardCurrentTime();
-
- var userId = await _userService.GetUserIdByUsername("user");
- var adminId = await _userService.GetUserIdByUsername("admin");
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, adminId);
-
- var postContentList = new string[] { "a", "b", "c", "d" };
-
- foreach (var (content, index) in postContentList.Select((v, i) => (v, i)))
- {
- await _timelinePostService.CreateTextPost(timelineName, userId, content, null);
- }
-
- var time2 = _clock.ForwardCurrentTime();
-
- {
- var posts = await _timelinePostService.GetPosts(timelineName, time2);
- posts.Should().HaveCount(0);
- }
-
- await _userDeleteService.DeleteUser("user");
-
- {
- var posts = await _timelinePostService.GetPosts(timelineName, time2);
- posts.Should().HaveCount(0);
- }
-
- {
- var posts = await _timelinePostService.GetPosts(timelineName, time2, true);
- posts.Should().HaveCount(4);
- }
- }
- }
-}
diff --git a/BackEnd/Timeline.Tests/Services/TimelineServiceTest.cs b/BackEnd/Timeline.Tests/Services/TimelineServiceTest.cs
deleted file mode 100644
index 70f54ede..00000000
--- a/BackEnd/Timeline.Tests/Services/TimelineServiceTest.cs
+++ /dev/null
@@ -1,149 +0,0 @@
-using FluentAssertions;
-using Microsoft.Extensions.Logging.Abstractions;
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using Timeline.Models;
-using Timeline.Services;
-using Timeline.Services.Exceptions;
-using Timeline.Tests.Helpers;
-using Xunit;
-
-namespace Timeline.Tests.Services
-{
- public class TimelineServiceTest : DatabaseBasedTest
- {
- private readonly PasswordService _passwordService = new PasswordService();
-
- private readonly TestClock _clock = new TestClock();
-
- private UserPermissionService _userPermissionService = default!;
-
- private UserService _userService = default!;
-
- private TimelineService _timelineService = default!;
-
- protected override void OnDatabaseCreated()
- {
- _userPermissionService = new UserPermissionService(Database);
- _userService = new UserService(NullLogger<UserService>.Instance, Database, _passwordService, _userPermissionService, _clock);
- _timelineService = new TimelineService(Database, _userService, _clock);
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_GetLastModified(string timelineName)
- {
- var time = _clock.ForwardCurrentTime();
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- var t = await _timelineService.GetTimelineLastModifiedTime(timelineName);
-
- t.Should().Be(time);
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_GetUnqiueId(string timelineName)
- {
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- var uniqueId = await _timelineService.GetTimelineUniqueId(timelineName);
-
- uniqueId.Should().NotBeNullOrEmpty();
- }
-
- [Theory]
- [InlineData("@user")]
- [InlineData("tl")]
- public async Task Timeline_LastModified(string timelineName)
- {
- var initTime = _clock.ForwardCurrentTime();
-
- void Check(TimelineInfo timeline)
- {
- timeline.NameLastModified.Should().Be(initTime);
- timeline.LastModified.Should().Be(_clock.GetCurrentTime());
- }
-
- async Task GetAndCheck()
- {
- Check(await _timelineService.GetTimeline(timelineName));
- }
-
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- Check(await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user")));
-
- await GetAndCheck();
-
- _clock.ForwardCurrentTime();
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Visibility = TimelineVisibility.Public });
- await GetAndCheck();
-
- _clock.ForwardCurrentTime();
- await _timelineService.ChangeMember(timelineName, new List<string> { "admin" }, null);
- await GetAndCheck();
- }
-
- [Theory]
- [InlineData("@admin")]
- [InlineData("tl")]
- public async Task Title(string timelineName)
- {
- var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal);
- if (!isPersonal)
- await _timelineService.CreateTimeline(timelineName, await _userService.GetUserIdByUsername("user"));
-
- {
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be(timelineName);
- }
-
- {
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Title = null });
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be(timelineName);
- }
-
- {
- await _timelineService.ChangeProperty(timelineName, new TimelineChangePropertyRequest { Title = "atitle" });
- var timeline = await _timelineService.GetTimeline(timelineName);
- timeline.Title.Should().Be("atitle");
- }
- }
-
- [Fact]
- public async Task ChangeName()
- {
- _clock.ForwardCurrentTime();
-
- await _timelineService.Awaiting(s => s.ChangeTimelineName("!!!", "newtl")).Should().ThrowAsync<ArgumentException>();
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "!!!")).Should().ThrowAsync<ArgumentException>();
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "newtl")).Should().ThrowAsync<TimelineNotExistException>();
-
- await _timelineService.CreateTimeline("tl", await _userService.GetUserIdByUsername("user"));
- await _timelineService.CreateTimeline("tl2", await _userService.GetUserIdByUsername("user"));
-
- await _timelineService.Awaiting(s => s.ChangeTimelineName("tl", "tl2")).Should().ThrowAsync<EntityAlreadyExistException>();
-
- var time = _clock.ForwardCurrentTime();
-
- await _timelineService.ChangeTimelineName("tl", "newtl");
-
- {
- var timeline = await _timelineService.GetTimeline("newtl");
- timeline.Name.Should().Be("newtl");
- timeline.LastModified.Should().Be(time);
- timeline.NameLastModified.Should().Be(time);
- }
- }
- }
-}