vendredi 27 novembre 2015

How to create unit testing for function upload image below?

    public Image UserAddImage(HttpPostedFileBase file, long userID)
    {
        User user = db.Users.Find(userID);
        if (user == null)
        {
            return null;
        }
        else {
            Image photo = new Image();
            try
            {
                if (file != null)
                {
                    string[] whiteListedExt = { ".jpg", ".gif", ".png", ".tiff" };
                    Stream stream = file.InputStream;
                    string extension = Path.GetExtension(file.FileName);
                    if (whiteListedExt.Contains(extension))
                    {
                        string pic = Guid.NewGuid() + user.UserID.ToString() + extension;
                        using (AmazonS3Client s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2))
                            EventZoneUtility.FileUploadToS3("eventzone", pic, stream, true, s3Client);
                        Image image = new Image();
                        image.ImageLink = "http://ift.tt/1jnxTs1" + pic;
                        image.UserID = user.UserID;
                        image.UploadDate = DateTime.Today;
                        if (UserDatabaseHelper.Instance.UpdateAvatar(user, image))
                        {
                            return image;
                        }
                    } 
                }
            }
            catch
            {
            }
        }
        return null;
    }

Aucun commentaire:

Enregistrer un commentaire