aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Mock/Data/TestUsers.cs
blob: bc2df469eb81e7e3341ff666f7daa5814c373a14 (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
using System;
using System.Collections.Generic;
using Timeline.Entities;
using Timeline.Models;
using Timeline.Services;

namespace Timeline.Tests.Mock.Data
{
    public class MockUser
    {
        public MockUser(string username, string password, bool administrator)
        {
            Info = new UserInfo(username, administrator);
            Password = password;
        }

        public UserInfo Info { get; set; }
        public string Username => Info.Username;
        public string Password { get; set; }
        public bool Administrator => Info.Administrator;


        public static MockUser User { get; } = new MockUser("user", "userpassword", false);
        public static MockUser Admin { get; } = new MockUser("admin", "adminpassword", true);

        public static IReadOnlyList<UserInfo> UserInfoList { get; } = new List<UserInfo> { User.Info, Admin.Info };

        // emmmmmmm. Never reuse the user instances because EF Core uses them, which will cause strange things.
        public static IEnumerable<User> CreateMockEntities()
        {
            var passwordService = new PasswordService();
            User Create(MockUser user)
            {
                return new User
                {
                    Name = user.Username,
                    EncryptedPassword = passwordService.HashPassword(user.Password),
                    RoleString = UserUtility.IsAdminToRoleString(user.Administrator),
                    Avatar = UserAvatar.Create(DateTime.Now)
                };
            }

            return new List<User>
            {
                Create(User),
                Create(Admin)
            };
        }
    }
}