aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.Tests')
-rw-r--r--Timeline.Tests/Helpers/TestApplication.cs29
-rw-r--r--Timeline.Tests/Helpers/TestClock.cs43
-rw-r--r--Timeline.Tests/Helpers/TestDatabase.cs76
-rw-r--r--Timeline.Tests/IntegratedTests/IntegratedTestBase.cs3
-rw-r--r--Timeline.Tests/IntegratedTests/TimelineTest.cs72
-rw-r--r--Timeline.Tests/Services/TimelineServiceTest.cs94
6 files changed, 282 insertions, 35 deletions
diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs
index 45807516..684ffe2c 100644
--- a/Timeline.Tests/Helpers/TestApplication.cs
+++ b/Timeline.Tests/Helpers/TestApplication.cs
@@ -10,14 +10,13 @@ using System.IO;
using System.Threading.Tasks;
using Timeline.Configs;
using Timeline.Entities;
-using Timeline.Migrations;
using Xunit;
namespace Timeline.Tests.Helpers
{
public class TestApplication : IAsyncLifetime
{
- public SqliteConnection DatabaseConnection { get; private set; }
+ public TestDatabase Database { get; }
public IHost Host { get; private set; }
@@ -25,30 +24,16 @@ namespace Timeline.Tests.Helpers
public TestApplication()
{
-
+ Database = new TestDatabase(false);
}
public async Task InitializeAsync()
{
+ await Database.InitializeAsync();
+
WorkDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(WorkDir);
- DatabaseConnection = new SqliteConnection("Data Source=:memory:;");
- await DatabaseConnection.OpenAsync();
-
- var options = new DbContextOptionsBuilder<DatabaseContext>()
- .UseSqlite(DatabaseConnection).Options;
-
- using (var context = new DatabaseContext(options))
- {
- await context.Database.EnsureCreatedAsync();
- context.JwtToken.Add(new JwtTokenEntity
- {
- Key = JwtTokenGenerateHelper.GenerateKey()
- });
- await context.SaveChangesAsync();
- }
-
Host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
@@ -62,7 +47,7 @@ namespace Timeline.Tests.Helpers
{
services.AddDbContext<DatabaseContext>(options =>
{
- options.UseSqlite(DatabaseConnection);
+ options.UseSqlite(Database.Connection);
});
})
.ConfigureWebHost(webBuilder =>
@@ -79,9 +64,9 @@ namespace Timeline.Tests.Helpers
await Host.StopAsync();
Host.Dispose();
- await DatabaseConnection.CloseAsync();
- await DatabaseConnection.DisposeAsync();
Directory.Delete(WorkDir, true);
+
+ await Database.DisposeAsync();
}
}
}
diff --git a/Timeline.Tests/Helpers/TestClock.cs b/Timeline.Tests/Helpers/TestClock.cs
new file mode 100644
index 00000000..de7d0eb7
--- /dev/null
+++ b/Timeline.Tests/Helpers/TestClock.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Services;
+
+namespace Timeline.Tests.Helpers
+{
+ public class TestClock : IClock
+ {
+ private DateTime? _currentTime = null;
+
+ public DateTime GetCurrentTime()
+ {
+ return _currentTime ?? DateTime.Now;
+ }
+
+ public void SetCurrentTime(DateTime? mockTime)
+ {
+ _currentTime = mockTime;
+ }
+
+ public DateTime SetMockCurrentTime()
+ {
+ var time = new DateTime(2000, 1, 1, 1, 1, 1);
+ _currentTime = time;
+ return time;
+ }
+
+ public DateTime ForwardCurrentTime()
+ {
+ return ForwardCurrentTime(TimeSpan.FromDays(1));
+ }
+
+ public DateTime ForwardCurrentTime(TimeSpan timeSpan)
+ {
+ if (_currentTime == null)
+ return SetMockCurrentTime();
+ _currentTime.Value.Add(timeSpan);
+ return _currentTime.Value;
+ }
+ }
+}
diff --git a/Timeline.Tests/Helpers/TestDatabase.cs b/Timeline.Tests/Helpers/TestDatabase.cs
new file mode 100644
index 00000000..40f8f2d4
--- /dev/null
+++ b/Timeline.Tests/Helpers/TestDatabase.cs
@@ -0,0 +1,76 @@
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging.Abstractions;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Migrations;
+using Timeline.Models;
+using Timeline.Services;
+using Xunit;
+
+namespace Timeline.Tests.Helpers
+{
+ public class TestDatabase : IAsyncLifetime
+ {
+ private readonly bool _createUser;
+
+ public TestDatabase(bool createUser = true)
+ {
+ _createUser = createUser;
+ Connection = new SqliteConnection("Data Source=:memory:;");
+ }
+
+ public async Task InitializeAsync()
+ {
+ await Connection.OpenAsync();
+
+ using (var context = CreateContext())
+ {
+ await context.Database.EnsureCreatedAsync();
+ context.JwtToken.Add(new JwtTokenEntity
+ {
+ Key = JwtTokenGenerateHelper.GenerateKey()
+ });
+ await context.SaveChangesAsync();
+
+ if (_createUser)
+ {
+ var passwordService = new PasswordService();
+ var userService = new UserService(NullLogger<UserService>.Instance, context, passwordService);
+
+ await userService.CreateUser(new User
+ {
+ Username = "admin",
+ Password = "adminpw",
+ Administrator = true,
+ Nickname = "administrator"
+ });
+
+ await userService.CreateUser(new User
+ {
+ Username = "user",
+ Password = "userpw",
+ Administrator = false,
+ Nickname = "imuser"
+ });
+ }
+ }
+ }
+
+ public async Task DisposeAsync()
+ {
+ await Connection.CloseAsync();
+ await Connection.DisposeAsync();
+ }
+
+ public SqliteConnection Connection { get; }
+
+ public DatabaseContext CreateContext()
+ {
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(Connection).Options;
+
+ return new DatabaseContext(options);
+ }
+ }
+}
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
index b5aec512..7cf27297 100644
--- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
+++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.TestHost;
+using FluentAssertions;
+using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs
index b6a56e94..6736fecd 100644
--- a/Timeline.Tests/IntegratedTests/TimelineTest.cs
+++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs
@@ -78,10 +78,12 @@ namespace Timeline.Tests.IntegratedTests
return $"timelines/t{id}/{(subpath ?? "")}";
}
+ public delegate string TimelineUrlGenerator(int userId, string subpath = null);
+
public static IEnumerable<object[]> TimelineUrlGeneratorData()
{
- yield return new[] { new Func<int, string, string>(GeneratePersonalTimelineUrl) };
- yield return new[] { new Func<int, string, string>(GenerateOrdinaryTimelineUrl) };
+ yield return new[] { new TimelineUrlGenerator(GeneratePersonalTimelineUrl) };
+ yield return new[] { new TimelineUrlGenerator(GenerateOrdinaryTimelineUrl) };
}
private static string GeneratePersonalTimelineUrlByName(string name, string subpath = null)
@@ -545,7 +547,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task Description_Should_Work(Func<int, string, string> generator)
+ public async Task Description_Should_Work(TimelineUrlGenerator generator)
{
using var client = await CreateClientAsUser();
@@ -585,7 +587,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task Member_Should_Work(Func<int, string, string> generator)
+ public async Task Member_Should_Work(TimelineUrlGenerator generator)
{
var getUrl = generator(1, null);
using var client = await CreateClientAsUser();
@@ -682,7 +684,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task Visibility_Test(Func<int, string, string> generator)
+ public async Task Visibility_Test(TimelineUrlGenerator generator)
{
var userUrl = generator(1, "posts");
var adminUrl = generator(0, "posts");
@@ -765,7 +767,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task Permission_Post_Create(Func<int, string, string> generator)
+ public async Task Permission_Post_Create(TimelineUrlGenerator generator)
{
using (var client = await CreateClientAsUser())
{
@@ -817,7 +819,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task Permission_Post_Delete(Func<int, string, string> generator)
+ public async Task Permission_Post_Delete(TimelineUrlGenerator generator)
{
async Task<long> CreatePost(int userNumber)
{
@@ -885,7 +887,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task TextPost_ShouldWork(Func<int, string, string> generator)
+ public async Task TextPost_ShouldWork(TimelineUrlGenerator generator)
{
{
using var client = await CreateClientAsUser();
@@ -963,7 +965,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task GetPost_Should_Ordered(Func<int, string, string> generator)
+ public async Task GetPost_Should_Ordered(TimelineUrlGenerator generator)
{
using var client = await CreateClientAsUser();
@@ -991,7 +993,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task CreatePost_InvalidModel(Func<int, string, string> generator)
+ public async Task CreatePost_InvalidModel(TimelineUrlGenerator generator)
{
using var client = await CreateClientAsUser();
@@ -1035,7 +1037,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task ImagePost_ShouldWork(Func<int, string, string> generator)
+ public async Task ImagePost_ShouldWork(TimelineUrlGenerator generator)
{
var imageData = ImageHelper.CreatePngWithSize(100, 200);
@@ -1119,7 +1121,7 @@ namespace Timeline.Tests.IntegratedTests
[Theory]
[MemberData(nameof(TimelineUrlGeneratorData))]
- public async Task ImagePost_400(Func<int, string, string> generator)
+ public async Task ImagePost_400(TimelineUrlGenerator generator)
{
using var client = await CreateClientAsUser();
@@ -1145,5 +1147,51 @@ namespace Timeline.Tests.IntegratedTests
.And.HaveCommonBody(ErrorCodes.TimelineController.PostNoData);
}
}
+
+ [Theory]
+ [MemberData(nameof(TimelineUrlGeneratorData))]
+ public async Task LastModified(TimelineUrlGenerator generator)
+ {
+ using var client = await CreateClientAsUser();
+
+ DateTime lastModified;
+
+ {
+ var res = await client.GetAsync(generator(1));
+ lastModified = res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<TimelineInfo>()
+ .Which.LastModified;
+ }
+
+ await Task.Delay(1000);
+
+ {
+ var res = await client.PatchAsJsonAsync(generator(1), new TimelinePatchRequest { Description = "123" });
+ lastModified = res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<TimelineInfo>()
+ .Which.LastModified.Should().BeAfter(lastModified).And.Subject.Value;
+ }
+
+ {
+ var res = await client.GetAsync(generator(1));
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<TimelineInfo>()
+ .Which.LastModified.Should().Be(lastModified);
+ }
+
+ await Task.Delay(1000);
+
+ {
+ var res = await client.PutAsync(generator(1, "members/user2"), null);
+ res.Should().HaveStatusCode(200);
+ }
+
+ {
+ var res = await client.GetAsync(generator(1));
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<TimelineInfo>()
+ .Which.LastModified.Should().BeAfter(lastModified);
+ }
+ }
}
}
diff --git a/Timeline.Tests/Services/TimelineServiceTest.cs b/Timeline.Tests/Services/TimelineServiceTest.cs
new file mode 100644
index 00000000..cb2ade61
--- /dev/null
+++ b/Timeline.Tests/Services/TimelineServiceTest.cs
@@ -0,0 +1,94 @@
+using Castle.Core.Logging;
+using FluentAssertions;
+using FluentAssertions.Xml;
+using Microsoft.Extensions.Logging.Abstractions;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.Entities;
+using Timeline.Models;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.Services
+{
+ public class TimelineServiceTest : IAsyncLifetime, IDisposable
+ {
+ private TestDatabase _testDatabase = new TestDatabase();
+
+ private DatabaseContext _databaseContext;
+
+ 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;
+
+ private UserService _userService;
+
+ private TimelineService _timelineService;
+
+ public TimelineServiceTest()
+ {
+ }
+
+ public async Task InitializeAsync()
+ {
+ await _testDatabase.InitializeAsync();
+ _databaseContext = _testDatabase.CreateContext();
+ _dataManager = new DataManager(_databaseContext, _eTagGenerator);
+ _userService = new UserService(NullLogger<UserService>.Instance, _databaseContext, _passwordService);
+ _timelineService = new TimelineService(NullLogger<TimelineService>.Instance, _databaseContext, _dataManager, _userService, _imageValidator, _clock);
+ }
+
+ public async Task DisposeAsync()
+ {
+ await _testDatabase.DisposeAsync();
+ await _databaseContext.DisposeAsync();
+ }
+
+ public void Dispose()
+ {
+ _eTagGenerator.Dispose();
+ }
+
+ [Theory]
+ [InlineData("@user")]
+ [InlineData("tl")]
+ public async Task Timeline_LastModified(string timelineName)
+ {
+ _clock.ForwardCurrentTime();
+
+ void Check(Models.Timeline timeline)
+ {
+ timeline.NameLastModified.Should().Be(_clock.GetCurrentTime());
+ 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();
+ }
+ }
+}