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 }); }
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); }
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(); }
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); }
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(); }
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 }); }
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); }
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); }
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(); }
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_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(); }
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); }
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); } }
private async Task should_not_raise_event_given_missing_user_info(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 { User = new User { Id = null } }; // when webSocket.Raise(x => x.OnMessage += null, null, inboundMessage); // then lastUser.ShouldBeNull(); slackConnection.UserCache.ShouldBeEmpty(); }
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_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 }); }
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); }
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); }
private async Task should_throw_exception_given_empty_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(string.Empty)); // then exception.Message.ShouldBe("Value cannot be null.\r\nParameter name: channelName"); }
private async Task should_throw_exception_given_null_user_id( 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.JoinDirectMessageChannel(null)); // then exception.Message.ShouldBe("Value cannot be null.\r\nParameter name: user"); }
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>() }); }
private async Task should_throw_exception_given_null_chat_hub( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object }; await slackConnection.Initialise(connectionInfo); // when var exception = await Assert.ThrowsAsync <MissingChannelException>(() => slackConnection.Say(new BotMessage { ChatHub = null })); // then exception.Message.ShouldBe("When calling the Say() method, the message parameter must have its ChatHub property set."); }
private async Task should_send_ping( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given const string slackKey = "key-yay"; var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object, SlackKey = slackKey }; await slackConnection.Initialise(connectionInfo); // when await slackConnection.Ping(); // then webSocket.Verify(x => x.SendMessage(It.IsAny <PingMessage>())); }
private async Task should_not_close_websocket_when_websocket_is_disconnected( [Frozen] Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given webSocket .Setup(x => x.IsAlive) .Returns(false); var info = GetDummyConnectionInformation(webSocket); await slackConnection.Initialise(info); // when await slackConnection.Close(); // then webSocket.Verify(x => x.Close(), Times.Never); }
private async Task should_raise_event( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection, SlackUser slackUser, Fixture fixture) { // given var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object, Users = new Dictionary <string, SlackUser> { { slackUser.Id, slackUser } } }; await slackConnection.Initialise(connectionInfo); SlackChannelCreated channelCreated = null; slackConnection.OnChannelCreated += channel => { channelCreated = channel; return(Task.CompletedTask); }; var inboundMessage = new ChannelCreatedMessage { Channel = new Channel { Creator = slackUser.Id, Id = fixture.Create <string>(), Name = fixture.Create <string>() } }; // when webSocket.Raise(x => x.OnMessage += null, null, inboundMessage); // then channelCreated.Id.ShouldBe(inboundMessage.Channel.Id); channelCreated.Name.ShouldBe(inboundMessage.Channel.Name); channelCreated.Creator.ShouldBe(slackUser); slackConnection.ConnectedHubs.ContainsKey(inboundMessage.Channel.Id).ShouldBeTrue(); }
private async Task should_raise_event( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object }; await slackConnection.Initialise(connectionInfo); SlackChatHub lastHub = null; slackConnection.OnChatHubJoined += hub => { lastHub = hub; return(Task.CompletedTask); }; var inboundMessage = new DmChannelJoinedMessage { Channel = new Im { User = "******", Id = "channel-id", IsIm = true, IsOpen = true } }; // when webSocket.Raise(x => x.OnMessage += null, null, inboundMessage); // then slackConnection.ConnectedHubs.ContainsKey("channel-id").ShouldBeTrue(); slackConnection.ConnectedHubs["channel-id"].ShouldBe(lastHub); lastHub.ShouldLookLike(new SlackChatHub { Id = "channel-id", Name = "@test-user", Type = SlackChatHubType.DM }); }
private void should_be_connected_if_websocket_is_alive( Mock <IWebSocketClient> webSocketClient, SlackConnection connection) { // given var info = new ConnectionInformation { WebSocket = webSocketClient.Object }; webSocketClient .Setup(x => x.IsAlive) .Returns(true); // when connection.Initialise(info).Wait(); // then connection.IsConnected.ShouldBeTrue(); }
private async Task throws_exception_if_uri_isnt_slack( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection, string slackKey, Fixture fixture) { // given var downloadUri = new Uri($"https://something.com/{fixture.Create<string>()}"); var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object, SlackKey = slackKey }; await slackConnection.Initialise(connectionInfo); // when var exception = Should.Throw <ArgumentException>(async() => await slackConnection.DownloadFile(downloadUri)); // then exception.ShouldNotBeNull(); exception.Message.ShouldContain("Invalid uri"); }
private async Task should_not_raise_exception(Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given var connectionInfo = new ConnectionInformation { WebSocket = webSocket.Object }; await slackConnection.Initialise(connectionInfo); slackConnection.OnUserJoined += user => throw new NotImplementedException("THIS SHOULDN'T BUBBLE UP"); var inboundMessage = new UserJoinedMessage { User = new User { Id = null } }; // when & then (does not throw) webSocket.Raise(x => x.OnMessage += null, null, inboundMessage); }
private async Task should_detect_disconnect( Mock <IWebSocketClient> webSocket, SlackConnection slackConnection) { // given bool connectionChangedValue = false; slackConnection.OnDisconnect += () => connectionChangedValue = true; var info = new ConnectionInformation { WebSocket = webSocket.Object }; await slackConnection.Initialise(info); // when webSocket.Raise(x => x.OnClose += null, new EventArgs()); // then connectionChangedValue.ShouldBeTrue(); slackConnection.IsConnected.ShouldBeFalse(); slackConnection.ConnectedSince.ShouldBeNull(); }