Beispiel #1
0
    def test_valid_paths(self):
        """Assert the valid paths is lazy-loaded on attribute access."""
        find_recipients = tasks._FindRecipients()

        self.assertTrue(find_recipients._valid_paths is None)
        self.assertEqual(fmn_lib.load_rules(root="fmn.rules"), find_recipients.valid_paths)
        self.assertTrue(find_recipients._valid_paths is find_recipients.valid_paths)
Beispiel #2
0
    def test_user_preferences(self):
        """Assert the user preferences are lazy-loaded on attribute access."""
        find_recipients = tasks._FindRecipients()
        expected_preferences = fmn_lib.load_preferences(
            cull_disabled=True, cull_backends=['desktop'])

        self.assertTrue(find_recipients._user_preferences is None)
        self.assertEqual(expected_preferences, find_recipients.user_preferences)
        self.assertTrue(find_recipients._user_preferences is find_recipients.user_preferences)
Beispiel #3
0
    def test_run_fmn_message(self, mock_update_prefs):
        """Assert messages with .fmn. in the topic cause preferences to be refreshed."""
        find_recipients = tasks._FindRecipients()
        message = {
            'topic': 'fmn.internal.refresh_cache',
            'body': 'jcline.id.fedoraproject.org',
        }

        find_recipients.run(message)
        mock_update_prefs.assert_called_once_with(
            'jcline.id.fedoraproject.org', find_recipients.user_preferences)
Beispiel #4
0
    def test_run_no_recipients(self, mock_conns):
        """Assert messages without any recipients results in no messages to the backend."""
        find_recipients = tasks._FindRecipients()
        user = models.User(
            openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org')
        fmn_lib.defaults.create_defaults_for(self.sess, user)
        self.sess.commit()
        message = {
            'topic': 'com.example.fmn.message',
            'body': {
                'msg': {'openid': 'jcline.id.fedoraproject.org'}
            }
        }

        find_recipients.run(message)

        conn = mock_conns.__getitem__.return_value.acquire.return_value.__enter__.return_value
        self.assertEqual(0, conn.Producer.return_value.publish.call_count)
Beispiel #5
0
    def test_run_recipients_found(self, mock_conns, mock_rules_cache, mock_fmt_sse):
        """Assert messages are sent to the backend when recipients are found."""
        mock_fmt_sse.return_value = 'Such pretty, very message'
        find_recipients = tasks._FindRecipients()

        mock_rules_cache.configure(backend='dogpile.cache.memory')

        # Set up a user with preferences to match the message
        user = models.User(
            openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org')
        self.sess.add(user)
        context = models.Context(
            name='sse', description='description', detail_name='SSE', icon='wat')
        self.sess.add(context)
        self.sess.commit()
        fmn_lib.defaults.create_defaults_for(self.sess, user, detail_values={'sse': 'jcline'})
        self.sess.commit()
        preference = models.Preference.query.filter_by(
            context_name='sse', openid='jcline.id.fedoraproject.org').first()
        preference.enabled = True
        self.sess.add(preference)
        self.sess.commit()

        message = {
            "topic": "org.fedoraproject.prod.buildsys.build.state.change",
            'body': {
                "username": "******",
                "i": 1,
                "timestamp": 1505399391.0,
                "msg_id": "2017-7c65d9ff-85c0-42bb-8288-9b6112cb3da2",
                "topic": "org.fedoraproject.prod.buildsys.build.state.change",
                "msg": {
                    "build_id": 970796,
                    "old": 0,
                    "name": "fedmsg",
                    "task_id": 21861152,
                    "attribute": "state",
                    "request": [
                        ("git://pkgs.fedoraproject.org/rpms/fedmsg?#"
                         "870987e84539239a22170475bbf13ac4d2ef4382"),
                        "f26-candidate",
                        {},
                    ],
                    "instance": "primary",
                    "version": "1.0.1",
                    "owner": "jcline",
                    "new": 1,
                    "release": "4.fc26"
                }
            }
        }

        expected_published_message = {
            'context': u'sse',
            'recipient': {
                'triggered_by_links': True,
                'markup_messages': False,
                'user': u'jcline.id.fedoraproject.org',
                'filter_name': u'Events referring to my username',
                u'SSE': u'jcline',
                'filter_oneshot': False,
                'filter_id': 2,
                'shorten_links': False,
                'verbose': True
            },
            'fedmsg': message,
            'formatted_message': 'Such pretty, very message'
        }

        find_recipients.run(message)
        conn = mock_conns.__getitem__.return_value.acquire.return_value.__enter__.return_value
        conn.Producer.return_value.publish.assert_called_with(
            expected_published_message,
            routing_key=constants.BACKEND_QUEUE_PREFIX + 'sse',
            declare=[Queue(constants.BACKEND_QUEUE_PREFIX + 'sse', durable=True)],
        )