Checkpoint: 4/9/2019
[margebot.git] / tests / test_marge.py
index 8e9d450..2fc7429 100644 (file)
@@ -1,5 +1,5 @@
 """
-Mrgebot tests
+Margebot tests
 """
 # pragma pylint: disable=invalid-name
 # pragma pylint: disable=missing-docstring
@@ -12,12 +12,14 @@ Mrgebot tests
 from datetime import datetime
 import logging
 import json
+from queue import Empty
 
-import errbot
-from errbot.backends.test import testbot   # pylint: disable=unused-import
 import gitlab
 from dateutil.relativedelta import relativedelta
 import pytest
+import requests
+import errbot
+from errbot.backends.test import testbot   # pylint: disable=unused-import
 from plugins.marge import deltastr
 
 
@@ -31,9 +33,13 @@ class TestMarge(object):
     extra_plugin_dir = "./plugins/"
     loglevel = logging.INFO
 
+    @pytest.fixture(autouse=True)
+    def no_requests(self, monkeypatch):
+        monkeypatch.setattr(requests.sessions.Session, 'request', None)
+
     @pytest.fixture
     def margebot(self, testbot, monkeypatch, mocker):
-        testbot.push_message("!plugin config Marge {'CHATROOM_HOST': 'conference.test.com', 'GITLAB_HOST': 'gitlab.test.com', 'GITLAB_ADMIN_TOKEN': 'fake-token', 'CRONTAB':  '0 * * * *', 'VERIFY_SSL': True, 'CRONTAB_SOAK_HOURS': 1, 'WEBHOOK_URL': 'https://webhood.errbot.com:3142/margebot'}")
+        testbot.push_message("!plugin config Marge {'CHATROOM_HOST': 'conference.test.com', 'GITLAB_HOST': 'gitlab.test.com', 'GITLAB_ADMIN_TOKEN': 'fake-token', 'CRONTAB':  '0 * * * *', 'VERIFY_SSL': True, 'CRONTAB_SOAK_HOURS': 1, 'WEBHOOK_URL': 'https://webhook.errbot.com:3142/margebot'}")
         testbot.pop_message()
         testbot.push_message("!plugin config Webserver {'HOST': '0.0.0.0', 'PORT':3141, 'SSL': {'certificate': '', 'enabled': False, 'host': '0.0.0.0', 'key': '', 'port': 3142}}")
         testbot.pop_message()
@@ -138,13 +144,49 @@ class TestMarge(object):
                 return [{'user': 'user1', 'id': 3001}, {'user': 'user2', 'id': 3002}]
         monkeypatch.setattr(gitlab.Gitlab, 'getusers', mock_getusers)
 
+        def mock_getproject(self, repo):
+            return {
+                'id': 'projectid'
+            }
+        monkeypatch.setattr(gitlab.Gitlab, 'getproject', mock_getproject)
+
+        def mock_getprojecthooks(self, repo):
+            if repo == "(missing)":
+                return []
+            else:
+                return [{'id': 'hook_id', 'merge_requests_events': True, 'url': 'url'}]
+        monkeypatch.setattr(gitlab.Gitlab, 'getprojecthooks', mock_getprojecthooks)
+
         # Waiting on Gitlab 8.9 or greater
         # def mock_getapprovals(id):
         #      return []
         # monkeypatch.setattr(gitlab.Gitlab, 'getapprovals', mock_getapprovals)
 
+        def mock_getmergerequests(self, project, page, per_page, state=None):
+            return []
+        monkeypatch.setattr(gitlab.Gitlab, 'getmergerequests', mock_getmergerequests)
+
+        def mock_addprojecthook(self, project, url, **kwargs):
+            return True
+        monkeypatch.setattr(gitlab.Gitlab, 'addprojecthook', mock_addprojecthook)
+
+        def mock_editprojecthook(self, project, hook_id, url, **kwargs):
+            return True
+        monkeypatch.setattr(gitlab.Gitlab, 'editprojecthook', mock_editprojecthook)
         return gitlab.Gitlab
 
+
+#    @pytest.fixture
+#    def MargeGitlab(self, monkeypatch):
+#        def mock_addprojecthook_extra(self, project, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
+#            return True
+#        monkeypatch.setattr('plugins.marge.MargeGitlab.addprojecthook_extra', mock_addprojecthook_extra)
+#
+#        def mock_editprojecthook_extra(self, project, hook_id, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
+#            return True
+#        monkeypatch.setattr('plugins.marge.MargeGitlab.editprojecthook_extra', mock_editprojecthook_extra)
+#        return marge.MargeGitlab
+
     @pytest.fixture
     def gitlab_no_reviews(self, gitlab, monkeypatch):
         def mock_getmergerequest(self, project, iid):
