public void ExecuteMethodReturnsCorrectResultWithCorrectInput()
        {
            var mockedDatabase = new Mock <IDatabase>();
            var mockedFactory  = new Mock <IWIMFactory>();

            var sut = new CreateMember(mockedFactory.Object, mockedDatabase.Object);

            var mockedMember = new Mock <IMember>();

            var parameters = new List <string>()
            {
                "IvanIvanov"
            };
            var fakeListMembers = new List <IMember>();

            mockedDatabase.Setup(x => x.ListAllMembers).Returns(fakeListMembers);

            mockedFactory.Setup(x => x.CreateMember("IvanIvanov")).Returns(mockedMember.Object);

            mockedDatabase.Setup(x => x.AddMemberToList(mockedMember.Object));

            var expected = string.Format(GlobalConstants.MemberCreated, "IvanIvanov");

            var mockedActivity = new Mock <IActivity>();

            mockedFactory.Setup(x => x.CreateActivity(expected, mockedMember.Object));

            mockedMember.Setup(x => x.AddMemberActivity(mockedActivity.Object));

            Assert.AreEqual(expected, sut.Execute(parameters));
        }
        public void ExecuteMethodPassThrowAddMemberToListMethod()
        {
            var mockedDatabase = new Mock <IDatabase>();
            var mockedFactory  = new Mock <IWIMFactory>();

            var sut = new CreateMember(mockedFactory.Object, mockedDatabase.Object);

            var mockedMember = new Mock <IMember>();

            var parameters = new List <string>()
            {
                "IvanIvanov"
            };
            var fakeListMembers = new List <IMember>();

            mockedDatabase.Setup(x => x.ListAllMembers).Returns(fakeListMembers);

            mockedFactory.Setup(x => x.CreateMember("IvanIvanov")).Returns(mockedMember.Object);

            var expected = string.Format(GlobalConstants.MemberCreated, "IvanIvanov");

            var mockedActivity = new Mock <IActivity>();

            mockedFactory.Setup(x => x.CreateActivity(expected, mockedMember.Object));

            mockedMember.Setup(x => x.AddMemberActivity(mockedActivity.Object));

            sut.Execute(parameters);
            mockedDatabase.Verify(x => x.AddMemberToList(mockedMember.Object), Times.Once);
        }
Exemplo n.º 3
0
        public void CreateMember(User ur)
        {
            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = "Data Source = localhost; Initial Catalog = Annonser; Integrated Security = SSPI;";
            conn.Open();

            if (ur.FirstName != "" || ur.Email != "")
            {
                string sql = "insert into [User](Firstname,Lastname,Email,Username,Password) values (@Firstname, @Lastname, @Email, @Username, @Password)";
                sql += "Select cast(@@identity as int)";

                SqlCommand cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@Firstname", ur.FirstName);
                cmd.Parameters.AddWithValue("@Lastname", ur.LastName);
                cmd.Parameters.AddWithValue("@Email", ur.Email);
                cmd.Parameters.AddWithValue("@Username", ur.Username);
                cmd.Parameters.AddWithValue("@Password", ur.Password);

                int newID = (int)cmd.ExecuteScalar();
                UserID = newID;

                CreateMember cm = new CreateMember();
                cm.Close();
            }
            else
            {
                MessageBox.Show("Uppgifter saknas försök igen");
            }
        }
Exemplo n.º 4
0
        public IActionResult OnPost(CreateMember record)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Connect to Database
            DBConnection  dbstring     = new DBConnection();
            string        DbConnection = dbstring.DbString();
            SqlConnection conn         = new SqlConnection(DbConnection);

            conn.Open();

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection  = conn;
                command.CommandText = @"INSERT INTO Member (Username, FirstName, LastName, Email, Password, Role, SecurityQuestion, SecurityAnswer) VALUES (@User, @FName, @LName, @UEmail, @Pass, @URole, @SQ, @SA)";


                // Add book to database
                command.Parameters.AddWithValue("@User", record.Username);
                command.Parameters.AddWithValue("@FName", record.FirstName);
                command.Parameters.AddWithValue("@LName", record.LastName);
                command.Parameters.AddWithValue("@UEmail", record.Email);


                // Encypt the Password

                // Chand, M. (2020, April 16). Compute SHA256 Hash In C#. Retrieved from c-sharpcorner: https://www.c-sharpcorner.com/article/compute-sha256-hash-in-c-sharp/
                string HashedPassword = "";

                using (SHA256 sha256Hash = SHA256.Create())
                {
                    // Get a Byte array
                    byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(record.Password));

                    // Convert Byte array to string
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        builder.Append(bytes[i].ToString("x2"));
                    }

                    HashedPassword = builder.ToString();
                }
                command.Parameters.AddWithValue("@Pass", HashedPassword);
                command.Parameters.AddWithValue("@URole", "Customer");
                command.Parameters.AddWithValue("@S1", record.SecurityQuestion);
                command.Parameters.AddWithValue("@S1", record.SecurityAnswer);



                command.ExecuteNonQuery();
            }

            return(RedirectToPage("Login"));
        }
