public async void Delete_User()
        {
            var options = new DbContextOptionsBuilder <DashboardDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_User")
                          .Options;

            var user1 = new User()
            {
                UserId   = 1,
                Email    = "*****@*****.**",
                Name     = "John",
                Surname  = "Smith",
                Password = "******",
                IsActive = true
            };

            var user2 = new User()
            {
                UserId   = 2,
                Email    = "*****@*****.**",
                Name     = "John",
                Surname  = "Smith",
                Password = "******",
                IsActive = true
            };

            using (var context = new DashboardDbContext(options))
            {
                var mockCheckEmailService = new CheckEmailService(new DashboardDbContext(options), new Mock <ILogger <CheckEmailService> >().Object);

                var service = new UserCrudService(context, _mockLogger.Object, mockCheckEmailService, _mockSendGridService.Object);
                await service.CreateUserAsync(user1);

                await service.CreateUserAsync(user2);

                await context.SaveChangesAsync();

                await service.DeleteUserAsync(user1.UserId);

                await context.SaveChangesAsync();
            }

            using (var context = new DashboardDbContext(options))
            {
                Assert.Equal(1, context.Users.CountAsync().Result);
                Assert.Equal(user2.UserId, context.Users.SingleAsync().Result.UserId);
            }
        }
        /// <summary>
        /// Creates new portal in Db
        /// </summary>
        /// <param name="portal">Portal to create</param>
        /// <returns>Portal Created (or null if error)</returns>
        public async Task <Portal> CreateNewPortalAsync(Portal portal)
        {
            try
            {
                await _context.Portals.AddAsync(portal);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(null);
            }

            _logger.LogInformation("Portal (Id: {0}) created.", portal.Id);
            return(portal);
        }
Beispiel #3
0
 /// <summary>
 /// Writes Last response data to portal entity
 /// </summary>
 /// <param name="portal">Portal</param>
 /// <param name="response">Last response</param>
 /// <returns></returns>
 private async Task WriteLastResponseData(Portal portal, PortalResponse response)
 {
     portal.LastRequestDateTime     = response.RequestDateTime;
     portal.LastRequestErrorMessage = response.ErrorMessage;
     portal.LastRequestResponseTime = response.ResponseTime;
     portal.LastRequestStatus       = response.Status;
     await _context.SaveChangesAsync();
 }
        public async void Update_User_Changes_Password_Not_Needed()
        {
            var options = new DbContextOptionsBuilder <DashboardDbContext>()
                          .UseInMemoryDatabase(databaseName: "Update_User_Changes_Password_Not_Needed")
                          .Options;

            var user = new User()
            {
                UserId   = 1,
                Email    = "*****@*****.**",
                Name     = "John",
                Surname  = "Smith",
                Password = "******",
                IsActive = true
            };

            var userUpdated = new User()
            {
                UserId   = 1,
                Email    = "*****@*****.**",
                Name     = "Leon",
                Surname  = "Smith",
                IsActive = true
            };

            var userHashedPassword        = "";
            var userUpdatedHashedPassword = "";

            using (var context = new DashboardDbContext(options))
            {
                var mockCheckEmailService = new CheckEmailService(new DashboardDbContext(options), new Mock <ILogger <CheckEmailService> >().Object);

                var service = new UserCrudService(context, _mockLogger.Object, mockCheckEmailService, _mockSendGridService.Object);
                await service.CreateUserAsync(user);

                userHashedPassword = context.Users.SingleOrDefaultAsync().Result.Password;
                await context.SaveChangesAsync();
            }

            using (var context = new DashboardDbContext(options))
            {
                var mockCheckEmailService = new CheckEmailService(new DashboardDbContext(options), new Mock <ILogger <CheckEmailService> >().Object);

                var service = new UserCrudService(context, _mockLogger.Object, mockCheckEmailService, _mockSendGridService.Object);
                await service.UpdateUserAsync(userUpdated);

                userUpdatedHashedPassword = context.Users.SingleOrDefaultAsync().Result.Password;
                await context.SaveChangesAsync();
            }

            using (var context = new DashboardDbContext(options))
            {
                Assert.Equal(1, context.Users.CountAsync().Result);
                Assert.Equal(userHashedPassword, userUpdatedHashedPassword);
            }
        }
        /// <summary>
        /// Deletes refresh token from DB
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="refreshToken">Refresh token to delete</param>
        /// <returns>Deleting was succesful</returns>
        private async Task <bool> DeleteRefreshToken(int userId, string refreshToken)
        {
            var tokenInDb = await _context.RefreshTokens
                            .FirstOrDefaultAsync(t => t.UserId == userId && string.Equals(refreshToken, t.Token));

            if (tokenInDb == null)
            {
                return(false);
            }

            try
            {
                _context.Remove(tokenInDb);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(false);
            }
        }
