Пример #1
0
def run(settings):
    'Connect to IMAP server and process messages'
    countByKey = sms.process(settings)
    return '\n'.join('%s: %s' % (key.capitalize(), count)
                     for key, count in countByKey.iteritems())
Пример #2
0
    def test_update_smsAddress(self):
        'Make sure that updating smsAddresses works'
        url = self.get_url('user_update')
        db.query(SMSAddress).delete()
        userSID, userSCode, userSEmail = db.query(
            User.id, User.code,
            User.email).filter_by(username=self.userS['username']).first()
        userAID, userACode, userAEmail = db.query(
            User.id, User.code,
            User.email).filter_by(username=self.userA['username']).first()
        userIID, userICode, userIEmail = db.query(
            User.id, User.code,
            User.email).filter_by(username=self.userI['username']).first()

        imapServer = sms.connect(self.router.registry.settings)
        # Register an invalid email address
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userSID, userSCode),
                                 fromWhom=''))
        # Register using an invalid userID or userCode
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (-1, userSCode),
                                 fromWhom='sms_' + userSEmail))
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userSID, -1),
                                 fromWhom='sms_' + userSEmail))
        # Register three email addresses
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userSID, userSCode),
                                 fromWhom='sms_' + userSEmail))
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userAID, userACode),
                                 fromWhom='sms_' + userAEmail))
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userIID, userICode),
                                 fromWhom='sms_' + userIEmail))
        # Register an address that already exists for the given user
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (userSID, userSCode),
                                 fromWhom='sms_' + userSEmail))
        # Remove an email address
        imapServer.revive(
            'inbox',
            imapIO.build_message(subject='%s-%s' % (0, 'xxx'),
                                 fromWhom='sms_' + userSEmail))
        # Process
        sms.process(self.router.registry.settings)
        # Make sure we only have two registered SMS address
        self.assertEqual(2, db.query(SMSAddress).count())
        smsAddressA = db.query(SMSAddress).filter_by(user_id=userAID).first()
        smsAddressI = db.query(SMSAddress).filter_by(user_id=userIID).first()

        # Get token
        self.login(self.userI)
        token = get_token(self.get(url).unicode_body)

        params = ReplaceableDict(token=token,
                                 smsAddressAction='activate',
                                 smsAddressID=smsAddressI.id)
        # Activate an smsAddress that doesn't belong to the user
        self.assert_json(
            self.post(url, params.replace(smsAddressID=smsAddressA.id)), 0)
        # Activate an smsAddress
        self.assert_json(self.post(url, params), 1)
        self.assertEqual(
            db.query(
                SMSAddress.is_active).filter_by(user_id=userIID).first()[0],
            True)

        params = ReplaceableDict(token=token,
                                 smsAddressAction='deactivate',
                                 smsAddressID=smsAddressI.id)
        # Deactivate an smsAddress that doesn't belong to the user
        self.assert_json(
            self.post(url, params.replace(smsAddressID=smsAddressA.id)), 0)
        # Deactivate an smsAddress
        self.assert_json(self.post(url, params), 1)
        self.assertEqual(
            db.query(
                SMSAddress.is_active).filter_by(user_id=userIID).first()[0],
            False)

        params = ReplaceableDict(token=token,
                                 smsAddressAction='remove',
                                 smsAddressID=smsAddressI.id)
        # Remove an smsAddress that doesn't belong to the user
        self.assert_json(
            self.post(url, params.replace(smsAddressID=smsAddressA.id)), 0)
        # Remove an smsAddress that does belong to the user
        self.assert_json(self.post(url, params), 1)
        self.assertEqual(
            db.query(SMSAddress).filter_by(id=smsAddressI.id).count(), 0)

        params = ReplaceableDict(token=token)
        # Send an invalid command
        self.assert_json(self.post(url, params.replace(smsAddressAction='')),
                         0)
        self.assert_json(
            self.post(url, params.replace(smsAddressAction='xxx')), 0)