def oauth_authorized(service_provider_name):
     session_user = get_current_user()
     try:
         current_provider = providers.get_by_name(service_provider_name)
         token, secret = current_provider.get_access_tokens()
         fresh_user = get_user_by_token(current_provider, token, secret)
         if session_user is not None:
             if fresh_user.id == session_user.id:
                 flash('This provider was already linked to this account.')
             else:
                 flash('Merging accounts is not currently supported.')
         else:
             log_user_in(fresh_user)
     except ServiceProviderNotFound:
         flash('Provider not found.')
     except UserDeniedRequest:
         flash('You denied us access.')
     except UserNotFound:
         if session_user is None:
             try:
                 user = get_user_by_remote_id(current_provider, token=(token,secret))
             except UserNotFound:
                 user = create_user()
             log_user_in(user)
         add_SP_to_user_by_id(
             get_current_user().id, current_provider, token, secret)
     next_url = request.args.get('next') or url_for('show_user')
     return redirect(next_url)
    def test_get_user_by_tokens(self):
        user_created = uf.create_user()
        uf.add_SP_to_user(user_created, sp, 'test', 'test')

        user_retrieved = uf.get_user_by_token(sp, 'test', 'test')
        self.assertEqual(user_created, user_retrieved)
 def test_get_user_when_none_exist(self):
     try:
         user = uf.get_user_by_token(sp, 'test', 'test')
         self.fail()
     except uf.UserNotFound:
         pass