Beispiel #6
0
        public async void Authorize_Valid_User()
        {
            var options = new DbContextOptionsBuilder <DashboardDbContext>()
                          .UseInMemoryDatabase(databaseName: "Authorize_Valid_User")
                          .Options;

            var user = new User()
            {
                Email    = "*****@*****.**",
                Password = "******",
                IsActive = true
            };

            using (var context = new DashboardDbContext(options))
            {
                var serviceCheckEmail = new CheckEmailService(context, _mockLoggerCheckEmail.Object);
                var serviceCrud       = new UserCrudService(context, _mockLoggerUserCrud.Object, serviceCheckEmail, _mockSendGridService.Object);

                var userCreate = new User()
                {
                    Email    = user.Email,
                    Password = user.Password,
                    IsActive = user.IsActive
                };

                await serviceCrud.CreateUserAsync(userCreate);

                await context.SaveChangesAsync();
            }

            using (var context = new DashboardDbContext(options))
            {
                var serviceAuth = new UserAuthService(context, _mockLogger.Object);
                var result      = serviceAuth.AuthUser(user);

                Assert.Equal(1, await context.Users.CountAsync());
                Assert.NotNull(result);
                Assert.Equal(user.Email, result.Email);
            }
        }
        public async void Toggle_Active()
        {
            var options = new DbContextOptionsBuilder <DashboardDbContext>()
                          .UseInMemoryDatabase(databaseName: "Toggle_Active")
                          .Options;

            var user = new User()
            {
                UserId   = 1,
                Email    = "*****@*****.**",
                Name     = "John",
                Surname  = "Smith",
                Password = "******",
                IsActive = true
            };

            using (var context = new DashboardDbContext(options))
            {
                var mockCheckEmailService = new CheckEmailService(new DashboardDbContext(options), new Mock <ILogger <CheckEmailService> >().Object);

                var service = new UserCrudService(context, _mockLogger.Object, mockCheckEmailService, _mockSendGridService.Object);
                await service.CreateUserAsync(user);

                await context.SaveChangesAsync();
            }

            using (var context = new DashboardDbContext(options))
            {
                var mockCheckEmailService = new CheckEmailService(new DashboardDbContext(options), new Mock <ILogger <CheckEmailService> >().Object);

                var service = new UserCrudService(context, _mockLogger.Object, mockCheckEmailService, _mockSendGridService.Object);
                await service.ToggleActiveAsync(user.UserId);

                Assert.Equal(!user.IsActive, context.Users.SingleOrDefaultAsync().Result.IsActive);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Create User
        /// </summary>
        /// <param name="user">User to create</param>
        /// <returns>Boolean</returns>
        public async Task <bool> CreateUserAsync(User user)
        {
            user.isPermanent = false;

            user.Password = new HashService()
                            .Hash(user.Password);

            if (!_checkEmailService.IsAlreadyTaken(user.Email))
            {
                try
                {
                    _context.Users.Add(user);
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.ToString());
                    return(false);
                }
                return(true);
            }
            return(false);
        }
Beispiel #9
0
 public async Task SaveChangesAsync()
 {
     await _dbContext.SaveChangesAsync();
 }