public async Task UnknownUser()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username = "******",
                Password = "******"
            };
            BesteUserAuthentificationResponse authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.USER_UNKNOWN);

            ModifyUserResponse response = await besteUser.EditUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.USER_UNKNOWN);

            response = await besteUser.ChangePasswordByUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.USER_UNKNOWN);

            response = await besteUser.DeleteUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.USER_UNKNOWN);
        }
        public async Task CreateUserAndDelete()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            User loginUser = new User
            {
                Username = user.Username,
                Password = user.Password
            };

            response = await besteUser.DeleteUser(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);
        }
        public async Task CreateUserAndChangePasswortBreakRules()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            User loginUser = new User
            {
                Username = user.Username,
                Password = "******"
            };

            response = await besteUser.ChangePasswordByUser(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.PASSWORD_GUIDELINES_ERROR);
        }
        internal async static Task ChangePassword(WebSocketHandler webSocketHandler)
        {
            Command resonseCommand = await ModifyUser(async() => {
                return(await BesteUser.ChangePasswordByUser(webSocketHandler.ReceivedCommand.CommandData.ToString()));
            },
                                                      "ChangePassword",
                                                      webSocketHandler);

            await webSocketHandler.Send(resonseCommand);
        }
        internal async static Task GetUser(WebSocketHandler webSocketHandler)
        {
            Command resonseCommand = await GetUsers(async() =>
            {
                return(await BesteUser.GetUser(webSocketHandler.ReceivedCommand.CommandData.ToString()));
            },
                                                    "GetUser",
                                                    webSocketHandler);

            await webSocketHandler.Send(resonseCommand);
        }
        public async Task CreateUserMissingParams()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User();

            user.Username = "******";
            user.Password = "******";
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.MISSING_USER_PARAMS);
        }
        public async Task WrongParameters()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username = "",
                Password = ""
            };
            BesteUserAuthentificationResponse authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.WRONG_PARAMETER);
        }
        private static async Task <ModifySettingsResponse> EditServerSetting(ServerSetting serverSettings, User user, string token)
        {
            ServerSetting dbServerSetting = await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                serverSettings.TableId = TABLE_ID;
                return(await context.LoadAsync(serverSettings));
            });

            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                try
                {
                    if (dbServerSetting == null)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SETTING_NOT_FOUND);
                    }
                    if (SDaysTDieServerHandler.IsServerRunningBySeed(dbServerSetting.WorldGenSeed))
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.SERVER_MUST_BE_STOPPED);
                    }
                    QueryResponse response = await GetServerSettingBySpecificProperty("WorldGenSeed", serverSettings.WorldGenSeed);
                    if (response.Items.Count > 0 && Convert.ToInt32(response.Items[0]["Id"].N.ToString()) != dbServerSetting.Id)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.GAME_SEED_ALREADY_EXISTS);
                    }
                    string oldUserId = dbServerSetting.UserUuid;
                    GetUsersResponse getUsersResponse = await BesteUser.GetUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
                    if (getUsersResponse.Result != GetUsersResult.SUCCESS)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.USER_NOT_FOUND);
                    }
                    User dbUser = getUsersResponse.Users[0];
                    serverSettings.UserUuid = dbUser.Uuid;
                    serverSettings.CopyAllButId(dbServerSetting);
                    if (oldUserId != getUsersResponse.Users[0].Uuid)
                    {
                        await AddOrUpdateServerSettingRight("EditServerSettings", serverSettings.Id, dbUser.Uuid, token);
                    }
                    await context.SaveAsync(dbServerSetting);
                    return new ModifySettingsResponse(ModifySettingsResult.SETTING_EDITED);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString() + "\n" + ex.StackTrace);
                    return new ModifySettingsResponse(ModifySettingsResult.EXCEPTION);
                }
            }));
        }
        public async Task CreateUserWrongPasswordGuidelines()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User();

            user.Username  = "******";
            user.Lastname  = "Lastname";
            user.Firstname = "Firstname";
            user.Email     = "Email";
            user.Password  = "******";
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.PASSWORD_GUIDELINES_ERROR);
        }
        public async Task CreateUserAndLogin()
        {
            BesteUser besteUser = new BesteUser();

            User user = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };

            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            User loginUser = new User
            {
                Username = user.Username,
                Password = user.Password
            };
            BesteUserAuthentificationResponse authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.MUST_CHANGE_PASSWORT);

            response = await besteUser.ChangePasswordByUser(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.SUCCESS);
        }
        public async Task CreateUserAndWrongPasswortCounter()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            User loginUser = new User();

            loginUser.Username = user.Username;
            loginUser.Password = user.Password + "1";

            BesteUserAuthentificationResponse authResponse;

            for (int i = 0; i < 13; i++)
            {
                authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));

                ValiateResponse(authResponse, BesteUserAuthentificationResult.WRONG_PASSWORD);
            }

            loginUser.Password = user.Password;
            authResponse       = await besteUser.Authenticate(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.WRONG_PASSWORD_COUNTER_TOO_HIGH);
        }
        public async Task CreateUserAndEdit()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            User loginUser = new User
            {
                Username           = "******",
                Lastname           = "Lastname",
                Firstname          = "Firstname",
                Email              = "Email",
                Password           = "******",
                MustChangePassword = false
            };

            response = await besteUser.EditUser(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            BesteUserAuthentificationResponse authResponse = await besteUser.Authenticate(JsonConvert.SerializeObject(loginUser, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(authResponse, BesteUserAuthentificationResult.SUCCESS);
        }
        internal async static Task HandleLogin(WebSocketHandler webSocketHandler)
        {
            User user = JsonConvert.DeserializeObject <User>(webSocketHandler.ReceivedCommand.CommandData.ToString());
            BesteUserAuthentificationResponse response = await BesteUser.Authenticate(webSocketHandler.ReceivedCommand.CommandData.ToString());

            if (response.Result == BesteUserAuthentificationResult.SUCCESS ||
                response.Result == BesteUserAuthentificationResult.MUST_CHANGE_PASSWORT)
            {
                webSocketHandler.User = response.UserData;
                List <PureRight> pureRights = new List <PureRight>();
                pureRights.Add(new PureRight
                {
                    Authorized     = true,
                    Operation      = "ChangePassword_" + webSocketHandler.User.Username,
                    RecourceModule = "User"
                });
                pureRights.Add(new PureRight
                {
                    Authorized     = true,
                    Operation      = "EditUser_" + webSocketHandler.User.Username,
                    RecourceModule = "User"
                });
                pureRights.Add(new PureRight
                {
                    Authorized     = true,
                    Operation      = "DeleteUser_" + webSocketHandler.User.Username,
                    RecourceModule = "User"
                });
                pureRights.Add(new PureRight
                {
                    Authorized     = true,
                    Operation      = "GetUser_" + webSocketHandler.User.Username,
                    RecourceModule = "User"
                });
                webSocketHandler.ConnectedUserToken = await RightControl.Register(webSocketHandler.User.Uuid, pureRights);
            }
            Command resonseCommand = new Command("AuthentificationResponse", response);
            await webSocketHandler.Send(resonseCommand);
        }
        private static async Task <ModifySettingsResponse> AddServerSetting(ServerSetting serverSettings, User user, string token)
        {
            return(await AmazonDynamoDBFactory.ExecuteInTransactionContext(async (client, context) =>
            {
                try
                {
                    QueryResponse response = await GetServerSettingBySpecificProperty("WorldGenSeed", serverSettings.WorldGenSeed);
                    if (response.Items.Count > 0)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.GAME_SEED_ALREADY_EXISTS);
                    }
                    GetUsersResponse getUsersResponse = await BesteUser.GetUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
                    if (getUsersResponse.Result != GetUsersResult.SUCCESS)
                    {
                        return new ModifySettingsResponse(ModifySettingsResult.USER_NOT_FOUND);
                    }
                    serverSettings.TableId = TABLE_ID;
                    serverSettings.UserUuid = getUsersResponse.Users[0].Uuid;
                    serverSettings.ServerConfigFilepath = "";
                    serverSettings.ServerPort = 0;
                    serverSettings.TelnetPassword = "";
                    serverSettings.TelnetPort = 0;
                    serverSettings.TerminalWindowEnabled = false;
                    serverSettings.Id = await GenerateNewServerSettingsId();

                    await CreateRightsForNewServerSettings(serverSettings, user, token);

                    await context.SaveAsync(serverSettings);
                    return new ModifySettingsResponse(ModifySettingsResult.SETTING_ADDED);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString() + "\n" + ex.StackTrace);
                    return new ModifySettingsResponse(ModifySettingsResult.EXCEPTION);
                }
            }));
        }
        public async Task ForcedJsonSerializationErrors()
        {
            BesteUser          besteUser = new BesteUser();
            ModifyUserResponse response  = await besteUser.CreateUser("no json]");

            ValiateResponse(response, ModifyUserResult.JSON_ERROR);

            response = await besteUser.ChangePasswordByUser("no json]");

            ValiateResponse(response, ModifyUserResult.JSON_ERROR);

            response = await besteUser.DeleteUser("no json]");

            ValiateResponse(response, ModifyUserResult.JSON_ERROR);

            response = await besteUser.EditUser("no json]");

            ValiateResponse(response, ModifyUserResult.JSON_ERROR);

            BesteUserAuthentificationResponse authResponse = await besteUser.Authenticate("no json]");

            ValiateResponse(authResponse, BesteUserAuthentificationResult.JSON_ERROR);
        }
        public async Task CreateDuplicateUser()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.USER_ALREADY_EXISTS);
        }
        public async Task GetUsers()
        {
            BesteUser besteUser = new BesteUser();
            User      user      = new User
            {
                Username  = "******",
                Lastname  = "Lastname",
                Firstname = "Firstname",
                Email     = "A_C_Email",
                Password  = "******"
            };
            ModifyUserResponse response = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            user.Username = "******";
            user.Email    = "A_B_Email";
            response      = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            user.Username = "******";
            user.Email    = "A_A_Email";
            response      = await besteUser.CreateUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(response, ModifyUserResult.SUCCESS);

            GetUsersParams   getUsersParams  = new GetUsersParams(10, 0, SortUsersBy.USERNAME);
            GetUsersResponse getUserResponse = await besteUser.GetUsers(JsonConvert.SerializeObject(getUsersParams, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(getUserResponse, GetUsersResult.SUCCESS);
            if (getUserResponse.Users.Count < 3)
            {
                Assert.Fail("getUserResponse.Users.Count < 3");
            }
            if (getUserResponse.Users[0].Username != "A_A_User")
            {
                Assert.Fail("getUserResponse.Users[0].Username != 'A_A_User'");
            }

            getUsersParams  = new GetUsersParams(10, 1, SortUsersBy.USERNAME);
            getUserResponse = await besteUser.GetUsers(JsonConvert.SerializeObject(getUsersParams, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(getUserResponse, GetUsersResult.SUCCESS);
            if (getUserResponse.Users.Count < 2)
            {
                Assert.Fail("getUserResponse.Users.Count < 2");
            }
            if (getUserResponse.Users[0].Username != "A_B_User")
            {
                Assert.Fail("getUserResponse.Users[0].Username != 'A_B_User'");
            }

            getUsersParams  = new GetUsersParams(1, 1, SortUsersBy.USERNAME);
            getUserResponse = await besteUser.GetUsers(JsonConvert.SerializeObject(getUsersParams, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(getUserResponse, GetUsersResult.SUCCESS);
            if (getUserResponse.Users.Count != 1)
            {
                Assert.Fail("getUserResponse.Users.Count != 1");
            }
            if (getUserResponse.Users[0].Username != "A_B_User")
            {
                Assert.Fail("getUserResponse.Users[0].Username != 'A_B_User'");
            }

            getUsersParams  = new GetUsersParams(10, 2, SortUsersBy.EMAIL);
            getUserResponse = await besteUser.GetUsers(JsonConvert.SerializeObject(getUsersParams, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(getUserResponse, GetUsersResult.SUCCESS);

            if (getUserResponse.Users[0].Email != "A_C_Email")
            {
                Assert.Fail("getUserResponse.Users[0].Email != 'A_C_Email'");
            }

            getUserResponse = await besteUser.GetUser(JsonConvert.SerializeObject(user, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));

            ValiateResponse(getUserResponse, GetUsersResult.SUCCESS);
            if (getUserResponse.Users[0].Email != "A_A_Email")
            {
                Assert.Fail("getUserResponse.Users[0].Email != 'A_A_Email'");
            }
        }