예제 #1
0
        public void TestAddRemove()
        {
            UserDatabase userDb = new UserDatabase(_settingsLookup[ConnectionKey]);

            GetTestUser(out var name, out User user);

            // Test User is not there
            Assert.IsNotNull(userDb.LoadedItems);
            int           numberInitialItems = userDb.LoadedItems.Count;
            List <string> userNames          = userDb.LoadedItems.ToList().Select(x => x.Name).ToList();

            Assert.IsFalse(userNames.Contains(name));

            // Add new User
            userDb.AddNewItemToDatabase(user);
            userDb.ConnectToDatabase();
            Assert.AreNotEqual(numberInitialItems, userDb.LoadedItems.Count);
            Assert.AreEqual(1, userDb.LoadedItems.Count - numberInitialItems);
            List <string> userNames2 = userDb.LoadedItems.ToList().Select(x => x.Name).ToList();

            Assert.IsTrue(userNames2.Contains(name));

            // Remove the new user
            userDb.RemoveItemFromDatabase(user);
            userDb.ConnectToDatabase();

            Assert.AreEqual(userDb.LoadedItems.Count, numberInitialItems);
            List <string> userNames3 = userDb.LoadedItems.ToList().Select(x => x.Name).ToList();

            Assert.IsFalse(userNames3.Contains(name));
        }
예제 #2
0
        public IActionResult Post([FromBody] UserAddRequest addRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            UserAddResponse response = new UserAddResponse
            {
                ErrorCode  = (int)UserResponseCode.Success,
                Name       = addRequest.Name,
                FailReason = "",
                UserId     = ""
            };

            if (_userDatabase.LoadedItems.Any(x => x.Name == addRequest.Name))
            {
                response.ErrorCode  = (int)UserResponseCode.DuplicateName;
                response.FailReason = "Cannot Add as User name already exists";
                return(Ok(response));
            }

            if (_userDatabase.LoadedItems.Any(x => x.Email == addRequest.Email))
            {
                response.ErrorCode  = (int)UserResponseCode.DuplicateEmail;
                response.FailReason = "Cannot Add as User e-mail already exists";
                return(Ok(response));
            }

            User newUser = new User(addRequest.Name, addRequest.Password, addRequest.Email, addRequest.Description);

            _userDatabase.AddNewItemToDatabase(newUser);

            response.UserId = newUser.Id.ToString();
            return(Ok(response));
        }
예제 #3
0
        /// <summary>
        /// Adds a new user login.
        /// </summary>
        /// <param name="addRequest">The new user login to try to add.</param>
        /// <returns>The action result.</returns>
        public UserAddResponse AddNewUser(UserAddRequest addRequest)
        {
            UserAddResponse response = new UserAddResponse
            {
                ErrorCode  = (int)UserResponseCode.Success,
                Name       = addRequest.Name,
                FailReason = "",
                UserId     = ""
            };

            if (_userDatabase.LoadedItems.Any(x => x.Name == addRequest.Name))
            {
                response.ErrorCode  = (int)UserResponseCode.DuplicateName;
                response.FailReason = "Cannot Add as User name already exists";
                return(response);
            }

            if (_userDatabase.LoadedItems.Any(x => x.Email == addRequest.Email))
            {
                response.ErrorCode  = (int)UserResponseCode.DuplicateEmail;
                response.FailReason = "Cannot Add as User e-mail already exists";
                return(response);
            }

            User newUser =
                new User(addRequest.Name, addRequest.Password, addRequest.Email, addRequest.Description);

            _userDatabase.AddNewItemToDatabase(newUser);

            // Send the authorization e-mail.
            SendAuthorizationEmail(newUser);

            // return the unverified e-mail to the user display
            response.UserId = newUser.Id.ToString();
            return(response);
        }