Esempio n. 1
0
        private async Task should_raise_message_reaction_event(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var fixture        = new Fixture();
            var connectionInfo = new ConnectionInformation
            {
                Users =
                {
                    { "userABC",    new SlackUser {
                          Id = "userABC", Name = "i-have-a-name"
                      } },
                    { "secondUser", new SlackUser {
                          Id = "secondUser", Name = "i-have-a-name-too-thanks"
                      } }
                },
                SlackChatHubs = new Dictionary <string, SlackChatHub>
                {
                    { "chat-hub-1", new SlackChatHub() }
                },
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            ISlackReaction lastReaction = null;

            slackConnection.OnReaction += reaction =>
            {
                lastReaction = reaction;
                return(Task.CompletedTask);
            };

            var inboundMessage = new ReactionMessage
            {
                User           = "******",
                Reaction       = fixture.Create <string>(),
                RawData        = fixture.Create <string>(),
                ReactingToUser = "******",
                Timestamp      = fixture.Create <double>(),
                ReactingTo     = new MessageReaction
                {
                    Channel = "chat-hub-1"
                }
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            lastReaction.ShouldLookLike(new SlackMessageReaction
            {
                User           = connectionInfo.Users["userABC"],
                Reaction       = inboundMessage.Reaction,
                RawData        = inboundMessage.RawData,
                ChatHub        = connectionInfo.SlackChatHubs["chat-hub-1"],
                ReactingToUser = connectionInfo.Users["secondUser"],
                Timestamp      = inboundMessage.Timestamp
            });
        }
Esempio n. 2
0
        private async Task should_raise_event(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            DateTime lastTimestamp = DateTime.MinValue;

            slackConnection.OnPong += timestamp =>
            {
                lastTimestamp = timestamp;
                return(Task.CompletedTask);
            };

            var inboundMessage = new PongMessage
            {
                Timestamp = DateTime.Now
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            lastTimestamp.ShouldBe(inboundMessage.Timestamp);
        }
Esempio n. 3
0
        private async Task should_pong_monitor(
            [Frozen] Mock <IMonitoringFactory> monitoringFactory,
            Mock <IPingPongMonitor> pingPongMonitor,
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            monitoringFactory
            .Setup(x => x.CreatePingPongMonitor())
            .Returns(pingPongMonitor.Object);

            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            var inboundMessage = new PongMessage
            {
                Timestamp = DateTime.Now
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            pingPongMonitor.Verify(x => x.Pong(), Times.Once);
        }
Esempio n. 4
0
        private async Task should_return_expected_slack_purpose(
            [Frozen] Mock <IConnectionFactory> connectionFactory,
            Mock <IChannelClient> channelClient,
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            const string slackKey       = "key-yay";
            const string channelName    = "public-channel-name";
            const string channelPurpose = "new purpose";

            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object, SlackKey = slackKey
            };
            await slackConnection.Initialise(connectionInfo);

            connectionFactory
            .Setup(x => x.CreateChannelClient())
            .Returns(channelClient.Object);

            channelClient
            .Setup(x => x.SetPurpose(slackKey, channelName, channelPurpose))
            .ReturnsAsync(channelPurpose);

            // when
            var result = await slackConnection.SetChannelPurpose(channelName, channelPurpose);

            // then
            result.ShouldLookLike(new SlackPurpose
            {
                ChannelName = channelName,
                Purpose     = channelPurpose
            });
        }
Esempio n. 5
0
        private async Task should_upload_file_from_stream(
            [Frozen] Mock <IConnectionFactory> connectionFactory,
            Mock <IFileClient> fileClient,
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            const string slackKey = "key-yay";
            const string fileName = "expected-file-name";
            var          chatHub  = new SlackChatHub {
                Id = "channelz-id"
            };
            var stream = new MemoryStream();

            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object, SlackKey = slackKey
            };
            await slackConnection.Initialise(connectionInfo);

            connectionFactory
            .Setup(x => x.CreateFileClient())
            .Returns(fileClient.Object);

            // when
            await slackConnection.Upload(chatHub, stream, fileName);

            // then
            fileClient
            .Verify(x => x.PostFile(slackKey, chatHub.Id, stream, fileName), Times.Once);
        }
Esempio n. 6
0
        private async Task should_be_successful(
            [Frozen] Mock <IConnectionFactory> connectionFactory,
            Mock <IChannelClient> channelClient,
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            const string slackKey    = "key-yay";
            const string channelName = "public-channel-name";

            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object, SlackKey = slackKey
            };
            await slackConnection.Initialise(connectionInfo);

            connectionFactory
            .Setup(x => x.CreateChannelClient())
            .Returns(channelClient.Object);

            // when
            await slackConnection.ArchiveChannel(channelName);

            // then
            channelClient.Verify(x => x.ArchiveChannel(slackKey, channelName), Times.Once);
        }
Esempio n. 7
0
        private async Task should_raise_event(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            const string hubId   = "this-is-the-id";
            SlackChatHub lastHub = null;

            slackConnection.OnChatHubJoined += hub =>
            {
                lastHub = hub;
                return(Task.CompletedTask);
            };

            var inboundMessage = new GroupJoinedMessage
            {
                Channel = new Group {
                    Id = hubId
                }
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            lastHub.Id.ShouldBe(hubId);
            lastHub.Type.ShouldBe(SlackChatHubType.Group);
            slackConnection.ConnectedHubs.ContainsKey(hubId).ShouldBeTrue();
            slackConnection.ConnectedHubs[hubId].ShouldBe(lastHub);
        }
        private async Task should_not_raise_message_event_given_message_from_self(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation
            {
                Self = new ContactDetails {
                    Id = "self-id", Name = "self-name"
                },
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            var inboundMessage = new ChatMessage
            {
                MessageType = MessageType.Message,
                User        = connectionInfo.Self.Id
            };

            bool messageRaised = false;

            slackConnection.OnMessageReceived += message =>
            {
                messageRaised = true;
                return(Task.CompletedTask);
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            messageRaised.ShouldBeFalse();
        }
        private async Task should_not_raise_message_event_given_null_message(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation
            {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            bool messageRaised = false;

            slackConnection.OnMessageReceived += message =>
            {
                messageRaised = true;
                return(Task.CompletedTask);
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, null);

            // then
            messageRaised.ShouldBeFalse();
        }
Esempio n. 10
0
        private void should_initialise_slack_connection(SlackConnection connection)
        {
            // given
            var info = new ConnectionInformation
            {
                Self = new ContactDetails {
                    Id = "self-id"
                },
                Team = new ContactDetails {
                    Id = "team-id"
                },
                Users = new Dictionary <string, SlackUser> {
                    { "userid", new SlackUser()
                      {
                          Name = "userName"
                      } }
                },
                SlackChatHubs = new Dictionary <string, SlackChatHub> {
                    { "some-hub", new SlackChatHub() }
                },
                WebSocket = new Mock <IWebSocketClient>().Object
            };

            // when
            connection.Initialise(info).Wait();

            // then
            connection.Self.ShouldBe(info.Self);
            connection.Team.ShouldBe(info.Team);
            connection.UserCache.ShouldBe(info.Users);
            connection.ConnectedHubs.ShouldBe(info.SlackChatHubs);
            connection.ConnectedSince.HasValue.ShouldBeTrue();
        }
Esempio n. 11
0
        private async Task should_initialise_ping_pong_monitor(
            [Frozen] Mock <IMonitoringFactory> monitoringFactory,
            Mock <IPingPongMonitor> pingPongMonitor,
            SlackConnection connection)
        {
            // given
            var info = new ConnectionInformation
            {
                WebSocket = new Mock <IWebSocketClient>().Object
            };

            pingPongMonitor
            .Setup(x => x.StartMonitor(It.IsAny <Func <Task> >(), It.IsAny <Func <Task> >(), It.IsAny <TimeSpan>()))
            .Returns(Task.CompletedTask);

            monitoringFactory
            .Setup(x => x.CreatePingPongMonitor())
            .Returns(pingPongMonitor.Object);

            // when
            await connection.Initialise(info);

            // then
            pingPongMonitor.Verify(x => x.StartMonitor(It.IsNotNull <Func <Task> >(), It.IsNotNull <Func <Task> >(), TimeSpan.FromMinutes(2)), Times.Once);
        }
Esempio n. 12
0
        private async Task should_send_message(
            [Frozen] Mock <IConnectionFactory> connectionFactory,
            Mock <IChatClient> chatClient,
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            const string slackKey = "key-yay";

            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object, SlackKey = slackKey
            };
            await slackConnection.Initialise(connectionInfo);

            connectionFactory
            .Setup(x => x.CreateChatClient())
            .Returns(chatClient.Object);

            var message = new BotMessage
            {
                Text    = "some text",
                ChatHub = new SlackChatHub {
                    Id = "channel-id"
                },
                Attachments = new List <SlackAttachment>()
            };

            // when
            await slackConnection.Say(message);

            // then
            chatClient
            .Verify(x => x.PostMessage(slackKey, message.ChatHub.Id, message.Text, message.Attachments), Times.Once);
        }
Esempio n. 13
0
        private async Task should_not_raise_event_given_missing_data(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            SlackChannelCreated channelCreated = null;

            slackConnection.OnChannelCreated += channel =>
            {
                channelCreated = channel;
                return(Task.CompletedTask);
            };

            var inboundMessage = new ChannelCreatedMessage {
                Channel = null
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            channelCreated.ShouldBeNull();
            slackConnection.ConnectedHubs.ShouldBeEmpty();
        }
Esempio n. 14
0
        private async Task should_raise_file_comment_reaction_event(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var fixture        = new Fixture();
            var connectionInfo = new ConnectionInformation
            {
                Users =
                {
                    { "some-user",    new SlackUser {
                          Id = "some-user", Name = "i-have-a-name"
                      } },
                    { "another-user", new SlackUser {
                          Id = "another-user", Name = "i-have-a-name-too-thanks"
                      } }
                },
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            ISlackReaction lastReaction = null;

            slackConnection.OnReaction += reaction =>
            {
                lastReaction = reaction;
                return(Task.CompletedTask);
            };

            var file           = fixture.Create <string>();
            var fileComment    = fixture.Create <string>();
            var inboundMessage = new ReactionMessage
            {
                User           = "******",
                Reaction       = fixture.Create <string>(),
                RawData        = fixture.Create <string>(),
                ReactingToUser = "******",
                Timestamp      = fixture.Create <double>(),
                ReactingTo     = new FileCommentReaction
                {
                    File        = file,
                    FileComment = fileComment
                }
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            lastReaction.ShouldLookLike(new SlackFileCommentReaction
            {
                User           = connectionInfo.Users["some-user"],
                Reaction       = inboundMessage.Reaction,
                File           = file,
                FileComment    = fileComment,
                RawData        = inboundMessage.RawData,
                ReactingToUser = connectionInfo.Users["another-user"],
                Timestamp      = inboundMessage.Timestamp
            });
        }
Esempio n. 15
0
        private async Task should_return_stream(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection,
            string slackKey,
            Fixture fixture)
        {
            using (var httpTest = new HttpTest())
            {
                // given
                var downloadUri = new Uri($"https://files.slack.com/{fixture.Create<string>()}");

                var connectionInfo = new ConnectionInformation {
                    WebSocket = webSocket.Object, SlackKey = slackKey
                };
                await slackConnection.Initialise(connectionInfo);

                httpTest
                .RespondWithJson(new { fakeObject = true });

                // when
                var result = await slackConnection.DownloadFile(downloadUri);

                // then
                result.ShouldNotBeNull();
                httpTest
                .ShouldHaveCalled(downloadUri.AbsoluteUri)
                .WithOAuthBearerToken(slackKey);
            }
        }
Esempio n. 16
0
        private async Task should_not_raise_event_given_message_is_missing_user_information(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation
            {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            var inboundMessage = new ChatMessage
            {
                MessageType = MessageType.Message,
                User        = null
            };

            bool messageRaised = false;

            slackConnection.OnMessageReceived += message =>
            {
                messageRaised = true;
                return(Task.CompletedTask);
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            messageRaised.ShouldBeFalse();
        }
Esempio n. 17
0
        public async Task should_connect()
        {
            SlackConnection.IsConnected.ShouldBeTrue();

            await SlackConnection.Close();

            SlackConnection.IsConnected.ShouldBeFalse();
        }
Esempio n. 18
0
        public void CreateClientWithNotFinishedConnection_ReturnsNoConnectionClient()
        {
            var       slackClientFactory = new SlackClientFactory();
            const int organizationId     = 1;

            var client = slackClientFactory.Create(organizationId, SlackConnection.New(organizationId));

            Assert.IsInstanceOf <NotConnectedSlackClient>(client);
        }
Esempio n. 19
0
        public async Task should_connect_and_get_channels()
        {
            // given

            // when
            var channels = await SlackConnection.GetChannels();

            // then
            channels.Any().ShouldBeTrue();
        }
Esempio n. 20
0
        public async Task should_connect_and_get_users()
        {
            // given

            // when
            var users = await SlackConnection.GetUsers();

            // then
            users.Any().ShouldBeTrue();
        }
Esempio n. 21
0
        public async Task should_upload_to_channel_from_file_system()
        {
            // given
            var chatHub = SlackConnection.ConnectedChannel(Config.Slack.TestChannel);

            // when
            await SlackConnection.Upload(chatHub, _filePath);

            // then
        }
Esempio n. 22
0
        public async Task should_send_typing_indicator()
        {
            // given
            SlackChatHub channel = SlackConnection.ConnectedChannel(Config.Slack.TestChannel);

            // when
            await SlackConnection.IndicateTyping(channel);

            // then
        }
Esempio n. 23
0
        public void CreateClientWithFinishedConnection_ReturnsWorkingSlackClient()
        {
            var       slackClientFactory = new SlackClientFactory();
            const int organizationId     = 1;
            var       slackConnection    = SlackConnection.New(organizationId);

            slackConnection.SetAccessToken("some token");

            var client = slackClientFactory.Create(organizationId, slackConnection);

            Assert.IsInstanceOf <SlackClient>(client);
        }
Esempio n. 24
0
        public async Task should_say_something_on_channel()
        {
            // given
            var message = new BotMessage
            {
                Text    = "Test text for INT test",
                ChatHub = SlackConnection.ConnectedChannel(Config.Slack.TestChannel)
            };

            // when
            await SlackConnection.Say(message);

            // then
        }
Esempio n. 25
0
        private async Task should_raise_event(Mock <IWebSocketClient> webSocket, SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            SlackUser lastUser = null;

            slackConnection.OnUserJoined += user =>
            {
                lastUser = user;
                return(Task.CompletedTask);
            };

            var inboundMessage = new UserJoinedMessage
            {
                MessageType = MessageType.Team_Join,
                User        = new User
                {
                    Id             = "some-id",
                    Name           = "my-name",
                    TimeZoneOffset = -231,
                    IsBot          = true,
                    Presence       = "active",
                    Profile        = new Profile
                    {
                        Email = "*****@*****.**"
                    }
                }
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            lastUser.ShouldLookLike(new SlackUser
            {
                Id             = "some-id",
                Name           = "my-name",
                TimeZoneOffset = -231,
                IsBot          = true,
                Online         = true,
                Email          = "*****@*****.**"
            });

            slackConnection.UserCache.ContainsKey(inboundMessage.User.Id).ShouldBeTrue();
            slackConnection.UserCache[inboundMessage.User.Id].ShouldBe(lastUser);
        }
Esempio n. 26
0
        public async Task should_upload_to_channel_from_stream()
        {
            // given
            var          chatHub  = SlackConnection.ConnectedChannel(Config.Slack.TestChannel);
            const string fileName = "slackconnector-test-stream-upload.txt";

            // when
            using (var fileStream = EmbeddedResourceFileReader.ReadEmbeddedFile("UploadTest.txt"))
            {
                await SlackConnection.Upload(chatHub, fileStream, fileName);
            }

            // then
        }
Esempio n. 27
0
        public async Task <string> GetSlackConnectionLinkForOrganization(int organizationId)
        {
            var slackConnection = await _slackConnectionRepository.TryGetForOrganization(organizationId)
                                  .IfNoneAsync(
                async() =>
            {
                var newConnection = SlackConnection.New(organizationId);
                await _slackConnectionRepository.Create(newConnection);
                return(newConnection);
            });

            var addToSlackLink = _addToSlackLinkProvider.GetLink(slackConnection.Id);

            return(addToSlackLink);
        }
Esempio n. 28
0
        private async Task should_throw_exception_given_null_channel_name(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation {
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            // when
            var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => slackConnection.CreateChannel(null));

            // then
            exception.Message.ShouldBe("Value cannot be null.\r\nParameter name: channelName");
        }
Esempio n. 29
0
        private async Task should_raise_event(
            Mock <IWebSocketClient> webSocket,
            SlackConnection slackConnection)
        {
            // given
            var connectionInfo = new ConnectionInformation
            {
                Users = { { "userABC", new SlackUser {
                                Id = "userABC", Name = "i-have-a-name"
                            } } },
                WebSocket = webSocket.Object
            };
            await slackConnection.Initialise(connectionInfo);

            var inboundMessage = new ChatMessage
            {
                User           = "******",
                MessageType    = MessageType.Message,
                Text           = "amazing-text",
                RawData        = "I am raw data yo",
                MessageSubType = MessageSubType.channel_leave
            };

            SlackMessage receivedMessage = null;

            slackConnection.OnMessageReceived += message =>
            {
                receivedMessage = message;
                return(Task.CompletedTask);
            };

            // when
            webSocket.Raise(x => x.OnMessage += null, null, inboundMessage);

            // then
            receivedMessage.ShouldLookLike(new SlackMessage
            {
                Text = "amazing-text",
                User = new SlackUser {
                    Id = "userABC", Name = "i-have-a-name"
                },
                RawData        = inboundMessage.RawData,
                MessageSubType = SlackMessageSubType.ChannelLeave,
                Files          = Enumerable.Empty <SlackFile>()
            });
        }
Esempio n. 30
0
        public async Task should_connect_and_stuff()
        {
            // given

            // when
            SlackConnection.OnDisconnect      += SlackConnector_OnDisconnect;
            SlackConnection.OnMessageReceived += SlackConnectorOnMessageReceived;

            // then
            SlackConnection.IsConnected.ShouldBeTrue();
            //Thread.Sleep(TimeSpan.FromMinutes(1));

            // when
            await SlackConnection.Close();

            SlackConnection.IsConnected.ShouldBeFalse();
        }