예제 #1
0
  def setUp(self):
    super(FacebookPageTest, self).setUp()
    for config in (appengine_config, granary.appengine_config,
                   oauth_dropins.appengine_config):
      setattr(config, 'FACEBOOK_APP_ID', 'my_app_id')
      setattr(config, 'FACEBOOK_APP_SECRET', 'my_app_secret')

    self.handler.messages = []
    self.auth_entity = oauth_facebook.FacebookAuth(
      id='my_string_id', auth_code='my_code', access_token_str='my_token',
      user_json=json.dumps({'id': '212038',
                            'name': 'Ryan Barrett',
                            'username': '******',
                            'bio': 'something about me',
                            'type': 'user',
                            }))
    self.auth_entity.put()
    self.fb = FacebookPage.new(self.handler, auth_entity=self.auth_entity,
                               features=['listen'])
    self.fb.put()

    self.post_activity = copy.deepcopy(gr_facebook_test.ACTIVITY)
    fb_id_and_url = {
      'id': 'tag:facebook.com,2013:222', # this is fb_object_id
      'url': 'https://www.facebook.com/212038/posts/222',
      }
    self.post_activity.update(fb_id_and_url)
    self.post_activity['object'].update(fb_id_and_url)
예제 #2
0
  def test_match_facebook_username_url(self):
    """Facebook URLs use username and user id interchangeably, and one
    does not redirect to the other. Make sure we can still find the
    relationship if author's publish syndication links using their
    username
    """
    auth_entity = oauth_facebook.FacebookAuth(
      id='my_string_id', auth_code='my_code', access_token_str='my_token',
      user_json=json.dumps({'id': '212038', 'username': '******'}))
    auth_entity.put()

    source = FacebookPage.new(self.handler, auth_entity=auth_entity,
                              domain_urls=['http://author'])
    # facebook activity comes to us with the numeric id
    self.activity['object']['url'] = 'http://facebook.com/212038/posts/314159'

    self.expect_requests_get('http://author', """
    <html class="h-feed">
      <div class="h-entry">
        <a class="u-url" href="http://author/post/permalink"></a>
      </div>
    </html>""")

    # user sensibly publishes syndication link using their username
    self.expect_requests_get('http://author/post/permalink', """
    <html class="h-entry">
      <a class="u-url" href="http://author/post/permalink"></a>
      <a class="u-syndication" href="http://facebook.com/snarfed.org/posts/314159"></a>
    </html>""")

    self.mox.ReplayAll()
    original_post_discovery.discover(source, self.activity)

    self.assertEquals(['http://author/post/permalink'],
                      self.activity['object']['upstreamDuplicates'])
  def test_match_facebook_username_url(self):
    """Facebook URLs use username and user id interchangeably, and one
    does not redirect to the other. Make sure we can still find the
    relationship if author's publish syndication links using their
    username
    """
    source = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    source.domain_urls = ['http://author']
    activity = self.activities[0]
    # facebook activity comes to us with the numeric id
    activity['object']['url'] = 'http://facebook.com/212038/posts/314159'
    activity['object']['content'] = 'content without links'

    self.expect_requests_get('http://author', """
    <html class="h-feed">
      <div class="h-entry">
        <a class="u-url" href="http://author/post/permalink"></a>
      </div>
    </html>""")

    # user sensibly publishes syndication link using their name
    self.expect_requests_get('http://author/post/permalink', """
    <html class="h-entry">
      <a class="u-url" href="http://author/post/permalink"></a>
      <a class="u-syndication" href="http://facebook.com/snarfed.org/posts/314159"></a>
    </html>""")

    self.mox.ReplayAll()
    original_post_discovery.discover(source, activity)

    self.assertEquals(['http://author/post/permalink'],
                      activity['object']['upstreamDuplicates'])
