public async Task ValidateVerificationCode_ShouldReturn_SuccessMessage_If_Params_Phone_Country_Verify_Are_Valid()
        {
            // Arrange
            var mockMessageHandler = new Mock <HttpMessageHandler>();

            var expectedOutput = await UserBuilder.Builder()
                                 .WithPhoneNumber("9876543210", "91")
                                 .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));

            var iCredentials = Options.Create <TwilioNetworkCredentials>(TestCredentials);

            mockMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("Test Content")
            }));
            var service = new TwilioVerificationService(new HttpClient(mockMessageHandler.Object), iCredentials, userRepositoryMock.Object);

            // Act
            var result = await service.ValidateVerificationCodeAsync(TEST_COUNTRY_CODE, expectedOutput.PhoneNumber.PhoneNumber, TEST_VERIFICATION_CODE);

            // Assert
            result.IsSuccessed.Should().BeTrue();
        }
        // api key invalid case
        public async Task GenerateVerificationCode_ShouldReturn_ErrorMessage_If_Api_Key_Is_Invalid()
        {
            // Arrange
            var mockMessageHandler = new Mock <HttpMessageHandler>();
            var expectedOutput     = await UserBuilder.Builder()
                                     .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                     .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));
            var iCredentials = Options.Create <TwilioNetworkCredentials>(InvalidCredentials);

            mockMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Unauthorized,
                Content    = new StringContent("Test Unauthorized User")
            }));

            var service = new TwilioVerificationService(new HttpClient(mockMessageHandler.Object), iCredentials, userRepositoryMock.Object);

            // Act
            var result = await service.GenerateVerificationCodeAsync("91", "8897322553");

            // Assert
            result.IsSuccessed.Should().BeFalse();
            result.GetErrorString().Should().Be(Constants.PhoneVerifyMessages.Twilio_Unauthorized);
        }
Ejemplo n.º 3
0
        private static Result <User> BuildUser(UserRegistrationDTO userRegistrationDTO, byte[] randomByteArray)
        {
            var hashingServiceMock = new Mock <IHashingService>();


            var ageResult = Age.Create(userRegistrationDTO.DateOfBirth);
            var lookingForGenderResult = Gender.Create(userRegistrationDTO.LookingFor);
            var matchPreference        = UserMatchPreference.Create(lookingForGenderResult.Value, userRegistrationDTO.MinAge, userRegistrationDTO.MaxAge, userRegistrationDTO.Distance);
            var phoneNumberResult      = UserPhoneNumber.Create(userRegistrationDTO.PhoneNumber, userRegistrationDTO.CountryCode);
            var genderResult           = Gender.Create(userRegistrationDTO.Gender);
            var loctionResult          = Location.Create(userRegistrationDTO.Latitude, userRegistrationDTO.Longitude);


            hashingServiceMock.Setup(x => x.GenerateSalt()).Returns(randomByteArray);
            hashingServiceMock.Setup(x => x.GetHash(userRegistrationDTO.Password, It.IsAny <byte[]>())).Returns(randomByteArray);
            var passwordSalt   = hashingServiceMock.Object.GenerateSalt();
            var passwordHash   = hashingServiceMock.Object.GetHash(userRegistrationDTO.Password, passwordSalt);
            var passwordResult = Password.Create(passwordHash, passwordSalt);

            var result = UserBuilder.Builder()
                         .WithName(userRegistrationDTO.FullName)
                         .WithPhoneNumber(phoneNumberResult.Value)
                         .WithPassword(passwordResult.Value)
                         .WithGender(genderResult.Value)
                         .WithMatchPreference(matchPreference.Value)
                         .WithAge(ageResult.Value)
                         .WithBucketList(userRegistrationDTO.BucketList)
                         .WithFunAndInterestingThings(userRegistrationDTO.FunAndInteresting)
                         .WithSchool(userRegistrationDTO.School)
                         .WithProfession(userRegistrationDTO.Profession)
                         .WithLocation(loctionResult.Value)
                         .Build();

            return(result);
        }
