public void Setup()
        {
            permissions = new MockPermissionsProvider();
            users       = new MockUserProvider();

            provider = new MockConnectionProvider(GablarskiProtocol.Instance);

            context = new MockServerContext(provider)
            {
                Settings            = new ServerSettings(),
                UserProvider        = users,
                PermissionsProvider = permissions,
                ChannelsProvider    = new LobbyChannelProvider(),
            };

            context.Sources     = new ServerSourceHandler(context, new ServerSourceManager(context));
            context.Channels    = new ServerChannelHandler(context);
            context.UserManager = new ServerUserManager();
            context.Users       = handler = new ServerUserHandler(context, context.UserManager);

            context.Start();

            var cs = provider.GetConnections(GablarskiProtocol.Instance);

            server = new ServerConnectionBuffer(cs.Item2);
            client = new ConnectionBuffer(cs.Item1);

            var observers = provider.GetConnections(GablarskiProtocol.Instance);

            sobserver = observers.Item2;
            observer  = new ConnectionBuffer(observers.Item1);
        }
        public void KickUserFromServer()
        {
            var cs = provider.GetConnections(GablarskiProtocol.Instance);
            var s  = new ServerConnectionBuffer(cs.Item2);
            var c  = new ConnectionBuffer(cs.Item1);

            var admin = JoinAsGuest(s, c, "admin");

            client.DequeueAndAssertMessage <UserJoinedMessage>();
            permissions.EnablePermissions(admin.UserId, PermissionName.KickPlayerFromServer);

            var target = JoinAsGuest(server, client, "target");

            c.DequeueAndAssertMessage <UserJoinedMessage>();

            handler.OnKickUserMessage(new MessageEventArgs <KickUserMessage> (s,
                                                                              new KickUserMessage(target, true)));

            var kicked = client.DequeueAndAssertMessage <UserKickedMessage>();

            Assert.AreEqual(target.UserId, kicked.UserId);
            Assert.AreEqual(true, kicked.FromServer);

            kicked = c.DequeueAndAssertMessage <UserKickedMessage>();
            Assert.AreEqual(target.UserId, kicked.UserId);
            Assert.AreEqual(true, kicked.FromServer);

            Assert.AreEqual(target.UserId, c.DequeueAndAssertMessage <UserDisconnectedMessage>().UserId);
        }
 public void Teardown()
 {
     handler  = null;
     server   = null;
     observer = null;
     context  = null;
 }
        public void KickUserFromChannel()
        {
            var cs = provider.GetConnections(GablarskiProtocol.Instance);
            var s  = new ServerConnectionBuffer(cs.Item2);
            var c  = new ConnectionBuffer(cs.Item1);

            var admin = JoinAsGuest(s, c, "admin");

            client.DequeueAndAssertMessage <UserJoinedMessage>();
            permissions.EnablePermissions(admin.UserId, PermissionName.KickPlayerFromChannel);

            var target = JoinAsGuest(server, client, "target");

            c.DequeueAndAssertMessage <UserJoinedMessage>();

            IChannelInfo altChannel = new ChannelInfo {
                Name = "Channel 2"
            };

            context.ChannelsProvider.SaveChannel(altChannel);
            c.DequeueAndAssertMessage <ChannelListMessage>();
            client.DequeueAndAssertMessage <ChannelListMessage>();
            altChannel = context.ChannelsProvider.GetChannels().Single(ch => ch.Name == "Channel 2");
            handler.Move(target, altChannel);
            var moved = client.DequeueAndAssertMessage <UserChangedChannelMessage>();

            Assert.AreEqual(altChannel.ChannelId, moved.ChangeInfo.TargetChannelId);
            Assert.AreEqual(target.UserId, moved.ChangeInfo.TargetUserId);

            handler.OnKickUserMessage(new MessageEventArgs <KickUserMessage> (s,
                                                                              new KickUserMessage(target, false)));

            var kicked = client.DequeueAndAssertMessage <UserKickedMessage>();

            Assert.AreEqual(target.UserId, kicked.UserId);
            Assert.AreEqual(false, kicked.FromServer);

            moved = client.DequeueAndAssertMessage <UserChangedChannelMessage>();
            Assert.AreEqual(context.ChannelsProvider.DefaultChannel.ChannelId, moved.ChangeInfo.TargetChannelId);
            Assert.AreEqual(target.UserId, moved.ChangeInfo.TargetUserId);
        }