public Post_When_Existing_Should()
            {
                dateTimeCreatorMock = new Mock <IDateTimeCreator>();
                expected            = new ActivationCodeToAdd
                {
                    PhoneNumber    = "07930123456",
                    ActivationCode = "BAC132"
                };
                var existing = new ActivationCode
                {
                    Id          = 101,
                    PhoneNumber = "07930123456",
                    Code        = "ABC0405"
                };
                var expectedDateTime = new DateTime(2001, 5, 4);

                dataStoreMock = new Mock <IDataStore>();
                dataStoreMock.Setup(x => x.GetActivationCode(expected.PhoneNumber))
                .Returns(existing);
                dataStoreMock.Setup(x => x.UpdateActivationCode(
                                        It.Is <ActivationCode>(y => y.Id == existing.Id &&
                                                               y.PhoneNumber == expected.PhoneNumber &&
                                                               y.Code == expected.ActivationCode &&
                                                               y.UpdatedAt == expectedDateTime)))
                .Returns(true);
                dateTimeCreatorMock.Setup(x => x.Create())
                .Returns(expectedDateTime);
                loggerMock = new Mock <ILogger <ActivationCodesController> >();

                sut = new ActivationCodesController(loggerMock.Object, dataStoreMock.Object, dateTimeCreatorMock.Object);
            }
Ejemplo n.º 2
0
        public async Task <bool> PostActivationCode(string phoneNumber, string activationCode)
        {
            var activationCodeToAdd = new ActivationCodeToAdd
            {
                PhoneNumber    = phoneNumber,
                ActivationCode = activationCode
            };
            var json     = JsonConvert.SerializeObject(activationCodeToAdd);
            var response = await httpClient.PostAsync($"{config.ExternalMobileTelecomsNetworkApiUrl}/api/activationcodes", new StringContent(json, System.Text.Encoding.UTF8, "application/json"));

            return(response.StatusCode == System.Net.HttpStatusCode.OK);
        }
            public Post_When_Inserting_Should()
            {
                dateTimeCreatorMock = new Mock <IDateTimeCreator>();

                expected = new ActivationCodeToAdd
                {
                    PhoneNumber    = "07930123456",
                    ActivationCode = "BAC132"
                };
                dataStoreMock = new Mock <IDataStore>();
                dataStoreMock.Setup(x => x.InsertActivationCode(
                                        It.Is <ActivationCode>(y => y.PhoneNumber == expected.PhoneNumber &&
                                                               y.Code == expected.ActivationCode)))
                .Returns(true);
                loggerMock = new Mock <ILogger <ActivationCodesController> >();

                sut = new ActivationCodesController(loggerMock.Object, dataStoreMock.Object, dateTimeCreatorMock.Object);
            }
Ejemplo n.º 4
0
        public IActionResult Post([FromBody] ActivationCodeToAdd activationCodeToAdd)
        {
            var isSuccess = false;

            using (dataStore.BeginTransaction())
            {
                var existing = dataStore.GetActivationCode(activationCodeToAdd.PhoneNumber);

                if (existing != null)
                {
                    logger.LogDebug("Existing Activation Code for PhoneNumber {phoneNumber}", activationCodeToAdd.PhoneNumber);
                    var updatedAt = dateTimeCreator.Create();
                    logger.LogDebug("Updating Activation Code at {updatedAt}", updatedAt);

                    isSuccess = dataStore.UpdateActivationCode(
                        new ActivationCode
                    {
                        Id          = existing.Id,
                        PhoneNumber = existing.PhoneNumber,
                        Code        = activationCodeToAdd.ActivationCode,
                        UpdatedAt   = updatedAt
                    });
                }
                else
                {
                    isSuccess = dataStore.InsertActivationCode(
                        new ActivationCode
                    {
                        PhoneNumber = activationCodeToAdd.PhoneNumber,
                        Code        = activationCodeToAdd.ActivationCode
                    });
                }
            }

            if (!isSuccess)
            {
                logger.LogError("Failed to save ActivationCode for PhoneNumber {phoneNumber}", activationCodeToAdd.PhoneNumber);
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }

            return(new OkResult());
        }