Ejemplo n.º 1
0
        public void SimpleUpdate()
        {
            // given
            var            client    = this.fixture.UserClient;
            var            messageId = PostedMessage(client);
            UpdateResponse actual    = null;

            // when
            using (var sync = new InSync(nameof(SlackClient.Update)))
            {
                client.Update(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    messageId,
                    this.fixture.Config.TestChannel,
                    "[changed]",
                    attachments: SlackMother.SomeAttachments,
                    as_user: true);
            }

            // then
            Assert.True(actual.ok, "Error while posting message to channel. ");
            Assert.Equal(actual.message.text, "[changed]");
            Assert.Equal(actual.message.type, "message");
        }
Ejemplo n.º 2
0
        public void SimpleUpdate()
        {
            // given
            var            client    = ClientHelper.GetClient(_config.Slack.UserAuthToken);
            var            messageId = PostedMessage(client);
            UpdateResponse actual    = null;

            // when
            using (var sync = new InSync())
            {
                client.Update(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    messageId,
                    _config.Slack.TestChannel,
                    "[changed]",
                    attachments: SlackMother.SomeAttachments,
                    as_user: true);
            }

            // then
            Assert.IsTrue(actual.Ok, "Error while posting message to channel. ");
            Assert.AreEqual(actual.message.text, "[changed]");
            Assert.AreEqual(actual.message.type, "message");
        }
Ejemplo n.º 3
0
        public void UserList()
        {
            var client = this.fixture.UserClient;
            UserListResponse actual = null;

            using (var sync = new InSync(nameof(SlackClient.UserLookup)))
            {
                client.GetUserList(response =>
                {
                    actual = response;
                    sync.Proceed();
                });
            }

            Assert.True(actual.ok, "Error while fetching user list.");
            Assert.True(actual.members.Any());

            // apparently deleted users do indeed have null values, so if the first user returned is deleted then there are failures.
            var someMember = actual.members.Where(x => !x.deleted).First();

            Assert.NotNull(someMember.id);
            Assert.NotNull(someMember.color);
            Assert.NotNull(someMember.real_name);
            Assert.NotNull(someMember.name);
            Assert.NotNull(someMember.team_id);
            Assert.NotNull(someMember.tz);
            Assert.NotNull(someMember.tz_label);
        }
Ejemplo n.º 4
0
        public void TestManualSubscribePresenceChangeAndManualPresenceChange()
        {
            // Arrange
            slackClient = this.fixture.CreateUserClient();
            using (var sync = new InSync())
            {
                slackClient.OnPresenceChanged += x =>
                {
                    if (x.user == slackClient.MySelf.id)
                    {
                        // Assert
                        sync.Proceed();
                    }
                };

                slackClient.SubscribePresenceChange(slackClient.MySelf.id);
            }

            using (var sync = new InSync())
            {
                slackClient.OnPresenceChanged += x =>
                {
                    if (x is ManualPresenceChange && x.user == slackClient.MySelf.id)
                    {
                        // Assert
                        sync.Proceed();
                    }
                };

                // Act
                slackClient.EmitPresence(x => x.AssertOk(), Presence.away);
                slackClient.EmitPresence(x => x.AssertOk(), Presence.auto);
            }
        }
Ejemplo n.º 5
0
        public void PostEphemeralMessage()
        {
            // given
            var client = this.fixture.UserClient;
            PostEphemeralResponse actual = null;

            string userName = this.fixture.Config.DirectMessageUser;
            string userId   = client.Users.First(x => x.name.Equals(userName, StringComparison.OrdinalIgnoreCase)).id;

            // when
            using (var sync = new InSync(nameof(SlackClient.PostEphemeralMessage)))
            {
                client.PostEphemeralMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    this.fixture.Config.TestChannel,
                    "Hi there!",
                    userId);
            }

            // then
            Assert.True(actual.ok, "Error while posting message to channel. ");
            Assert.Null(actual.error);
        }
Ejemplo n.º 6
0
        public void BlocksInAttachment()
        {
            // given
            var client = this.fixture.UserClient;
            PostMessageResponse actual = null;

            // when
            using (var sync = new InSync(nameof(SlackClient.PostMessage)))
            {
                client.PostMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    this.fixture.Config.TestChannel,
                    "These blocks are in an attachment",
                    attachments: new []
                {
                    new Attachment
                    {
                        blocks = SlackMother.SomeBlocks
                    }
                });
            }

            // then
            Assert.True(actual.ok, "Error while posting message to channel. ");
        }
