class TestOptOutApplication(VumiTestCase): @inlineCallbacks def setUp(self): self.app_helper = self.add_helper(AppWorkerHelper(OptOutApplication)) self.app = yield self.app_helper.get_app_worker({}) conv = yield self.app_helper.create_conversation() yield self.app_helper.start_conversation(conv) self.conversation = yield self.app_helper.get_conversation(conv.key) vumi_api = self.app_helper.vumi_helper.get_vumi_api() self.opt_out_store = OptOutStore(vumi_api.manager, self.conversation.user_account.key) @inlineCallbacks def test_sms_opt_out(self): yield self.app_helper.make_dispatch_inbound("STOP", from_addr="12345", to_addr="666", conv=self.conversation) [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(msg.get('content'), "You have opted out") opt_out = yield self.opt_out_store.get_opt_out("msisdn", "12345") self.assertNotEqual(opt_out, None) @inlineCallbacks def test_sms_opt_out_no_account(self): yield self.app_helper.make_dispatch_inbound("STOP", from_addr="12345", to_addr="666") [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual( msg.get('content'), "Your opt-out was received but we failed to link it " "to a specific service, please try again later.") @inlineCallbacks def test_http_opt_out(self): yield self.app_helper.make_dispatch_inbound("STOP", from_addr="12345", to_addr="666", conv=self.conversation, transport_type="http_api") [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(json.loads(msg['content']), { "msisdn": "12345", "opted_in": False, }) opt_out = yield self.opt_out_store.get_opt_out("msisdn", "12345") self.assertNotEqual(opt_out, None)
class TestOptOutApplication(VumiTestCase): @inlineCallbacks def setUp(self): self.app_helper = self.add_helper(AppWorkerHelper(OptOutApplication)) self.app = yield self.app_helper.get_app_worker({}) conv = yield self.app_helper.create_conversation() yield self.app_helper.start_conversation(conv) self.conversation = yield self.app_helper.get_conversation(conv.key) vumi_api = self.app_helper.vumi_helper.get_vumi_api() self.opt_out_store = OptOutStore( vumi_api.manager, self.conversation.user_account.key) @inlineCallbacks def test_sms_opt_out(self): yield self.app_helper.make_dispatch_inbound( "STOP", from_addr="12345", to_addr="666", conv=self.conversation) [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(msg.get('content'), "You have opted out") opt_out = yield self.opt_out_store.get_opt_out("msisdn", "12345") self.assertNotEqual(opt_out, None) @inlineCallbacks def test_sms_opt_out_no_account(self): yield self.app_helper.make_dispatch_inbound( "STOP", from_addr="12345", to_addr="666") [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(msg.get('content'), "Your opt-out was received but we failed to link it " "to a specific service, please try again later.") @inlineCallbacks def test_http_opt_out(self): yield self.app_helper.make_dispatch_inbound( "STOP", from_addr="12345", to_addr="666", conv=self.conversation, transport_type="http_api") [msg] = self.app_helper.get_dispatched_outbound() self.assertEqual(json.loads(msg['content']), { "msisdn": "12345", "opted_in": False, }) opt_out = yield self.opt_out_store.get_opt_out("msisdn", "12345") self.assertNotEqual(opt_out, None)
def show_opt_outs(self, user_api, email_address): opt_out_store = OptOutStore(user_api.manager, user_api.user_account_key) opt_outs = opt_out_store.list_opt_outs() print "Address Type, Address, Message ID, Timestamp" print "============================================" for key in opt_outs: addr_type, _colon, addr = key.partition(":") opt_out = opt_out_store.get_opt_out(addr_type, addr) print "%s, %s, %s, %s" % (addr_type, addr, opt_out.message, opt_out.created_at)
def _filter_opted_out_contacts(self, contacts, delivery_class): # TODO: Less hacky address type handling. address_type = 'gtalk' if delivery_class == 'gtalk' else 'msisdn' contacts = yield contacts opt_out_store = OptOutStore( self.api.manager, self.user_api.user_account_key) filtered_contacts = [] for contact in contacts: contact_addr = contact.addr_for(delivery_class) if contact_addr: opt_out = yield opt_out_store.get_opt_out( address_type, contact_addr) if not opt_out: filtered_contacts.append(contact) returnValue(filtered_contacts)