public async Task CorrectlyRemoveOldRoleWithValidId() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "CorrectlyRemoveOldRoleWithValidId") .Options; var user = new User() { Id = "valid-user" }; var role = new IdentityUserRole <string>() { UserId = "valid-user", RoleId = "2" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.UserRoles.Add(role); context.SaveChanges(); var userService = new UserService(context); var result = await userService.PromoteUser("valid-user"); Assert.IsNull(context.UserRoles.FirstOrDefault(ur => ur.UserId == result.Id && ur.RoleId == "2")); Assert.AreEqual(1, context.UserRoles.Count()); } }
public async Task CorrectlyUnLockWithValidParameters() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "CorrectlyUnLockWithValidParameters") .Options; var user = new User() { Id = "test-user-id", Locked = true }; var role = new IdentityUserRole <string>() { RoleId = "2", UserId = "test-user-id" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.UserRoles.Add(role); context.SaveChanges(); var userService = new UserService(context); var result = await userService.UnLockUser("test-user-id"); Assert.IsFalse(result.Locked); Assert.IsNotNull(context.Users.FirstOrDefault(us => !us.Locked && us.Id == "test-user-id")); } }
public async Task ThrowEntityNotFoundException_WhenCardWithGivenIdIsNotFoundInDatabase() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ThrowEntityNotFoundException_WhenCardWithGivenIdIsNotFoundInDatabase") .Options; var currencyServiceMock = new Mock <ICurrencyRateApiService>(); string userId = "userId"; string cardId = "cardId"; double amountInUserCurrency = 50.05; string description = "1234567890"; using (var context = new CasinoContext(contextOptions)) { var usre = new User() { Id = userId }; context.SaveChanges(); var transactionService = new TransactionService(context, currencyServiceMock.Object); await Assert.ThrowsExceptionAsync <EntityNotFoundException>(() => transactionService.AddWithdrawTransaction(userId, cardId, amountInUserCurrency, description)); } }
public async Task RetrieveWalletIfExists() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "RetrieveWalletIfExists") .Options; var user = new User() { Id = "valid-user" }; var wallet = new Wallet() { Id = "wallet-test", User = user, CurrencyId = 1 }; using (var context = new CasinoContext(contextOptions)) { context.Wallets.Add(wallet); context.SaveChanges(); var walletService = new WalletService(context); var result = await walletService.RetrieveWallet("valid-user"); Assert.IsNotNull(result); Assert.AreEqual("wallet-test", result.Id); Assert.AreEqual(1, result.CurrencyId); } }
public async Task CreateWalletIfNoneFound() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "CreateWalletIfNoneFound") .Options; var user = new User() { Id = "test-user-id", IsDeleted = false }; var currency = new Currency() { Id = 1, Name = "Test-currency" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.Currencies.Add(currency); context.SaveChanges(); var walletService = new WalletService(context); var result = await walletService.CreateWallet("test-user-id", 1); Assert.IsNotNull(result); Assert.AreEqual("test-user-id", result.UserId); Assert.AreEqual(1, result.CurrencyId); } }
public async Task ThrowIfWalletAlreadyExists() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ThrowIfWalletAlreadyExists") .Options; var user = new User() { Id = "test-user-id", IsDeleted = false }; var wallet = new Wallet() { Id = "test-wallet", User = user }; using (var context = new CasinoContext(contextOptions)) { context.Wallets.Add(wallet); context.SaveChanges(); var walletService = new WalletService(context); await Assert.ThrowsExceptionAsync <EntityAlreadyExistsException> (async() => await walletService.CreateWallet("test-user-id", 1)); } }
public async Task ThrowIfUserIsAdmin() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ThrowIfUserIsAdmin") .Options; var user = new User() { Id = "invalid-user", Locked = false }; var role = new IdentityUserRole <string>() { RoleId = "1", UserId = "invalid-user" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.UserRoles.Add(role); context.SaveChanges(); var userService = new UserService(context); await Assert.ThrowsExceptionAsync <InvalidAdministratorOperationException> (async() => await userService.LockUser("invalid-user")); } }
public IActionResult Register(User newUser) { if (ModelState.IsValid) { if (db.Users.Any(user => user.Email == newUser.Email)) { ModelState.AddModelError("Email", "Email is already in use"); return(View("Index")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); db.Add(newUser); db.SaveChanges(); HttpContext.Session.SetInt32("UserId", newUser.UserId); HttpContext.Session.SetString("FirstName", newUser.FirstName); HttpContext.Session.SetInt32("Balance", newUser.Balance); return(RedirectToAction("Dashboard", new { userId = newUser.UserId })); } else { return(View("Index")); } }
public async Task ReturnTransactionByType() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ReturnTransactionByType") .Options; var currencyServiceMock = new Mock <ICurrencyRateApiService>(); string userId = "id"; double originalAmount = 1; var newBankCard = new BankCard() { Id = "id1", }; string description = "1234567890"; var transactionTypeTest = new TransactionType() { Id = 1, Name = "Win" }; var newTransaction = new Transaction() { UserId = userId, OriginalAmount = originalAmount, Description = description, TransactionType = transactionTypeTest, Card = newBankCard }; using (var context = new CasinoContext(contextOptions)) { var transactionService = new TransactionService(context, currencyServiceMock.Object); await context.Transactions.AddAsync(newTransaction); context.BankCards.Add(newBankCard); context.SaveChanges(); var transActionTypeCount = await transactionService .GetTransactionByType("Win") .ToAsyncEnumerable() .Count(); Assert.AreEqual(1, transActionTypeCount); } }
public async Task ThrowIfRoleNotFound() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ThrowIfRoleNotFound") .Options; var user = new User() { Id = "valid-user" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.SaveChanges(); var userService = new UserService(context); await Assert.ThrowsExceptionAsync <EntityNotFoundException> (async() => await userService.PromoteUser("valid-user")); } }
public async Task ThrowIfFoundUserIsDeleted() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "EditUserAliasShouldThrowIfFoundUserIsDeleted") .Options; var user = new User() { Id = "test-user-id", IsDeleted = true }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.SaveChanges(); var userService = new UserService(context); await Assert.ThrowsExceptionAsync <EntityNotFoundException> (async() => await userService.EditUserAlias("test-user-alias", "test-user-id")); } }
public async Task ThrowIfCurrencyNotFound() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ThrowIfCurrencyNotFound") .Options; var user = new User() { Id = "test-user-id", IsDeleted = false }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.SaveChanges(); var walletService = new WalletService(context); await Assert.ThrowsExceptionAsync <EntityNotFoundException> (async() => await walletService.CreateWallet("test-user-id", 1)); } }
public async Task CorrectlyEditAlias() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "EditUserShouldCorrectlyEditAlias") .Options; var user = new User() { Id = "test-user-id", IsDeleted = false, Alias = "before" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.SaveChanges(); var userService = new UserService(context); var result = await userService.EditUserAlias("after", "test-user-id"); Assert.AreEqual("after", result.Alias); Assert.IsTrue(context.Users.Where(u => u.Alias == "after").Count() == 1); } }
public async Task ReturnCorrectUser() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ReturnCorrectUser") .Options; var user = new User() { Id = "valid-user", IsDeleted = false, Alias = "Test" }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(user); context.SaveChanges(); var userService = new UserService(context); var result = await userService.RetrieveUser("valid-user"); Assert.IsNotNull(result); Assert.AreEqual("Test", result.Alias); } }
public async Task ReturnListOfAllUsersThatAreNotDeleted() { var contextOptions = new DbContextOptionsBuilder <CasinoContext>() .UseInMemoryDatabase(databaseName: "ReturnListOfAllUsersThatAreNotDeleted") .Options; var validUserOne = new User() { Id = "validUserOne", IsDeleted = false }; var validUserTwo = new User() { Id = "validUserTwo", IsDeleted = false }; var invalidUser = new User() { Id = "invalidUser", IsDeleted = true }; using (var context = new CasinoContext(contextOptions)) { context.Users.Add(validUserOne); context.Users.Add(validUserTwo); context.Users.Add(invalidUser); context.SaveChanges(); var userService = new UserService(context); var result = await userService.GetAllUsers(); Assert.IsTrue(result.Count() == 2); Assert.IsTrue(result.FirstOrDefault(us => us.Id == validUserOne.Id) != null); Assert.IsTrue(result.FirstOrDefault(us => us.Id == validUserTwo.Id) != null); Assert.IsFalse(result.FirstOrDefault(us => us.Id == invalidUser.Id) != null); } }