Ejemplo n.º 7
0
        private SlackSocketClient CreateClient(string authToken, IWebProxy proxySettings = null)
        {
            SlackSocketClient client;

            LoginResponse loginResponse = null;

            using (var syncClient = new InSync($"{nameof(SlackClient.Connect)} - Connected callback"))
                using (var syncClientSocket = new InSync($"{nameof(SlackClient.Connect)} - SocketConnected callback"))
                    using (var syncClientSocketHello = new InSync($"{nameof(SlackClient.Connect)} - SocketConnected hello callback"))
                    {
                        client          = new SlackSocketClient(authToken, proxySettings);
                        client.OnHello += () => syncClientSocketHello.Proceed();
                        client.Connect(x =>
                        {
                            loginResponse = x;

                            Console.WriteLine($"Connected {x.ok}");
                            syncClient.Proceed();
                            if (!x.ok)
                            {
                                // If connect fails, socket connect callback is not called
                                syncClientSocket.Proceed();
                                syncClientSocketHello.Proceed();
                            }
                        }, () =>
                        {
                            Console.WriteLine("Socket Connected");
                            syncClientSocket.Proceed();
                        });
                    }

            loginResponse.AssertOk();

            return(client);
        }
Ejemplo n.º 8
0
        public void UploadFile_Succeeds()
        {
            // Arrange
            const int    FileSize = 500;
            const string FileName = "MyFile.bin";

            byte[] data = new byte[FileSize];

            // Act
            FileUploadResponse fileUploadResponse = null;

            using (var sync = new InSync(nameof(SlackClient.UploadFile)))
            {
                this.fixture.UserClient.UploadFile(c =>
                {
                    fileUploadResponse = c;
                    Assert.True(c.ok);
                    sync.Proceed();
                },
                                                   data, FileName, new[] { this.fixture.Config.TestChannel });
            }

            // Assert
            using (var sync = new InSync(nameof(SlackClient.UploadFile)))
            {
                this.fixture.UserClient.GetFileInfo(c =>
                {
                    Assert.True(c.ok);
                    Assert.Equal(FileSize, c.file.size);
                    Assert.Equal(FileName, c.file.name);
                    Assert.Equal(new[] { this.fixture.Config.TestChannel }, c.file.channels);
                    sync.Proceed();
                }, fileUploadResponse.file.id);
            }
        }
Ejemplo n.º 9
0
        public void UserList()
        {
            var client = this.fixture.UserClient;
            UserListResponse actual = null;

            using (var sync = new InSync(nameof(SlackClient.UserLookup)))
            {
                client.GetUserList(response =>
                {
                    actual = response;
                    sync.Proceed();
                });
            }

            Assert.True(actual.ok, "Error while fetching user list.");
            Assert.True(actual.members.Any());

            var someMember = actual.members.First();

            Assert.NotNull(someMember.id);
            Assert.NotNull(someMember.color);
            Assert.NotNull(someMember.real_name);
            Assert.NotNull(someMember.name);
            Assert.NotNull(someMember.team_id);
            Assert.NotNull(someMember.tz);
            Assert.NotNull(someMember.tz_label);
        }
Ejemplo n.º 10
0
        public void UpdatePresence()
        {
            var client = this.fixture.UserClient;

            using (var sync = new InSync(nameof(SlackClient.EmitPresence)))
            {
                client.EmitPresence((presence) =>
                {
                    presence.AssertOk();
                    sync.Proceed();
                }, Presence.away);
            }
        }
Ejemplo n.º 11
0
        public void UpdatePresence()
        {
            var client = ClientHelper.GetClient(_config.Slack.UserAuthToken);

            using (var sync = new InSync())
            {
                client.EmitPresence((presence) =>
                {
                    presence.AssertOk();
                    sync.Proceed();
                }, Presence.away);
            }
        }
Ejemplo n.º 12
0
        private AccessTokenResponse GetAccessToken(SlackClientHelpers slackClientHelpers, string clientId, string clientSecret, string redirectUri, string authCode)
        {
            AccessTokenResponse accessTokenResponse = null;

            using (var sync = new InSync(nameof(slackClientHelpers.GetAccessToken)))
            {
                slackClientHelpers.GetAccessToken(response =>
                {
                    accessTokenResponse = response;
                    sync.Proceed();
                }, clientId, clientSecret, redirectUri, authCode);
            }

            return(accessTokenResponse);
        }
