Ejemplo n.º 1
0
def is_subscribed_test_data(user_first):
    """
    This fixture creates two social networks and add credentials for first social network.
    We actually want to test 'is_subscribed' field in social networks data from API.
    """
    old_records = SocialNetwork.query.filter(
        SocialNetwork.name.in_(['SN1', 'SN2'])).all()
    for sn in old_records:
        if sn.id is not None:
            try:
                SocialNetwork.delete(sn.id)
            except Exception:
                db.session.rollback()
    test_social_network1 = SocialNetwork(name='SN1', url='www.SN1.com')
    SocialNetwork.save(test_social_network1)
    test_social_network2 = SocialNetwork(name='SN2', url='www.SN1.com')
    SocialNetwork.save(test_social_network2)

    test_social_network1_credentials = UserSocialNetworkCredential(
        user_id=user_first['id'],
        social_network_id=test_social_network1.id,
        access_token='lorel ipsum',
        refresh_token='lorel ipsum')
    UserSocialNetworkCredential.save(test_social_network1_credentials)
    return test_social_network1, test_social_network2, test_social_network1_credentials
Ejemplo n.º 2
0
    def callback(self, oauth_verifier):
        """
        This method is called from endpoint http://127.0.0.1:8007/v1/twitter-callback/<int:user_id>
        defined in app.py.
        Here we use "oauth_verifier" to get access token for the user.
        If we get any error in getting access token, we log the error and raise InternalServerError.

        Once we have the access_token, we get the member_id (id of getTalent user on Twitter's website) of user
        from Twitter API and save its credentials in database table "UserSocialNetworkCredential".

        :param string oauth_verifier: Token received from Twitter when user successfully connected to its account.
        """
        self.auth.request_token = json.loads(
            redis_store.get('twitter_request_token_%s' % self.user.id))
        redis_store.delete('twitter_request_token_%s' % self.user.id)
        try:
            self.auth.get_access_token(oauth_verifier)
        except tweepy.TweepError as error:
            logger.exception(
                'Failed to get access token from Twitter for User(id:%s). \nError: %s'
                % (self.user.id, error.message))
            raise InternalServerError(
                'Failed to get access token from Twitter')

        access_token = self.auth.access_token
        # This may be used later
        # access_token_secret = self.auth.access_token_secret
        api = tweepy.API(self.auth)
        twitter_user = api.me()
        user_credentials = UserSocialNetworkCredential.get_by_user_and_social_network_id(
            self.user.id, self.social_network.id)
        if not user_credentials:
            user_credentials_obj = UserSocialNetworkCredential(
                user_id=self.user.id,
                social_network_id=self.social_network.id,
                member_id=twitter_user.id,
                access_token=access_token)
            UserSocialNetworkCredential.save(user_credentials_obj)

        logger.info(
            'User(id:%s) is now connected with Twitter. Member id on twitter is %s'
            % (self.user.id, twitter_user.id))
        return redirect(get_web_app_url())
Ejemplo n.º 3
0
    def save_user_credentials_in_db(cls, user_credentials):
        """
        :param user_credentials: user's social network credentials
        :type user_credentials: dict

        - It checks if user_credentials are already in database. If a record
            is found, it updates the record otherwise it saves as new record.

        - It is called e.g. from refresh_access_token() inside
            social_network_service/meetup.py

        :Example:
                from social_network_service.meetup import Meetup
                sn = Meetup(user_id=1)
                sn.save_user_credentials_in_db(data)

        **See Also**
        .. seealso:: refresh_access_token() method of Meetup class
            inside social_network_service/meetup.py

        :return user's social network credentials
        :rtype: common.models.user.UserCredentials
        """
        user_credentials_in_db = UserSocialNetworkCredential.get_by_user_and_social_network_id(
            user_credentials['user_id'], user_credentials['social_network_id'])
        try:
            if user_credentials_in_db:
                user_credentials_in_db.update(**user_credentials)
            else:
                user_credentials_in_db = UserSocialNetworkCredential(**user_credentials)
                UserSocialNetworkCredential.save(user_credentials_in_db)
            return user_credentials_in_db
        except:
            logger.exception('save_user_credentials_in_db: user_id: %s',
                             user_credentials['user_id'])
            raise SNServerException('APIError: Unable to create user credentials')