Exemplo n.º 1
0
        public void TimeoutUserSendsCorrectPackets()
        {
            var sim = SimAuth.Value;
            var ws  = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                var chat      = sut as IChatService;
                var id        = ws.LastId.GetValueOrDefault() + 1;
                var replyJson = BuildTimeoutReply(id);

                var task = chat.TimeoutUserAsync("SomeTestUser", TimeSpan.FromMinutes(5));
                ws.InjectPacket(replyJson);

                task.Wait(Simulator.TIMEOUT);                 // Wait for TimeoutUserAsync to complete

                // Validate packet sent
                ws.LastPacket["type"].Should().NotBeNull();
                ws.LastPacket["type"].Value <string>().Should().Be("method");
                ws.LastPacket["method"].Should().NotBeNull();
                ws.LastPacket["method"].Value <string>().Should().Be("timeout");
                ws.LastPacket["arguments"].Should().NotBeNull();
                var args = ws.LastPacket["arguments"].Values <string>().ToArray();
                args.Should().HaveCount(2);
                args[0].Should().Be("SomeTestUser");
                args[1].Should().Be("5m0s");

                task.Result.Should().BeTrue();
            }
        }
Exemplo n.º 2
0
        public void WillReconnectToNextServer()
        {
            var sim = SimAnon.Value;
            var ws  = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                var url1 = ws.ConnectUrl;

                // Prepare new ClientWebSocket for consumption by client code, and dispose the old one
                sim.ChatWebSocket = new SimulatedClientWebSocket(true, false, Simulator.CHAT_WELCOME);
                ws.Dispose();
                ws = sim.ChatWebSocket;
                bool reconnectSucceeded = ws.JoinedChat.Wait(Simulator.TIMEOUT);

                var url2 = ws.ConnectUrl;

                reconnectSucceeded.Should().BeTrue();
                url1.Should().NotBeNull();
                url2.Should().NotBeNull();
                url1.Should().NotBe(url2);
            }
        }
Exemplo n.º 3
0
        public void AddCorrectHeaders()
        {
            var sim = SimAuth.Value;
            var ws  = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                ws.Headers.Should().Contain("x-is-bot", "true");
            }
        }
Exemplo n.º 4
0
        public void ImplementsCorrectInterfaces()
        {
            var sim    = SimAnon.Value;
            var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!");
            var ws     = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                var result = Assert.Raises <ChatMessageEventArgs>(x => sut.ChatMessage += x, x => sut.ChatMessage -= x, () => ws.InjectPacket(packet));
                Assert.IsAssignableFrom <IChatService>(result.Sender);
                Assert.IsAssignableFrom <IStreamService>(result.Sender);
            }
        }
        public void ImplementCorrectInterfaces()
        {
            var packet = BuildLiveEvent("channel:1234:update", followers: 66);

            var sim = SimAnon.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                var result = Assert.Raises <ServiceUpdatedEventArgs>(x => sut.Updated += x, x => sut.Updated -= x, () => ws.InjectPacket(packet));
                Assert.IsAssignableFrom <IChatService>(result.Sender);
                Assert.IsAssignableFrom <IStreamService>(result.Sender);
            }
        }
        public void DontRaiseEventWhenViewersIsSameAsBefore()
        {
            var packet = BuildLiveEvent("channel:1234:update", viewers: 35);

            var sim = SimAnon.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                ws.InjectPacket(packet);                    // 1st
                using (var monitor = sut.Monitor())
                {
                    ws.InjectPacket(packet);                      // 2nd
                    monitor.Should().NotRaise(nameof(sut.Updated));
                }
            }
        }
        public void WillReconnect()
        {
            var sim = SimAnon.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                // Prepare new ClientWebSocket for consumption by client code, and dispose the old one
                sim.ConstellationWebSocket = new SimulatedClientWebSocket(false, false, Simulator.CONSTALLATION_WELCOME);
                ws.Dispose();
                ws = sim.ConstellationWebSocket;
                var connectedAndJoined = ws.JoinedConstallation.Wait(Simulator.TIMEOUT);

                connectedAndJoined.Should().BeTrue();
            }
        }
Exemplo n.º 8
0
        public void HandlesNullAvatar()
        {
            var sim    = SimAnon.Value;
            var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!", roles: new string[] { "Owner", "Mod" }, avatar: null);
            var ws     = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                using (var monitor = sut.Monitor())
                {
                    ws.InjectPacket(packet);
                    monitor.Should().Raise(nameof(sut.ChatMessage))
                    .WithArgs <ChatMessageEventArgs>(a => a.Properties.ContainsKey("AvatarUrl") && a.UserName == "connor")
                    .WithSender(sut);
                }
            }
        }
