#377 Implement two widgets' should_invalidate methods
Merged 4 years ago by abompard. Opened 4 years ago by abompard.

@@ -10,10 +10,11 @@ 

  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 @@ 

          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:

@@ -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))

@@ -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))

@@ -76,4 +76,15 @@ 

          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"]

+             )

@@ -87,4 +87,10 @@ 

          )

  

      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'])