public void CustomerPictureModelBinder_Returns_CustomerPicture_With_Photo()
        {
            //Arrange
            int    customerId    = 15;
            string photoAsString = Convert.ToBase64String(new byte[] { 0x10, 0x10, 0x01 });

            //Create a mock of the posted file.
            SHttpPostedFileBase customerPhoto = new SHttpPostedFileBase();

            customerPhoto.InputStreamGet   = () => new MemoryStream(Convert.FromBase64String(photoAsString));
            customerPhoto.ContentLengthGet = () => photoAsString.Length;


            //Create the binder
            IValueProvider provider = new SIValueProvider()
            {
                //Mock the contains prefix string to return true only for the Photo and the CustomerId
                ContainsPrefixString = x => x.StartsWith("CustomerPicture.Photo") || x.StartsWith("CustomerPicture.CustomerId") || x.Equals("CustomerPicture"),
                //Mock the values returned for Photo and CustomerId.
                GetValueString = x =>
                {
                    switch (x)
                    {
                    case "CustomerPicture.Photo":
                        return(new ValueProviderResult(customerPhoto, photoAsString, null));

                    case "CustomerPicture.CustomerId":
                        return(new ValueProviderResult(15, "15", null));

                    default:
                        return(null);
                    }
                }
            };


            ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(CustomerPicture));

            CustomerPictureModelBinder binder = new CustomerPictureModelBinder();

            ControllerContext context = new ControllerContext();

            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                ModelName     = "CustomerPicture",
                ValueProvider = provider,
                ModelMetadata = metadata
            };

            //Act
            CustomerPicture result = binder.BindModel(context, bindingContext) as CustomerPicture;


            //Assert
            Assert.AreEqual(customerId, result.CustomerId);
            Assert.IsNull(result.Customer);
        }
Exemple #2
0
        public CustomerPicture AddIcon(CustomerPicture newPicture)
        {
            var old = this.GetIconById(newPicture.Username);

            db.Add(newPicture);
            if (old != null)
            {
                this.Delete(old);
            }
            return(newPicture);
        }
        public void CustomerPictureModelBinder_Returns_CustomerPicture_Without_Photo_When_Photo_Isnt_HttpPostedFile()
        {
            //Arrange
            int customerId = 15;

            SValueProviderResult photoResult = new SValueProviderResult(null, "AAA", null);

            photoResult.ConvertToTypeCultureInfo = (type, culture) => null;
            IValueProvider provider = new SIValueProvider()
            {
                ContainsPrefixString = x => x.StartsWith("CustomerPicture.Photo") || x.StartsWith("CustomerPicture.CustomerId") || x.Equals("CustomerPicture"),
                GetValueString       = x =>
                {
                    switch (x)
                    {
                    case "CustomerPicture.Photo":
                        return(photoResult);

                    case "CustomerPicture.CustomerId":
                        return(new ValueProviderResult(15, "15", null));

                    default:
                        return(null);
                    }
                }
            };

            ModelMetadata metadata             = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(CustomerPicture));
            CustomerPictureModelBinder binder  = new CustomerPictureModelBinder();
            ControllerContext          context = new ControllerContext();

            ModelBindingContext bindingContext = new ModelBindingContext()
            {
                ModelName     = "CustomerPicture",
                ValueProvider = provider,
                ModelMetadata = metadata
            };

            //Act
            CustomerPicture result = binder.BindModel(context, bindingContext) as CustomerPicture;


            //Assert
            Assert.AreEqual(customerId, result.CustomerId);
            Assert.IsNull(result.Customer);
            Assert.IsNull(result.Photo);
        }
Exemple #4
0
        /// <summary>
        /// Returns the picture of the customer.
        /// </summary>
        /// <param name="customerCode">The customer code.</param>
        /// <returns>The picture of the customer.</returns>
        public ActionResult CustomerPicture(string customerCode)
        {
            //Get the customer aggregate
            CustomerPicture photo = _CustomerService.FindCustomerByCode(customerCode).CustomerPicture;

            //Check if the customer has a photo and if it is a valid photo.
            if (photo != null && photo.Photo != (byte [])null)
            {
                //Return the associated customer photo.
                return(File(photo.Photo, "img"));
            }
            else
            {
                // Return a default picture if the customer hasn't got any photo.
                return(File("~/Content/Images/Unknown.png", "img/png"));
            }
        }
Exemple #5
0
 public void Delete(CustomerPicture deletePicture)
 {
     db.Remove(deletePicture);
 }
Exemple #6
0
 public CustomerPicture Add(CustomerPicture newPicture)
 {
     db.Add(newPicture);
     return(newPicture);
 }