Exemplo n.º 9
0
        public void RaisesUserLeftEvent()
        {
            var sim    = SimAnon.Value;
            var packet = BuildUserJoinOrLeave(sim, "SomeNewUser", 34103083, isJoin: false);
            var ws     = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                using (var monitor = sut.Monitor())
                {
                    ws.InjectPacket(packet);
                    monitor.Should().Raise(nameof(sut.UserLeft))
                    .WithArgs <ChatUserInfoEventArgs>(a => a.UserId == 34103083 && a.UserName == "SomeNewUser" && a.ServiceName == "Mixer" && a.ChannelId == sim.ChannelInfo.Id)
                    .WithSender(sut);
                }
            }
        }
Exemplo n.º 10
0
        public void DetectsUserRoles()
        {
            var sim    = SimAnon.Value;
            var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!", roles: new string[] { "Owner", "Mod" });
            var ws     = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                using (var monitor = sut.Monitor())
                {
                    ws.InjectPacket(packet);
                    monitor.Should().Raise(nameof(sut.ChatMessage))
                    .WithArgs <ChatMessageEventArgs>(a => a.Message == "Hello world!" && a.UserName == "connor" && a.UserId == 56789 && a.IsModerator && a.IsOwner && !a.IsWhisper)
                    .WithSender(sut);
                }
            }
        }
Exemplo n.º 11
0
        public void CanCombineEvent()
        {
            var packet = BuildLiveEvent("channel:1234:update", followers: 22, viewers: 43, online: true);

            var sim = SimAnon.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
                using (var monitor = sut.Monitor())
                {
                    ws.InjectPacket(packet);
                    monitor.Should().Raise(nameof(sut.Updated))
                    .WithArgs <ServiceUpdatedEventArgs>(a => a.NewFollowers == 22 && a.NewViewers == 43 && a.IsOnline == true && a.ServiceName == "Mixer" && a.ChannelId == sim.ChannelInfo.Id)
                    .WithSender(sut);
                }
            }
        }
Exemplo n.º 12
0
        public void WillConnectAndJoinAnonymously()
        {
            var sim = SimAnon.Value;

            var ws = sim.ChatWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                var connectedAndJoined = ws.JoinedChat.Wait(Simulator.TIMEOUT);
                connectedAndJoined.Should().BeTrue();

                ws.LastPacket["method"].Should().NotBeNull();
                ws.LastPacket["method"].Value <string>().Should().Be("auth");
                ws.LastPacket["arguments"].Should().NotBeNull();
                var args = ws.LastPacket["arguments"].ToString(Formatting.None);
                args.Should().Be($"[{sim.ChannelInfo.Id}]");
            }
        }
Exemplo n.º 13
0
        public void CanHandleRealDataDump()
        {
            var sim = SimAuth.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                foreach (var line in File.ReadAllLines("Services/Mixer/Data/ConstellationDump.json"))
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }
                    ws.InjectPacket(line);
                }

                sut.StopAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);
            }
        }
Exemplo n.º 14
0
        public void WillConnectAndJoin()
        {
            var sim = SimAnon.Value;
            var ws  = sim.ConstellationWebSocket;

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                var connectedAndJoined = ws.JoinedConstallation.Wait(Simulator.TIMEOUT);

                connectedAndJoined.Should().BeTrue();
                // {{"id": 1,"type": "method","method": "livesubscribe","params": {"events": ["channel:1234:update" ]}}}
                ws.LastPacket["method"].Should().NotBeNull();
                ws.LastPacket["method"].Value <string>().Should().Be("livesubscribe");
                var args = $"[{sim.ChannelInfo.Id},{sim.ChannelInfo.UserId},\"{sim.ChatAuthKey}\"]";
                ws.LastPacket["params"].Should().NotBeNull();
                ws.LastPacket["params"]["events"].Should().NotBeNull();
                var events = ws.LastPacket["params"]["events"].ToString(Formatting.None);
                events.Should().ContainAll($"channel:{sim.ChannelInfo.Id}:update");
            }
        }
Exemplo n.º 15
0
        public void CantSendAnononymously()
        {
            var sim = SimAnon.Value;
            var ws  = sim.ChatWebSocket;

            const string text = "Some test message";

            using (var sut = new MixerService(sim.Config, LoggerFactory, sim))
            {
                sut.StartAsync(sim.Cancel.Token).Wait(Simulator.TIMEOUT);

                var chat      = sut as IChatService;
                var id        = ws.LastId.GetValueOrDefault() + 1;
                var replyJson = BuildMsgReply(sim, id, text);

                var task = chat.SendMessageAsync(text);
                ws.InjectPacket(replyJson);

                task.Wait(Simulator.TIMEOUT);                 // Wait for SendMessageAsync to complete
                task.Result.Should().BeFalse();
            }
        }