private UploadWizardCompleteModel ProcessDataFile(EducationSecurityPrincipal user, UploadWizardModel model, int studentIdColumn, Stream stream)
 {
     var dataTable = DataFileParser.ExtractValues(stream, '\t', model.NumberOfFields, model.FieldNameRow, model.FirstDataRow, model.LastDataRow);
     var completeModel = CheckUploadErrors(user, model, dataTable);
     if (completeModel.RowErrors.Count() > 0)
     {
         return completeModel;
     }
     CustomDataOrigin origin = CreateCustomDataOrigin(user, model);
     Dictionary<int, CustomField> customFieldsDicitonary = CreateCustomFieldsDictionary(model);
     ProcessRows(user, studentIdColumn, dataTable, completeModel, origin, customFieldsDicitonary);
     RepositoryContainer.Save();
     HandleRowErrors(user, completeModel, customFieldsDicitonary);
     return completeModel;
 }
 private CustomDataOrigin CreateCustomDataOrigin(EducationSecurityPrincipal user, UploadWizardModel uploadModel)
 {
     var origin = new CustomDataOrigin
     {
         CreatingUser = user.Identity.User,
         FileName = uploadModel.BlobAddress,
         AzureBlobKey = uploadModel.BlobAddress,
         Source = uploadModel.Source,
         WasManualEntry = false
     };
     CustomDataOriginRepository.Add(origin);
     return origin;
 }
 private Dictionary<int, CustomField> CreateCustomFieldsDictionary(UploadWizardModel uploadModel)
 {
     var customFieldsDicitonary = new Dictionary<int, CustomField>();
     for (int i = 0; i < uploadModel.CustomFields.Count; i++)
     {
         if (uploadModel.CustomFields[i].SelectedCustomFieldId == 0)
         {
             customFieldsDicitonary.Add(i, null);
         }
         else
         {
             var selectedId = (int)uploadModel.CustomFields[i].SelectedCustomFieldId;
             var value = CustomFieldRepository.Items.Include(c => c.CustomFieldType).Where(c => c.Id == selectedId).SingleOrDefault();
             customFieldsDicitonary.Add(i, value);
         }
     }
     return customFieldsDicitonary;
 }
 public UploadWizardCompleteModel GenerateUploadWizardCompleteViewModel(EducationSecurityPrincipal user, UploadWizardModel model)
 {
     int studentIdColumn = model.CustomFields.Select((v, i) => new { ViewModel = v, Index = i }).First(c => c.ViewModel.SelectedCustomFieldId == 0).Index;
     using (var stream = new MemoryStream())
     {
         DataFileBlobContainer.DownloadToStream(model.BlobAddress, stream);
         return ProcessDataFile(user, model, studentIdColumn, stream);
     }
 }
 private UploadWizardCompleteModel CheckUploadErrors(EducationSecurityPrincipal user, UploadWizardModel model, DataTable dataTable)
 {
     UploadWizardCompleteModel completeModel = new UploadWizardCompleteModel();
     if (dataTable.Columns.Count != model.CustomFields.Count())
     {
         completeModel.RowErrors.Add("There is a different amount of columns in the file than listed. Please re-submit the file and try again.");
         completeModel.ProcessedRowCount = completeModel.SuccessfulRowsCount = 0;
         return completeModel;
     }
     foreach (var field in model.CustomFields)
     {
         if (field.SelectedCustomFieldId != 0)
         {
             IPermission customFieldPermission = PermissionFactory.Current.Create("UploadCustomFieldData", CustomFieldRepository.Items.Single(c => c.Id == field.SelectedCustomFieldId));
             if (!customFieldPermission.TryGrantAccess(user))
             {
                 completeModel.RowErrors.Add("You don't have access to one or more of the selected custom fields. Re-submit and try again.");
                 completeModel.ProcessedRowCount = completeModel.SuccessfulRowsCount = 0;
                 return completeModel;
             }
         }
     }
     return completeModel;
 }
        public void GivenPermissionGrantsAccess_WhenIPostCompleteTheWizard_ThenRecordIsImported()
        {
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", null)).IgnoreArguments().Return(MockRepository.GenerateMock<IPermission>());
            List<CustomFieldValue> actualAdded = new List<CustomFieldValue>();
            Repositories.MockCustomFieldValueRepository.Expect(m => m.Add(null)).IgnoreArguments().Do(new Action<CustomFieldValue>((item) => { actualAdded.Add(item); }));
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/StudentApprovesProviderAndHasServiceOffering.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 2,
                NumberOfFields = 3,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 7
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 4
                    },
                }
            };
            foreach (var field in uploadModel.CustomFields)
            {
                if (field.SelectedCustomFieldId != 0)
                {
                    PermissionFactory.Current.Expect(m => m.Create("UploadCustomFieldData", Repositories.MockCustomFieldRepository.Items.Where(c => c.Id == field.SelectedCustomFieldId).Single())).Return(MockRepository.GenerateMock<IPermission>());
                }
            }

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.AreEqual(1, actualAdded.Count);
        }
        public void GivenThePublicFieldIsOfDateTimeAndThereIsAnInvalidRow_WhenIPostCompleteTheWizard_ThenAnUploadWizardCompleteViewModelIsReturned()
        {
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", null)).IgnoreArguments().Return(MockRepository.GenerateMock<IPermission>());
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/InvalidDateField.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 5,
                NumberOfFields = 2,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 5
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                }
            };
            foreach (var field in uploadModel.CustomFields)
            {
                if (field.SelectedCustomFieldId != 0)
                {
                    PermissionFactory.Current.Expect(m => m.Create("UploadCustomFieldData", Repositories.MockCustomFieldRepository.Items.Where(c => c.Id == field.SelectedCustomFieldId).Single())).Return(MockRepository.GenerateMock<IPermission>());
                }
            }

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(model.ProcessedRowCount, model.SuccessfulRowsCount + 1);
            Assert.IsTrue(model.ProcessedRowCount > 0);
            Assert.IsTrue(model.RowErrors.Count == 1);
            Repositories.MockCustomDataOriginRepository.AssertWasCalled(m => m.Add(null), options => options.IgnoreArguments());
            Repositories.MockRepositoryContainer.AssertWasCalled(m => m.Save());
            Repositories.MockCustomFieldValueRepository.AssertWasCalled(m => m.Add(null), options => options.IgnoreArguments());
            Repositories.MockRepositoryContainer.AssertWasCalled(m => m.Save());
        }
        public void GivenNoRightsToSelectedCustomFields_WhenIPostcompleteThenWizard_ThenExceptionThrown()
        {
            List<CustomFieldValue> actualAdded = new List<CustomFieldValue>();
            Repositories.MockCustomFieldValueRepository.Expect(m => m.Add(null)).IgnoreArguments().Do(new Action<CustomFieldValue>((item) => { actualAdded.Add(item); }));
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/HappyPath.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 5,
                NumberOfFields = 3,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 2
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 3
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                }
            };

            IPermission permission = MockRepository.GenerateMock<IPermission>();
            permission.Expect(p => p.GrantAccess(User)).Throw(new EntityAccessUnauthorizedException());
            PermissionFactory.Current.Expect(m => m.Create("UploadCustomFieldData", Repositories.MockCustomFieldRepository.Items.Where(c => c.Id == 2).Single())).Return(permission);

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.AreEqual(0, actualAdded.Count);
            Assert.AreEqual(1, model.RowErrors.Count);
            Assert.AreEqual("You don't have access to one or more of the selected custom fields. Re-submit and try again.", model.RowErrors[0]);
        }
        public void GivenPermissionDoesntGrantAccess_AndFileHasStudentIdThatApprovesProvider_AndProviderDoesNotServeStudent_WhenIPostCompleteTheWizard_ThenRecordIsNotImported()
        {
            IPermission permission = MockRepository.GenerateMock<IPermission>();
            permission.Expect(p => p.GrantAccess(User)).Throw(new EntityAccessUnauthorizedException());
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", null)).IgnoreArguments().Return(permission);

            List<CustomFieldValue> actualAdded = new List<CustomFieldValue>();
            Repositories.MockCustomFieldValueRepository.Expect(m => m.Add(null)).IgnoreArguments().Do(new Action<CustomFieldValue>((item) => { actualAdded.Add(item); }));
            User.Identity.User.UserRoles = new List<UserRole>
            {
                new UserRole
                {
                    Role = new Role { Name = SecurityRoles.Provider }, Providers = Data.Providers
                }
            };
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/StudentApprovesProviderButHasNoServiceOffering.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 2,
                NumberOfFields = 3,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 7
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 4
                    },
                }
            };

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.AreEqual(0, actualAdded.Count);
        }
        public void GivenFileHasTwoColumnsInAdditionToStudentId_AndOnlyIdHasData_WhenIPostCompleteTheWizard_ThenAnUploadWizardCompleteViewModelIsNotReturned()
        {
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", null)).IgnoreArguments().Return(MockRepository.GenerateMock<IPermission>());
            List<CustomFieldValue> actualAdded = new List<CustomFieldValue>();
            Repositories.MockCustomFieldValueRepository.Expect(m => m.Add(null)).IgnoreArguments().Do(new Action<CustomFieldValue>((item) => { actualAdded.Add(item); }));
            User.Identity.User.UserRoles = new List<UserRole>
            {
                new UserRole
                {
                    Role = new Role { Name = SecurityRoles.DataAdmin }
                }
            };
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/NoOtherFieldButIDPopulated.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 2,
                NumberOfFields = 3,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 2
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 6
                    },
                }
            };
            foreach (var field in uploadModel.CustomFields)
            {
                if (field.SelectedCustomFieldId != 0)
                {
                    PermissionFactory.Current.Expect(m => m.Create("UploadCustomFieldData", Repositories.MockCustomFieldRepository.Items.Where(c => c.Id == field.SelectedCustomFieldId).Single())).Return(MockRepository.GenerateMock<IPermission>());
                }
            }

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.IsTrue(model.SuccessfulRowsCount == 0);
            Assert.IsTrue(model.RowErrors.Count == 1);
        }
        public void WhenIPostCompleteTheWizardWithPartialRights_ThenAnUploadWizardCompleteViewModelIsReturned()
        {
            var students = Repositories.MockStudentRepository.Items.Where(s => s.StudentSISId == "10" || s.StudentSISId == "20" || s.StudentSISId == "30");
            IPermission permission = MockRepository.GenerateMock<IPermission>();
            permission.Expect(p => p.GrantAccess(User)).Throw(new EntityAccessUnauthorizedException());
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", students.ToArray()[0])).Return(MockRepository.GenerateMock<IPermission>());
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", students.ToArray()[1])).Return(permission);
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", students.ToArray()[2])).Return(permission);
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/HappyPath.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 5,
                NumberOfFields = 3,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 2
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 3
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                }
            };
            foreach (var field in uploadModel.CustomFields)
            {
                if (field.SelectedCustomFieldId != 0)
                {
                    PermissionFactory.Current.Expect(m => m.Create("UploadCustomFieldData", Repositories.MockCustomFieldRepository.Items.Where(c => c.Id == field.SelectedCustomFieldId).Single())).Return(MockRepository.GenerateMock<IPermission>());
                }
            }

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(1, model.SuccessfulRowsCount);
            Assert.IsTrue(model.ProcessedRowCount == 3);
            Assert.IsTrue(model.RowErrors.Count == 2);
            Repositories.MockCustomDataOriginRepository.AssertWasCalled(m => m.Add(null), options => options.IgnoreArguments());
            Repositories.MockRepositoryContainer.AssertWasCalled(m => m.Save());
            Repositories.MockCustomFieldValueRepository.AssertWasCalled(m => m.Add(null), options => options.IgnoreArguments());
            Repositories.MockRepositoryContainer.AssertWasCalled(m => m.Save());
        }
        public void GivenUserIndicatesADifferentAmountOfColumnsThanAreInTheFile_WhenIPostCompleteTheWizard_ThenTheModelHasErrors()
        {
            PermissionFactory.Current.Expect(m => m.Create("ProcessDataFile", null)).IgnoreArguments().Return(MockRepository.GenerateMock<IPermission>());
            List<CustomFieldValue> actualAdded = new List<CustomFieldValue>();
            Repositories.MockCustomFieldValueRepository.Expect(m => m.Add(null)).IgnoreArguments().Do(new Action<CustomFieldValue>((item) => { actualAdded.Add(item); }));
            User.Identity.User.UserRoles = new List<UserRole>
            {
                new UserRole
                {
                    Role = new Role { Name = SecurityRoles.DataAdmin }
                }
            };
            var uploadModel = new UploadWizardModel
            {
                BlobAddress = Path.GetFullPath(ConfigurationManager.AppSettings["FileUploadTemplatePath"] + "DataFileWizard/ColumnNumberError.txt"),
                FieldNameRow = 1,
                FirstDataRow = 2,
                LastDataRow = 5,
                NumberOfFields = 2,
                Source = "test",
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 2
                    },
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    },
                }
            };

            var model = Target.GenerateUploadWizardCompleteViewModel(User, uploadModel) as UploadWizardCompleteModel;

            Assert.AreEqual(0, actualAdded.Count);
            Assert.AreEqual(1, model.RowErrors.Count);
            Assert.AreEqual("There is a different amount of columns in the file than listed. Please re-submit the file and try again.", model.RowErrors[0]);
        }
        public void GivenSubmitButtonWasClicked_WhenUploadWizard2_ThenTheAppropriateViewIsReturned()
        {
            var model = new UploadWizardModel
            {
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    }
                }
            };
            MockLogicManager.Expect(m => m.GenerateUploadWizardCompleteViewModel(User, model)).Return(new UploadWizardCompleteModel());
            var result = Target.UploadWizard2(model, "submit") as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ViewName.Equals("UploadWizard3"));
            result.AssertGetViewModel<UploadWizardCompleteModel>();
        }
        public void GivenSubmitButtonWasClicked_WhenIPost_ThenTheAppropriateViewIsReturned()
        {
            var model = new UploadWizardModel
            {
                CustomFields = new List<CustomFieldSelectModel>
                {
                    new CustomFieldSelectModel
                    {
                        SelectedCustomFieldId = 0
                    }
                }
            };
            var result = Target.UploadWizard2(model, "submit") as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ViewName.Equals("UploadWizard3"));
        }