public async Task Put_Should_Update_Return_Url() { ClientReturnUrl toUpdateReturnUrl = null; using (var context = new DaOAuthContext(_dbContextOptions)) { toUpdateReturnUrl = context.ClientReturnUrls.Where(c => c.ClientId.Equals(_sammyClient.Id)).FirstOrDefault(); } Assert.IsNotNull(toUpdateReturnUrl); var toUpdateReturnUrlDto = new UpdateReturnUrlDto() { IdReturnUrl = toUpdateReturnUrl.Id, ReturnUrl = "http://updated.return.url" }; var httpResponseMessage = await _client.PutAsJsonAsync("returnUrl", toUpdateReturnUrlDto); Assert.IsTrue(httpResponseMessage.IsSuccessStatusCode); ClientReturnUrl updatedReturnUrl = null; using (var context = new DaOAuthContext(_dbContextOptions)) { updatedReturnUrl = context.ClientReturnUrls.Where(c => c.Id.Equals(toUpdateReturnUrl.Id)).SingleOrDefault(); } Assert.IsNotNull(updatedReturnUrl); Assert.AreNotEqual(toUpdateReturnUrl.ReturnUrl, updatedReturnUrl.ReturnUrl); Assert.AreEqual(toUpdateReturnUrlDto.ReturnUrl, updatedReturnUrl.ReturnUrl); }
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 async Task Post_Should_Create_Client() { var toCreateClient = new CreateClientDto() { ClientType = _confidentialClientType.Wording, Description = "a new client for logged user !", Name = "logged user's client", ReturnUrls = new List <string>() { "http://return.com" }, ScopesIds = new List <int>() { _scope1.Id, _scope2.Id } }; var httpResponseMessage = await _client.PostAsJsonAsync("clients", toCreateClient); 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 clientId = Int32.Parse(location.Split('/', StringSplitOptions.RemoveEmptyEntries).Last()); Client myNewClient = null; ClientReturnUrl myClientReturnUrl = null; IList <ClientScope> myClientScopes = null; using (var context = new DaOAuthContext(_dbContextOptions)) { myNewClient = context.Clients.Where(c => c.Id.Equals(clientId)).SingleOrDefault(); myClientReturnUrl = context.ClientReturnUrls.Where(c => c.ClientId.Equals(clientId)).SingleOrDefault(); myClientScopes = context.ClientsScopes.Where(c => c.ClientId.Equals(clientId)).ToList(); } Assert.IsNotNull(myNewClient); Assert.AreEqual(toCreateClient.Description, myNewClient.Description); Assert.AreEqual(toCreateClient.Name, myNewClient.Name); Assert.IsTrue(myNewClient.IsValid); Assert.IsTrue(myNewClient.PublicId.Length > 0); Assert.IsTrue(myNewClient.ClientSecret.Length > 0); Assert.AreEqual(DateTime.Now.Date, myNewClient.CreationDate.Date); Assert.IsNotNull(myClientReturnUrl); Assert.AreEqual(toCreateClient.ReturnUrls.First(), myClientReturnUrl.ReturnUrl); Assert.IsNotNull(myClientScopes); Assert.IsTrue(toCreateClient.ScopesIds.Count() > 0); Assert.AreEqual(toCreateClient.ScopesIds.Count(), myClientScopes.Count()); foreach (var scopeId in toCreateClient.ScopesIds) { Assert.IsTrue(myClientScopes.Select(cs => cs.ScopeId).Contains(scopeId)); } }
public void Update(ClientReturnUrl toUpdate) { var ru = FakeDataBase.Instance.ClientReturnUrls.Where(x => x.Id.Equals(toUpdate.Id)).FirstOrDefault(); if (ru != null) { ru.ReturnUrl = toUpdate.ReturnUrl; } }
public void Delete(ClientReturnUrl toDelete) { var ru = FakeDataBase.Instance.ClientReturnUrls.Where(x => x.Id.Equals(toDelete.Id)).FirstOrDefault(); if (ru != null) { FakeDataBase.Instance.ClientReturnUrls.Remove(ru); } }
public int Add(ClientReturnUrl toAdd) { if (FakeDataBase.Instance.ClientReturnUrls.Count() == 0) { toAdd.Id = 1; } else { toAdd.Id = FakeDataBase.Instance.ClientReturnUrls.Max(u => u.Id) + 1; } FakeDataBase.Instance.ClientReturnUrls.Add(toAdd); return(toAdd.Id); }
public async Task Delete_Should_Delete_Return_Url() { ClientReturnUrl nonDeletedReturnUrl = null; using (var context = new DaOAuthContext(_dbContextOptions)) { nonDeletedReturnUrl = context.ClientReturnUrls.Where(c => c.ClientId.Equals(_sammyClient.Id)).FirstOrDefault(); } Assert.IsNotNull(nonDeletedReturnUrl); var httpResponseMessage = await _client.DeleteAsync($"returnUrl/{nonDeletedReturnUrl.Id}"); Assert.IsTrue(httpResponseMessage.IsSuccessStatusCode); ClientReturnUrl deletedReturnUrl = null; using (var context = new DaOAuthContext(_dbContextOptions)) { deletedReturnUrl = context.ClientReturnUrls.Where(c => c.Id.Equals(nonDeletedReturnUrl.Id)).FirstOrDefault(); } Assert.IsNull(deletedReturnUrl); }
public void Init() { _validUserCreator = new User() { CreationDate = DateTime.Now, EMail = "*****@*****.**", FullName = "Sammy le Crabe", Id = 646, IsValid = true, Password = new byte[] { 0 }, UserName = "******" }; _validUserNonCreator = new User() { CreationDate = DateTime.Now, EMail = "*****@*****.**", FullName = "Jack le Crabe", Id = 7215, IsValid = true, Password = new byte[] { 0 }, UserName = "******" }; _invalidUser = new User() { CreationDate = DateTime.Now, EMail = "*****@*****.**", FullName = "Nop le Crabe", Id = 2765, IsValid = false, Password = new byte[] { 0 }, UserName = "******" }; _validClient = new Client() { ClientSecret = "abcdefghijklmnopqrstuv", ClientTypeId = 1, CreationDate = DateTime.Now, Description = "test", Id = 777, IsValid = true, Name = "test", PublicId = "abc", UserCreatorId = _validUserCreator.Id }; _anotherValidClient = new Client() { ClientSecret = "fghjzertukh", ClientTypeId = 1, CreationDate = DateTime.Now, Description = "test again", Id = 778, IsValid = true, Name = "test again", PublicId = "def", UserCreatorId = _validUserCreator.Id }; _invalidClient = new Client() { ClientSecret = "abcdefghijklmnopqrstuv", ClientTypeId = 1, CreationDate = DateTime.Now, Description = "test_invalid", Id = 776, IsValid = false, Name = "test_invalid", PublicId = "abc_invalid", UserCreatorId = _invalidUser.Id }; FakeDataBase.Instance.Clients.Add(_validClient); FakeDataBase.Instance.Clients.Add(_invalidClient); FakeDataBase.Instance.Clients.Add(_anotherValidClient); FakeDataBase.Instance.Users.Add(_validUserCreator); FakeDataBase.Instance.Users.Add(_validUserNonCreator); FakeDataBase.Instance.Users.Add(_invalidUser); _clientReturnUrl = new ClientReturnUrl() { Id = 6131, ClientId = _validClient.Id, ReturnUrl = "http://www.perdu.com" }; FakeDataBase.Instance.ClientReturnUrls.Add(_clientReturnUrl); _scope = new Scope() { Id = 2245, RessourceServerId = 1, Wording = "test", NiceWording = "test" }; FakeDataBase.Instance.Scopes.Add(_scope); FakeDataBase.Instance.ClientsScopes.Add(new ClientScope() { Id = 154, ClientId = _validClient.Id, ScopeId = _scope.Id }); FakeDataBase.Instance.UsersClient.Add(new UserClient() { ClientId = _invalidClient.Id, CreationDate = DateTime.Now, Id = 9595, IsActif = true, UserId = _validUserCreator.Id }); FakeDataBase.Instance.UsersClient.Add(new UserClient() { ClientId = _validClient.Id, CreationDate = DateTime.Now, Id = 378, IsActif = true, UserId = _validUserCreator.Id }); FakeDataBase.Instance.UsersClient.Add(new UserClient() { ClientId = _validClient.Id, CreationDate = DateTime.Now, Id = 3784, IsActif = true, UserId = _validUserNonCreator.Id }); FakeDataBase.Instance.UsersClient.Add(new UserClient() { ClientId = _validClient.Id, CreationDate = DateTime.Now, Id = 3785, IsActif = true, UserId = _invalidUser.Id }); _service = new ClientService() { Configuration = FakeConfigurationHelper.GetFakeConf(), RepositoriesFactory = new FakeRepositoriesFactory(), StringLocalizerFactory = new FakeStringLocalizerFactory(), Logger = new FakeLogger(), RandomService = new FakeRandomService(1, _generatedClientSecret) }; _repo = new FakeClientRepository(); }
public int CreateClient(CreateClientDto toCreate) { IList <ValidationResult> ExtendValidation(CreateClientDto toValidate) { var resource = this.GetErrorStringLocalizer(); IList <ValidationResult> result = new List <ValidationResult>(); if (toValidate.ReturnUrls == null || toValidate.ReturnUrls.Count() == 0) { result.Add(new ValidationResult(resource["CreateClientDtoDefaultReturnUrlRequired"])); } foreach (var ur in toValidate.ReturnUrls) { if (!Uri.TryCreate(ur, UriKind.Absolute, out var u)) { result.Add(new ValidationResult(resource["CreateClientDtoReturnUrlIncorrect"])); } } if (toValidate.ClientType != ClientTypeName.Confidential && toValidate.ClientType != ClientTypeName.Public) { result.Add(new ValidationResult(resource["CreateClientDtoTypeIncorrect"])); } using (var context = RepositoriesFactory.CreateContext()) { var userRepo = RepositoriesFactory.GetUserRepository(context); var user = userRepo.GetByUserName(toValidate.UserName); if (user == null || !user.IsValid) { result.Add(new ValidationResult(String.Format(resource["CreateClientDtoInvalidUser"], toCreate.UserName))); } var userClientRepo = RepositoriesFactory.GetUserClientRepository(context); if (!String.IsNullOrEmpty(toValidate.Name) && userClientRepo.GetAllByCriteriasCount(toValidate.UserName, toValidate.Name, null, null) > 0) { result.Add(new ValidationResult(resource["CreateClientDtoNameAlreadyUse"])); } } return(result); } Logger.LogInformation(String.Format("Try to create client by user {0}", toCreate != null ? toCreate.UserName : String.Empty)); this.Validate(toCreate, ExtendValidation); using (var context = RepositoriesFactory.CreateContext()) { var returnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context); var clientRepo = RepositoriesFactory.GetClientRepository(context); var userClientRepo = RepositoriesFactory.GetUserClientRepository(context); var userRepo = RepositoriesFactory.GetUserRepository(context); var clientScopeRepo = RepositoriesFactory.GetClientScopeRepository(context); var user = userRepo.GetByUserName(toCreate.UserName); var client = new Client() { ClientSecret = RandomService.GenerateRandomString(16), ClientTypeId = toCreate.ClientType.Equals(ClientTypeName.Confidential, StringComparison.OrdinalIgnoreCase) ? (int)EClientType.CONFIDENTIAL : (int)EClientType.PUBLIC, CreationDate = DateTime.Now, Description = toCreate.Description, IsValid = true, Name = toCreate.Name, PublicId = RandomService.GenerateRandomString(16), UserCreatorId = user.Id }; clientRepo.Add(client); foreach (var ur in toCreate.ReturnUrls) { var clientReturnUrl = new ClientReturnUrl() { ReturnUrl = ur, ClientId = client.Id }; returnUrlRepo.Add(clientReturnUrl); } var userClient = new UserClient() { ClientId = client.Id, CreationDate = DateTime.Now, IsActif = true, RefreshToken = String.Empty, UserId = user.Id }; userClientRepo.Add(userClient); if (toCreate.ScopesIds != null) { foreach (var sId in toCreate.ScopesIds) { clientScopeRepo.Add(new ClientScope() { ClientId = client.Id, ScopeId = sId }); } } context.Commit(); return(client.Id); } }