예제 #4
0
  def test_get_activities(self):
    owned_event = copy.deepcopy(as_facebook_test.EVENT)
    owned_event['id'] = '888'
    owned_event['owner']['id'] = '212038'
    self.expect_urlopen(
      'https://graph.facebook.com/me/posts?offset=0&access_token=my_token',
      json.dumps({'data': [as_facebook_test.POST]}))
    self.expect_urlopen(
      'https://graph.facebook.com/me/photos/uploaded?access_token=my_token',
      json.dumps({'data': [as_facebook_test.POST]}))
    self.expect_urlopen(
      'https://graph.facebook.com/me/events?access_token=my_token',
      json.dumps({'data': [as_facebook_test.EVENT, owned_event]}))
    self.expect_urlopen(
      'https://graph.facebook.com/145304994?access_token=my_token',
      json.dumps(as_facebook_test.EVENT))
    self.expect_urlopen(
      'https://graph.facebook.com/888?access_token=my_token',
      json.dumps(owned_event))
    self.expect_urlopen(
      'https://graph.facebook.com/888/invited?access_token=my_token',
      json.dumps({'data': as_facebook_test.RSVPS}))
    self.mox.ReplayAll()

    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    event_activity = page.as_source.event_to_activity(owned_event)
    for k in 'attending', 'notAttending', 'maybeAttending', 'invited':
      event_activity['object'][k] = as_facebook_test.EVENT_OBJ_WITH_ATTENDEES[k]
    self.assert_equals([self.post_activity, as_facebook_test.ACTIVITY, event_activity],
                       page.get_activities())
  def _test_match_facebook_username(self, user_obj, **source_params):
    auth_entity = oauth_facebook.FacebookAuth(
      id='my_string_id', auth_code='my_code', access_token_str='my_token',
      user_json=json.dumps(user_obj))
    auth_entity.put()

    fb = FacebookPage.new(self.handler, auth_entity=auth_entity,
                          domain_urls=['http://author'], **source_params)
    fb.put()
    fb.updates = {}
    # facebook activity comes to us with the numeric id
    self.activity['object']['url'] = 'http://facebook.com/212038/posts/314159'

    self.expect_requests_get('http://author', """
    <html class="h-feed">
      <div class="h-entry">
        <a class="u-url" href="http://author/post/permalink"></a>
      </div>
    </html>""")

    # user sensibly publishes syndication link using their username
    self.expect_requests_get('http://author/post/permalink', """
    <html class="h-entry">
      <a class="u-url" href="http://author/post/permalink"></a>
      <a class="u-syndication" href="http://facebook.com/snarfed.org/posts/314159"></a>
    </html>""")

    self.expect_urlopen(
      'https://graph.facebook.com/v2.2/212038_314159?access_token=my_token', '{}')

    self.mox.ReplayAll()
    self.assert_discover(['http://author/post/permalink'], source=fb)
예제 #6
0
  def setUp(self):
    super(FacebookPageTest, self).setUp()
    for config in (appengine_config, granary.appengine_config,
                   oauth_dropins.appengine_config):
      setattr(config, 'FACEBOOK_APP_ID', 'my_app_id')
      setattr(config, 'FACEBOOK_APP_SECRET', 'my_app_secret')

    self.handler.messages = []
    self.auth_entity = oauth_facebook.FacebookAuth(
      id='my_string_id', auth_code='my_code', access_token_str='my_token',
      user_json=json.dumps({'id': '212038',
                            'name': 'Ryan Barrett',
                            'username': '******',
                            'bio': 'something about me',
                            'type': 'user',
                            }),
      pages_json=json.dumps([]))
    self.auth_entity.put()
    self.fb = FacebookPage.new(self.handler, auth_entity=self.auth_entity,
                               features=['listen'])
    self.fb.put()

    self.page = {
      'id': '108663232553079',
      'about': 'Our vegetarian cooking blog',
      'category': 'Home/garden website',
      'name': 'Hardly Starving',
      'type': 'page',
      'access_token': 'page_token',
    }
예제 #7
0
  def test_revoked(self):
    self.expect_urlopen(
      'https://graph.facebook.com/me/posts?offset=0&access_token=my_token',
      json.dumps({'error': {'code': 190, 'error_subcode': 458}}), status=400)
    self.mox.ReplayAll()

    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    self.assertRaises(models.DisableSource, page.get_activities)
예제 #8
0
  def test_other_error(self):
    self.expect_urlopen(
      'https://graph.facebook.com/me/posts?offset=0&access_token=my_token',
      json.dumps({'error': {'code': 190, 'error_subcode': 789}}), status=400)
    self.mox.ReplayAll()

    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    self.assertRaises(urllib2.HTTPError, page.get_activities)
