Exemplo n.º 1
0
        public dynamic Save()
        {
            var viewModel = this.Bind <ConnectionViewModel>();

            var validationResult = _connectionValidator.Validate(viewModel);

            if (!validationResult.Success)
            {
                return(Response.AsJson <ValidationResult>(validationResult));
            }

            ConnectionModel model = _mapper.Map <ConnectionViewModel, ConnectionModel>(viewModel);

            model.Id = Guid.NewGuid();

            // encrypt the password if it's set
            if (!String.IsNullOrEmpty(model.Password))
            {
                byte[] key = _encryptionProvider.NewKey();
                model.Key      = Convert.ToBase64String(key);
                model.Password = _encryptionProvider.SimpleEncrypt(model.Password, key);
            }

            // add the connection and return success
            _userStore.Connections.Add(model);
            _userStore.Save();

            return(Response.AsJson <ValidationResult>(new ValidationResult()));
        }
        public void Validate_IsValid_ReturnsSuccess(WorkflowStoreType storeType)
        {
            ConnectionModel model = DataHelper.CreateConnectionModel(storeType);

            ValidationResult result = _connectionValidator.Validate(model);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(0, result.Messages.Count);
        }
Exemplo n.º 3
0
        public void Save_AuthTest()
        {
            // setup
            var currentUser = new UserIdentity()
            {
                Id = Guid.NewGuid(), UserName = "******"
            };
            var browser      = CreateBrowser(currentUser);
            var connectionId = Guid.NewGuid();

            ConnectionModel connection = new ConnectionModel()
            {
                Id = connectionId
            };

            _userStore.Connections.Returns(new List <ConnectionModel>()
            {
                connection
            });
            _userStore.GetConnection(connectionId).Returns(connection);

            foreach (string claim in Claims.AllClaims)
            {
                _connectionValidator.Validate(Arg.Any <ConnectionViewModel>()).Returns(new ValidationResult());

                currentUser.Claims = new string[] { claim };

                // execute
                var response = browser.Post(Actions.Connection.Save, (with) =>
                {
                    with.HttpRequest();
                    with.FormsAuth(currentUser.Id, new Nancy.Authentication.Forms.FormsAuthenticationConfiguration());
                    with.FormValue("id", connectionId.ToString());
                });

                // assert
                if (claim == Claims.ConnectionAdd)
                {
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                }
                else
                {
                    Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode);
                }
            }
        }