def setUp(self): super(LoadPreferenceTests, self).setUp() user = models.User(openid='jcline', openid_url='http://jcline.id.fedoraproject.org/') user2 = models.User( openid='bowlofeggs', openid_url='http://bowlofeggs.id.fedoraproject.org/') context = models.Context(name='pidgeon', description='A bird', detail_name='Bird', icon='?') context2 = models.Context(name='post', description='Old school', detail_name='The Post', icon='?') pref = models.Preference(openid='jcline', enabled=True, context_name='pidgeon') pref2 = models.Preference(openid='jcline', enabled=False, context_name='post') pref3 = models.Preference(openid='bowlofeggs', enabled=False, context_name='post') for obj in (user, user2, context, context2, pref, pref2, pref3): self.sess.add(obj) self.sess.commit()
def test_irc_pending(self, mock_conns): """Assert the expected message is dispatched for pending irc nick confirmation.""" user = models.User( openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') context = models.Context( name='irc', description='description', detail_name='irc nick', icon='wat') confirmation = models.Confirmation( secret='a'*32, detail_value='jcline', user=user, context=context) self.sess.add(confirmation) self.sess.commit() expected_message = { 'context': 'irc', 'recipient': { 'irc nick': 'jcline', 'user': '******', 'triggered_by_links': False, 'confirmation': True, }, 'fedmsg': {}, 'formatted_message': u'', # right now the backend still formats the message } tasks.confirmations() conn = mock_conns.__getitem__.return_value.acquire.return_value.__enter__.return_value self.assertEqual( expected_message, conn.Producer.return_value.publish.call_args_list[0][0][0])
def setUp(self): super(BatchTests, self).setUp() 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() self. 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" } } } self.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 }
def test_expired_reaped(self, mock_conns): """Assert confirmations that have expired are deleted and not sent.""" user = models.User( openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') context = models.Context( name='irc', description='description', detail_name='irc nick', icon='wat') created = datetime.datetime.utcnow() - datetime.timedelta(days=2) confirmation = models.Confirmation( created_on=created, secret='a'*32, detail_value='jcline', user=user, context=context) self.sess.add(confirmation) self.sess.commit() tasks.confirmations() conn = mock_conns.__getitem__.return_value.acquire.return_value.__enter__.return_value self.assertEqual(0, conn.Producer.return_value.publish.call_count)
def setUp(self): super(BatchReadyTests, self).setUp() # Set up a user with preferences to match the message self.user = models.User( openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') self.sess.add(self.user) self.context = models.Context( name='sse', description='description', detail_name='SSE', icon='wat') self.sess.add(self.context) self.sess.commit() fmn_lib.defaults.create_defaults_for(self.sess, self.user, detail_values={'sse': 'jcline'}) self.sess.commit() self.preference = models.Preference.query.filter_by( context_name='sse', openid='jcline.id.fedoraproject.org').first() self.preference.enabled = True self.sess.add(self.preference) self.sess.commit()
def test_handled_valid(self, mock_conns): """ Assert confirmation is placed in the 'valid' state until a backend marks it otherwise. """ user = models.User( openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') context = models.Context( name='irc', description='description', detail_name='irc nick', icon='wat') confirmation = models.Confirmation( secret='a'*32, detail_value='jcline', user=user, context=context) self.sess.add(confirmation) self.sess.commit() tasks.confirmations() confirmation = models.Confirmation.query.all() self.assertEqual(1, len(confirmation)) self.assertEqual('valid', confirmation[0].status)
def test_deliver_550_new_email(self): """ Assert when email fails with 550 and the email set doesn't match FAS, the preference is updated with the new email. """ user = models.User(openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') self.sess.add(user) context = models.Context(name='email', description='description', detail_name='email', icon='wat') self.sess.add(context) self.sess.commit() defaults.create_defaults_for( self.sess, user, detail_values={'email': '*****@*****.**'}) self.sess.commit() preference = models.Preference.query.filter_by( context_name='email', openid='jcline.id.fedoraproject.org').first() preference.enabled = True self.sess.add(preference) self.sess.commit() config = { 'fmn.email.mailserver': 'localhost:25', 'fmn.email.from_address': '*****@*****.**', } backend = mail.EmailBackend(config) recipient = { 'email address': '*****@*****.**', 'user': '******', } backend.deliver('my mail', recipient, {}) preference = models.Preference.query.filter_by( context_name='email', openid='jcline.id.fedoraproject.org').first() self.assertTrue(preference.enabled) self.assertEqual(u'*****@*****.**', preference.detail_values[0].value)
def test_email_pending(self, mock_conns): """Assert the expected message is dispatched for pending email confirmation.""" user = models.User( openid='jcline.id.fedoraproject.org', openid_url='http://jcline.id.fedoraproject.org') context = models.Context( name='email', description='description', detail_name='email', icon='wat') confirmation = models.Confirmation( secret='a'*32, detail_value='*****@*****.**', user=user, context=context) self.sess.add(confirmation) self.sess.commit() expected_email = """Precedence: Bulk Auto-Submitted: auto-generated From: [email protected] To: [email protected] Subject: Confirm notification email jcline.id.fedoraproject.org has requested that notifications be sent to this email address * To accept, visit this address: http://localhost:5000/confirm/accept/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * Or, to reject you can visit this address: http://localhost:5000/confirm/reject/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Alternatively, you can ignore this. This is an automated message, please email [email protected] if you have any concerns/issues/abuse.""" expected_message = { 'context': 'email', 'recipient': { 'email': '*****@*****.**', 'user': '******', 'triggered_by_links': False, 'confirmation': True, }, 'fedmsg': {}, 'formatted_message': expected_email, } tasks.confirmations() conn = mock_conns.__getitem__.return_value.acquire.return_value.__enter__.return_value self.assertEqual( expected_message, conn.Producer.return_value.publish.call_args_list[0][0][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)], )