aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs
blob: 4abcdb92db23484f82f2e8f6f7b8cd343711c0ac (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Timeline.Models;
using Timeline.Models.Http;
using Xunit;
using Xunit.Abstractions;

namespace Timeline.Tests.IntegratedTests
{
    [Obsolete("Old test.")]
    public class TimelineTest : BaseTimelineTest
    {
        public TimelineTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
        {

        }

        [Fact]
        public async Task TimelineGet_Should_Work()
        {
            using var client = await CreateDefaultClient();

            await client.TestGetAssertInvalidModelAsync("timelines/@!!!");
            await client.TestGetAssertInvalidModelAsync("timelines/!!!");

            {
                var body = await client.TestGetAsync<HttpTimeline>("timelines/@user1");
                body.Owner.Should().BeEquivalentTo(await client.GetUserAsync("user1"));
                body.Visibility.Should().Be(TimelineVisibility.Register);
                body.Description.Should().Be("");
                body.Members.Should().NotBeNull().And.BeEmpty();
                var links = body._links;
                links.Should().NotBeNull();
                links.Self.Should().NotBeNull();
                links.Posts.Should().NotBeNull();
            }

            {
                var body = await client.TestGetAsync<HttpTimeline>("timelines/t1");
                body.Owner.Should().BeEquivalentTo(await client.GetUserAsync("user1"));
                body.Visibility.Should().Be(TimelineVisibility.Register);
                body.Description.Should().Be("");
                body.Members.Should().NotBeNull().And.BeEmpty();
                var links = body._links;
                links.Should().NotBeNull();
                links.Self.Should().NotBeNull();
                links.Posts.Should().NotBeNull();
            }
        }

        [Fact]
        public async Task TimelineList_Should_Work()
        {
            using var client = await CreateDefaultClient();

            var result = new List<HttpTimeline>
            {
                await client.GetTimelineAsync("@user1")
            };

            for (int i = 0; i <= TestUserCount; i++)
            {
                result.Add(await client.GetTimelineAsync($"t{i}"));
            }


            var body = await client.TestGetAsync<List<HttpTimeline>>("timelines");
            body.Should().BeEquivalentTo(result);
        }

        [Fact]
        public async Task TimelineListWithQuery_Should_Work()
        {
            {
                using var client = await CreateDefaultClient();

                await client.TestGetAssertInvalidModelAsync("timelines?relate=us!!");
                await client.TestGetAssertInvalidModelAsync("timelines?relateType=aaa");
                await client.TestGetAssertInvalidModelAsync("timelines?visibility=aaa");
            }

            {
                using var client = await CreateClientAsUser();

                await client.PutTimelineMemberAsync("@user1", "user3");
                await client.PutTimelineMemberAsync("t1", "user3");
                await client.PatchTimelineAsync("@user1", new() { Visibility = TimelineVisibility.Public });
                await client.PatchTimelineAsync("t1", new() { Visibility = TimelineVisibility.Register });
            }

            {
                using var client = await CreateClientAs(2);

                await client.PutTimelineMemberAsync("@user2", "user3");
                await client.PutTimelineMemberAsync("t2", "user3");
                await client.PatchTimelineAsync("@user2", new() { Visibility = TimelineVisibility.Register });
                await client.PatchTimelineAsync("t2", new() { Visibility = TimelineVisibility.Private });
            }

            {
                using var client = await CreateClientAs(3);
                await client.PatchTimelineAsync("@user3", new HttpTimelinePatchRequest { Visibility = TimelineVisibility.Private });
                await client.PatchTimelineAsync("t3", new HttpTimelinePatchRequest { Visibility = TimelineVisibility.Register });
            }

            {
                var testResultRelate = new List<HttpTimeline>();
                var testResultOwn = new List<HttpTimeline>();
                var testResultJoin = new List<HttpTimeline>();
                var testResultOwnPrivate = new List<HttpTimeline>();
                var testResultRelatePublic = new List<HttpTimeline>();
                var testResultRelateRegister = new List<HttpTimeline>();
                var testResultJoinPrivate = new List<HttpTimeline>();
                var testResultPublic = new List<HttpTimeline>();

                using var client = await CreateDefaultClient();

                {
                    var timeline = await client.GetTimelineAsync("@user1");
                    testResultRelate.Add(timeline);
                    testResultJoin.Add(timeline);
                    testResultRelatePublic.Add(timeline);
                    testResultPublic.Add(timeline);
                }

                {
                    var timeline = await client.GetTimelineAsync("t1");
                    testResultRelate.Add(timeline);
                    testResultJoin.Add(timeline);
                    testResultRelateRegister.Add(timeline);
                }

                {
                    var timeline = await client.GetTimelineAsync("@user2");
                    testResultRelate.Add(timeline);
                    testResultJoin.Add(timeline);
                    testResultRelateRegister.Add(timeline);
                }

                {
                    var timeline = await client.GetTimelineAsync("t2");
                    testResultRelate.Add(timeline);
                    testResultJoin.Add(timeline);
                    testResultJoinPrivate.Add(timeline);
                }

                {
                    var timeline = await client.GetTimelineAsync("@user3");
                    testResultRelate.Add(timeline);
                    testResultOwn.Add(timeline);
                    testResultOwnPrivate.Add(timeline);
                }

                {
                    var timeline = await client.GetTimelineAsync("t3");
                    testResultRelate.Add(timeline);
                    testResultOwn.Add(timeline);
                    testResultRelateRegister.Add(timeline);
                }

                async Task TestAgainst(string url, List<HttpTimeline> against)
                {
                    var body = await client.TestGetAsync<List<HttpTimeline>>(url);
                    body.Should().BeEquivalentTo(against);
                }

                await TestAgainst("timelines?relate=user3", testResultRelate);
                await TestAgainst("timelines?relate=user3&relateType=own", testResultOwn);
                await TestAgainst("timelines?relate=user3&visibility=public", testResultRelatePublic);
                await TestAgainst("timelines?relate=user3&visibility=register", testResultRelateRegister);
                await TestAgainst("timelines?relate=user3&relateType=join&visibility=private", testResultJoinPrivate);
                await TestAgainst("timelines?relate=user3&relateType=own&visibility=private", testResultOwnPrivate);
                await TestAgainst("timelines?visibility=public", testResultPublic);
            }
        }

        [Fact]
        public async Task TimelineCreate_Should_Work()
        {
            {
                using var client = await CreateDefaultClient();
                await client.TestPostAssertUnauthorizedAsync("timelines", new HttpTimelineCreateRequest { Name = "aaa" });
            }

            {
                using var client = await CreateClientAsUser();

                await client.TestPostAssertInvalidModelAsync("timelines", new HttpTimelineCreateRequest { Name = "!!!" });

                {
                    var body = await client.TestPostAsync<HttpTimeline>("timelines", new HttpTimelineCreateRequest { Name = "aaa" });
                    body.Should().BeEquivalentTo(await client.GetTimelineAsync("aaa"));
                }

                await client.TestPostAssertErrorAsync("timelines", new HttpTimelineCreateRequest { Name = "aaa" }, errorCode: ErrorCodes.Conflict.Timeline);
            }
        }

        [Fact]
        public async Task TimelineDelete_Should_Work()
        {
            {
                using var client = await CreateDefaultClient();
                await client.TestDeleteAssertUnauthorizedAsync("timelines/t1");
            }

            {
                using var client = await CreateClientAs(2);
                await client.TestDeleteAssertForbiddenAsync("timelines/t1");
            }

            {
                using var client = await CreateClientAsAdministrator();

                await client.TestDeleteAssertInvalidModelAsync("timelines/!!!");
                await client.TestDeleteAsync("timelines/t2", true);
                await client.TestDeleteAsync("timelines/t2", false);
            }

            {
                using var client = await CreateClientAs(1);

                await client.TestDeleteAssertInvalidModelAsync("timelines/!!!");
                await client.TestDeleteAsync("timelines/t1", true);
                await client.TestDeleteAssertForbiddenAsync("timelines/t1");
            }
        }


        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task TimelineDescription_Should_Work(TimelineNameGenerator generator)
        {
            // TODO! Permission tests.

            using var client = await CreateClientAsUser();
            var timelineName = generator(1);

            {
                var timeline = await client.GetTimelineAsync(timelineName);
                timeline.Description.Should().BeEmpty();
            }

            const string mockDescription = "haha";

            {
                var timeline = await client.PatchTimelineAsync(timelineName, new() { Description = mockDescription });
                timeline.Description.Should().Be(mockDescription);
            }

            {
                var timeline = await client.GetTimelineAsync(timelineName);
                timeline.Description.Should().Be(mockDescription);
            }

            {
                var timeline = await client.PatchTimelineAsync(timelineName, new() { Description = null });
                timeline.Description.Should().Be(mockDescription);
            }

            {
                var timeline = await client.GetTimelineAsync(timelineName);
                timeline.Description.Should().Be(mockDescription);
            }

            {
                var timeline = await client.PatchTimelineAsync(timelineName, new() { Description = "" });
                timeline.Description.Should().BeEmpty();
            }

            {
                var timeline = await client.GetTimelineAsync(timelineName);
                timeline.Description.Should().BeEmpty();
            }
        }

        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task Member_Should_Work(TimelineNameGenerator generator)
        {
            // TODO! Invalid model tests.
            // TODO! Permission tests.

            using var client = await CreateClientAsUser();

            var timelineName = generator(1);

            async Task AssertMembers(List<HttpUser> members)
            {
                var body = await client.GetTimelineAsync(timelineName);
                body.Members.Should().NotBeNull().And.BeEquivalentTo(members);
            }

            async Task AssertEmptyMembers()
            {
                var body = await client.GetTimelineAsync(timelineName);
                body.Members.Should().NotBeNull().And.BeEmpty();
            }

            await AssertEmptyMembers();
            await client.TestPutAssertErrorAsync($"timelines/{timelineName}/members/usernotexist", errorCode: ErrorCodes.NotExist.User);
            await AssertEmptyMembers();
            await client.PutTimelineMemberAsync(timelineName, "user2");
            await AssertMembers(new List<HttpUser> { await client.GetUserAsync("user2") });
            await client.DeleteTimelineMemberAsync(timelineName, "user2");
            await AssertEmptyMembers();
            await client.TestDeleteAssertErrorAsync($"timelines/{timelineName}/members/usernotexist");
            await AssertEmptyMembers();
        }

        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task Timeline_LastModified(TimelineNameGenerator generator)
        {
            using var client = await CreateClientAsUser();

            DateTime lastModified;

            {
                var body = await client.GetTimelineAsync(generator(1));
                lastModified = body.LastModified;
            }

            await Task.Delay(1000);

            {
                var body = await client.PatchTimelineAsync(generator(1), new() { Description = "123" });
                lastModified = body.LastModified.Should().BeAfter(lastModified).And.Subject!.Value;
            }

            {
                var body = await client.GetTimelineAsync(generator(1));
                body.LastModified.Should().Be(lastModified);
            }

            await Task.Delay(1000);

            await client.PutTimelineMemberAsync(generator(1), "user2");

            {
                var body = await client.GetTimelineAsync(generator(1));
                body.LastModified.Should().BeAfter(lastModified);
            }
        }

        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task Title(TimelineNameGenerator generator)
        {
            using var client = await CreateClientAsUser();

            {
                var body = await client.GetTimelineAsync(generator(1));
                body.Title.Should().Be(body.Name);
            }

            {
                var body = await client.PatchTimelineAsync(generator(1), new HttpTimelinePatchRequest { Title = "atitle" });
                body.Title.Should().Be("atitle");
            }

            {
                var body = await client.GetTimelineAsync(generator(1));
                body.Title.Should().Be("atitle");
            }
        }

        [Fact]
        public async Task ChangeName()
        {
            {
                using var client = await CreateDefaultClient();
                await client.TestPatchAssertUnauthorizedAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "tttttttt" });
            }

            {
                using var client = await CreateClientAs(2);
                await client.TestPatchAssertForbiddenAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "tttttttt" });
            }

            using (var client = await CreateClientAsUser())
            {
                await client.TestPatchAssertInvalidModelAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "!!!" });

                await client.TestPatchAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "newt" });

                await client.TestGetAsync("timelines/t1", expectedStatusCode: HttpStatusCode.NotFound);

                {
                    var body = await client.TestGetAsync<HttpTimeline>("timelines/newt");
                    body.Name.Should().Be("newt");
                }
            }
        }

        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task Color(TimelineNameGenerator generator)
        {
            using var client = await CreateClientAsUser();

            {
                var timeline = await client.TestGetAsync<HttpTimeline>($"timelines/{generator(1)}");
                timeline.Color.Should().Be(null);
            }

            await client.TestPatchAssertInvalidModelAsync($"timelines/{generator(1)}", new HttpTimelinePatchRequest { Color = "!!!" });

            {
                var timeline = await client.TestPatchAsync<HttpTimeline>($"timelines/{generator(1)}", new HttpTimelinePatchRequest { Color = "#112233" });
                timeline.Color.Should().Be("#112233");
            }

            {
                var timeline = await client.TestGetAsync<HttpTimeline>($"timelines/{generator(1)}");
                timeline.Color.Should().Be("#112233");
            }
        }

        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task Get_Manageable_Postable(TimelineNameGenerator generator)
        {
            {
                using var client = await CreateClientAsUser();
                var timeline = await client.TestGetAsync<HttpTimeline>($"timelines/{generator(1)}");
                timeline.Manageable.Should().Be(true);
                timeline.Postable.Should().Be(true);
            }

            {
                using var client = await CreateClientAs(2);
                var timeline = await client.TestGetAsync<HttpTimeline>($"timelines/{generator(1)}");
                timeline.Manageable.Should().Be(false);
                timeline.Postable.Should().Be(false);
            }
        }

        [Theory]
        [InlineData("")]
        [InlineData("default")]
        public async Task Patch_Timeline_Color_Default(string value)
        {
            using var client = await CreateClientAsUser();
            var timeline = await client.TestPatchAsync<HttpTimeline>("timelines/t1", new HttpTimelinePatchRequest { Color = "#111111" });
            timeline.Color.Should().NotBeNull();
            var timeline2 = await client.TestPatchAsync<HttpTimeline>("timelines/t1", new HttpTimelinePatchRequest { Color = value });
            timeline2.Color.Should().BeNull();
        }
    }
}