Ejemplo n.º 1
0
        public async Task AddUserToExamAsync_InvalidPasswordPassed_ReturnsUnauthorizedResult()
        {
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "1"),
            }));
            string password       = "******";
            var    mapperMock     = new Mock <IMapper>();
            var    repositoryMock = new Mock <IRepositoryWrapper>();


            repositoryMock.Setup(r => r.ExamRepository.GetExamAsync(It.IsAny <int>())).ReturnsAsync(new Exam()
            {
                PasswordHash = new byte[5],
                PasswordSalt = new byte[5]
            });
            mapperMock.Setup(m => m.Map <IEnumerable <ExamForListDto> >(It.IsAny <IEnumerable <Exam> >())).Returns(new List <ExamForListDto>());



            var controllerMock = new ExamsController(repositoryMock.Object, mapperMock.Object);

            controllerMock.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = user
                }
            };
            var result = await controllerMock.AddUserToExamAsync(1, 1, password);

            Assert.IsType <UnauthorizedResult>(result);
        }
Ejemplo n.º 2
0
        public async Task AddUserToExamAsync_ValidParametersPassed_ReturnsCreatedAtRouteResult()
        {
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "1"),
            }));

            byte[] passwordHash, passwordSalt;
            string password       = "******";
            var    mapperMock     = new Mock <IMapper>();
            var    repositoryMock = new Mock <IRepositoryWrapper>();

            password.CreatePasswordHash(out passwordHash, out passwordSalt);

            repositoryMock.Setup(r => r.ExamRepository.GetExamAsync(It.IsAny <int>())).ReturnsAsync(new Exam()
            {
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt
            });
            repositoryMock.Setup(r => r.UserExamRepository.Add(It.IsAny <UserExam>()));
            repositoryMock.Setup(r => r.SaveAllAsync()).ReturnsAsync(true);



            var controllerMock = new ExamsController(repositoryMock.Object, mapperMock.Object);

            controllerMock.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = user
                }
            };
            var result = await controllerMock.AddUserToExamAsync(1, 1, password);

            Assert.IsType <CreatedAtRouteResult>(result);
        }