Ejemplo n.º 4
0
        public async Task <Result <User> > CreateUserAsync(UserRegistrationDTO userRegistrationDTO)
        {
            if (string.IsNullOrWhiteSpace(userRegistrationDTO.Password))
            {
                return(Result.Fail <User>(Password_Is_Required));
            }

            var ageResult = Age.Create(userRegistrationDTO.DateOfBirth);
            var lookingForGenderResult = Gender.Create(userRegistrationDTO.LookingFor);
            var matchPreference        = UserMatchPreference.Create(lookingForGenderResult.Value, userRegistrationDTO.MinAge, userRegistrationDTO.MaxAge, userRegistrationDTO.Distance);
            var phoneNumberResult      = UserPhoneNumber.Create(userRegistrationDTO.PhoneNumber, userRegistrationDTO.CountryCode);
            var genderResult           = Gender.Create(userRegistrationDTO.Gender);

            var passwordSalt   = _hashingService.GenerateSalt();
            var passwordHash   = _hashingService.GetHash(userRegistrationDTO.Password, passwordSalt);
            var passwordResult = Password.Create(passwordHash, passwordSalt);

            var locationResult = Location.Create(userRegistrationDTO.Latitude, userRegistrationDTO.Longitude);

            var finalResult = Result.Combine(ageResult,
                                             lookingForGenderResult,
                                             matchPreference,
                                             phoneNumberResult,
                                             genderResult,
                                             passwordResult,
                                             locationResult);

            if (!finalResult.IsSuccessed)
            {
                return(Result.Fail <User>(finalResult.GetErrorString()));
            }

            var userResult = UserBuilder.Builder()
                             .WithName(userRegistrationDTO.FullName)
                             .WithPhoneNumber(phoneNumberResult.Value)
                             .WithPassword(passwordResult.Value)
                             .WithGender(genderResult.Value)
                             .WithMatchPreference(matchPreference.Value)
                             .WithAge(ageResult.Value)
                             .WithBucketList(userRegistrationDTO.BucketList)
                             .WithFunAndInterestingThings(userRegistrationDTO.FunAndInteresting)
                             .WithSchool(userRegistrationDTO.School)
                             .WithProfession(userRegistrationDTO.Profession)
                             .WithLocation(locationResult.Value)
                             .Build();

            var setUserInterestsResult = await _userDomainService.SetUserInterests(userResult.Value, userRegistrationDTO.InterestIds);

            if (!setUserInterestsResult.IsSuccessed)
            {
                return(Result.Fail <User>(setUserInterestsResult.GetErrorString()));
            }

            return(userResult);
        }
        public async Task ValidateVerificationCode_Should_Return_Error_If_VerificationCode_Not_Yet_Generated(string countryCode, string phoneNumber, int verificationCode)
        {
            var emailHelperMock      = new Mock <IEmailHelper>();
            var randomNumberProvider = new Mock <IRandomNumberProvider>();
            var expectedOutput       = await UserBuilder.Builder()
                                       .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                       .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));
            var verificationCodeDataStoreMock = new Mock <IVerificationCodeDataStore>();
            var emailVerificationService      = new EmailVerificationService(emailHelperMock.Object, randomNumberProvider.Object, verificationCodeDataStoreMock.Object, userRepositoryMock.Object);
            var result = await emailVerificationService.ValidateVerificationCodeAsync(countryCode, phoneNumber, verificationCode);

            Assert.IsFalse(result.IsSuccessed);
            Assert.AreEqual(Constants.PhoneVerifyMessages.Error_Message, result.GetErrorString());
        }
        public async Task GenerateVerificationCode_Should_Return_True_If_countryCode_And_phoneNumber_Are_ValidAsync(string countryCode, string phoneNumber)
        {
            var emailHelperMock               = new Mock <IEmailHelper>();
            var randomNumberProvider          = new Mock <IRandomNumberProvider>();
            var verificationCodeDataStoreMock = new Mock <IVerificationCodeDataStore>();

            var expectedOutput = await UserBuilder.Builder()
                                 .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                 .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));

            var emailVerificationService = new EmailVerificationService(emailHelperMock.Object, randomNumberProvider.Object, verificationCodeDataStoreMock.Object, userRepositoryMock.Object);
            var result = await emailVerificationService.GenerateVerificationCodeAsync(countryCode, phoneNumber);

            result.IsSuccessed.Should().BeTrue();
        }
        public async Task SendEmailAsync_Gets_Invoked_If_GenerateVerificationCode_Is_Called_First_Time_With_Valid_PhoneNumber(string countryCode, string phoneNumber)
        {
            var emailHelperMock      = new Mock <IEmailHelper>();
            var randomNumberProvider = new Mock <IRandomNumberProvider>();
            var expectedOutput       = await UserBuilder.Builder()
                                       .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                       .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));
            var verificationCodeDataStoreMock = new Mock <IVerificationCodeDataStore>();

            verificationCodeDataStoreMock.Setup(x => x.GetValueOrDefault(It.IsAny <string>())).Returns(TEST_VERIFICATION_CODE);
            verificationCodeDataStoreMock.Setup(x => x.ContainsKey(It.IsAny <string>())).Returns(false);
            var emailVerificationService = new EmailVerificationService(emailHelperMock.Object, randomNumberProvider.Object, verificationCodeDataStoreMock.Object, userRepositoryMock.Object);
            var result = await emailVerificationService.GenerateVerificationCodeAsync(countryCode, phoneNumber);

            Assert.IsTrue(result.IsSuccessed);
            emailHelperMock.Verify(x => x.SendAsync(It.IsAny <string>()), Times.Once);
        }
        public async Task ValidateVerificationCode_Should_Return_Error_If_VerificationCode_Generated_But_Supplied_Incorrect_VerificationCode(string countryCode, string phoneNumber, int verificationCode)
        {
            var emailHelperMock      = new Mock <IEmailHelper>();
            var randomNumberProvider = new Mock <IRandomNumberProvider>();
            var expectedOutput       = await UserBuilder.Builder()
                                       .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                       .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));
            randomNumberProvider.Setup(x => x.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(verificationCode);
            var verificationCodeDataStoreMock = new Mock <IVerificationCodeDataStore>();

            verificationCodeDataStoreMock.Setup(x => x.GetValueOrDefault(It.IsAny <string>())).Returns(verificationCode);
            verificationCodeDataStoreMock.Setup(x => x.ContainsKey(It.IsAny <string>())).Returns(true);
            var emailVerificationService = new EmailVerificationService(emailHelperMock.Object, randomNumberProvider.Object, verificationCodeDataStoreMock.Object, userRepositoryMock.Object);
            var result = await emailVerificationService.ValidateVerificationCodeAsync(countryCode, phoneNumber, TEST_VERIFICATION_CODE_OTHER);

            Assert.IsFalse(result.IsSuccessed);
            Assert.AreEqual(Constants.PhoneVerifyMessages.Code_Error, result.GetErrorString());
        }
        public async Task ValidateVerificationCode_Should_Return_True_When_Supplied_Correct_VerificationCode(string countryCode, string phoneNumber, int verificationCode)
        {
            var emailHelperMock      = new Mock <IEmailHelper>();
            var randomNumberProvider = new Mock <IRandomNumberProvider>();
            var expectedOutput       = await UserBuilder.Builder()
                                       .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                       .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));

            userRepositoryMock.Setup(x => x.SaveChangesAsync(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok()));
            randomNumberProvider.Setup(x => x.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(verificationCode);
            var verificationCodeDataStoreMock = new Mock <IVerificationCodeDataStore>();

            verificationCodeDataStoreMock.Setup(x => x.GetValueOrDefault(It.IsAny <string>())).Returns(verificationCode);
            verificationCodeDataStoreMock.Setup(x => x.ContainsKey(It.IsAny <string>())).Returns(true);
            var emailVerificationService = new EmailVerificationService(emailHelperMock.Object, randomNumberProvider.Object, verificationCodeDataStoreMock.Object, userRepositoryMock.Object);
            var result = await emailVerificationService.ValidateVerificationCodeAsync(countryCode, phoneNumber, verificationCode);

            Assert.IsTrue(result.IsSuccessed);
        }
        public async Task ValidateVerificationCode_ShouldReturn_ErrorMessage_If_Phone_Country_Verification_Are_Invalid()
        {
            // Arrange
            var _user = await UserBuilder.Builder()
                        .WithPhoneNumber("9876543210", "91")
                        .BuildAsync();

            var mockMessageHandler = new Mock <HttpMessageHandler>();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(_user)));

            var iCredentials = Options.Create <TwilioNetworkCredentials>(TestCredentials);

            mockMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new TwilioErrorResponse()
                {
                    errors = new Errors()
                    {
                        message = Constants.PhoneVerifyMessages.Country_Phone_VerifyCode_Invalid
                    }
                }))
            }));
            var service = new TwilioVerificationService(new HttpClient(mockMessageHandler.Object), iCredentials, userRepositoryMock.Object);

            // Act
            var result = await service.ValidateVerificationCodeAsync("dfgsd", "fdgsdfg", 8484);

            // Assert
            result.IsSuccessed.Should().BeFalse();
            result.GetErrorString().Should().Be(Constants.PhoneVerifyMessages.Country_Phone_VerifyCode_Invalid);
        }
        public async Task GenerateVerificationCode_ShouldReturn_ErrorMessage_If_Country_Code_Is_Invalid()
        {
            // Arrange
            var mockMessageHandler = new Mock <HttpMessageHandler>();
            var expectedOutput     = await UserBuilder.Builder()
                                     .WithPhoneNumber(TEST_NUMBER, TEST_COUNTRY_CODE)
                                     .BuildAsync();

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetUserByPhoneNumber(It.IsAny <string>())).Returns(Task.FromResult(Result.Ok <User>(expectedOutput)));
            var iCredentials = Options.Create <TwilioNetworkCredentials>(TestCredentials);

            mockMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .Returns(Task.FromResult(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new TwilioErrorResponse()
                {
                    errors = new Errors()
                    {
                        message = "test"
                    }
                }))
            }));

            var service = new TwilioVerificationService(new HttpClient(mockMessageHandler.Object), iCredentials, userRepositoryMock.Object);

            // Act
            var result = await service.GenerateVerificationCodeAsync("eg", TEST_NUMBER);

            // Assert
            result.IsSuccessed.Should().BeFalse();
            result.GetErrorString().Should().Be("test");
        }