def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. self.skip_me('Needs recorded authorize test') redir_uri = 'https://lg-local.example.com/id/auth/callback/blogger' expected_redirect = compose_url( 'https://accounts.google.com/o/oauth2/auth', query={ 'access_type': 'offline', 'approval_prompt': 'force', 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'scope': 'https://www.googleapis.com/auth/blogger', 'response_type': 'code', }, ) redirect_uri = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(redirect_uri, expected_redirect) auth = yield self.service.finish_authorization( client_name='testing', args={ 'redirect_uri': redirect_uri, 'code': 'foo', }, ) self.assertIsNotNone(auth.token) self.assertIsNotNone(auth.refresh_token) self.assertEqual(AUTHN_TEST_USER['id'], auth.user_id) yield self.set_authorization( user_id=auth.user_id, token=auth.token, refresh_token=auth.refresh_token ) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method, ) profile = yield self.call_method('profile') expected_profile = dict( name=AUTHN_TEST_USER['username'], username=AUTHN_TEST_USER['username'], ) self._test_method_result_keys('profile', profile, expected_profile) num_posts = yield self.call_method('num_posts') self.assertEqual( num_posts, {'num': AUTHN_TEST_USER['num_posts']} )
def _x_and_verify(self, method, path, args, **kwargs): fetch_args = { x:kwargs[x] for x in [ 'headers', 'list_as_set' ] if kwargs.get(x) } if fetch_args.get('headers'): if not fetch_args['headers'].get('X-Client'): fetch_args['headers']['X-Client'] = [ 'testing' ] else: fetch_args['headers'] = { 'X-Client': [ 'testing' ] } if method in [ 'GET', 'DELETE' ]: if args: path = compose_url(path, query=args) response = self.fetch(path=path, method=method, **fetch_args) elif method in [ 'POST', 'PUT' ]: if not args: args = '' response = self.fetch( path=path, method=method, postdata=args, **fetch_args ) else: raise AttributeError(method) if kwargs.get('response_type'): def verify(response): if not isinstance(kwargs['response_type'], list): kwargs['response_type'] = [kwargs['response_type']] self.assertEqual(response.headers['Content-Type'], kwargs['response_type']) return response response.addCallback(verify) if kwargs.get('response_location'): def verify(response): self.assertRegexpMatches( response.headers.get('Location', [])[0], kwargs['response_location'], ) return response response.addCallback(verify) if kwargs.get('result'): def verify(response): self.assertEqual(response.body,kwargs['result']) return response response.addCallback(verify) elif kwargs.get('result_matches'): def verify(response): self.assertRegexpMatches(response.body,kwargs['result_matches']) return response response.addCallback(verify) if kwargs.get('response_code'): def verify(response): self.assertEqual(response.code,kwargs['response_code']) return response response.addCallback(verify) return response
def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. redir_uri = "https://lg-local.example.com/id/auth/callback/foursquare" expected_redirect = compose_url( "https://foursquare.com/oauth2/authenticate", query={ "redirect_uri": redir_uri, "client_id": self.service.app_info[self.service.environment]["app_id"], "response_type": "code", }, ) rv = yield self.service.start_authorization(client_name="testing", args={"redirect_uri": redir_uri}) self.assertURIEqual(rv, expected_redirect) user_auth = yield self.service.finish_authorization( client_name="testing", args={"redirect_uri": redir_uri, "code": "foo"} ) self.assertTrue(user_auth.token) self.assertEqual(AUTHN_TEST_USER["user_id"], user_auth.user_id) # Verify this authorization is any good. yield self.set_authorization(user_id=user_auth.user_id, token=user_auth.token) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method ) profile = yield self.call_method("profile") # profile calls should be consistent across calls expected_profile = dict( name=AUTHN_TEST_USER["name"], username=AUTHN_TEST_USER["username"], bio=AUTHN_TEST_USER["bio"], profile_picture_link=AUTHN_TEST_USER["picture"], profile_link=AUTHN_TEST_USER["link"], gender=AUTHN_TEST_USER["gender"], email=AUTHN_TEST_USER["email"], ) self._test_method_result_keys("profile", profile, expected_profile) num_friends = yield self.call_method("num_friends") self.assertEqual(num_friends, {"num": AUTHN_TEST_USER["num_friends"]}) num_mayorships = yield self.call_method("num_mayorships") self.assertEqual(num_mayorships, {"num": AUTHN_TEST_USER["num_mayorships"]}) num_photos = yield self.call_method("num_photos") self.assertEqual(num_photos, {"num": AUTHN_TEST_USER["num_photos"]}) num_lists = yield self.call_method("num_lists") self.assertEqual(num_lists, {"num": AUTHN_TEST_USER["num_lists"]}) num_checkins = yield self.call_method("num_checkins") self.assertEqual(num_checkins, {"num": AUTHN_TEST_USER["num_checkins"]})
def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. redir_uri = 'https://lg-local.example.com/id/auth/callback/reddit' expected_redirect = compose_url( 'https://ssl.reddit.com/api/v1/authorize', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'response_type': 'code', 'scope': 'identity', 'duration': 'permanent', 'state': 'foo' }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) user_auth = yield self.service.finish_authorization( client_name='testing', args={ 'redirect_uri': redir_uri, 'code': '9cH1ZoHamQ6Zj0AnprFxEbuH2Xc', 'state': 'foo', }, ) # XXX Add in check for CommandContext instead of Authorization # Do we have a better test for the oauth_token? self.assertTrue(user_auth.token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_auth.user_id) # Verify this authorization is any good. yield self.set_authorization( user_id=user_auth.user_id, token=user_auth.token, ) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method, ) profile = yield self.call_method('profile') expected_profile = dict( username=AUTHN_TEST_USER['username'], profile_link=AUTHN_TEST_USER['profile_link'], ) self._test_method_result_keys('profile', profile, expected_profile)
def notest_authorize_with_error(self): self.skip_me("Disabled until extended permissions are supported") redir_uri = 'https://lg-local.example.com/authn/facebook' expected_redirect = compose_url( 'https://graph.facebook.com/oauth/authorize', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'scope': self.service.permissions['testing']['scope'], }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) response = self.submit_login_form( uri=expected_redirect, args=dict( username=AUTHN_TEST_USER['email'], password=AUTHN_TEST_USER['password'], ), ) response = self.follow_redirect_until_not(response, r'facebook.com') # If we get this, it is the "grant permissions to app" page if not self.is_redirect(response.status_code): logging.info("Denying the grant permissions") self.handle_grant_permissions(response, grant=False) response = self.follow_redirect_until_not(response, r'facebook.com') if not self.is_redirect(response.status_code): self.display_error(response, "Failed to grant permissions") return code = re.search(r'code=(.*)', response.headers['location']).group(1) user_auth = yield self.service.finish_authorization( client_name='testing', args={ 'redirect_uri': redir_uri, 'code': code, }, ) # Do we have a better test for the oauth_token? self.assertTrue(user_auth.token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_auth.user_id)
def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. redir_uri = 'https://lg-local.example.com/id/auth/callback/soundcloud' expected_redirect = compose_url( 'https://api.soundcloud.com/connect', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'response_type': 'code', 'access_type': 'offline', 'approval_prompt': 'auto', 'scope': 'non-expiring', }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) auth = yield self.service.finish_authorization( client_name='testing', args={ 'code': 'foo', 'redirect_uri': redir_uri, } ) oauth_token = auth.token user_id = auth.user_id oauth_secret = auth.secret self.assertTrue(oauth_token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_id) yield self.set_authorization( user_id=user_id, token=oauth_token, secret=oauth_secret ) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method, ) profile = yield self.call_method('profile') expected_profile = dict( name=AUTHN_TEST_USER['name'], username=AUTHN_TEST_USER['username'], bio=AUTHN_TEST_USER['bio'], profile_link=AUTHN_TEST_USER['profile_link'], profile_picture_link=AUTHN_TEST_USER['profile_picture_link'], ) self._test_method_result_keys('profile', profile, expected_profile) num_followers = yield self.call_method('num_followers') self.assertEqual( num_followers, {'num': AUTHN_TEST_USER['num_followers']} ) num_following = yield self.call_method('num_following') self.assertEqual( num_following, {'num': AUTHN_TEST_USER['num_following']} ) num_public_playlists = yield self.call_method('num_public_playlists') self.assertEqual( num_public_playlists, {'num': AUTHN_TEST_USER['num_public_playlists']} ) num_public_tracks = yield self.call_method('num_public_tracks') self.assertEqual( num_public_tracks, {'num': AUTHN_TEST_USER['num_public_tracks']} )
def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. redir_uri = 'https://lg-local.example.com/id/auth/callback/instagram' expected_redirect = compose_url( 'https://api.instagram.com/oauth/authorize', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'response_type': 'code', }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) user_auth = yield self.service.finish_authorization( client_name='testing', args={ 'redirect_uri': redir_uri, 'code': 'foo' }, ) # Do we have a better test for the oauth_token? self.assertTrue(user_auth.token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_auth.user_id) # Verify this authorization is any good. yield self.set_authorization( user_id=user_auth.user_id, token=user_auth.token, ) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method, ) profile = yield self.call_method('profile') expected_profile = dict( name=AUTHN_TEST_USER['name'], username=AUTHN_TEST_USER['username'], bio=AUTHN_TEST_USER['bio'], profile_picture_link=AUTHN_TEST_USER['picture'], profile_link=AUTHN_TEST_USER['profile_link'], ) self._test_method_result_keys('profile', profile, expected_profile) followers = yield self.call_method('num_followers') self.assertEqual(followers, {'num': 0}, 'num_followers') followed = yield self.call_method('num_followed') self.assertEqual(followed, {'num': 4}, 'num_followed') media = yield self.call_method('num_media') self.assertEqual(media, {'num': 1}, 'num_media') comments = yield self.call_method('num_comments') self.assertEqual(comments, {'num': 1}, 'num_comments') likes = yield self.call_method('num_likes') self.assertEqual(likes, {'num': 0}, 'num_likes') # Verify that we get the account creation time correct result = yield self.call_method('account_created_timestamp') got_timestamp = result['timestamp'] expected_timestamp = 1342548276 month_in_seconds = 31 * 24 * 60 * 60 # make sure the timestamp we got is within 31 days of the one we expect self.assertLess(abs(got_timestamp - expected_timestamp), month_in_seconds)
def profile_url(self, token): return compose_url('', query={ 'format': 'json', 'method': 'vimeo.people.getInfo', 'user_id': token })
def construct_endpoint_url(self, **kwargs): args = kwargs args["format"] = "json" args["nojsoncallback"] = "1" return compose_url("", query=kwargs)
def test_recorded_authorize(self): # Note: This test requires recorded responses from Lightning Console. redir_uri = 'https://lg-local.example.com/id/auth/callback/runkeeper' expected_redirect = compose_url( 'https://runkeeper.com/apps/authorize', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'response_type': 'code', }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) auth = yield self.service.finish_authorization( client_name='testing', args={ 'code': 'foo', 'redirect_uri': redir_uri, }, ) oauth_token = auth.token user_id = auth.user_id oauth_secret = auth.secret self.assertTrue(oauth_token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_id) # Verify this authorization is any good. yield self.set_authorization( user_id=user_id, token=oauth_token, ) for method in self.service.daemon_object()._recurring: yield self.service.daemon_object().run( authorization=self.authorization, timestamp=int(time.time()), daemon_method=method, ) profile = yield self.call_method('profile') expected_profile = dict( name=AUTHN_TEST_USER['name'], gender=AUTHN_TEST_USER['gender'], profile_picture_link=AUTHN_TEST_USER['picture'], profile_link=AUTHN_TEST_USER['profile_link'], ) self._test_method_result_keys('profile', profile, expected_profile) num_activities = yield self.call_method('num_activities') self.assertEqual( num_activities, {'num': AUTHN_TEST_USER['num_activities']} ) num_calories = yield self.call_method('num_calories') self.assertEqual( num_calories, {'num': AUTHN_TEST_USER['num_calories']} ) num_comments = yield self.call_method('num_comments') self.assertEqual( num_comments, {'num': AUTHN_TEST_USER['num_comments']} ) num_friends = yield self.call_method('num_friends') self.assertEqual( num_friends, {'num': AUTHN_TEST_USER['num_friends']} ) total_distance_result = yield self.call_method('total_distance') total_distance = total_distance_result['num'] self.assertAlmostEqual( total_distance, AUTHN_TEST_USER['total_distance'], 3 ) total_duration = yield self.call_method('total_duration') self.assertEqual( total_duration, {'num': AUTHN_TEST_USER['total_duration']} ) # Verify that we get the fist activity time correct result = yield self.call_method('account_created_timestamp') got_timestamp = result['timestamp'] expected_timestamp = 1363028119 month_in_seconds = 31 * 24 * 60 * 60 # make sure the timestamp we got is within 31 days of the one we expect self.assertLess(abs(got_timestamp - expected_timestamp), month_in_seconds)
def test_authorize(self): self.skip_me('Need to fix bug with playback of responses') redir_uri = 'https://lg-local.example.com/id/auth/callback/wordpress' expected_redirect = compose_url( 'https://public-api.wordpress.com/oauth2/authorize', query={ 'redirect_uri': redir_uri, 'client_id': self.service.app_info[self.service.environment]['app_id'], 'response_type': 'code', }, ) rv = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertURIEqual(rv, expected_redirect) response = self.submit_login_form( uri=expected_redirect, args=dict( username=AUTHN_TEST_USER['username'], password=AUTHN_TEST_USER['password'], ), ) login_cookies = response.cookies response = self.follow_redirect_until_not(response, r'wordpress.com/') # Now, submit the authorize the authorize page soup = BeautifulSoup(response.content) arguments = self.extract_form_fields(soup.form) url = compose_url(soup.form['action'], query=arguments) response = requests.get( url, headers=self.headers(referer=response.request.url), cookies=login_cookies, # We need to trap the redirect back to localhost:5000 allow_redirects=False, ) response = self.follow_redirect_until_not(response, r'wordpress.com/') if not self.is_redirect(response.status_code): self.display_error(response, 'Error Condition') return args = get_arguments_from_redirect(response) args['redirect_uri'] = redir_uri user_auth = yield self.service.finish_authorization( client_name='testing', args=args, ) # XXX Add in check for CommandContext instead of Authorization # Do we have a better test for the oauth_token? self.assertTrue(user_auth.token) self.assertEqual(AUTHN_TEST_USER['user_id'], user_auth.user_id) # Verify this authorization is any good. yield self.set_authorization( user_id=user_auth.user_id, token=user_auth.token, ) yield self.daemon.run( authorization=self.authorization, timestamp=int(time.time()), ) profile = yield self.call_method('profile') expected_profile = dict( name=AUTHN_TEST_USER['name'], username=AUTHN_TEST_USER['username'], email=AUTHN_TEST_USER['email'], profile_picture_link=AUTHN_TEST_USER['picture'], profile_link=AUTHN_TEST_USER['profile_link'], ) self._test_method_result_keys('profile', profile, expected_profile)