public async Task GetUserByIdAsyncTest()
        {

            // Create the user
            var created = await UserHelper.CreateNewUserAsync();
            // Setup user token
            string token = await UserHelper.AuthenticateAsync(created.Username, created.Password);
            
            // Get the created user
            GetUserResponse getUserResponse = null;
            IUserService userService = new UserService();
            App.SetLoggedInUser(token);
            var getUserRequest = new GetUserRequest() { UserId = created.Id };
            getUserResponse = await userService.GetUserAsync(getUserRequest);
            ApiHelper.EnsureValidResponse(getUserResponse);
            Assert.IsNotNull(getUserResponse.User);

            Assert.IsTrue(getUserResponse.User.Username == created.Username);
            Assert.IsTrue(getUserResponse.User.FirstName == created.FirstName);
            Assert.IsTrue(getUserResponse.User.LastName == created.LastName);
            Assert.IsTrue(getUserResponse.User.Email == created.Email);
            Assert.IsTrue(getUserResponse.User.DateOfBirth == created.DateOfBirth);
            Assert.IsTrue(getUserResponse.User.Phone == created.Phone);
            var user2 = getUserResponse.User;
            Console.WriteLine("Username: {0}", user2.Username);
            Console.WriteLine("Firstname: {0}", user2.FirstName);
            Console.WriteLine("Lastname: {0}", user2.LastName);
            Console.WriteLine("Email: {0}", user2.Email);
            Console.WriteLine("Birthdate: {0}", user2.DateOfBirth);
            Console.WriteLine("Phone: {0}", user2.Phone);

        }
        public static async Task<User> CreateNewUserAsync(User user = null)
        {
            // Create user
            user = user ?? new User()
            {
                Username = "******" + Unique.String,                  // ensure unique user name
                Email = "john.doe@" + Unique.String + ".com",           // unique but useless email address
                Password = "******",
                DateOfBirth = DateTime.Today.AddYears(-25),
                FirstName = "John",
                LastName = "Doe",
                Phone = "987-654-3210",
                Location = new Geocode(18, 19)
            };

            Console.WriteLine("Creating new user with username {0}.", user.Username);

            IUserService userService = new UserService();
            var createRequest = new CreateUserRequest() { User = user };
            var createResponse = await userService.CreateUserAsync(createRequest);
            var created = createResponse.User;
            Assert.IsNotNull(created, "Initial user creation failed.");
            Console.WriteLine("Created new user with username {0} and id {1}.", created.Username, created.Id);
            // Setup the password
            created.Password = user.Password;
            return created;
        }
 public static async Task<string> AuthenticateAsync(string username, string password)
 {
     Console.WriteLine("Authenticating user with username {0} and password {1}", username, password);
     IUserService userService = new UserService();
     var authRequest = new AuthenticateUserRequest();
     authRequest["username"] = username;
     authRequest["password"] = password;
     var authResponse = await userService.AuthenticateAsync(authRequest);
     ApiHelper.EnsureValidResponse(authResponse);
     return authResponse.Token;
 }
 public static async Task<User> GetExistingUserAsync(string id)
 {
     Console.WriteLine("Getting existing user with id {0}.", id);
     IUserService userService = new UserService();
     var getRequest = new GetUserRequest() { UserId = id };
     var getResponse = await userService.GetUserAsync(getRequest);
     Assert.IsNotNull(getResponse, "Cannot get updated user for user id {0}.", id);
     Assert.IsNotNull(getResponse.Status, "Status for get user call is null.");
     if (getResponse.Status.IsSuccessful == false)
         Assert.Fail(getResponse.Status.Message);
     Assert.IsNotNull(getResponse.User, "Get user for id {0} returned null.", id);
     return getResponse.User;
 }
        public async Task AuthenticateWithUsernamePasswordTestAsync()
        {

            // Create the user
            var user = await UserHelper.CreateNewUserAsync();
            IUserService userService = new UserService();
            var authRequest = new AuthenticateUserRequest();
            authRequest["username"] = user.Username;
            authRequest["password"] = user.Password;
            var authResponse = await userService.AuthenticateAsync(authRequest);
            ApiHelper.EnsureValidResponse(authResponse);
            Assert.IsTrue(string.IsNullOrWhiteSpace(authResponse.Token) == false, "Auth token is not valid.");
            Assert.IsNotNull(authResponse.User, "Authenticated user is null.");
            Console.WriteLine("Logged in user id: {0}", authResponse.User.Id);
            Console.WriteLine("Session token: {0}", authResponse.Token);
        }
        public async Task CreateUserAsyncTest()
        {
            var user = new User()
            {
                Username = "******" + Unique.String,                  // ensure unique user name
                Email = "john.doe@" + Unique.String + ".com",           // unique but useless email address
                Password = "******",
                DateOfBirth = DateTime.Today.AddYears(-25),
                FirstName = "John",
                LastName = "Doe",
                Phone = "987-654-3210",
                Location = new Geocode(18, 19)
            };

            IUserService userService = new UserService();

            // Create user
            var request = new CreateUserRequest() { User = user };
            var response = await userService.CreateUserAsync(request);
            ApiHelper.EnsureValidResponse(response);
            Assert.IsNotNull(response.User, "User in response is null.");
            Assert.IsTrue(string.IsNullOrWhiteSpace(response.User.Id) == false);
            Console.WriteLine("Created user with id {0}.", response.User.Id);
        }
        public async Task DeleteUserAsyncTest()
        {
            // Create new user
            var newUser = await UserHelper.CreateNewUserAsync();

            // Authenticate the user
            var token = await UserHelper.AuthenticateAsync(newUser.Username, newUser.Password);
            App.SetLoggedInUser(token);

            // Delete the user
            var request = new DeleteUserRequest() { UserId = newUser.Id };
            IUserService userService = new UserService();
            var response = await userService.DeleteUserAsync(request);
            ApiHelper.EnsureValidResponse(response);

            // Try to get the user
            var getResponse = await userService.GetUserAsync(new GetUserRequest() { UserId = newUser.Id });
            ApiHelper.EnsureValidResponse(getResponse, false);
            Assert.IsFalse(getResponse.Status.IsSuccessful, "Get for an non-existant user did not fail.");
            Console.WriteLine("Get user error message: {0}", getResponse.Status.Message);
        }
        public async Task ChangePasswordWithTokenTest()
        {
            // Create user
            var newUser = await UserHelper.CreateNewUserAsync();

            // Authenticate with existing password
            var token = await UserHelper.AuthenticateAsync(newUser.Username, newUser.Password);
            App.SetLoggedInUser(token);

            // Change password
            var newPassword = "******";
            var request = new ChangePasswordRequest() { UserId = token, IdType = "token", OldPassword = newUser.Password, NewPassword = newPassword };
            IUserService userService = new UserService();
            var response = await userService.ChangePasswordAsync(request);
            ApiHelper.EnsureValidResponse(response);

            // Authenticate with new password
            token = await UserHelper.AuthenticateAsync(newUser.Username, newPassword);
            Assert.IsTrue(string.IsNullOrWhiteSpace(token) == false, "Authentication failed for username {0} and password {1}.", newUser.Username, newPassword);
        }
        public async Task UpdateUserAsyncTest()
        {
            // Create user
            var created = await UserHelper.CreateNewUserAsync();

            // Get auth token
            var token = await UserHelper.AuthenticateAsync(created.Username, created.Password);
            App.SetLoggedInUser(token);

            // Update user
            created.Username = "******" + Unique.String;
            created.Email = "john.doe@" + Unique.String + ".com";
            created.DateOfBirth = DateTime.Today.AddYears(-30);
            created.FirstName = "John_updated";
            created.LastName = "Doe_updated";
            created.Phone = "999-888-1234";
            created.Location = new Geocode(20, 21);

            var updateRequest = new UpdateUserRequest() { UserId = created.Id };
            updateRequest.PropertyUpdates["username"] = created.Username;
            updateRequest.PropertyUpdates["email"] = created.Email;
            updateRequest.PropertyUpdates["firstname"] = created.FirstName;
            updateRequest.PropertyUpdates["lastname"] = created.LastName;
            updateRequest.PropertyUpdates["phone"] = created.Phone;
            updateRequest.PropertyUpdates["location"] = created.Location.ToString();
            updateRequest.PropertyUpdates["birthdate"] = created.DateOfBirth.Value.ToString(Formats.BirthDate);
            IUserService userService = new UserService();
            var response = await userService.UpdateUserAsync(updateRequest);

            // Ensure fields are updated
            Assert.IsNotNull(response, "Update user response is null.");
            Assert.IsNotNull(response.Status, "Update user response.status is null.");
            if (response.Status.IsSuccessful == false)
                Assert.Fail(response.Status.Message);
            Assert.IsNotNull(response.User);


            // Get updated user (just to be sure)
            var updated = await UserHelper.GetExistingUserAsync(created.Id);
            Console.WriteLine("Matching existing with updated user.");
            UserHelper.MatchUsers(updated, created);
        }