예제 #9
0
    def setUp(self):
        super(FacebookPageTest, self).setUp()
        for config in (appengine_config, granary.appengine_config,
                       oauth_dropins.appengine_config):
            setattr(config, 'FACEBOOK_APP_ID', 'my_app_id')
            setattr(config, 'FACEBOOK_APP_SECRET', 'my_app_secret')

        self.handler.messages = []
        self.auth_entity = oauth_facebook.FacebookAuth(
            id='my_string_id',
            auth_code='my_code',
            access_token_str='my_token',
            user_json=json_dumps({
                'id': '212038',
                'name': 'Ryan Barrett',
                'username': '******',
                'bio': 'something about me',
            }),
            pages_json=json_dumps([]),
            type='user')
        self.auth_entity.put()
        self.fb = FacebookPage.new(self.handler,
                                   auth_entity=self.auth_entity,
                                   features=['listen'])
        self.fb.put()

        self.page_json = {
            'id': '108663232553079',
            'about': 'Our vegetarian cooking blog',
            'category': 'Home/garden website',
            'name': 'Hardly Starving',
            'access_token': 'page_token',
        }
        self.page_auth_entity = oauth_facebook.FacebookAuth(
            id=self.page_json['id'],
            user_json=json_dumps(self.page_json),
            auth_code='my_code',
            access_token_str='my_token',
            type='page')
        self.page_auth_entity.put()

        self.page = FacebookPage.new(self.handler,
                                     auth_entity=self.page_auth_entity,
                                     features=['listen'])
        self.page.put()
예제 #10
0
 def test_new(self):
   page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
   self.assertEqual(self.auth_entity, page.auth_entity.get())
   self.assertEqual('my_token', page.as_source.access_token)
   self.assertEqual('212038', page.key.id())
   self.assertEqual('http://graph.facebook.com/snarfed.org/picture?type=large',
                    page.picture)
   self.assertEqual('Ryan Barrett', page.name)
   self.assertEqual('snarfed.org', page.username)
   self.assertEqual('user', page.type)
   self.assertEqual('https://facebook.com/snarfed.org', page.silo_url())
예제 #11
0
 def prepare_person_tags():
     FacebookPage(id='555', username='******').put()
     FacebookPage(id='666', inferred_username='******').put()
     FacebookPage(id='777', domains=['my.domain']).put()
     input_urls = (
         'https://unknown/',
         'https://www.facebook.com/444',
         'https://www.facebook.com/username',
         'https://www.facebook.com/inferred',
         'https://www.facebook.com/unknown',
         'https://my.domain/',
     )
     expected_urls = (
         'https://unknown/',
         'https://www.facebook.com/444',
         'https://www.facebook.com/555',
         'https://www.facebook.com/666',
         'https://www.facebook.com/unknown',
         'https://www.facebook.com/777',
     )
     return input_urls, expected_urls
예제 #12
0
  def test_expired_sends_notification(self):
    self.expect_urlopen(
      'https://graph.facebook.com/me/posts?offset=0&access_token=my_token',
      json.dumps({'error': {'code': 190, 'error_subcode': 463}}), status=400)

    params = {
      'template': "Brid.gy's access to your account has expired. Click here to renew it now!",
       'href': 'https://www.brid.gy/facebook/start',
      'access_token': 'my_app_id|my_app_secret',
      }
    self.expect_urlopen('https://graph.facebook.com/212038/notifications', '',
                        data=urllib.urlencode(params))
    self.mox.ReplayAll()

    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    self.assertRaises(models.DisableSource, page.get_activities)
예제 #13
0
  def test_canonicalize_syndication_url(self):
    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)

    for expected, input in (
      ('https://facebook.com/212038/posts/314159',
       'http://facebook.com/snarfed.org/posts/314159'),
      ('https://facebook.com/212038/photos.php?fbid=314159',
       'https://www.facebook.com/snarfed.org/photos.php?fbid=314159'),
      ('https://facebook.com/212038/posts/314159',
       'https://facebook.com/permalink.php?story_fbid=314159&id=212038'),
      ('https://facebook.com/212038/posts/314159',
       'https://facebook.com/permalink.php?story_fbid=314159&amp;id=212038'),
      # make sure we don't touch user.name when it appears elsewhere in the url
      ('https://facebook.com/25624/posts/snarfed.org',
       'http://www.facebook.com/25624/posts/snarfed.org')):
      self.assertEqual(expected, page.canonicalize_syndication_url(input))
