Ejemplo n.º 1
0
        public async Task PostAsync_SaveTabConfiguration_ReturnsOkResult()
        {
            // ARRANGE
            var tabConfigurationDetail = new TabConfigurationViewModel
            {
                TeamId           = "team 1",
                ChannelId        = "channel 1",
                LearningModuleId = Guid.NewGuid(),
            };

            var tabConfigurationEntity = new TabConfiguration
            {
                TeamId           = tabConfigurationDetail.TeamId,
                ChannelId        = tabConfigurationDetail.ChannelId,
                LearningModuleId = tabConfigurationDetail.LearningModuleId,
            };

            var groupId = Guid.NewGuid().ToString();

            this.unitOfWork.Setup(uow => uow.TabConfigurationRepository.Add(It.IsAny <TabConfiguration>())).Returns(tabConfigurationEntity);

            // ACT
            var result = (ObjectResult)await this.tabConfigurationController.PostAsync(groupId, tabConfigurationDetail);

            var resultValue = (TabConfiguration)result.Value;

            // ASSERT
            Assert.AreEqual(result.StatusCode, StatusCodes.Status200OK);
            Assert.AreEqual(resultValue.LearningModuleId, tabConfigurationDetail.LearningModuleId);
            Assert.AreEqual(resultValue.TeamId, tabConfigurationDetail.TeamId);
            Assert.AreEqual(resultValue.ChannelId, tabConfigurationDetail.ChannelId);
        }
Ejemplo n.º 2
0
        public void TabConfigurationViewModel_ValidModel_NoValidationError()
        {
            var teamPreferenceViewModel = new TabConfigurationViewModel
            {
                TeamId           = "test",
                ChannelId        = "test",
                LearningModuleId = Guid.NewGuid(),
            };

            Assert.IsTrue(!this.ValidateModel(teamPreferenceViewModel).Any());
        }
Ejemplo n.º 3
0
        public void TabConfigurationViewModel_EmptyLearningModuleIDGuid_ModelValidationError()
        {
            var teamPreferenceViewModel = new TabConfigurationViewModel
            {
                TeamId           = "test",
                ChannelId        = "test",
                LearningModuleId = Guid.Empty,
            };
            var modelValidationResult = this.ValidateModel(teamPreferenceViewModel);

            Assert.IsTrue(modelValidationResult.Any(
                              v => v.ErrorMessage.Contains("empty GUID")));
        }
Ejemplo n.º 4
0
        public void TabConfigurationViewModel_MissingRequiredField_ModelValidationError()
        {
            var teamPreferenceViewModel = new TabConfigurationViewModel
            {
                LearningModuleId = Guid.NewGuid(),
            };
            var modelValidationResult = this.ValidateModel(teamPreferenceViewModel);

            Assert.IsTrue(modelValidationResult.Any(
                              v => v.MemberNames.Contains("ChannelId") && v.ErrorMessage.Contains("required")));
            Assert.IsTrue(modelValidationResult.Any(
                              v => v.MemberNames.Contains("TeamId") && v.ErrorMessage.Contains("required")));
        }
Ejemplo n.º 5
0
        public async Task PatchAsync_EmptyTabID_ReturnsBadRequest()
        {
            // ARRANGE
            var tabConfigurationDetail = new TabConfigurationViewModel
            {
                LearningModuleId = Guid.NewGuid(),
            };

            // ACT
            var result = (ObjectResult)await this.tabConfigurationController.PatchAsync(Guid.NewGuid().ToString(), Guid.Empty, tabConfigurationDetail);

            // ASSERT
            Assert.AreEqual(result.StatusCode, StatusCodes.Status400BadRequest);
        }
Ejemplo n.º 6
0
        public async Task PatchAsync_RecordNotexistForGivenTabId_ReturnsNotFound()
        {
            // ARRANGE
            var tabConfigurationDetail = new TabConfigurationViewModel
            {
                TeamId           = "team 1",
                ChannelId        = "channel 1",
                LearningModuleId = Guid.NewGuid(),
                Id = Guid.NewGuid(),
            };

            this.unitOfWork.Setup(uow => uow.TabConfigurationRepository.FindAsync(It.IsAny <Expression <Func <TabConfiguration, bool> > >()))
            .ReturnsAsync(() => null);

            // ACT
            var result = (ObjectResult)await this.tabConfigurationController.PatchAsync(Guid.NewGuid().ToString(), tabConfigurationDetail.Id, tabConfigurationDetail);

            // ASSERT
            Assert.AreEqual(result.StatusCode, StatusCodes.Status404NotFound);
        }
        public async Task <IActionResult> PostAsync([FromQuery] string groupId, [FromBody] TabConfigurationViewModel tabConfigurationDetail)
        {
            this.RecordEvent("TabConfiguration - HTTP Post call.", RequestType.Initiated);
            this.logger.LogInformation("Call to add tab configuration details.");

            if (tabConfigurationDetail == null)
            {
                this.RecordEvent("TabConfiguration - HTTP Post call.", RequestType.Failed);
                return(this.BadRequest("Error while saving tab configuration details to storage."));
            }

            try
            {
                var tabConfigurationEntityModel = new TabConfiguration()
                {
                    // intentionally fetching group id from query string, as the same is validated by MustBeTeamMemberUserPolicy
                    GroupId          = groupId,
                    TeamId           = tabConfigurationDetail.TeamId,
                    ChannelId        = tabConfigurationDetail.ChannelId,
                    LearningModuleId = tabConfigurationDetail.LearningModuleId,
                    CreatedBy        = this.UserObjectId,
                    UpdatedBy        = this.UserObjectId,
                    CreatedOn        = DateTime.UtcNow,
                    UpdatedOn        = DateTime.UtcNow,
                };

                this.unitOfWork.TabConfigurationRepository.Add(tabConfigurationEntityModel);
                await this.unitOfWork.SaveChangesAsync();

                this.RecordEvent("TabConfiguration - HTTP Post call.", RequestType.Succeeded);
                return(this.Ok(tabConfigurationEntityModel));
            }
            catch (Exception ex)
            {
                this.RecordEvent("TabConfiguration - HTTP Post call.", RequestType.Failed);
                this.logger.LogError(ex, $"Error while saving tab configuration details.");
                throw;
            }
        }