Ejemplo n.º 13
0
        private static DeletedResponse DeleteMessage(SlackSocketClient client, string channel, DateTime messageTimestamp)
        {
            DeletedResponse deletedResponse = null;

            using (var sync = new InSync(nameof(SlackClient.DeleteMessage)))
            {
                client.DeleteMessage(response =>
                {
                    deletedResponse = response;
                    sync.Proceed();
                }, channel, messageTimestamp);
            }

            return(deletedResponse);
        }
Ejemplo n.º 14
0
        private static DateTime PostMessage(SlackSocketClient client, string channel)
        {
            MessageReceived sendMessageResponse = null;

            using (var sync = new InSync(nameof(SlackSocketClient.SendMessage)))
            {
                client.SendMessage(response =>
                {
                    sendMessageResponse = response;
                    sync.Proceed();
                }, channel, TestText);
            }

            Assert.NotNull(sendMessageResponse);
            Assert.Equal(TestText, sendMessageResponse.text);

            return(sendMessageResponse.ts);
        }
Ejemplo n.º 15
0
        private string PostedMessage(SlackSocketClient client)
        {
            string messageId = null;

            using (var sync = new InSync(nameof(SlackClient.PostMessage)))
            {
                client.PostMessage(
                    response =>
                {
                    messageId = response.ts;
                    Assert.True(response.ok, "Error while posting message to channel. ");
                    sync.Proceed();
                },
                    this.fixture.Config.TestChannel,
                    "Hi there!",
                    as_user: true);
            }
            return(messageId);
        }
Ejemplo n.º 16
0
        private SlackSocketClient CreateClient(string authToken, IWebProxy proxySettings = null, bool maintainPresenceChanges = false, Action <SlackSocketClient, PresenceChange> presenceChanged = null)
        {
            SlackSocketClient client;

            LoginResponse loginResponse = null;

            using (var syncClient = new InSync($"{nameof(SlackClient.Connect)} - Connected callback"))
                using (var syncClientSocket = new InSync($"{nameof(SlackClient.Connect)} - SocketConnected callback"))
                    using (var syncClientSocketHello = new InSync($"{nameof(SlackClient.Connect)} - SocketConnected hello callback"))
                    {
                        client = new SlackSocketClient(authToken, proxySettings, maintainPresenceChanges);

                        void OnPresenceChanged(PresenceChange x)
                        {
                            presenceChanged?.Invoke(client, x);
                        }

                        client.OnPresenceChanged += OnPresenceChanged;
                        client.OnHello           += () => syncClientSocketHello.Proceed();
                        client.Connect(x =>
                        {
                            loginResponse = x;

                            Console.WriteLine($"Connected {x.ok}");
                            syncClient.Proceed();
                            if (!x.ok)
                            {
                                // If connect fails, socket connect callback is not called
                                syncClientSocket.Proceed();
                                syncClientSocketHello.Proceed();
                            }
                        }, () =>
                        {
                            Console.WriteLine("Socket Connected");
                            syncClientSocket.Proceed();
                        });
                    }

            loginResponse.AssertOk();

            return(client);
        }
Ejemplo n.º 17
0
        public void PresenceSubscribe()
        {
            var client            = this.fixture.UserClientWithPresence;
            var directMessageUser = client.Users.FirstOrDefault(x => x.name == this.fixture.Config.DirectMessageUser);

            Assert.NotNull(directMessageUser);

            //UserListResponse actual = null;
            using (var sync = new InSync(nameof(SlackClient.UserLookup)))
            {
                client.OnPresenceChangeReceived += (user) =>
                {
                };
                client.OnUserChangeReceived += (user) =>
                {
                };
                client.SendPresenceSub(new[] { directMessageUser.id });
                sync.Proceed();
            }
        }
Ejemplo n.º 18
0
        public void TestConnectGetPresenceChanges()
        {
            // Arrange
            int presenceChangesRaisedCount = 0;

            using (var sync = new InSync(nameof(TestConnectGetPresenceChanges), this.fixture.ConnectionTimeout))
            {
                void OnPresenceChanged(SlackSocketClient sender, PresenceChange e)
                {
                    if (++presenceChangesRaisedCount == sender.Users.Count)
                    {
                        sync.Proceed();
                    }
                }

                // Act
                slackClient = this.fixture.CreateUserClient(maintainPresenceChangesStatus: true, presenceChanged: OnPresenceChanged);
            }

            // Assert
            Assert.True(slackClient.Users.All(x => x.presence != null));
        }
