Esempio n. 1
0
    def _on_auth(self, user):
        if not user:
            raise tornado.web.HTTPError(500, "Twitter auth failed")

        #is there an existing external account?
        current_user = self.get_current_user()
        authenticated_user = User.get("id=%s", current_user['id'])
        existing = Externalservice.by_user(authenticated_user,
                                           Externalservice.TWITTER)
        if existing:
            existing.service_id = user['access_token']['user_id']
            existing.service_secret = user['access_token']['secret']
            existing.service_key = user['access_token']['key']
            existing.screen_name = user['access_token']['screen_name']
            existing.save()
        else:
            external_service = Externalservice(
                user_id=authenticated_user.id,
                service_id=user['access_token']['user_id'],
                screen_name=user['access_token']['screen_name'],
                type=Externalservice.TWITTER,
                service_key=user['access_token']['key'],
                service_secret=user['access_token']['secret'])
            external_service.save()
        # if not, insert credentials for this user
        # if there is, update that account
        return self.render("tools/twitter-connected.html")
Esempio n. 2
0
    def test_find_mltshp_users(self):
        """
        find_mltshp_users should return all users that are on
        mltshp and also on a user's connected external service
        account.
        
        Create two other users. user2 has an externalservice
        connected, user3 does not. admin follows user2 on twitter, so user2
        should be returned as part of .find_mltshp_users result. When user3 has
        an external service connected, but is not in admin's ExternalRelationship
        graph, he will not be returned.
        """
        user2 = User(name='user2',
                     email='*****@*****.**',
                     email_confirmed=1,
                     is_paid=1)
        user2.save()
        user2_service = Externalservice(user_id=user2.id,
                                        service_id=1000,
                                        screen_name='user2',
                                        type=Externalservice.TWITTER,
                                        service_key='asdf',
                                        service_secret='qwer')
        user2_service.save()
        user3 = User(name='user3',
                     email='*****@*****.**',
                     email_confirmed=1,
                     is_paid=1)
        user3_service = Externalservice(user_id=user2.id,
                                        service_id=2000,
                                        screen_name='ephramzerb',
                                        type=Externalservice.TWITTER,
                                        service_key='asdf',
                                        service_secret='qwer')
        user3_service.save()
        user2.save()

        # We emulate admin connecting twitter account, and we add a Twitter relationship
        # to user2's twitter account.
        ExternalRelationship.add_relationship(self.user, 1000,
                                              Externalservice.TWITTER)
        # we should get user2 back.
        users = self.external_service.find_mltshp_users()
        self.assertEqual(1, len(users))
        self.assertEqual(user2.id, users[0].id)

        # now when we add user3, find_mltshp_users includes user3 in the result.
        ExternalRelationship.add_relationship(self.user, 2000,
                                              Externalservice.TWITTER)
        users = self.external_service.find_mltshp_users()
        self.assertEqual(2, len(users))
