< Summary

Information
Class: WebApi.Services.UserService
Assembly: AuthWebApi
File(s): /home/runner/work/SEP4/SEP4/backend/microservices/autorisering/WebApi/Services/UserService.cs
Line coverage
73%
Covered lines: 54
Uncovered lines: 19
Coverable lines: 73
Total lines: 120
Line coverage: 73.9%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ValidateUserAsync()50%2.18264.28%
GetUserAsync()100%210%
RegisterUserAsync()62.5%8.75877.27%
GetAllUsersAsync()100%6.05688.88%
UpdateUserAsync()100%210%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/autorisering/WebApi/Services/UserService.cs

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2using WebApi.DAO;
 3using WebApi.Models;
 4
 5namespace WebApi.Services
 6{
 7   public class UserService : IUserService
 8{
 9
 10    private readonly IUserDAO _userDAO;
 11
 512    public UserService(IUserDAO userDAO)
 513    {
 514        _userDAO = userDAO;
 515    }
 16
 17    public async Task<User> ValidateUserAsync(string username, string password)
 218    {
 19        try
 220        {
 221            User existingUser = await _userDAO.ValidateUserAsync(username, password);
 122            if (existingUser == null)
 023            {
 024                throw new UnauthorizedAccessException("Username or password is incorrect.");
 25            }
 126            return existingUser;
 27        }
 128        catch (UnauthorizedAccessException)
 129        {
 30            // Re-throw specific exceptions directly without wrapping
 131            throw;
 32        }
 033        catch (Exception ex)
 034        {
 35            // For unexpected errors, you might still want to wrap to add context or log them appropriately
 036            throw new Exception("An error occurred during user validation.", ex);
 37        }
 138    }
 39
 40    public async Task<User> GetUserAsync(string username)
 041    {
 042        return await _userDAO.GetUserAsync(username);
 043    }
 44
 45    public async Task RegisterUserAsync(UserCreationDTO userCreationDTO)
 246    {
 247        if (string.IsNullOrEmpty(userCreationDTO.Username) || string.IsNullOrEmpty(userCreationDTO.Password) || string.I
 048        {
 049            throw new ValidationException("Something went wrong."); // Vague response to make it harder for hackers
 50        }
 51
 252        if (userCreationDTO.Password.Length < 8)
 153        {
 154            throw new ValidationException("Password must be at least 8 characters long.");
 55        }
 56
 57        // Check for the uniqueness of the username and register the user
 58        try
 159        {
 160            var user = new User
 161            {
 162                Username = userCreationDTO.Username,
 163                Password = userCreationDTO.Password,
 164                Email = userCreationDTO.Email,
 165                Role = "User",
 166                Age = userCreationDTO.Age
 167            };
 68
 169            await _userDAO.RegisterUserAsync(user);
 170        }
 071        catch (Exception ex)
 072        {
 073            throw new Exception("Something went wrong.");
 74        }
 75
 176    }
 77
 78    public async Task<List<List<User>>> GetAllUsersAsync()
 179    {
 80        try
 181        {
 182            List<User> adminList = new List<User>();
 183            List<User> superUserList = new List<User>();
 184            List<User> userList = new List<User>();
 185            List<List<User>> allUsersList = new List<List<User>>();
 86
 987            foreach (User user in await _userDAO.GetAllUsersAsync())
 388            {
 389                if (user.Role == "Admin")
 190                {
 191                    adminList.Add(user);
 392                } else if (user.Role == "SuperUser")
 193                {
 194                    superUserList.Add(user);
 195                }
 96                else
 197                {
 198                    userList.Add(user);
 199                }
 3100            }
 101
 1102            allUsersList.Add(adminList);
 1103            allUsersList.Add(superUserList);
 1104            allUsersList.Add(userList);
 105
 1106            return allUsersList;
 107        }
 0108        catch (Exception ex)
 0109        {
 0110            throw new Exception($"Failed to retrieve all users sorted by role: {ex.Message}", ex);
 111        }
 1112    }
 113
 114    public async Task<User> UpdateUserAsync(User user)
 0115    {
 0116        return await _userDAO.UpdateUserAsync(user);
 0117    }
 118
 119}
 120}