예제 #14
0
  def test_get_activities_post_and_photo_duplicates(self):
    self.assertEqual(as_facebook_test.POST['object_id'],
                        as_facebook_test.PHOTO['id'])
    self.expect_urlopen(
      'https://graph.facebook.com/me/posts?offset=0&access_token=my_token',
      json.dumps({'data': [as_facebook_test.POST]}))
    self.expect_urlopen(
      'https://graph.facebook.com/me/photos/uploaded?access_token=my_token',
      json.dumps({'data': [as_facebook_test.PHOTO]}))
    self.expect_urlopen(
      'https://graph.facebook.com/me/events?access_token=my_token',
      json.dumps({}))
    self.mox.ReplayAll()

    page = FacebookPage.new(self.handler, auth_entity=self.auth_entity)
    self.assert_equals([self.post_activity], page.get_activities())
예제 #15
0
  def test_add_page_id_not_found(self):
    self.fb.key.delete()
    self.page.key.delete()
    self.auth_entity.pages_json = json.dumps([self.page_json])
    self.auth_entity.put()

    resp = facebook.application.get_response(
      '/facebook/add?id=999&auth_entity_key=%s' % self.auth_entity.key.urlsafe(),
      method='POST')

    self.assertEquals(302, resp.status_code)
    self.assert_equals('http://localhost/#!' + urllib.quote(
        "OK, you're not signed up. Hope you reconsider!"),
      resp.headers['location'])
    self.assertIsNone(self.fb.key.get())
    self.assertIsNone(FacebookPage.get_by_id('108663232553079'))
예제 #16
0
  def test_add_page_id_not_found(self):
    self.fb.key.delete()
    self.page.key.delete()
    self.auth_entity.pages_json = json.dumps([self.page_json])
    self.auth_entity.put()

    resp = facebook.application.get_response(
      '/facebook/add?id=999&auth_entity_key=%s' % self.auth_entity.key.urlsafe(),
      method='POST')

    self.assertEquals(302, resp.status_code)
    self.assert_equals('http://localhost/#!' + urllib.quote(
        "OK, you're not signed up. Hope you reconsider!"),
      resp.headers['location'])
    self.assertIsNone(self.fb.key.get())
    self.assertIsNone(FacebookPage.get_by_id('108663232553079'))
예제 #17
0
  def test_add_page(self):
    self.fb.key.delete()
    self.page.key.delete()
    self.auth_entity.pages_json = json.dumps([self.page_json])
    self.auth_entity.put()

    self.expect_urlopen(oauth_facebook.API_PAGE_URL + '&access_token=page_token',
                        json.dumps(self.page_json))
    self.mox.ReplayAll()

    resp = facebook.application.get_response(
      '/facebook/add?id=108663232553079&auth_entity_key=%s' %
        self.auth_entity.key.urlsafe(),
      method='POST')

    self.assertEquals(302, resp.status_code)
    self.assertEquals('http://localhost/facebook/108663232553079',
                      resp.headers['Location'])
    self.assertIsNone(self.fb.key.get())
    self.assertIsNotNone(FacebookPage.get_by_id('108663232553079'))
예제 #18
0
  def test_add_page(self):
    self.fb.key.delete()
    self.page.key.delete()
    self.auth_entity.pages_json = json.dumps([self.page_json])
    self.auth_entity.put()

    self.expect_urlopen(oauth_facebook.API_PAGE_URL + '&access_token=page_token',
                        json.dumps(self.page_json))
    self.mox.ReplayAll()

    resp = facebook.application.get_response(
      '/facebook/add?id=108663232553079&auth_entity_key=%s' %
        self.auth_entity.key.urlsafe(),
      method='POST')

    self.assertEquals(302, resp.status_code)
    self.assertEquals('http://localhost/facebook/108663232553079',
                      resp.headers['Location'])
    self.assertIsNone(self.fb.key.get())
    self.assertIsNotNone(FacebookPage.get_by_id('108663232553079'))