public Task<bool> CreateToken(TokenModel model) { if (!model.IsValid()) { return Task.FromResult(false); } if (!_appRepository.Any(x => x.Id == model.AppId)) { return Task.FromResult(false); } var entity = new Token { CreatedBy = model.CreatedBy, AppId = model.AppId, Key = model.Token, UsageCount = 0, IsAppActive = true }; _tokenRepository.Create(entity); _tokenRepository.SaveChanges(); if (!_tokenRepository.SaveChanges()) Task.FromResult(true); return Task.FromResult(true); }
public JsonResult NewToken(int appId) { var token = new TokenModel { CreationDate = DateTime.Now, CreationDateStr = DateTime.Now.ToString("f"), UsageCount = 1, Token = Guid.NewGuid().ToString().Replace("-", "") }; return Json(token, JsonRequestBehavior.DenyGet); }
public async Task<JsonResult> NewToken(int appId) { var result = new ResponseModel { Ok = false }; var token = new TokenModel { CreationDate = DateTime.Now, UsageCount = 0, Token = Guid.NewGuid().ToNoDashString(), AppId = appId, CreatedBy = User.Identity.GetUserId() }; var isOk = await _appService.CreateToken(token); if (!isOk) return Json(result, JsonRequestBehavior.DenyGet); result.Ok = true; result.Result = token; return Json(result, JsonRequestBehavior.DenyGet); }
public async void create_token_should_return_true_if_token_model_is_valid() { //arrange var validModel = new TokenModel { AppId = 1, Token = "token" }; var appEntity = new App { Id = 1, Name = "testApp", Url = "test.com", UserEmail = "*****@*****.**" }; var appRepository = new Mock<IRepository<App>>(); var tokenRepository = new Mock<IRepository<Token>>(); appRepository.Setup(x => x.Any(It.IsAny<Expression<Func<App, bool>>>())).Returns(true); tokenRepository.Setup(x => x.Create(It.IsAny<Token>())).Returns(new Token { Id = 1 }); tokenRepository.Setup(x => x.SaveChanges()).Returns(true); //act var sut = new AppServiceBuilder().WithAppRepository(appRepository.Object) .WithTokenRepository(tokenRepository.Object) .Build(); var result = await sut.CreateToken(validModel); //assert Assert.IsTrue(result); Assert.IsInstanceOf<IAppService>(sut); }
public async void create_token_should_return_true_if_app_is_exist() { //arrange var validModel = new TokenModel { AppId = 1, Token = "token" }; var appRepository = new Mock<IRepository<App>>(); var tokenRepository = new Mock<IRepository<Token>>(); appRepository.Setup(x => x.Any(It.IsAny<Expression<Func<App, bool>>>())).Returns(true); tokenRepository.Setup(x => x.Create(It.IsAny<Token>())).Returns(new Token { Id = 1 }); tokenRepository.Setup(x => x.SaveChanges()).Returns(true); //act var sut = new AppServiceBuilder().WithAppRepository(appRepository.Object) .WithTokenRepository(tokenRepository.Object) .Build(); var result = await sut.CreateToken(validModel); //assert Assert.IsTrue(result); Assert.IsAssignableFrom<bool>(result); Assert.IsInstanceOf<IAppService>(sut); appRepository.Verify(x => x.Any(It.IsAny<Expression<Func<App, bool>>>()), Times.Once); tokenRepository.Verify(x => x.Create(It.IsAny<Token>()), Times.Once); tokenRepository.Verify(x => x.SaveChanges(), Times.AtLeastOnce); }
public async void create_token_should_return_false_if_token_model_is_invalid() { //arrange var invalidModel = new TokenModel(); //act var sut = new AppServiceBuilder().Build(); var result = await sut.CreateToken(invalidModel); //assert Assert.IsFalse(result); Assert.IsInstanceOf<IAppService>(sut); }