Exemplo n.º 1
0
            public async Task ShouldCreateUser()
            {
                UserCreationDeserializer user = new UserCreationDeserializer()
                {
                    FirstName   = "Jhon",
                    LastName    = "Smith",
                    Email       = (DateTimeOffset.Now.ToUnixTimeSeconds().ToString()) + "*****@*****.**",
                    Password    = "******",
                    PhoneNumber = "0619198793",
                    DateOfBirth = DateTime.Today,
                    Location    = "Fake_address"
                };

                string json = JsonConvert.SerializeObject(user, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                HttpResponseMessage response = await HttpClient.PostAsync("api/user", new StringContent(json, System.Text.Encoding.UTF8, "application/json"));

                string stringResult = await response.Content.ReadAsStringAsync();

                User obj = JsonConvert.DeserializeObject <User>(stringResult);

                Assert.Equal(HttpStatusCode.Created, response.StatusCode);
                Assert.NotNull(obj);
                Assert.Equal(obj.Email, user.Email);
                Assert.Equal("Foreigner", obj.Role.Name);
            }
Exemplo n.º 2
0
        public User CreateUser(UserCreationDeserializer userDsl)
        {
            int phone;

            int.TryParse(userDsl.PhoneNumber, out phone);
            var promotion = PromotionServices.GetForeignPromotion();

            Role role;

            GetRole(null, out role);

            string password;

            GeneratePasswordSha256(userDsl.Password, out password);

            User user = Context.User.Add(new User()
            {
                FirstName   = userDsl.FirstName,
                LastName    = userDsl.LastName,
                Email       = userDsl.Email,
                Password    = password,
                PhoneNumber = phone,
                Promotion   = promotion,
                DateOfBirth = userDsl.DateOfBirth,
                Role        = role,
                Location    = userDsl.Location
            }).Entity;

            Context.SaveChanges();
            return(user);
        }
Exemplo n.º 3
0
        public IActionResult Create([FromBody] UserCreationDeserializer userFromRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ;
            User userFromModel;

            try {
                userFromModel = UserServices.CreateUser(userFromRequest);
            } catch (DbUpdateException e) {
                string message = e.InnerException.Message;
                return(BadRequest(message));
            }

            if (userFromModel == null)
            {
                return(BadRequest("Error"));
            }

            return(new CreatedAtRouteResult(null, userFromModel));
        }