public async Task Post_Should_Create_Return_Url() { var toCreateReturnUrl = new CreateReturnUrlDto() { ClientPublicId = _sammyClient.PublicId, ReturnUrl = "http://back.to.test" }; var httpResponseMessage = await _client.PostAsJsonAsync("returnUrl", toCreateReturnUrl); Assert.AreEqual(HttpStatusCode.Created, httpResponseMessage.StatusCode); Assert.IsTrue(httpResponseMessage.Headers.Contains("location")); var location = httpResponseMessage.Headers.GetValues("location").Single(); Assert.IsTrue(location.Length > 0); var returnUrlId = Int32.Parse(location.Split('/', StringSplitOptions.RemoveEmptyEntries).Last()); ClientReturnUrl myReturnUrl = null; Client myClient = null; using (var context = new DaOAuthContext(_dbContextOptions)) { myReturnUrl = context.ClientReturnUrls.Where(c => c.Id.Equals(returnUrlId)).SingleOrDefault(); myClient = context.Clients.Where(c => c.PublicId.Equals(toCreateReturnUrl.ClientPublicId)).SingleOrDefault(); } Assert.IsNotNull(myReturnUrl); Assert.IsNotNull(myClient); Assert.AreEqual(toCreateReturnUrl.ClientPublicId, myClient.PublicId); Assert.AreEqual(toCreateReturnUrl.ReturnUrl, myReturnUrl.ReturnUrl); }
public IActionResult Post(CreateReturnUrlDto toCreate) { toCreate.UserName = User.Identity.Name; var createdId = _service.CreateReturnUrl(toCreate); var currentUrl = UriHelper.GetDisplayUrl(Request); return(Created($"{currentUrl}/{createdId}", null)); }
public int CreateReturnUrl(CreateReturnUrlDto toCreate) { this.Validate(toCreate); var idCreated = 0; var resource = this.GetErrorStringLocalizer(); if (!Uri.TryCreate(toCreate.ReturnUrl, UriKind.Absolute, out var u)) { throw new DaOAuthServiceException(resource["CreateReturnUrlReturnUrlIncorrect"]); } using (var context = RepositoriesFactory.CreateContext()) { var userRepo = RepositoriesFactory.GetUserRepository(context); var user = userRepo.GetByUserName(toCreate.UserName); if (user == null || !user.IsValid) { throw new DaOAuthServiceException(resource["CreateReturnUrlInvalidUser"]); } var ucRepo = RepositoriesFactory.GetUserClientRepository(context); var uc = ucRepo.GetUserClientByClientPublicIdAndUserName(toCreate.ClientPublicId, toCreate.UserName); if (uc == null) { throw new DaOAuthServiceException(resource["CreateReturnUrlBadUserNameOrClientId"]); } var clientRepo = RepositoriesFactory.GetClientRepository(context); var client = clientRepo.GetByPublicId(toCreate.ClientPublicId); if (client == null || !client.IsValid) { throw new DaOAuthServiceException(resource["CreateReturnUrlInvalidClient"]); } var existingReturnUrl = client.ClientReturnUrls.FirstOrDefault(c => c.ReturnUrl.Equals(toCreate.ReturnUrl, StringComparison.OrdinalIgnoreCase)); if (existingReturnUrl != null) { idCreated = existingReturnUrl.Id; } else { var returnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context); idCreated = returnUrlRepo.Add(new Domain.ClientReturnUrl() { ClientId = client.Id, ReturnUrl = toCreate.ReturnUrl }); } context.Commit(); } return(idCreated); }