Пример #1
0
        public void ShouldFetchAllComplaintsFromDb(string title, string description)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var complaintService       = new ComplaintService(context);
            var complaintEntityEntries = new List <EntityEntry <ComplaintEntity> >();
            var complaintIds           = new List <int>();

            for (var i = 0; i < 5; i++)
            {
                complaintEntityEntries.Add(
                    context.Complaints.Add(new ComplaintEntity
                {
                    Title       = title,
                    Description = description
                }));
            }

            context.SaveChanges();

            // Act
            var complaintsInDb = complaintService.GetAll().ToList();

            // Assert
            Assert.Equal(complaintsInDb.Count, complaintEntityEntries.Count);
            foreach (var complaint in complaintEntityEntries)
            {
                Assert.NotNull(complaintsInDb.Find(x => x.Id == complaint.Entity.Id));
            }
        }
Пример #2
0
        public void ShouldThrowAnExceptionIfUsernameIsTaken(string firstName, string lastName, string username, string password)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var authenticationOptions = Options.Create(new Authentication
            {
                Secret = "mockSecret"
            });
            var _userService = new UserService(context, authenticationOptions);
            var userEntity1  = new UserEntity
            {
                FirstName = firstName,
                LastName  = lastName,
                Username  = username
            };

            var userEntity2 = new UserEntity
            {
                FirstName = "testname",
                LastName  = "testsurname",
                Username  = username
            };

            var user1 = _userService.Create(userEntity1, password);

            // Act + Assert
            Assert.Throws <AuthenticationException>(() => _userService.Create(userEntity2, password));
        }
Пример #3
0
        public void ShouldCreateNewUserInInMemoryDatabase(string firstName, string lastName, string username, string password)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            {
                var authenticationOptions = Options.Create(new Authentication
                {
                    Secret = "mockSecret"
                });
                var _userService = new UserService(context, authenticationOptions);
                var userEntity   = new UserEntity
                {
                    FirstName = firstName,
                    LastName  = lastName,
                    Username  = username
                };

                // Act
                var createdUser = _userService.Create(userEntity, password);

                // Assert
                var userInDb = _userService.GetUserById(createdUser.Id);
                Assert.Equal(createdUser, userInDb);
            }
        }
Пример #4
0
        public void ShouldThrowAnExceptionGivenIdIfItemNotFoundInDatabase(int complaintId)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var complaintService = new ComplaintService(context);

            // Act + Assert
            Assert.Throws <ComplaintException>(() => complaintService.GetComplaintById(complaintId));
        }
Пример #5
0
        public void ShouldAddComplaintToDatabase(string title, string description)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var complaintService = new ComplaintService(context);
            var complaintEntity  = new ComplaintEntity
            {
                Title       = title,
                Description = description
            };

            // Act
            var createdComplaint = complaintService.AddComplaint(complaintEntity);

            // Assert
            var complaintInDb = context.Complaints.Find(createdComplaint.Id);

            Assert.NotNull(complaintInDb);
            Assert.Equal(createdComplaint, complaintInDb);
        }
Пример #6
0
        public void ShouldFetchCorrectComplaintFromDb(string title, string description)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var complaintService = new ComplaintService(context);
            var complaintEntity  = new ComplaintEntity
            {
                Title       = title,
                Description = description
            };

            var createdComplaint = context.Complaints.Add(complaintEntity);

            context.SaveChanges();

            // Act
            var complaintInDb = complaintService.GetComplaintById(createdComplaint.Entity.Id);

            // Assert
            Assert.Equal(complaintInDb, createdComplaint.Entity);
        }
Пример #7
0
        public void ShouldThrowAnAuthenticationExceptionGivenUsernameIsIncorrect(string username, string password)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var authenticationOptions = Options.Create(new Authentication
            {
                Secret = "mockSecret"
            });
            var _userService = new UserService(context, authenticationOptions);
            var userEntity   = new UserEntity
            {
                FirstName = "name",
                LastName  = "surname",
                Username  = "******"
            };

            var registeredUser = _userService.Create(userEntity, password);

            // Act + Assert
            Assert.Throws <AuthenticationException>(() => _userService.Authenticate(username, password));
        }
Пример #8
0
        public void ShouldAuthenticateUserGivenCredentialsAreCorrect(string firstName, string lastName, string username, string password)
        {
            // Arrange
            using var context = new ComplaintsContext(DbFixtureProvider.CreateNewContextOptions());
            var authenticationOptions = Options.Create(new Authentication
            {
                Secret = "mockSecret"
            });
            var _userService = new UserService(context, authenticationOptions);
            var userEntity   = new UserEntity
            {
                FirstName = firstName,
                LastName  = lastName,
                Username  = username
            };

            var registeredUser = _userService.Create(userEntity, password);

            // Act
            var loggedInUser = _userService.Authenticate(registeredUser.Username, password);

            // Assert
            Assert.Equal(registeredUser, loggedInUser);
        }