From 2b75eeece26aa09a646ae545d5c4dc29393e9ccb Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Aug 25 2017 08:54:40 +0000 Subject: Implement two widgets' should_invalidate methods Fixes: #349, #350 --- diff --git a/hubs/tests/widgets/__init__.py b/hubs/tests/widgets/__init__.py index 653e9d6..73de0fc 100644 --- a/hubs/tests/widgets/__init__.py +++ b/hubs/tests/widgets/__init__.py @@ -10,10 +10,11 @@ from hubs.tests import APPTest, FakeAuthorization, widget_instance class WidgetTest(APPTest): plugin = None # The name in hubs.widgets.registry + initial_widget_config = None def populate(self): super(WidgetTest, self).populate() - hub = hubs.models.Hub.by_name('ralph') + hub = hubs.models.Hub.query.get("ralph") widget = hubs.models.Widget( plugin='library', index=51, @@ -23,6 +24,17 @@ class WidgetTest(APPTest): hub.widgets.append(widget) self.session.commit() + def _add_widget_under_test(self): + hub = hubs.models.Hub.query.get("ralph") + self.widget = hubs.models.Widget( + plugin=self.plugin, + index=1, + _config=json.dumps(self.initial_widget_config or {}), + ) + hub.widgets.append(self.widget) + self.session.commit() + self.session.refresh(self.widget) + def _test_view_authz(self): # Test authorizations on the root view. if not self.plugin: diff --git a/hubs/tests/widgets/test_githubissues.py b/hubs/tests/widgets/test_githubissues.py new file mode 100644 index 0000000..d36c243 --- /dev/null +++ b/hubs/tests/widgets/test_githubissues.py @@ -0,0 +1,54 @@ +from __future__ import unicode_literals + +from . import WidgetTest + + +class TestGithubIssues(WidgetTest): + + plugin = "githubissues" + initial_widget_config = { + "org": "fedora-infra", + "repo": "fedora-hubs", + } + + def populate(self): + super(TestGithubIssues, self).populate() + self._add_widget_under_test() + + def _get_should_invalidate_result(self, msg): + func = self.widget.module.get_cached_functions()['GetIssues'] + return func(self.widget).should_invalidate(msg) + + def test_should_invalidate_wrong_topic(self): + msg = {'topic': 'hubs.widget.update.WRONG.TOPIC'} + self.assertFalse(self._get_should_invalidate_result(msg)) + + def test_should_invalidate_good_match(self): + msg = { + 'topic': 'tests.github.issue.opened', + 'msg': { + 'organization': 'fedora-infra', + 'repository': 'fedora-hubs', + } + } + self.assertTrue(self._get_should_invalidate_result(msg)) + + def test_should_invalidate_wrong_org(self): + msg = { + 'topic': 'tests.github.issue.opened', + 'msg': { + 'organization': 'not_fedora_infra', + 'repository': 'fedora-hubs', + } + } + self.assertFalse(self._get_should_invalidate_result(msg)) + + def test_should_invalidate_wrong_repo(self): + msg = { + 'topic': 'tests.github.issue.opened', + 'msg': { + 'organization': 'fedora-infra', + 'repository': 'not-fedora-hubs', + } + } + self.assertFalse(self._get_should_invalidate_result(msg)) diff --git a/hubs/tests/widgets/test_pagureissues.py b/hubs/tests/widgets/test_pagureissues.py new file mode 100644 index 0000000..a55c272 --- /dev/null +++ b/hubs/tests/widgets/test_pagureissues.py @@ -0,0 +1,45 @@ +from __future__ import unicode_literals + +from . import WidgetTest + + +class TestPagureIssues(WidgetTest): + + plugin = "pagureissues" + initial_widget_config = { + "repo": "fedora-hubs", + } + + def populate(self): + super(TestPagureIssues, self).populate() + self._add_widget_under_test() + + def _get_should_invalidate_result(self, msg): + func = self.widget.module.get_cached_functions()['GetIssues'] + return func(self.widget).should_invalidate(msg) + + def test_should_invalidate_wrong_topic(self): + msg = {'topic': 'hubs.widget.update.WRONG.TOPIC'} + self.assertFalse(self._get_should_invalidate_result(msg)) + + def test_should_invalidate_good_match(self): + msg = { + 'topic': 'tests.pagure.issue.new', + 'msg': { + "project": { + "name": "fedora-hubs", + }, + }, + } + self.assertTrue(self._get_should_invalidate_result(msg)) + + def test_should_invalidate_wrong_repo(self): + msg = { + 'topic': 'tests.pagure.issue.new', + 'msg': { + "project": { + "name": "not-fedora-hubs", + }, + }, + } + self.assertFalse(self._get_should_invalidate_result(msg)) diff --git a/hubs/widgets/githubissues/__init__.py b/hubs/widgets/githubissues/__init__.py index e2a25e2..0575545 100644 --- a/hubs/widgets/githubissues/__init__.py +++ b/hubs/widgets/githubissues/__init__.py @@ -76,4 +76,15 @@ class GetIssues(CachedFunction): return all_issues def should_invalidate(self, message): - raise NotImplementedError + if ".github.issue." not in message["topic"]: + return False + try: + org = message['msg']['organization'] + repo = message['msg']['repository'] + except KeyError: + return False + return ( + org == self.instance.config["org"] + and + repo == self.instance.config["repo"] + ) diff --git a/hubs/widgets/pagureissues/__init__.py b/hubs/widgets/pagureissues/__init__.py index d8db995..3ba90d9 100644 --- a/hubs/widgets/pagureissues/__init__.py +++ b/hubs/widgets/pagureissues/__init__.py @@ -87,4 +87,10 @@ class GetIssues(CachedFunction): ) def should_invalidate(self, message): - raise NotImplementedError + if ".pagure.issue." not in message["topic"]: + return False + try: + repo = message['msg']['project']['name'] + except KeyError: + return False + return (repo == self.instance.config['repo'])