public void Create_ShouldThrowDomainValidationException_WhenRoleLevelIsAccountAndNoAccountIdsProvided(ApplicationLevel level, string errorCode) { var createAppResourceCmd = ApplicationResourceCmdGenerator.CreateApplicationResourceCommand; createAppResourceCmd.ApplicationLevel = level; var appResource = ApplicationResource.Create(createAppResourceCmd); var addActionCmd = ApplicationResourceCmdGenerator.AddResourceActionCommand; var action = appResource.AddResourceAction(addActionCmd); var roleCmdGen = new RoleCmdGenerator(); var createRoleCmd = roleCmdGen.CreateRoleCommand; createRoleCmd.ApplicationLevel = level; var role = Role.Create(createRoleCmd); var updateRolePermissions = roleCmdGen.UpdateRolePermissionsCommand; updateRolePermissions.Permissions.First().Action = action; role.UpdatePermissions(updateRolePermissions); var userCmdGen = new UserCmdGenerator(); var createUserCmd = userCmdGen.CreateUserCommand; createUserCmd.UserRoles = new[] { new UserCommandRole { Role = role } }; var ex = Assert.Throws <DomainValidationException>(() => User.Create(createUserCmd)); Assert.Single(ex.ValidationErrors); Assert.Equal(errorCode, ex.ValidationErrors.Single().Code); }
public void Create_ShouldSetAllPropertiesCorrectly_WhenCommandIsValid() { var roleCmdGen = new RoleCmdGenerator(); var createRoleCmd = roleCmdGen.CreateRoleCommand; createRoleCmd.ApplicationLevel = ApplicationLevel.Organisation; var roleOrgLevel = Role.Create(createRoleCmd); createRoleCmd.ApplicationLevel = ApplicationLevel.Account; var roleAccLevel = Role.Create(createRoleCmd); var userCmdGen = new UserCmdGenerator(); var createUserCmd = userCmdGen.CreateUserCommand; createUserCmd.UserRoles = new[] { new UserCommandRole { Role = roleOrgLevel, GrantedOrganisations = new[] { 1L, 2L } }, new UserCommandRole { Role = roleAccLevel, GrantedAccounts = new[] { 3L, 4L } } }; var user = User.Create(createUserCmd); Assert.Equal(createUserCmd.Name, user.Name); Assert.Equal(createUserCmd.Email, user.Email); Assert.Equal(createUserCmd.Login, user.Login); Assert.Equal(createUserCmd.Country, user.Country); Assert.Equal(createUserCmd.Currency, user.Currency); Assert.All(user.Roles, role => Assert.Contains(createUserCmd.UserRoles, cmdItem => role.Role.Id == cmdItem.Role.Id && ((role.Accounts == null && cmdItem.GrantedAccounts == null) || role.Accounts?.SequenceEqual(cmdItem.GrantedAccounts ?? Enumerable.Empty <long>()) == true) && ((role.Organisations == null && cmdItem.GrantedOrganisations == null) || role.Organisations?.SequenceEqual(cmdItem.GrantedOrganisations ?? Enumerable.Empty <long>()) == true))); }
/// <summary> /// Main client thread. /// </summary> private void ClientThread() { _gameState.Reset(); _connection = DotaGameConnection.CreateWith(_details); _handshake = new DotaHandshake(_details, _gameState, _connection); _signon = new DotaSignon(_gameState, _connection, _details); _game = new DotaGame(_gameState, _connection); _commandGenerator = new UserCmdGenerator(_gameState, _connection); foreach (var cont in Controllers) { cont.Initialize(_details.SteamId, _gameState, _commandGenerator); } long handshake_requested = 0; long handshake_giveup = new TimeSpan(0, 0, 0, 10).Ticks; // Map states in the StateMachine to handlers var metastates = new Dictionary <States, Metastates>() { { States.HANDSHAKE_REQUEST, Metastates.HANDSHAKE }, { States.HANDSHAKE_CONNECT, Metastates.HANDSHAKE }, { States.CONNECTED, Metastates.SIGNON }, { States.LOADING, Metastates.SIGNON }, { States.PRESPAWN, Metastates.SIGNON }, { States.SPAWN, Metastates.SIGNON }, { States.PLAY, Metastates.GAME }, }; var processors = new Dictionary <Metastates, IHandler>() { { Metastates.HANDSHAKE, _handshake }, { Metastates.SIGNON, _signon }, { Metastates.GAME, _game } }; _stateMachine = new StateMachine <States, Events>(States.DISCONNECTED); //temporary shit _stateMachine.OnTransitioned(transition => { if (transition.Source == transition.Destination) { return; } Callback?.Invoke(this, new CallbackEventArgs(new DotaGameClient.SessionStateTransition(transition.Source, transition.Destination))); }); _stateMachine.OnUnhandledTrigger((states, events) => { Console.WriteLine("Unhandled trigger: " + events.ToString("G")); }); var disconnected = new Action(() => { if (_connection == null) { return; } Running = true; Stop(); }); _stateMachine.Configure(States.DISCONNECTED) .Ignore(Events.TICK) .Ignore(Events.DISCONNECTED) .OnEntry(disconnected) .Permit(Events.REQUEST_CONNECT, States.HANDSHAKE_REQUEST); _stateMachine.Configure(States.HANDSHAKE_REJECTED) .Permit(Events.DISCONNECTED, States.DISCONNECTED) .OnEntry(() => { Callback?.Invoke(this, new CallbackEventArgs(new DotaGameClient.HandshakeRejected(_handshake.rejected_reason))); _stateMachine.Fire(Events.DISCONNECTED); }); _stateMachine.Configure(States.HANDSHAKE_REQUEST) .OnEntry(() => handshake_requested = DateTime.Now.Ticks) .OnEntry(_handshake.RequestHandshake) .Ignore(Events.TICK) .Permit(Events.HANDSHAKE_CHALLENGE, States.HANDSHAKE_CONNECT) .Permit(Events.REJECTED, States.HANDSHAKE_REJECTED) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.HANDSHAKE_CONNECT) .OnEntry(_handshake.RespondHandshake) .Ignore(Events.TICK) .PermitReentry(Events.HANDSHAKE_CHALLENGE) // possibly re-enter? .Permit(Events.HANDSHAKE_COMPLETE, States.CONNECTED) .Permit(Events.REJECTED, States.HANDSHAKE_REJECTED) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.CONNECTED) .OnEntry(_signon.EnterConnected) .Ignore(Events.TICK) .Permit(Events.LOADING_START, States.LOADING) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.LOADING) .OnEntry(_signon.EnterNew) .Ignore(Events.TICK) .Permit(Events.CONNECTED, States.CONNECTED) .Permit(Events.PRESPAWN_START, States.PRESPAWN) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.PRESPAWN) .OnEntry(_signon.EnterPrespawn) .Ignore(Events.TICK) .Permit(Events.SPAWNED, States.SPAWN) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.SPAWN) .OnEntry(_signon.EnterSpawn) .Ignore(Events.TICK) .Permit(Events.BASELINE, States.PLAY) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Configure(States.PLAY) .OnEntryFrom(Events.BASELINE, () => { _game.EnterGame(); _commandGenerator.Reset(); }) .OnEntryFrom(Events.TICK, () => { _gameState.Update(); foreach (var cont in Controllers) { cont.Tick(); } _commandGenerator.Tick(); _gameState.Created.Clear(); _gameState.Deleted.Clear(); }) .PermitReentry(Events.TICK) .Permit(Events.DISCONNECTED, States.DISCONNECTED); _stateMachine.Fire(Events.REQUEST_CONNECT); long next_tick = DateTime.Now.Ticks; while (Running && _stateMachine.State != States.DISCONNECTED && _stateMachine.State != States.HANDSHAKE_REJECTED) { try { if (next_tick > DateTime.Now.Ticks) { Thread.Sleep(1); continue; } if (_stateMachine == null) { break; } if (_stateMachine.State == States.HANDSHAKE_REQUEST && (DateTime.Now.Ticks - handshake_requested) > handshake_giveup) { _stateMachine.Fire(Events.DISCONNECTED); continue; } if (_connection.state == DotaGameConnection.State.Closed) { _stateMachine.Fire(Events.DISCONNECTED); continue; } if (_connection == null) { break; } List <byte[]> outBand = _connection.GetOutOfBand(); List <DotaGameConnection.Message> inBand = _connection.GetInBand(); foreach (byte[] message in outBand) { Nullable <Events> e = processors[metastates[_stateMachine.State]].Handle(message); if (e.HasValue) { _stateMachine.Fire(e.Value); } } foreach (DotaGameConnection.Message message in inBand) { Nullable <Events> e = processors[metastates[_stateMachine.State]].Handle(message); if (e.HasValue) { _stateMachine.Fire(e.Value); } } _stateMachine.Fire(Events.TICK); if (_gameState.TickInterval > 0) { next_tick += (uint)(_gameState.TickInterval * 1000 * 10000 /* ticks per ms */); } else { next_tick += 50 * 1000; } int remain = (int)(next_tick - DateTime.Now.Ticks) / 10000; if (remain > 0) { Thread.Sleep(1); } else if (remain < 0) { next_tick = DateTime.Now.Ticks; } } catch (Exception ex) { Callback?.Invoke(this, new CallbackEventArgs(new DotaGameClient.LogMessage("Ignored error in session loop, " + ex.Message))); } } if (Running) { Stop(); } }