Exemplo n.º 5
0
        public ActionResult CreateMember(ContentModel contentModel)
        {
            var model = new CreateMember(contentModel?.Content);

            model.Metadata = new ViewMetadata
            {
                PageTitle   = model.Name,
                Description = model.Description
            };

            return(View("CreateMember", model));
        }
Exemplo n.º 6
0
        public OperationResult Create(CreateMember command)
        {
            var operation = new OperationResult();

            if (_memberRepository.Exist(x => x.Mobile == command.Mobile))
            {
                return(operation.Failed(ApplicationMessages.DuplicatedRecord));
            }
            var member = new Member(command.FullName, command.Mobile);

            _memberRepository.Create(member);
            _memberRepository.SaveChanges();
            return(operation.Succedded());
        }
        public IActionResult OnPost(CreateMember NewUser)
        {
            // Connect to Database
            DBConnection  dbstring     = new DBConnection();
            string        DbConnection = dbstring.DbString();
            SqlConnection conn         = new SqlConnection(DbConnection);

            conn.Open();


            using (SqlCommand command = new SqlCommand())

            {
                //Updates the password of the user with the new password
                command.Connection  = conn;
                command.CommandText = @"UPDATE Member SET Password = @UPass WHERE Id = @UID";

                // Encypt the Password

                // Chand, M. (2020, April 16). Compute SHA256 Hash In C#. Retrieved from c-sharpcorner: https://www.c-sharpcorner.com/article/compute-sha256-hash-in-c-sharp/
                string HashedPassword = "";

                using (SHA256 sha256Hash = SHA256.Create())
                {
                    // Get a Byte array
                    byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(NewUser.Password));

                    // Convert Byte array to string
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        builder.Append(bytes[i].ToString("x2"));
                    }

                    HashedPassword = builder.ToString();
                }
                command.Parameters.AddWithValue("@UPass", HashedPassword);
                command.Parameters.AddWithValue("@UID", HttpContext.Session.GetInt32(SessionKeyName1));


                command.ExecuteNonQuery();

                // clear session
                HttpContext.Session.Clear();

                // let user login
                return(RedirectToPage("../../Browser/Login"));
            }
        }
        public void ExecuteMethodThrowExeptionWhenInputParametersCountIsSmaller()
        {
            var mockedDatabase = new Mock <IDatabase>();
            var mockedFactory  = new Mock <IWIMFactory>();

            var sut = new CreateMember(mockedFactory.Object, mockedDatabase.Object);

            var parameters = new List <string>()
            {
            };

            var ex = Assert.ThrowsException <ArgumentException>(() => sut.Execute(parameters));

            Assert.AreEqual(GlobalConstants.ParametersCountInvalid, ex.Message);
        }
        public void ExecuteMethodReturnsCorrectMessageWhenMemberAlreadyExistsDatabase()
        {
            var mockedDatabase = new Mock <IDatabase>();
            var mockedFactory  = new Mock <IWIMFactory>();

            var fakeList = new List <IMember>();

            mockedDatabase.Setup(x => x.ListAllMembers).Returns(fakeList);

            var command      = new CreateMember(mockedFactory.Object, mockedDatabase.Object);
            var mockedMember = new Mock <IMember>();

            mockedMember.Setup(x => x.Name).Returns("IvanIvanov");
            fakeList.Add(mockedMember.Object);

            var parameters = new List <string> {
                "IvanIvanov"
            };
            var expected = string.Format(GlobalConstants.MemberAlreadyExist, "IvanIvanov");

            Assert.AreEqual(expected, command.Execute(parameters));
        }
Exemplo n.º 10
0
        public JsonResult OnPostCreate(CreateMember command)
        {
            var result = _memberApplication.Create(command);

            return(new JsonResult(result));
        }