Esempio n. 3
0
class ExternalserviceModelTests(BaseTestCase):
    def setUp(self):
        super(ExternalserviceModelTests, self).setUp()
        self.user = User(name='admin', email='*****@*****.**', email_confirmed=1)
        self.user.save()
        self.external_service = Externalservice(
                                user_id=self.user.id, 
                                service_id=11616, 
                                screen_name='torrez', 
                                type=Externalservice.TWITTER, 
                                service_key='asdf', 
                                service_secret='qwer')
        self.external_service.save()
            
    def test_get_by_user(self):
        external_service = Externalservice.by_user(self.user, Externalservice.TWITTER)
        
        self.assertEqual(external_service.user_id,           self.external_service.user_id           )
        self.assertEqual(external_service.service_id,        self.external_service.service_id        )
        self.assertEqual(external_service.screen_name,       self.external_service.screen_name       )
        self.assertEqual(external_service.type,              self.external_service.type              )
        self.assertEqual(external_service.service_key,       self.external_service.service_key       )
        self.assertEqual(external_service.service_secret,    self.external_service.service_secret    )
        self.assertEqual(external_service.created_at.strftime("%Y-%m-%d %H:%M:%S"),       self.external_service.created_at        )
        self.assertEqual(external_service.updated_at.strftime("%Y-%m-%d %H:%M:%S"),        self.external_service.updated_at        )
    
    def test_find_mltshp_users(self):
        """
        find_mltshp_users should return all users that are on
        mltshp and also on a user's connected external service
        account.
        
        Create two other users. user2 has an externalservice
        connected, user3 does not. admin follows user2 on twitter, so user2
        should be returned as part of .find_mltshp_users result. When user3 has
        an external service connected, but is not in admin's ExternalRelationship
        graph, he will not be returned.
        """
        user2 = User(name='user2', email='*****@*****.**', email_confirmed=1)
        user2.save()
        user2_service = Externalservice(
                                user_id=user2.id, 
                                service_id=1000,
                                screen_name='user2', 
                                type=Externalservice.TWITTER, 
                                service_key='asdf', 
                                service_secret='qwer')
        user2_service.save()
        user3 = User(name='user3', email='*****@*****.**', email_confirmed=1)
        user3_service = Externalservice(
                                user_id=user2.id, 
                                service_id=2000,
                                screen_name='ephramzerb', 
                                type=Externalservice.TWITTER, 
                                service_key='asdf', 
                                service_secret='qwer')
        user3_service.save()
        user2.save()
        
        # We emulate admin connecting twitter account, and we add a Twitter relationship
        # to user2's twitter account.
        ExternalRelationship.add_relationship(self.user, 1000, Externalservice.TWITTER)
        # we should get user2 back.
        users = self.external_service.find_mltshp_users()
        self.assertEqual(1, len(users))
        self.assertEqual(user2.id, users[0].id)
        
        # now when we add user3, find_mltshp_users includes user3 in the result.
        ExternalRelationship.add_relationship(self.user, 2000, Externalservice.TWITTER)
        users = self.external_service.find_mltshp_users()
        self.assertEqual(2, len(users))
