예제 #1
0
        public async Task CreateNewTestimonialCode_OneCode_Success()
        {
            // Definitions
            const string firstName          = "first;";
            const string lastName           = "last";
            const string description        = "description";
            const string adminPassword      = "******";
            const string expectedCode       = "123456";
            DateTime     expirationDateTime = DateTime.Now.AddDays(28);
            const int    expectedCodeLength = 6;

            // Arrange
            var createTestimonialCodeDto = new CreateTestimonialCodeDto(firstName, lastName, description, adminPassword);
            var serviceUnderTest         = CreateServiceUnderTest();

            _mocker.GetMock <ICryptoService>().Setup(ts => ts.GenerateCode(It.Is <int>((i) => i == expectedCodeLength))).Returns(expectedCode);
            _mocker.GetMock <IPasswordService>().Setup(ps => ps.CheckPassword(It.Is <string>((password) => password == adminPassword), It.IsAny <string>())).Returns(true);

            // Act
            var result = await serviceUnderTest.CreateNewTestimonialCode(createTestimonialCodeDto);

            // Assert
            result.ShouldNotBeNullOrEmpty();
            result.ShouldBe(expectedCode);
            result.Length.ShouldBe(6);

            _databaseContext.TestimonialCodes.Count().ShouldBe(1);
            var databaseCode = _databaseContext.TestimonialCodes.FirstOrDefault();

            databaseCode.ShouldNotBeNull();
            databaseCode.FirstName.ShouldBe(firstName);
            databaseCode.LastName.ShouldBe(lastName);
            databaseCode.Description.ShouldBe(description);
            databaseCode.Code.ShouldBe(expectedCode);
        }
        public async Task <string> CreateNewTestimonialCode(CreateTestimonialCodeDto createTestimonialCodeDto)
        {
            if (!_passwordService.CheckPassword(createTestimonialCodeDto.AdminPassword, _adminPasswordHash))
            {
                throw new IncorrectAdminPasswordException();
            }

            string code;

            // Ensure no clashes no matter how unlikely
            do
            {
                code = _cryptoService.GenerateCode(6);
            } while (_databaseContext.TestimonialCodes.Any(c => c.Code == code));

            await _databaseContext.TestimonialCodes.AddAsync(new TestimonialCodeModel(
                                                                 createTestimonialCodeDto.FirstName, createTestimonialCodeDto.LastName,
                                                                 createTestimonialCodeDto.Description, code, DateTime.Now.AddDays(28)));

            await _databaseContext.SaveChangesAsync();

            return(code);
        }
예제 #3
0
        public async Task CreateNewTestimonialCode_IncorrectAdminPassword()
        {
            // Definitions
            const string firstName          = "first;";
            const string lastName           = "last";
            const string description        = "description";
            const string adminPassword      = "******";
            const string expectedCode       = "123456";
            DateTime     expirationDateTime = DateTime.Now.AddDays(28);
            const int    expectedCodeLength = 6;

            // Arrange
            var createTestimonialCodeDto = new CreateTestimonialCodeDto(firstName, lastName, description, adminPassword);
            var serviceUnderTest         = CreateServiceUnderTest();

            _mocker.GetMock <ICryptoService>().Setup(ts => ts.GenerateCode(It.Is <int>((i) => i == expectedCodeLength))).Returns(expectedCode);
            _mocker.GetMock <IPasswordService>().Setup(ps => ps.CheckPassword(It.Is <string>((password) => password == adminPassword), It.IsAny <string>())).Returns(false);

            // Act
            await Should.ThrowAsync <IncorrectAdminPasswordException>(serviceUnderTest.CreateNewTestimonialCode(createTestimonialCodeDto));

            // Assert
            _databaseContext.TestimonialCodes.Count().ShouldBe(0);
        }
예제 #4
0
 public async Task <IActionResult> CreateTestimonialCode([FromBody] CreateTestimonialCodeDto createTestimonialCodeDto)
 {
     return(Ok(new { code = await _testimonialsService.CreateNewTestimonialCode(createTestimonialCodeDto) }));
 }