using Centron.BusinessLogic; using Centron.BusinessLogic.Administration.Logins; using Centron.BusinessLogic.EmployeeArea; using Centron.BusinessLogic.WebServices.Administration.Logins; using Centron.Data.Entities.Administration.Logins; using Centron.Data.WebServices.Administration.Login; using Centron.Tests.EndToEnd.Infrastructure; using System; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.WebAccounts { public class WebAccountTests : EndToEndTest { public WebAccountTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { this.Verifier.Settings.IgnoreProperty(f => f.CreatedVersion); this.Verifier.Settings.IgnoreProperty(f => f.ChangedVersion); } public override void Execute() { this.GetAllWebAccounts("1_AllWebAccounts_Original"); this.CreateNewWebAccount("2"); this.GetAllWebAccounts("2_AllWebAccounts_AfterCreateNewWebAccount"); this.UpdateWebAccount("3"); this.GetAllWebAccounts("3_AllWebAccounts_AfterWebAccountUpdate"); this.UpdateWebAccountPassword("4"); this.GetAllWebAccounts("4_AllWebAccounts_AfterUpdatePassword"); this.TryLoginWithWebAccount(); this.RenameWebAccountToExistingName("5"); this.CreateNewWebAccountWithDuplicateName("6"); this.RenameWebAccountToExistingEmployeeName("7"); } private void GetAllWebAccounts(string name) { using (var session = new BLSession()) { var webAccounts = session.GetBL().GetAllWebAccounts(); this.Verifier.Verify(name, webAccounts); } } private void CreateNewWebAccount(string namePrefix) { using (var session = new BLSession()) { // This references AccountAddressContact 1051, Tobias Koerner var webAccount = new WebAccountDTO { Username = "my-user", Password = "supersecret", Type = WebAccountDTO.TypeAddressContact, TypeI3D = 87, //The same as the AddressContactI3D AccountI3D = 1027, AccountAddressI3D = 1040, AccountAddressContactI3D = 1051, CustomerI3D = 10024, AddressI3D = 67, AddressContactI3D = 87, Status = 1, //1 is active, 0 is deactivated UseTwoFactorAuthentication = true, TwoFactorValidDurationInDays = 10, }; var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); var saveResult = session.GetBL().SaveWebAccount(webAccount, loggedInUser); this.Verifier.Verify($"{namePrefix}_CreateNewAccountResult", saveResult); } } private void UpdateWebAccount(string namePrefix) { WebAccountDTO webAccount; using (var session = new BLSession()) { var webAccountResult = session.GetBL().GetWebAccountByContactPersonI3D(87); this.Verifier.Verify($"{namePrefix}_GetWebAccountByContactPersonI3D_87", webAccountResult); webAccount = webAccountResult.Data; } using (var session = new BLSession()) { var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); webAccount.Username = "my-new-username"; var saveResult = session.GetBL().SaveWebAccount(webAccount, loggedInUser); this.Verifier.Verify($"{namePrefix}_SaveWebAccount_Result", saveResult); } } private void UpdateWebAccountPassword(string namePrefix) { WebAccountDTO webAccount; using (var session = new BLSession()) { var webAccountResult = session.GetBL().GetWebAccountByContactPersonI3D(87); webAccount = webAccountResult.Data; } using (var session = new BLSession()) { var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); var updatePasswordResult = session.GetBL().UpdatePassword(webAccount.I3D, "my-new-password-super-secret", loggedInUser); this.Verifier.Verify($"{namePrefix}_UpdatePassword_Result", updatePasswordResult); } } private void TryLoginWithWebAccount() { // Correct username and password const string Username = "my-new-username"; const string Password = "my-new-password-super-secret"; const int WebAccountI3D = 9; // Test-Methods static void LoginSuccess(string username, string password) => TryLogin(username, password, expectSuccess: true); static void LoginError(string username, string password) => TryLogin(username, password, expectSuccess: false); static void TryLogin(string username, string password, bool expectSuccess) { using var session = new BLSession(); var webAccount = session.GetBL().LoginWithWebAccount(username, password); if (expectSuccess) { Assert.NotNull(webAccount); Assert.Equal(WebAccountI3D, webAccount.I3D); } else { Assert.Null(webAccount); } } // Login works correctly when username and password are correct LoginSuccess(Username, Password); // Login doesn't work if incorrect username LoginError(Username + "bla", Password); // Login doesn't work if incorrect password LoginError(Username, Password + "bla"); // Login doesn't work if web-account is deactivated this.Sql($"UPDATE dbo.WebAccounts SET Status = 0 WHERE I3D = {WebAccountI3D}"); LoginError(Username, Password); // Don't forget to reset the value for the next tests, and make sure login works again this.Sql($"UPDATE dbo.WebAccounts SET Status = 1 WHERE I3D = {WebAccountI3D}"); LoginSuccess(Username, Password); // Login doesn't work if contact-person is deactivated this.Sql($"UPDATE dbo.Personen SET Status = 0 WHERE I3D = (SELECT PersonenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginError(Username, Password); // Don't forget to reset the value for the next tests, and make sure login works again this.Sql($"UPDATE dbo.Personen SET Status = 1 WHERE I3D = (SELECT PersonenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginSuccess(Username, Password); // Login doesn't work if address is deactivated this.Sql($"UPDATE dbo.Anschrif SET Status = 0 WHERE I3D = (SELECT AnschriftI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginError(Username, Password); // Don't forget to reset the value for the next tests, and make sure login works again this.Sql($"UPDATE dbo.Anschrif SET Status = 1 WHERE I3D = (SELECT AnschriftI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginSuccess(Username, Password); // Login doesn't work if customer is deactivated this.Sql($"UPDATE dbo.Kunden SET Status = 0 WHERE I3D = (SELECT KundenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginError(Username, Password); // Don't forget to reset the value for the next tests, and make sure login works again this.Sql($"UPDATE dbo.Kunden SET Status = 1 WHERE I3D = (SELECT KundenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginSuccess(Username, Password); // Login doesn't work if customer is locked this.Sql($"UPDATE dbo.Kunden SET Gesperrt = 1 WHERE I3D = (SELECT KundenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginError(Username, Password); // Don't forget to reset the value for the next tests, and make sure login works again this.Sql($"UPDATE dbo.Kunden SET Gesperrt = 0 WHERE I3D = (SELECT KundenI3D FROM dbo.WebAccounts WHERE I3D = {WebAccountI3D})"); LoginSuccess(Username, Password); } private void RenameWebAccountToExistingName(string namePrefix) { WebAccountDTO webAccount; using (var session = new BLSession()) { var webAccountResult = session.GetBL().GetWebAccountByContactPersonI3D(87); webAccount = webAccountResult.Data; } using (var session = new BLSession()) { var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); webAccount.Username = "admin123"; //An username that already exists var saveResult = session.GetBL().SaveWebAccount(webAccount, loggedInUser); this.Verifier.Verify($"{namePrefix}_UpdateWebAccount_DuplicateName_Result", saveResult); } } private void CreateNewWebAccountWithDuplicateName(string namePrefix) { using (var session = new BLSession()) { // This references AccountAddressContact 1051, Tobias Koerner var webAccount = new WebAccountDTO { Username = "admin123", //A username that already exists Password = "supersecret", // Type = 2, //Always hardcoded to 2 // TypeI3D = 87, //The same as the AddressContactI3D // // AccountI3D = 1027, // AccountAddressI3D = 1040, // AccountAddressContactI3D = 1051, // // CustomerI3D = 10024, // AddressI3D = 67, // AddressContactI3D = 87, // // Status = 1 //1 is active, 0 is deactivated }; var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); var saveResult = session.GetBL().SaveWebAccount(webAccount, loggedInUser); this.Verifier.Verify($"{namePrefix}_CreateNewAccount_DuplicateName_Result", saveResult); } } private void RenameWebAccountToExistingEmployeeName(string namePrefix) { WebAccountDTO webAccount; using (var session = new BLSession()) { var webAccountResult = session.GetBL().GetWebAccountByContactPersonI3D(87); webAccount = webAccountResult.Data; } using (var session = new BLSession()) { var currentUser = session.GetBL().GetAppUser(f => f.I3D == 11); var loggedInUser = new LoggedInUser(currentUser); webAccount.Username = "admin"; //An employee username that already exists var saveResult = session.GetBL().SaveWebAccount(webAccount, loggedInUser); this.Verifier.Verify($"{namePrefix}_UpdateWebAccount_DuplicateName_Result", saveResult); } } } }