Exemple #1
0
def parrots_finish(db):
    if request.query.denied:
        redirect('/twitter_denied.html')
    session = Sessions.findOne(db, {'oauth_token': request.query.oauth_token})
    if session:
        account = Accounts.findOne(db, session.account_id)
        if not account:
            response.status = 404
            return {'error': 'Invalid token'}
        twitter = Twitter()
        try:
            access_tokens = twitter.get_access_tokens(request.query.oauth_verifier,{'oauth_token': session.oauth_token, 'oauth_token_secret': session.oauth_token_secret})
            twitter.create_client(access_tokens.get('oauth_token'),access_tokens.get('oauth_token_secret'))
            headers, body = twitter.get('https://api.twitter.com/1/account/verify_credentials.json')
        except:
            redirect('/twitter_auth_problem.html')
            return
        if headers.status == 200:
            body = json.loads(body)
            parrot = Parrots.findOne(db, {'twitter_id': body.get('id')})
            if not parrot:
                new_parrot = Parrots(db, {
                    'twitter_info': body,
                    'twitter_id': body.get('id'),
                    'oauth_token': access_tokens.get('oauth_token'),
                    'oauth_token_secret': access_tokens.get('oauth_token_secret')
                })
                new_parrot.insert()
                parrot = new_parrot
            else:
                parrot.update({
                    'oauth_token': access_tokens.get('oauth_token'),
                    'oauth_token_secret': access_tokens.get('oauth_token_secret')
                })
            subscription = Subscriptions.findOne(db, {'account_id': account.id, 'parrot_id': parrot.id})
            subscription_parameters = {
                'parrot_id': parrot.id,
                'account_id': account.id,
                'active': True,
                'external_id': session.external_id,
                'twitter_screen_name': body.get("screen_name")
            }
            if not subscription:
                subscription = Subscriptions(db, subscription_parameters)
                subscription.insert()
            else:
                subscription.update(subscription_parameters)
            notification_id = _create_notification(db, account, parrot, subscription)
            if notification_id:
                redirect_url = generate_redirect_url(account.callback_url, session.external_id, subscription.id, notification_id)
                redirect(redirect_url)
    else:
        response.status = 404
        return {'error': 'Expired token'}
Exemple #2
0
    def test_cron4_no_subscription(self):
        account = Accounts(self.db, {
            'email': '*****@*****.**',
            'password': '******',
            'name': 'Daniel',
            'startup': 'Payparrot',
            'url': 'http://payparrot.com/',
            'callback_url': 'http://www.epistemonikos.org',
            'notification_url': 'http://www.epistemonikos.org',
        })
        account.insert()
        parrot = Parrots(self.db, {
            'twitter_id': '123123123',
            'oauth_token': 'asd',
            'oauth_token_secret': 'asdf',
            'twitter_info': {},
            'payments': [],
            'twitter_info': {
                'screen_name': 'danielgua'
            }
        })
        parrot.insert()
        subscription = Subscriptions(self.db, {'account_id': account.id, 'active': False, 'parrot_id': parrot.id, 'twitter_screen_name': parrot.twitter_info.get("screen_name")})
        subscription.insert()

        last_date = datetime.now();
        next_action_date = last_date;
        next_payment = NextPayments(self.db, {
            'account_id': account.id,
            'parrot_id': parrot.id,
            'action_date': next_action_date
        });
        next_payment.insert()


        from payparrot_scripts.crons.cron4 import main as cron4
        cron4()

        message = Queue.get_message('payments')
        self.assertFalse(message)

        self.assertEqual(0, self.db.next_payments.find({'_id': next_payment.id}).count())
Exemple #3
0
class TestParrots(unittest.TestCase):
    def setUp(self):
        self.app = pp_tests.get_app()
        self.connection, self.db = pp_tests.connect_to_mongo()
        self.account = pp_tests.create_account_and_login(self.app, self.db, {
            'email': '*****@*****.**',
            'password': '******',
            'name': 'Daniel',
            'startup': 'Payparrot',
            'url': 'http://payparrot.com/',
            'callback_url': 'http://www.epistemonikos.org',
            'notification_url': 'http://www.epistemonikos.org',
        })
        self.parrot = Parrots(self.db, {
            'twitter_id': '123123123',
            'oauth_token': 'asd',
            'oauth_token_secret': 'asdf',
            'twitter_info': {},
            'payments': [],
            'twitter_info': {
                'screen_name': 'danielgua'
            }
        })
        self.parrot.insert()
        self.subscription = Subscriptions(self.db, {'account_id': self.account.id, 'active': True, 'parrot_id': self.parrot.id, 'twitter_screen_name': self.parrot.twitter_info.get("screen_name")})
        self.subscription.insert()
            
        self.parrot1 = Parrots(self.db, {
            'twitter_id': '4322143214',
            'oauth_token': 'asd',
            'oauth_token_secret': 'asdf',
            'twitter_info': {},
            'payments': [],
            'twitter_info': {
                'screen_name': 'blabla'
            }
        })
        self.parrot1.insert()
        self.subscription1 = Subscriptions(self.db, {'account_id': self.account.id, 'active': True, 'parrot_id': self.parrot1.id,'twitter_screen_name': self.parrot1.twitter_info.get("screen_name")})        
        self.subscription1.insert()

    def tearDown(self):
        pp_tests.tear_down(self.db, self.app)
        self.connection.close()

    def test_invalid_token(self):
        response = self.app.get('/parrots/finish?oauth_token=lala', status = 404)
        self.assertEqual({'error': 'Expired token'}, response.json)

    def test_get_one_parrot_status(self):
        response = self.app.get('/accounts/%s/parrots/%s' % (self.account.id, self.parrot.id))
        self.assertEqual(200, response.status_int)
        self.assertEqual(self.parrot.JSON(), response.json)

    def test_delete_one_parrot(self):
        response = self.app.delete('/accounts/%s/parrots/%s' % (str(self.account.id), str(self.parrot.id)))
        self.assertEqual(204, response.status_int)
        self.app.get('/accounts/%s/parrots/%s' % (self.account.id, self.parrot.id), status=404)
    
    def test_get_parrots_with_date_filter(self):
        one_day = timedelta(days=1)
        from_ = datetime.now()-one_day*7
        from_text = str(from_)
        to_ = datetime.now() - one_day
        to_text = str(to_)
        #Hack to change date
        self.db.subscriptions.update({'parrot_id': self.parrot1.id},{'$set': {'created_at': from_+one_day*5}})
        response = self.app.get('/accounts/%s/parrots?from=%s&to=%s' % (self.account.id, from_text.split(" ")[0], to_text.split(" ")[0]))
        self.assertEqual(200, response.status_int)
        self.assertEqual(1, len(response.json))
        self.assertEqual(str(self.parrot1.id), response.json[0]['id'])

    def test_get_parrots_by_screen_name(self):
        response = self.app.get('/accounts/%s/parrots?screen_name=%s' % (self.account.id, 'danielgua'))
        self.assertEqual(200, response.status_int)
        self.assertEqual(1, len(response.json))
        # self.assertEqual(str(self.parrot1.id), response.json[0]['id'])