public async Task When_An_Exception_Is_Raised_While_Attempting_To_Create_A_Client_Then_Exception_Is_Thrown()  
        {
            // ARRANGE
            const string clientId = "client_id";
            const string code = "code";
            const string message = "message";
            var client = new SimpleIdentityServer.Core.Models.Client
            {
                ClientId = clientId
            };
            var parameter = new UpdateClientParameter
            {
                ClientId = clientId
            };
            InitializeFakeObjects();
            _clientRepositoryStub.Setup(c => c.GetClientByIdAsync(It.IsAny<string>()))
                .Returns(Task.FromResult(client));
            _generateClientFromRegistrationRequestStub.Setup(g => g.Execute(It.IsAny<UpdateClientParameter>()))
                .Callback(() =>
                {
                    throw new IdentityServerException(code, message);
                });

            // ACT & ASSERT
            var exception = await Assert.ThrowsAsync<IdentityServerManagerException>(() => _updateClientAction.Execute(parameter));
            Assert.True(exception.Code == code);
            Assert.True(exception.Message == message);
        }
Esempio n. 2
0
        public async Task <bool> Execute(UpdateClientParameter updateClientParameter)
        {
            if (updateClientParameter == null)
            {
                throw new ArgumentNullException(nameof(updateClientParameter));
            }

            var existedClient = await _clientRepository.GetClientByIdAsync(updateClientParameter.ClientId);

            if (existedClient == null)
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode,
                                                         string.Format(ErrorDescriptions.TheClientDoesntExist, updateClientParameter.ClientId));
            }

            SimpleIdentityServer.Core.Models.Client client = null;
            try
            {
                client = _generateClientFromRegistrationRequest.Execute(updateClientParameter);
            }
            catch (IdentityServerException ex)
            {
                throw new IdentityServerManagerException(ex.Code, ex.Message);
            }

            client.ClientId      = existedClient.ClientId;
            client.AllowedScopes = updateClientParameter.AllowedScopes == null
                ? new List <Scope>()
                : updateClientParameter.AllowedScopes.Select(s => new Scope
            {
                Name = s
            }).ToList();
            return(await _clientRepository.UpdateAsync(client));
        }
Esempio n. 3
0
        public async Task <bool> Execute(UpdateClientParameter updateClientParameter)
        {
            if (updateClientParameter == null)
            {
                throw new ArgumentNullException(nameof(updateClientParameter));
            }

            if (string.IsNullOrWhiteSpace(updateClientParameter.ClientId))
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode, string.Format(ErrorDescriptions.MissingParameter, "client_id"));
            }

            _managerEventSource.StartToUpdateClient(JsonConvert.SerializeObject(updateClientParameter));
            var existedClient = await _clientRepository.GetClientByIdAsync(updateClientParameter.ClientId);

            if (existedClient == null)
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode, string.Format(ErrorDescriptions.TheClientDoesntExist, updateClientParameter.ClientId));
            }

            SimpleIdentityServer.Core.Common.Models.Client client = null;
            try
            {
                client = _generateClientFromRegistrationRequest.Execute(updateClientParameter);
            }
            catch (IdentityServerException ex)
            {
                throw new IdentityServerManagerException(ex.Code, ex.Message);
            }

            client.ClientId      = existedClient.ClientId;
            client.AllowedScopes = updateClientParameter.AllowedScopes == null
                ? new List <Scope>()
                : updateClientParameter.AllowedScopes.Select(s => new Scope
            {
                Name = s
            }).ToList();
            var existingScopes = await _scopeRepository.SearchByNamesAsync(client.AllowedScopes.Select(s => s.Name));

            var notSupportedScopes = client.AllowedScopes.Where(s => !existingScopes.Any(sc => sc.Name == s.Name)).Select(s => s.Name);

            if (notSupportedScopes.Any())
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode, string.Format(ErrorDescriptions.TheScopesDontExist, string.Join(",", notSupportedScopes)));
            }

            var result = await _clientRepository.UpdateAsync(client);

            if (!result)
            {
                throw new IdentityServerManagerException(ErrorCodes.InternalErrorCode, ErrorDescriptions.TheClientCannotBeUpdated);
            }

            _managerEventSource.FinishToUpdateClient(JsonConvert.SerializeObject(updateClientParameter));
            return(result);
        }
