aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Services/TimelineServiceTest.cs
blob: 123c2b7fc8cc2b1f7a8590491150ca730f9be837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using FluentAssertions;
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 readonly 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)
        {
            var initTime = _clock.ForwardCurrentTime();

            void Check(Models.Timeline 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("@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 _timelineService.CreateTextPost(timelineName, userId, content, null);
            }

            var posts = await _timelineService.GetPosts(timelineName, testPoint);
            posts.Should().HaveCount(3)
                .And.Subject.Select(p => (p.Content as TextTimelinePostContent).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 _timelineService.CreateTextPost(timelineName, userId, content, null);
            }

            var posts = await _timelineService.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 _timelineService.DeletePost(timelineName, id);
            }

            posts = await _timelineService.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 _timelineService.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" });
        }
    }
}