Esempio n. 4
0
class TwitterTests(BaseAsyncTestCase):
    def setUp(self):
        super(TwitterTests, self).setUp()
        self.user = User(name="admin",
                         email="*****@*****.**",
                         email_confirmed=1)
        self.user.set_password('asdfasdf')
        self.user.save()
        self.sid = self.sign_in("admin", "asdfasdf")

        self.externalservice = Externalservice(user_id=self.user.id,
                                               service_id=555,
                                               screen_name='mltshp',
                                               type=Externalservice.TWITTER,
                                               service_key="blah",
                                               service_secret="mah")
        self.externalservice.save()

    def get_new_ioloop(self):
        return tornado.ioloop.IOLoop.instance()

    @twittertest
    def test_twitter_connect(self):
        request = HTTPRequest(self.get_url("/tools/twitter/connect"),
                              'GET', {'Cookie': "sid=%s" % (self.sid)},
                              follow_redirects=False)
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertTrue(response.headers['location'].startswith(
            "https://api.twitter.com/oauth/authorize?oauth"))

    def test_post_from_twitter(self):
        #provider = "https://api.twitter.com/1.1/account/verify_credentials.json"
        provider = self.get_url('/heartbeat')
        """
        Copies a file to the file-system, then POSTs the location and details to the upload method
        for processing
        """
        file_path = os.path.abspath("test/files/1.png")
        sha1 = Sourcefile.get_sha1_file_key(file_path)
        content_type = "image/png"

        file_name = os.path.basename(file_path)
        file_size = os.path.getsize(file_path)
        body = "media_name=%s&media_content_type=%s&media_sha1=%s&media_size=%s&media_path=%s" % (
            file_name, content_type, sha1, file_size, file_path)

        request = HTTPRequest(
            url=self.get_url('/upload'),
            method='POST',
            headers={
                'X-Auth-Service-Provider':
                provider,
                'X-Verify-Credentials-Authorization':
                'OAuth oauth_timestamp="1290404453", oauth_version="1.0", oauth_consumer_key="IQKbtAYlXLripLGPWd0HUA", oauth_token="37458155-JCG7c8oejM6N4TK4HJbXVC5VGq1gtaSUPt90wxFI", oauth_signature="9QxkJqBAJfZ83sbz6SCJKSaPn9U%3D", oauth_nonce="C7AB0CBC-9193-44EE-AFC1-6FE3BA51F048", oauth_signature_method="HMAC-SHA1"'
            },
            body=body,
        )
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.assertEqual(response.body,
                         "<mediaurl>http://s.mltshp.com/r/1.png</mediaurl>")
        sf = Sharedfile.get("id = %s", 1)
        self.assertEqual(sf.id, 1)
        self.assertEqual(sf.name, '1.png')
        self.assertEqual(sf.user_id, self.user.id)

    def test_posting_fails_when_provider_is_not_localhost(self):
        provider = "http://notes.torrez.org"
        """
        Copies a file to the file-system, then POSTs the location and details to the upload method
        for processing
        """
        file_path = os.path.abspath("test/files/1.png")
        sha1 = Sourcefile.get_sha1_file_key(file_path)
        content_type = "image/png"

        file_name = os.path.basename(file_path)
        file_size = os.path.getsize(file_path)
        body = "media_name=%s&media_content_type=%s&media_sha1=%s&media_size=%s&media_path=%s" % (
            file_name, content_type, sha1, file_size, file_path)

        request = HTTPRequest(
            url=self.get_url('/upload'),
            method='POST',
            headers={
                'X-Auth-Service-Provider':
                provider,
                'X-Verify-Credentials-Authorization':
                'OAuth oauth_timestamp="1290404453", oauth_version="1.0", oauth_consumer_key="IQKbtAYlXLripLGPWd0HUA", oauth_token="37458155-JCG7c8oejM6N4TK4HJbXVC5VGq1gtaSUPt90wxFI", oauth_signature="9QxkJqBAJfZ83sbz6SCJKSaPn9U%3D", oauth_nonce="C7AB0CBC-9193-44EE-AFC1-6FE3BA51F048", oauth_signature_method="HMAC-SHA1"'
            },
            body=body,
        )
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertEqual(response.code, 403)

    def test_post_from_twitter_with_message(self):
        provider = self.get_url('/heartbeat')
        """
        Copies a file to the file-system, then POSTs the location and details to the upload method
        for processing
        """
        file_path = os.path.abspath("test/files/1.png")
        sha1 = Sourcefile.get_sha1_file_key(file_path)
        content_type = "image/png"

        file_name = os.path.basename(file_path)
        file_size = os.path.getsize(file_path)
        message = "hey look\r\n at me!\r\n"
        body = "message=%s&media_name=%s&media_content_type=%s&media_sha1=%s&media_size=%s&media_path=%s" % (
            message, file_name, content_type, sha1, file_size, file_path)

        request = HTTPRequest(
            url=self.get_url('/upload'),
            method='POST',
            headers={
                'X-Auth-Service-Provider':
                provider,
                'X-Verify-Credentials-Authorization':
                'OAuth oauth_timestamp="1290404453", oauth_version="1.0", oauth_consumer_key="IQKbtAYlXLripLGPWd0HUA", oauth_token="37458155-JCG7c8oejM6N4TK4HJbXVC5VGq1gtaSUPt90wxFI", oauth_signature="9QxkJqBAJfZ83sbz6SCJKSaPn9U%3D", oauth_nonce="C7AB0CBC-9193-44EE-AFC1-6FE3BA51F048", oauth_signature_method="HMAC-SHA1"'
            },
            body=body,
        )
        self.http_client.fetch(request, self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.assertEqual(response.body,
                         "<mediaurl>http://s.mltshp.com/r/1.png</mediaurl>")
        sf = Sharedfile.get("id = %s", 1)
        self.assertEqual(sf.id, 1)
        self.assertEqual(sf.get_title(),
                         message.replace('\n', '').replace('\r', ''))
        self.assertEqual(sf.user_id, self.user.id)