def test_authorize(self): self.skip_me("this test doesn't work yet, although the thing it tests works") redir_uri = 'https://lg-local.example.com/id/auth/callback/etsy' # Just match a regex since we'll also get back a requestToken from # this since it's an OAuth 1.0 Service. expected_redirect = r'https://etsy.com/oauth/signin\?oauth_token=' redirect_uri = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) self.assertRegexpMatches(redirect_uri, expected_redirect) response = self.submit_login_form( uri=redirect_uri, args=dict( username=AUTHN_TEST_USER['email'], password=AUTHN_TEST_USER['password'], ), ) self.assertNotEqual(response.status_code, 404) response = self.follow_redirect_until_not(response, r'etsy.com') print response.content if not self.is_redirect(response.status_code): self.display_error(response, "Error Condition") return args = get_arguments_from_redirect(response) auth = yield self.service.finish_authorization( client_name='testing', args=args, ) oauth_token = auth.token user_id = auth.user_id oauth_secret = auth.secret self.assertEqual(AUTHN_TEST_USER['user_id'], user_id) yield self.set_authorization( user_id=user_id, token=oauth_token, secret=oauth_secret ) # Method tests against our actual user. profile = yield self.call_method('profile') expected_profile = dict( name='%s %s' % (AUTHN_TEST_USER['firstName'], AUTHN_TEST_USER['lastName']), profile_link=AUTHN_TEST_USER['profile_link'], profile_picture_link=AUTHN_TEST_USER['profile_picture_link'], headline=AUTHN_TEST_USER['headline'], bio='', username='', email='*****@*****.**', ) self._test_method_result_keys('profile', profile, expected_profile) num_favorites = yield self.call_method('num_favorites') self.assertEqual( num_favorites, {'num': AUTHN_TEST_USER['num_favorites']} )
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)
def test_authorize(self): redir_uri = "https://lg-local.example.com/id/auth/callback/linkedin" # Just match a regex since we'll also get back a requestToken from # this since it's an OAuth 1.0 Service. expected_redirect = r"https://api.linkedin.com/uas/oauth/authorize\?oauth_token=" redirect_uri = yield self.service.start_authorization(client_name="testing", args={"redirect_uri": redir_uri}) self.assertRegexpMatches(redirect_uri, expected_redirect) response = self.submit_login_form( uri=redirect_uri, args=dict(username=AUTHN_TEST_USER["email"], password=AUTHN_TEST_USER["password"]) ) self.assertNotEqual(response.status_code, 404) response = self.follow_redirect_until_not(response, r"linkedin.com") if not self.is_redirect(response.status_code): self.display_error(response, "Error Condition") return args = get_arguments_from_redirect(response) auth = yield self.service.finish_authorization(client_name="testing", args=args) oauth_token = auth.token user_id = auth.user_id oauth_secret = auth.secret self.assertEqual(AUTHN_TEST_USER["user_id"], user_id) yield self.set_authorization(user_id=user_id, token=oauth_token, secret=oauth_secret) # Method tests against our actual user. profile = yield self.call_method("profile") expected_profile = dict( bio=None, email=None, first_name=AUTHN_TEST_USER["firstName"], headline=AUTHN_TEST_USER["headline"], last_name=AUTHN_TEST_USER["lastName"], maiden_name=AUTHN_TEST_USER["maidenName"], name="%s %s" % (AUTHN_TEST_USER["firstName"], AUTHN_TEST_USER["lastName"]), profile_link=AUTHN_TEST_USER["profile_link"], profile_picture_link=AUTHN_TEST_USER["profile_picture_link"], username=None, ) self._test_method_result_keys("profile", profile, expected_profile) work = yield self.call_method("work") expected_work = { "data": [ dict( end_date_month=None, end_date_year=None, is_current=True, organization_name="Inflection", start_date_month=None, start_date_year=2011, title="Engineering", work_id=303007820, ) ] } self._test_method_result_keys("work", work["data"][0], expected_work["data"][0]) self._test_method_result_keys("education", education["data"][0], expected_education["data"][0]) contact = yield self.call_method("contact") expected_contact = dict( country_code="us", phone_numbers=[{"phone_number": u"5615551234"}], region="San Francisco Bay Area" ) self._test_method_result_keys("contact", contact, expected_contact)
def test_authorize(self): redir_uri = 'https://lg-local.example.com/authn/facebook' expected_redirect = yield self.service.start_authorization( client_name='testing', args={'redirect_uri': redir_uri}, ) 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): self.handle_grant_permissions(response) response = self.follow_redirect_until_not(response, r'facebook.com') # If we get this, it is the "grant extended permissions to app" page if not self.is_redirect(response.status_code): self.handle_grant_permissions(response) 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 args = get_arguments_from_redirect(response) args['redirect_uri'] = redir_uri user_auth = yield self.service.finish_authorization( client_name='testing', args=args, ) # 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) # What happens when we authenticate the same user again against FB? # Supposedly, we get a new oauth token. Is this correct? # Verify this authorization is any good. yield self.set_authorization( user_id=user_auth.user_id, token=user_auth.token, ) # Verify that we get the fist activity time correct result = yield self.call_method('account_created_timestamp') got_timestamp = result['timestamp'] expected_timestamp = 1341941735 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) # some of the other methods seem to blow up when we call self.run_daemon() for facebook, so # just call the one we need yield self.run_daemon_method('profile') profile = yield self.call_method('profile') expected_profile = dict( bio=None, email=AUTHN_TEST_USER['email'], first_name=AUTHN_TEST_USER['first_name'], gender=AUTHN_TEST_USER['gender'], headline=None, last_name=AUTHN_TEST_USER['last_name'], middle_name=None, name=AUTHN_TEST_USER['name'], profile_link=AUTHN_TEST_USER['link'], profile_picture_link='https://graph.facebook.com/100004005057849/picture', username=AUTHN_TEST_USER['username'], ) self._test_method_result_keys('profile', profile, expected_profile) birth = yield self.call_method('birth') expected_birth = dict( age=37, dob_day=16, dob_month=1, dob_year=1977, ) self._test_method_result_keys('birth', birth, expected_birth) website = yield self.call_method('website') expected_website = dict( website='http://google.com' ) self.assertEqual(website, expected_website) contact = yield self.call_method('contact') expected_contact = dict( city=None, country_code=None, phone_numbers=None, region=None, state=None, ) self.assertEqual(contact, expected_contact) work = yield self.call_method('work') expected_work = {'data': [dict( city='Redwood City', end_date_month=None, end_date_year=None, is_current=None, organization_name='Inflection', start_date_month=9, start_date_year=2011, state='CA', title='Software Engineer', work_id=None, )]} self.assertEqual(work, expected_work) education = yield self.call_method('education') expected_education = {'data': [dict( degree_earned='MSc', end_date_month=None, end_date_year=2013, field_of_study='Computer Engineering, Hustlin, Bachelor of Science in Animal Husbandry', school_name='School of Hard Knocks', start_date_month=None, start_date_year=None, education_id=None, )]} self.assertEqual(education, expected_education)