Esempio n. 4
0
        public async Task When_Executing_UpdateClient_Then_Operation_Is_Called()
        {
            // ARRANGE
            var parameter = new UpdateClientParameter
            {
                ClientId = "client_id"
            };

            InitializeFakeObjects();

            // ACT
            await _clientActions.UpdateClient(parameter);

            // ASSERT
            _updateClientActionStub.Verify(g => g.Execute(parameter));
        }
        public async Task When_No_Client_Id_Is_Passed_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            var parameter = new UpdateClientParameter
            {
                ClientId = null
            };

            // ACT
            var exception = await Assert.ThrowsAsync <IdentityServerManagerException>(() => _updateClientAction.Execute(parameter));

            // ASSERTS
            Assert.True(exception.Code == ErrorCodes.InvalidParameterCode);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.MissingParameter, "client_id"));
        }
        public async Task When_Client_Doesnt_Exist_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            const string clientId = "invalid_client_id";
            InitializeFakeObjects();
            _clientRepositoryStub.Setup(c => c.GetClientByIdAsync(It.IsAny<string>()))
                .Returns(Task.FromResult((SimpleIdentityServer.Core.Models.Client)null));
            var parameter = new UpdateClientParameter
            {
                ClientId = clientId
            };

            // ACT & ASSERT
            var exception = await Assert.ThrowsAsync<IdentityServerManagerException>(() => _updateClientAction.Execute(parameter));
            Assert.True(exception.Code == ErrorCodes.InvalidParameterCode);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.TheClientDoesntExist, clientId));
        }
        public async Task When_Passing_Correct_Parameter_Then_Update_Operation_Is_Called()
        {
            // ARRANGE
            const string clientId = "client_id";
            var          client   = new SimpleIdentityServer.Core.Common.Models.Client
            {
                ClientId = clientId
            };
            var parameter = new UpdateClientParameter
            {
                ClientId      = clientId,
                AllowedScopes = new List <string>
                {
                    "scope"
                }
            };

            InitializeFakeObjects();
            _clientRepositoryStub.Setup(c => c.GetClientByIdAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(client));
            _generateClientFromRegistrationRequestStub.Setup(g => g.Execute(It.IsAny <UpdateClientParameter>()))
            .Returns(client);
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >())).Returns(Task.FromResult((ICollection <Scope>) new List <Scope>
            {
                new Scope
                {
                    Name = "scope"
                }
            }));
            _clientRepositoryStub.Setup(c => c.UpdateAsync(It.IsAny <SimpleIdentityServer.Core.Common.Models.Client>())).Returns(Task.FromResult(true));

            // ACT
            await _updateClientAction.Execute(parameter);

            // ASSERTS
            _clientRepositoryStub.Verify(c => c.UpdateAsync(client));
        }
        public async Task When_Scope_Are_Not_Supported_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            const string clientId = "client_id";
            var          client   = new SimpleIdentityServer.Core.Common.Models.Client
            {
                ClientId = clientId
            };
            var parameter = new UpdateClientParameter
            {
                ClientId      = clientId,
                AllowedScopes = new List <string>
                {
                    "not_supported_scope"
                }
            };

            InitializeFakeObjects();
            _clientRepositoryStub.Setup(c => c.GetClientByIdAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(client));
            _generateClientFromRegistrationRequestStub.Setup(g => g.Execute(It.IsAny <UpdateClientParameter>()))
            .Returns(client);
            _scopeRepositoryStub.Setup(s => s.SearchByNamesAsync(It.IsAny <IEnumerable <string> >())).Returns(Task.FromResult((ICollection <Scope>) new List <Scope>
            {
                new Scope
                {
                    Name = "scope"
                }
            }));

            // ACT
            var exception = await Assert.ThrowsAsync <IdentityServerManagerException>(() => _updateClientAction.Execute(parameter));

            // ASSERTS
            Assert.Equal("invalid_parameter", exception.Code);
            Assert.Equal("the scopes 'not_supported_scope' don't exist", exception.Message);
        }
        public async Task When_Passing_Correct_Parameter_Then_Update_Operation_Is_Called()
        {
            // ARRANGE
            const string clientId = "client_id";
            var client = new SimpleIdentityServer.Core.Models.Client
            {
                ClientId = clientId
            };
            var parameter = new UpdateClientParameter
            {
                ClientId = clientId
            };
            InitializeFakeObjects();
            _clientRepositoryStub.Setup(c => c.GetClientByIdAsync(It.IsAny<string>()))
                .Returns(Task.FromResult(client));
            _generateClientFromRegistrationRequestStub.Setup(g => g.Execute(It.IsAny<UpdateClientParameter>()))
                .Returns(client);

            // ACT
            await _updateClientAction.Execute(parameter);

            // ASSERTS
            _clientRepositoryStub.Verify(c => c.UpdateAsync(client));
        }
 public Task <bool> UpdateClient(UpdateClientParameter updateClientParameter)
 {
     return(_updateClientAction.Execute(updateClientParameter));
 }