public VehicleControllerTests()
        {
            _context = new MockContext();
            _context.Users.Add(new User
            {
                Id = 1,
                Username = "******"
            });

            var make = new Make
            {
                Id = 1,
                Name = "make"
            };

            var model = new Model
            {
                Id = 1,
                Name = "model",
                Make = make
            };

            var user = new User
            {
                Id = 1,
                Email = "Test"
            };

            make.Models = new List<Model> {model};

            _context.Models.Add(model);
            _context.Makes.Add(make);

            _context.Vehicles.Add(new Vehicle
            {
                Id = 1,
                Year = "2015",
                Kilometers = 100,
                Model = model,
                User = user
            });
            _context.Vehicles.Add(new Vehicle
            {
                Id = 2,
                Year = "2014",
                Kilometers = 1000,
                Model = model,
                User = user
            });

            user.Vehicles = new List<Vehicle>();
            user.Vehicles.Add(_context.Vehicles.First());
            user.Vehicles.Add(_context.Vehicles.Last());
        }
Ejemplo n.º 2
0
        public void AddUser(User user, string password)
        {
            if (string.IsNullOrWhiteSpace(user.Username))
                throw new ArgumentException("Username required");
            if (string.IsNullOrWhiteSpace(password))
                throw new ArgumentException("Password required");
            if(_context.Users.Any(u => u.Username == user.Username))
                throw new ArgumentException("Username already taken");

            var salt = _encrpytor.GetSalt();
            user.Password = _encrpytor.GetPassword(salt, password);
            user.Salt = salt;

            _context.Users.Add(user);
            _context.SaveChanges();
        }