Ejemplo n.º 8
0
        public async Task PatchAsync_UpdateTabConfiguration_ReturnsOkResult()
        {
            // ARRANGE
            var id = Guid.NewGuid();
            var tabConfigurationDetail = new TabConfigurationViewModel
            {
                TeamId           = "team 1",
                ChannelId        = "channel 1",
                LearningModuleId = Guid.NewGuid(),
            };

            var tabConfigurationEntities = new List <TabConfiguration>();

            tabConfigurationEntities.Add(new TabConfiguration
            {
                TeamId           = "team 1",
                ChannelId        = "channel 1",
                LearningModuleId = Guid.NewGuid(),
            });

            this.unitOfWork.Setup(uow => uow.TabConfigurationRepository
                                  .FindAsync(It.IsAny <Expression <Func <TabConfiguration, bool> > >()))
            .ReturnsAsync(() => tabConfigurationEntities);

            this.unitOfWork.Setup(uow => uow.TabConfigurationRepository.Update(It.IsAny <TabConfiguration>())).Returns(tabConfigurationEntities[0]);

            // ACT
            var result = (ObjectResult)await this.tabConfigurationController.PatchAsync(Guid.NewGuid().ToString(), id, tabConfigurationDetail);

            var resultValue = (TabConfiguration)result.Value;

            // ASSERT
            Assert.AreEqual(result.StatusCode, StatusCodes.Status200OK);
            Assert.AreEqual(resultValue.LearningModuleId, tabConfigurationDetail.LearningModuleId);
            Assert.AreEqual(resultValue.Id, tabConfigurationDetail.Id);
        }
        public async Task <IActionResult> PatchAsync([FromQuery] string groupId, [FromRoute] Guid id, [FromBody] TabConfigurationViewModel tabConfigurationDetail)
        {
            this.RecordEvent("TabConfiguration - HTTP Patch call.", RequestType.Initiated);
            this.logger.LogInformation("TabConfiguration - HTTP Patch call initiated.");

            if (tabConfigurationDetail == null)
            {
                this.RecordEvent("TabConfiguration - HTTP Patch call.", RequestType.Failed);
                return(this.BadRequest("Error while updating tab configuration details to storage."));
            }

            try
            {
                if (id == null || id == Guid.Empty)
                {
                    this.logger.LogError("Tab Id is either null or empty.");
                    this.RecordEvent("TabConfiguration - HTTP Patch call failed.", RequestType.Failed);
                    return(this.BadRequest("Tab Id cannot be null or empty guid."));
                }

                var existingTabConfigurations = await this.unitOfWork.TabConfigurationRepository
                                                .FindAsync(tab => tab.Id == id && tab.GroupId == groupId);

                if (existingTabConfigurations == null)
                {
                    this.logger.LogError(StatusCodes.Status404NotFound, $"The tab configuration either not exists for id: {id} or is not mapped with provided group id.");
                    this.RecordEvent("TabConfiguration - HTTP Patch call failed.", RequestType.Failed);
                    return(this.NotFound($"No tab configuration detail exists for tab Id: {id}."));
                }

                // it is must that only one record from database should present
                if (existingTabConfigurations.Count() > 1)
                {
                    this.logger.LogError("Database error: more than one records found for same tab id and group id combination. This could happen if someone manipulated the database.");
                    this.RecordEvent("Resource - HTTP Get call failed.", RequestType.Failed);
                    return(this.StatusCode(StatusCodes.Status500InternalServerError, "An exception occured while fetching tab configuration details."));
                }

                var existingTabConfiguration = existingTabConfigurations.First();

                existingTabConfiguration.UpdatedOn        = DateTime.UtcNow;
                existingTabConfiguration.UpdatedBy        = this.UserObjectId;
                existingTabConfiguration.LearningModuleId = tabConfigurationDetail.LearningModuleId;

                this.unitOfWork.TabConfigurationRepository.Update(existingTabConfiguration);
                await this.unitOfWork.SaveChangesAsync();

                this.RecordEvent("TabConfiguration - HTTP Patch call.", RequestType.Succeeded);
                return(this.Ok(existingTabConfiguration));
            }
            catch (Exception ex)
            {
                this.RecordEvent("TabConfiguration - HTTP Patch call.", RequestType.Failed);
                this.logger.LogError(ex, $"TabConfiguration - HTTP Patch call failed, for tab Id: {id}.");
                throw;
            }
        }