aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/ServiceTests/User/RegisterCode/RegisterCodeServiceTest.cs
blob: a0e3d20ede1c3058622162f03eef0bc5aa4fbc69 (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
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Timeline.Services.User.RegisterCode;
using Xunit;

namespace Timeline.Tests.ServiceTests.User.RegisterCode
{
    public class RegisterCodeServiceTest : ServiceTestBase
    {
        private RegisterCodeService _registerCodeService = default!;

        protected override void OnInitialize()
        {
            _registerCodeService = new RegisterCodeService(Database, UserService);
        }

        protected override void OnDispose()
        {
            _registerCodeService.Dispose();
        }

        [Fact]
        public async Task RegisterCodeTest()
        {
            var a = await _registerCodeService.GetCurrentCodeAsync(AdminId);
            a.Should().BeNull();
            var b = await _registerCodeService.CreateNewCodeAsync(AdminId);
            b.Should().NotBeNullOrEmpty();
            var c = await _registerCodeService.GetCurrentCodeAsync(AdminId);
            c.Should().Be(b);
            var d = await _registerCodeService.CreateNewCodeAsync(AdminId);
            d.Should().NotBe(b);
            var e = await _registerCodeService.GetCodeOwnerAsync(d);
            e.Should().Be(AdminId);
            var f = await _registerCodeService.GetCodeOwnerAsync(b);
            f.Should().BeNull();
            var g = await _registerCodeService.GetCodeOwnerAsync(b, false);
            g.Should().Be(AdminId);
        }

        [Fact]
        public async Task RegisterInfoTest()
        {
            var registerCode = await _registerCodeService.CreateNewCodeAsync(AdminId);

            var a = await _registerCodeService.GetUserRegisterInfoAsync(UserId);
            a.Should().BeNull();

            var dateTime = DateTime.UtcNow;

            var b = await _registerCodeService.CreateRegisterInfoAsync(UserId, registerCode, dateTime);
            b.UserId.Should().Be(UserId);
            b.RegisterCode.Should().Be(registerCode);
            b.IntroducerId.Should().Be(AdminId);
            b.RegisterTime.Should().Be(dateTime);

            var c = await _registerCodeService.GetUserRegisterInfoAsync(UserId);
            c.Should().NotBeNull();
            c!.UserId.Should().Be(UserId);
            c.RegisterCode.Should().Be(registerCode);
            c.IntroducerId.Should().Be(AdminId);
            c.RegisterTime.Should().Be(dateTime);
        }

        [Fact]
        public async Task RegisterInfoTestWithOldCode()
        {
            await _registerCodeService.CreateNewCodeAsync(AdminId);

            // Make the old code disabled.
            var registerCode = await _registerCodeService.CreateNewCodeAsync(AdminId);

            var a = await _registerCodeService.GetUserRegisterInfoAsync(UserId);
            a.Should().BeNull();

            var dateTime = DateTime.UtcNow;

            var b = await _registerCodeService.CreateRegisterInfoAsync(UserId, registerCode, dateTime);
            b.UserId.Should().Be(UserId);
            b.RegisterCode.Should().Be(registerCode);
            b.IntroducerId.Should().Be(AdminId);
            b.RegisterTime.Should().Be(dateTime);

            var c = await _registerCodeService.GetUserRegisterInfoAsync(UserId);
            c.Should().NotBeNull();
            c!.UserId.Should().Be(UserId);
            c.RegisterCode.Should().Be(registerCode);
            c.IntroducerId.Should().Be(AdminId);
            c.RegisterTime.Should().Be(dateTime);
        }
    }
}