Ejemplo n.º 19
0
        public void ConversationList()
        {
            var client = this.fixture.UserClient;
            ConversationsListResponse actual = null;

            using (var sync = new InSync(nameof(SlackClient.ChannelLookup)))
            {
                client.GetConversationsList(response =>
                {
                    actual = response;
                    sync.Proceed();
                });
            }

            Assert.True(actual.ok, "Error while fetching conversation list.");
            Assert.True(actual.channels.Any());

            // check to null
            var someChannel = actual.channels.First();

            Assert.NotNull(someChannel.id);
            Assert.NotNull(someChannel.name);
        }
Ejemplo n.º 20
0
        public void AttachmentsWithActions()
        {
            // given
            var client = this.fixture.UserClient;
            PostMessageResponse actual = null;

            // when
            using (var sync = new InSync())
            {
                client.PostMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    this.fixture.Config.TestChannel,
                    string.Empty,
                    attachments: SlackMother.SomeAttachmentsWithActions);
            }

            // then
            Assert.True(actual.ok, "Error while posting message to channel. ");
        }
Ejemplo n.º 21
0
        public void ShouldJoinDirectMessageChannel()
        {
            // given
            var client = this.fixture.UserClient;
            JoinDirectMessageChannelResponse actual = null;

            string userName = this.fixture.Config.DirectMessageUser;
            string user     = client.Users.First(x => x.name.Equals(userName, StringComparison.InvariantCultureIgnoreCase)).id;

            // when
            using (var sync = new InSync(nameof(SlackClient.JoinDirectMessageChannel)))
            {
                client.JoinDirectMessageChannel(response =>
                {
                    actual = response;
                    sync.Proceed();;
                }, user);
            }

            // then
            Assert.True(actual.ok, "Error while joining user channel");
            Assert.NotEmpty(actual.channel.id);
        }
Ejemplo n.º 22
0
        public void Attachments()
        {
            // given
            var client = ClientHelper.GetClient(_config.Slack.UserAuthToken);
            PostMessageResponse actual = null;

            // when
            using (var sync = new InSync())
            {
                client.PostMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    _config.Slack.TestChannel,
                    string.Empty,
                    attachments: SlackMother.SomeAttachments);
            }

            // then
            Assert.IsTrue(actual.ok, "Error while posting message to channel. ");
        }
Ejemplo n.º 23
0
        private SlackSocketClient GetClient(string authToken)
        {
            SlackSocketClient client;

            using (var syncClient = new InSync($"{nameof(SlackClient.Connect)} - Connected callback"))
                using (var syncClientSocket = new InSync($"{nameof(SlackClient.Connect)} - SocketConnected callback"))
                {
                    client = new SlackSocketClient(authToken);
                    client.Connect(x =>
                    {
                        Console.WriteLine("Connected");
                        syncClient.Proceed();
                    }, () =>
                    {
                        Console.WriteLine("Socket Connected");
                        syncClientSocket.Proceed();
                    });
                }

            Assert.True(client.IsConnected, "Doh, still isn't connected");

            return(client);
        }
Ejemplo n.º 24
0
        public void SimpleMessageDelivery()
        {
            // given
            var client = this.fixture.UserClient;
            PostMessageResponse actual = null;

            // when
            using (var sync = new InSync(nameof(SlackClient.PostMessage)))
            {
                client.PostMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    this.fixture.Config.TestChannel,
                    "Hi there!");
            }

            // then
            Assert.True(actual.ok, "Error while posting message to channel. ");
            Assert.Equal("Hi there!", actual.message.text);
            Assert.Equal("message", actual.message.type);
        }
Ejemplo n.º 25
0
        public void SimpleMessageDelivery()
        {
            // given
            var client = ClientHelper.GetClient(_config.Slack.UserAuthToken);
            PostMessageResponse actual = null;

            // when
            using (var sync = new InSync())
            {
                client.PostMessage(
                    response =>
                {
                    actual = response;
                    sync.Proceed();
                },
                    _config.Slack.TestChannel,
                    "Hi there!");
            }

            // then
            Assert.IsTrue(actual.ok, "Error while posting message to channel. ");
            Assert.AreEqual(actual.message.text, "Hi there!");
            Assert.AreEqual(actual.message.type, "message");
        }