Repository: opsdroid/opsdroid (language: python)

Issue:
Do not parse own messages in Mattermost connector
# Description

Fixes #1775 

Includes PR #1774 because it's a prerequisite for being able to use Mattermost in the first place (branched this fix off the former fix).

## Status
**READY**

## Type of change

- Bug fix (non-breaking change which fixes an issue)

# How Has This Been Tested?

- Tested manually

I haven't had the time to dive into Opsdroid unit testing approach and workflow unfortunately.

# Checklist:

- [x] I have performed a self-review of my own code
- [ ] I have made corresponding changes to the documentation (if applicable)
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes



Discussion / hints:
Oooff... I think I broke this when It tried to fix the merge conflict 😢 
@FabioRosado should be fixed now.
I've slightly changed the logic to make sure a message is also parsed when the bot id is unknown for some reason. This fixes the unit test failure.

(Unrelated to this PR: at first I still had an assertion error in `tests/test_events.py::TestVideo::test_video` but that's solved by installing `ffmpeg`.)
# [Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid) Report
> Merging [#1776](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid) (d49722e) into [master](https://codecov.io/gh/opsdroid/opsdroid/commit/638e3a85e6a557a18097d06ef6d5f4d1c50bd0a1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid) (638e3a8) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/opsdroid/opsdroid/pull/1776/graphs/tree.svg?width=650&height=150&src=pr&token=9qJfZL4bOO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid)](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid)

```diff
@@           Coverage Diff           @@
##           master    #1776   +/-   ##
=======================================
  Coverage   99.28%   99.28%           
=======================================
  Files          78       78           
  Lines        4584     4586    +2     
=======================================
+ Hits         4551     4553    +2     
  Misses         33       33           
```


| [Impacted Files](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid) | Coverage Δ | |
|---|---|---|
| [opsdroid/connector/mattermost/\_\_init\_\_.py](https://codecov.io/gh/opsdroid/opsdroid/pull/1776/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid#diff-b3BzZHJvaWQvY29ubmVjdG9yL21hdHRlcm1vc3QvX19pbml0X18ucHk=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid). Last update [638e3a8...d49722e](https://codecov.io/gh/opsdroid/opsdroid/pull/1776?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opsdroid).

@FabioRosado thanks for merging -- you beat me to it, I just added a unit test to verify the new behaviour :-)
Oops apologies for that Feel free to create a new PR with the test 😄 

Fix patch:
diff --git a/docs/connectors/mattermost.md b/docs/connectors/mattermost.md
index 8d62ad39e..fe1a99733 100644
--- a/docs/connectors/mattermost.md
+++ b/docs/connectors/mattermost.md
@@ -44,3 +44,5 @@ hi
 opsdroid APP [9:06 PM]
 Hi daniccan
 ```
+
+The connector doesn't parse messages that have been posted by the bot itself.
diff --git a/opsdroid/connector/mattermost/__init__.py b/opsdroid/connector/mattermost/__init__.py
index 5d6bfb431..fe44d9c11 100644
--- a/opsdroid/connector/mattermost/__init__.py
+++ b/opsdroid/connector/mattermost/__init__.py
@@ -93,8 +93,8 @@ async def process_message(self, raw_message):
         if "event" in message and message["event"] == "posted":
             data = message["data"]
             post = json.loads(data["post"])
-            # don't parse our own messages (https://github.com/opsdroid/opsdroid/issues/1775)
-            # (but also parse if somehow our bot_id is unknown, like in the unit tests)
+            # if connected to Mattermost, don't parse our own messages
+            # (https://github.com/opsdroid/opsdroid/issues/1775)
             if self.bot_id is None or self.bot_id != post["user_id"]:
                 await self.opsdroid.parse(
                     Message(


Test patch:
diff --git a/tests/test_connector_mattermost.py b/tests/test_connector_mattermost.py
index 1bcf5b8a9..830b95683 100644
--- a/tests/test_connector_mattermost.py
+++ b/tests/test_connector_mattermost.py
@@ -153,6 +153,75 @@ async def test_process_message(self):
         await connector.process_message(message)
         self.assertTrue(connector.opsdroid.parse.called)
 
+    async def test_do_not_process_own_message(self):
+        """Test that we do not process our own messages when connected to Mattermost."""
+        connector = ConnectorMattermost(
+            {
+                "token": "abc123",
+                "url": "localhost",
+                "team-name": "opsdroid",
+                "scheme": "http",
+            },
+            opsdroid=OpsDroid(),
+        )
+        opsdroid = amock.CoroutineMock()
+        opsdroid.eventloop = self.loop
+        connector.mm_driver.login = mock.MagicMock()
+        connector.mm_driver.login.return_value = {"id": "1", "username": "opsdroid_bot"}
+        await connector.connect()
+        self.assertEqual("1", connector.bot_id)
+        self.assertEqual("opsdroid_bot", connector.bot_name)
+
+        connector.opsdroid = amock.CoroutineMock()
+        connector.opsdroid.eventloop = self.loop
+        connector.opsdroid.parse = amock.CoroutineMock()
+
+        post = json.dumps(
+            {
+                "id": "wr9wetwc87bgdcx6opkjaxwb7b",
+                "create_at": 1574001673420,
+                "update_at": 1574001673420,
+                "edit_at": 0,
+                "delete_at": 0,
+                "is_pinned": False,
+                "user_id": "1",
+                "channel_id": "hdnm8gbxfp8bmcns7oswmwur4r",
+                "root_id": "",
+                "parent_id": "",
+                "original_id": "",
+                "message": "hello",
+                "type": "",
+                "props": {},
+                "hashtags": "",
+                "pending_post_id": "mrtopue9oigr8poa3bgfq4if4a:1574001673372",
+                "metadata": {},
+            }
+        )
+
+        message = json.dumps(
+            {
+                "event": "posted",
+                "data": {
+                    "channel_display_name": "@daniccan",
+                    "channel_name": "546qd6zsyffcfcafd77a3kdadr__mrtopue9oigr8poa3bgfq4if4a",
+                    "channel_type": "D",
+                    "mentions": '["546qd6zsyffcfcafd77a3kdadr"]',
+                    "post": post,
+                    "sender_name": "@opsdroid_bot",
+                    "team_id": "",
+                },
+                "broadcast": {
+                    "omit_users": "",
+                    "user_id": "",
+                    "channel_id": "hdnm8gbxfp8bmcns7oswmwur4r",
+                    "team_id": "",
+                },
+                "seq": 4,
+            }
+        )
+        await connector.process_message(message)
+        self.assertFalse(connector.opsdroid.parse.called)
+
     async def test_send_message(self):
         connector = ConnectorMattermost(
             {