0
protected void UploadImage_Click(object sender, EventArgs e)
    {
        HttpPostedFile postedFile = ImageUploader.PostedFile;
        string fileExtension = 
Path.GetExtension(Path.GetFileName(postedFile.FileName));
        if (ImageUploader.HasFile && fileExtension.ToLower() == ".jpg" || 
fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")          
        {

            Stream fs = postedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] image = br.ReadBytes((int)fs.Length);
            Guid UserId = (Guid)Membership.GetUser().ProviderUserKey;

            string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                string sqlcmd = "insert into ProfileImage values(@UserId, @ImageData)";
                SqlCommand cmd = new SqlCommand(sqlcmd, con);
                cmd.Parameters.AddWithValue("@UserId", UserId);
                cmd.Parameters.AddWithValue("@ImageData", image);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                ErrorLabel.Visible = true;
                ErrorLabel.Text = "Upload Success!";
                ErrorLabel.ForeColor = System.Drawing.Color.Green;

            }

        }
        else
        {
            ErrorLabel.Visible = true;
            ErrorLabel.Text = "Upload failed: Only jpg, gif, and png allowed.";
            ErrorLabel.ForeColor = System.Drawing.Color.Red;

        }

Above is the code I am using to upload a file to the database for a user image. I need to know how to write a unit test that can return a GUID and not a null value to ensure the code works as intended.

Essentially, I want to add the currently logged in user userid from the membership provider as well as the uploaded image to the database so it can be easily retrieved later. When I test it by modifying the database to accept something other than the uniqueidentifier and the code to insert a temp userid, the insertion works. But now I want to make sure that I can actually obtain the currently logged in userid and add that to the database.

When I test this code, it says for the error that the

Guid UserId = (Guid)Membership.GetUser().ProviderUserKey

part is returning a null value, even if I add a user to the membership tables. How can I properly test the retrieval of a currently logged in user userid?

Dragon Wolf
  • 61
  • 1
  • 2
  • 11
  • If Membership.GetUser() is null, then maybe there is an issue with authentication from wherever you're testing the code? Maybe you're accessing this method from your test anonymously? – VB Did Nothing Wrong Dec 05 '17 at 16:19
  • This is a semester project, and the database is a local db, there is no production server. That said, I do know that, at least in my limited understanding, that since I don't actually have a user, that it makes sense the table returns null as that is the default value in the table. All my testing is done locally on the IIS, and I believe there is someway to unit test this properly. I am testing straight through VS where I am making this, and even when I create a user then upload image (tables populated) still returns null. – Dragon Wolf Dec 05 '17 at 16:57
  • Again, I think that's due to the fact I am testing locally. We never learned any real debugging or unit test things in this or previous courses, but I know this can be done. As mentioned, when I change the table to allow for an int data type instead of uniqueidentifer, as that is the default data type for userid with the membership provider, this code executes as intended. That wont get me the grade I want tho, as it needs to work under the regular membership provider, and thus, the userid needs to be of data type uniqueidentifer. I just want to see if I can unit test the retrieval of this. – Dragon Wolf Dec 05 '17 at 17:00

1 Answers1

1

After more digging and researching, found this in stackoverflow. Again, not a pro, lol, and didn't even know what proper keywords to use, but I found this, and I will give it a go! This seems likes its exactly what I was looking for, a unit test way to mock up a guid for a user.

Mocking Guid.NewGuid()

Dragon Wolf
  • 61
  • 1
  • 2
  • 11