Exemplo n.º 1
0
        public void CTOR_NullHotpToByteConverter_ThrowsArgumentNullException()
        {
            // arrange
            var sut = new OTPService(Mock.Of <ISettings>(), null, null);

            // act
            sut.GenerateOTP(null);

            // assert
            Assert.Fail();
        }
Exemplo n.º 2
0
        public void CTOR_NullSettings_ThrowsArgumentNullException()
        {
            // arrange
            var sut = new OTPService(null, null, null);

            // act
            sut.GenerateOTP(null);

            // assert
            Assert.Fail();
        }
Exemplo n.º 3
0
        public void CTOR_NullSecretGeneratorProvider_ThrowsArgumentNullException()
        {
            // arrange
            var sut = new OTPService(Mock.Of <ISettings>(),
                                     Mock.Of <IOneWayConverter <long, byte[]> >(), null);

            // act
            sut.GenerateOTP(null);

            // assert
            Assert.Fail();
        }
Exemplo n.º 4
0
        public void GenerateOTP_SecretGenerator_SecretIsGeneratedUsingUserId()
        {
            // arrange
            var secretGeneratorMock = new Mock <ISecretGeneratorProvider>();
            var userId       = "some user id";
            var userIdBase64 = "c29tZSB1c2VyIGlk";

            secretGeneratorMock.Setup(x => x.GenerateSecret(userId)).Returns(userIdBase64);
            var sut = new OTPService(this.MockSettings().Object,
                                     Mock.Of <IOneWayConverter <long, byte[]> >(), secretGeneratorMock.Object);

            // act
            sut.GenerateOTP(userId);

            // assert
            secretGeneratorMock.Verify(x => x.GenerateSecret(userId), Times.Once);
        }
Exemplo n.º 5
0
        public IActionResult GenerateOTP([FromBody] OTPRequest request)
        {
            OTPService service = new OTPService(configuration.GetConnectionString("MAF"));
            MobileOTP  oTP;

            try
            {
                oTP = service.GenerateOTP(request);
                SMSHelper helper = new SMSHelper(configuration);
                helper.Send(oTP.MobileNo, oTP.OTP);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(Ok());
        }
Exemplo n.º 6
0
        public void GenerateOTP_Settings_MakesUserOfTimeStep()
        {
            // arrange
            var secretGeneratorMock = new Mock <ISecretGeneratorProvider>();
            var userId       = "some user id";
            var userIdBase64 = "c29tZSB1c2VyIGlk";
            var settingsMock = this.MockSettings();

            secretGeneratorMock.Setup(x => x.GenerateSecret(userId)).Returns(userIdBase64);
            var sut = new OTPService(settingsMock.Object,
                                     Mock.Of <IOneWayConverter <long, byte[]> >(), secretGeneratorMock.Object);

            // act
            sut.GenerateOTP(userId);

            // assert
            settingsMock.VerifyGet(x => x.TimeStep, Times.AtLeastOnce());
        }
Exemplo n.º 7
0
        public void GenerateOTP_ValidInput_GeneratesValidOTPResult()
        {
            // arrange
            var secretGeneratorMock = new Mock <ISecretGeneratorProvider>();
            var userId            = "some user id";
            var userIdBase64      = "c29tZSB1c2VyIGlk";
            var settingsMock      = this.MockSettings();
            var dateTime          = new DateTime(2020, 1, 1, 0, 0, 1);
            var hotpByteConverter = new Mock <IOneWayConverter <long, byte[]> >();

            secretGeneratorMock.Setup(x => x.GenerateSecret(userId)).Returns(userIdBase64);
            var sut = new OTPService(settingsMock.Object, hotpByteConverter.Object, secretGeneratorMock.Object);

            // act
            var result = sut.GenerateOTP(userId, dateTime);

            // assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 8
0
        public void GenerateOTP_HotpToByteConverter_CovnertsBytes()
        {
            // arrange
            var secretGeneratorMock = new Mock <ISecretGeneratorProvider>();
            var userId            = "some user id";
            var userIdBase64      = "c29tZSB1c2VyIGlk";
            var settingsMock      = this.MockSettings();
            var dateTime          = new DateTime(2020, 1, 1, 0, 0, 1);
            var hotpByteConverter = new Mock <IOneWayConverter <long, byte[]> >();
            var hotpValue         = 52594560L;

            secretGeneratorMock.Setup(x => x.GenerateSecret(userId)).Returns(userIdBase64);
            var sut = new OTPService(settingsMock.Object, hotpByteConverter.Object, secretGeneratorMock.Object);

            // act
            sut.GenerateOTP(userId, dateTime);

            // assert
            hotpByteConverter.Verify(x => x.Convert(hotpValue), Times.Once());
        }