@@ -154,15 +196,32 @@ class TestMarge(object):
 
     @pytest.fixture
     def gitlab_one_review(self, gitlab, monkeypatch):
+        ret_mr = {'id': 'mr_id',
+                  'author': {'id': 2001},
+                  'created_at': 'Oct 29, 2017 2:37am',
+                  'merge_status': 'can_be_merged',
+                  'state': 'opened',
+                  'upvotes': 0,
+                  'web_url': 'http://gitlab.example.com/sample/mr/2001',
+                  'work_in_progress': False}
+
         def mock_getmergerequest(self, project, iid):
-            return {'author': {'id': 2001},
-                    'created_at': 'Oct 29, 2017 2:37am',
-                    'merge_status': 'can_be_merged',
-                    'state': 'opened',
-                    'upvotes': 0,
-                    'web_url': 'http://gitlab.example.com/sample/mr/2001',
-                    'work_in_progress': False}
+            return ret_mr
         monkeypatch.setattr(gitlab, 'getmergerequest', mock_getmergerequest)
+
+        def mock_getmergerequests(self, project, page, per_page, state=None):
+            if state and state == 'opened':
+                return [ret_mr]
+            else:
+                return []
+        monkeypatch.setattr(gitlab, 'getmergerequests', mock_getmergerequests)
+        return gitlab
+
+    @pytest.fixture
+    def gitlab_already_watching(self, gitlab, monkeypatch):
+        def mock_getprojecthooks(self, project):
+            return [{'id': 'hook_id', 'merge_requests_events': True, 'url': 'https://webhook.errbot.com:3142/margebot/room1,room2,room3'}]
+        monkeypatch.setattr(gitlab, 'getprojecthooks', mock_getprojecthooks)
         return gitlab
 
     # ============================================================================
@@ -187,12 +246,11 @@ class TestMarge(object):
         help_message = margebot.pop_message()
         assert "Marge" in help_message
         assert '!reviews' in help_message
-        assert '!hello' in help_message
-        assert '!xyzzy' in help_message
+        assert '!watchrepo' in help_message
 
-    def test_hello(self, margebot):
-        margebot.push_message('!hello')
-        assert 'Hi there' in margebot.pop_message()
+    def test_good_bot(self, margebot):
+        margebot.push_message('good bot')
+        assert 'Best bot' in margebot.pop_message()
 
 #    def test_xyzzy(self, margebot):
 #        margebot.push_message('!xyzzy')
@@ -204,6 +262,29 @@ class TestMarge(object):
         margebot.push_message('!webstatus')
         assert 'margebot/<rooms>' in margebot.pop_message()
 
+    def test_watchrepo(self, margebot, gitlab):
+        margebot.push_message('!watchrepo group/new_repo room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Now watching for new MRs in the group/new_repo repo to the room1,room2,room3 room(s)' in pm
+        assert 'No open MRs were found in the repo.' in pm
+
+    def test_watchrepo_already_watching_repo(self, margebot, gitlab_already_watching):
+        margebot.push_message('!watchrepo group/new_repo room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Already reporting group/new_repo MRs to the room1,room2,room3 room(s)' in pm
+
+    def test_watchrepo_updating_roomlist(self, margebot, gitlab_already_watching):
+        margebot.push_message('!watchrepo group/new_repo room4,room5,room6')
+        pm = margebot.pop_message()
+        assert 'Updating room list for group/new_repo MRs from room1,room2,room3 to room4,room5,room6' in pm
+
+#    @pytest.mark.skip(reason='until I figure out how to mock MargeGitlab')
+    def test_watchrepo_existing_mr(self, margebot, gitlab_one_review):
+        margebot.push_message('!watchrepo sample/mr room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Now watching for new MRs in the sample/mr repo to the room1,room2,room3 room(s)' in pm
+        assert '1 open MR was found in the repo.' in pm
+
     def test_gitlab_hook(self, margebot, gitlab):
         request = json.dumps({'object_kind': 'merge_request',
                               'object_attributes': {
@@ -325,3 +406,20 @@ class TestMarge(object):
         output = one_waiting_review.pop_message()
         assert 'These MRs need some attention' in output
         assert 'http://gitlab.example.com/sample/mr/2001 (opened' in output
+
+    def test_dont_blame_margebot(self, margebot):
+        margebot.push_message('I blame margebot')
+        assert "say it ain't so" in margebot.pop_message()
+
+    def test_margefest(self, margebot):
+        margebot.push_message('magfest')
+        assert 'MargeFest' in margebot.pop_message()
+
+    def test_margebot_sucks(self, margebot):
+        margebot.push_message('margebot sucks')
+        assert 'steering committee' in margebot.pop_message()
+
+    def test_margebot_unknown_command(self, margebot):
+        margebot.push_message('Margebot, alacazam')
+        with pytest.raises(Empty):
+            margebot.pop_message()