Exemplo n.º 1
0
 def application_based_auth(self):
     """
     This function does application based authentication with Twitter. This do not need any user interaction
     to connect with Twitter account because it makes request on behalf of App.
     It raises InternalServerError in case authentication fails.
     Here are the detailed docs https://dev.twitter.com/oauth/application-only.
     :return: Access token for getTalent app to access Twitter's API.
     :rtype: str
     """
     combined_key = self.consumer_key + ':' + self.consumer_secret
     combined_key = base64.b64encode(combined_key)
     headers = {
         'Authorization': 'Basic %s' % combined_key,
         'Content-Type': 'application/x-www-form-urlencoded'
     }
     data = {'grant_type': 'client_credentials'}
     result = http_request('post',
                           APPLICATION_BASED_AUTH_URL,
                           headers=headers,
                           data=data,
                           app=app)
     if result.ok:
         logger.info(
             'Successfully authenticated from Twitter API. User(id:%s).',
             self.user.id)
         access_token = result.json()['access_token']
         return access_token
     raise InternalServerError(
         'Error Occurred while authenticating from Twitter')
Exemplo n.º 2
0
 def test_post_with_valid_token(self, token_first,
                                test_eventbrite_credentials):
     """
     Send POST request with valid venue data to create venue endpoint and response should be 201
     """
     social_network = SocialNetwork.get_by_name(EVENTBRITE.title())
     venue = {
         "social_network_id": social_network.id,
         "zip_code": "54600",
         "address_line_2": "H# 163, Block A",
         "address_line_1": "New Muslim Town",
         "latitude": 0,
         "longitude": 0,
         "state": "CA",
         "city": "Lahore",
         "country": "Pakistan"
     }
     response = requests.post(SocialNetworkApiUrl.VENUES,
                              data=json.dumps(venue),
                              headers=get_headers(token_first))
     logger.info(response.text)
     assert response.status_code == codes.CREATED, 'Status should be Ok, Resource created (201)'
     assert 'Location' in response.headers
     response = response.json()
     assert response['id'] > 0
     Venue.session.commit()
     venue = Venue.get_by_id(response['id'])
     assert venue, 'Venue created successfully in db'
     Venue.delete(venue.id)
Exemplo n.º 3
0
 def process_events(self, events):
     """
     :param list events: contains events of a particular user for a specific social network.
     This is the function to process events once we have the events of some social network. It first maps the
     social network fields to gt-db fields. Then it checks if the event is present is db or not.
     If event is already in db, it updates the event fields otherwise it stores the event in db.
     Finally we import RSVPs of all the events in method post_process_events().
     """
     if events:
         self.pre_process_events(events)
     if events:
         logger.info(
             'Events of %s(UserId: %s) are being processed to save in database.'
             % (self.user.name, self.user.id))
         for event in events:
             try:
                 self.events.append(self.import_event(event))
             except Exception:
                 logger.exception(
                     '''Error occurred while importing event.
                                     UserId: %s,
                                     SocialNetworkId: %s
                                     Event: %s
                                  ''' %
                     (self.user.id, self.social_network.id, event))
     self.mark_vendor_deleted(events)
     if events:
         self.post_process_events()
Exemplo n.º 4
0
 def update_event(self):
     """
     It first creates/ updates a venue on Meetup.com and then passes that
     venue's id in event's payload to update event location along with event
     data.
     :exception EventNotCreated: raises exception if unable to update event
             on Meetup.com
     :return: id of event
     :rtype: int
     """
     # create url to update event
     url = get_url(self, Urls.EVENT).format(self.social_network_event_id)
     # create or update venue for event
     venue_id = self.add_location()
     # add venue id in event payload to update event venue on Meetup.com
     self.payload.update({'venue_id': venue_id})
     # Sleep for 10 / 30 seconds to avoid throttling
     time.sleep(0.34)
     response = http_request('POST',
                             url,
                             params=self.payload,
                             headers=self.headers,
                             user_id=self.user.id)
     if response.ok:
         event_id = response.json().get('id')
         logger.info('|  Event %s updated Successfully  |' %
                     self.payload['name'])
         self.data['social_network_event_id'] = event_id
         return self.save_event()
     else:
         error_message = 'Event was not created. Error occurred during ' \
                         'event update on Meetup'
         log_error({'user_id': self.user.id, 'error': error_message})
         raise EventNotCreated('ApiError: Unable to update event on Meetup')
Exemplo n.º 5
0
 def unpublish_event(self, event_id, method=HttpMethods.DELETE):
     """
     This function is used while running unit tests. It deletes the Event from database that were created
     during the lifetime of a unit test.
     :param int | long event_id: id of newly created event
     :param string method: http standard method , default is DELETE
     :return: True if event is deleted from vendor, False otherwise.
     :rtype: bool
     """
     # create url to unpublish event
     url = get_url(self, Urls.EVENT).format(event_id)
     # Sleep for 10 / 30 seconds to avoid throttling
     time.sleep(0.34)
     # params are None. Access token is present in self.headers
     response = http_request(method,
                             url,
                             headers=self.headers,
                             user_id=self.user.id)
     if response.ok:
         logger.info('|  Event has been unpublished (deleted)  |')
     else:
         error_message = "Event was not unpublished (deleted):%s" % response.text
         log_error({'user_id': self.user.id, 'error': error_message})
         raise EventNotUnpublished(
             'ApiError: Unable to remove event from %s' %
             self.social_network.name)
Exemplo n.º 6
0
 def test_match_event_organizer_fields(self, user_first, token_first,
                                       test_eventbrite_credentials):
     """
     Send POST request with valid event organizer data and response should be 201 (id in response content)
     Then get data from api and test if expected fields exist and have some data.
     """
     event_organizer = {
         "user_id": user_first['id'],
         "name": "organizer_%s" % fake.uuid4(),
         "email": "*****@*****.**",
         "about": "He is a testing engineer"
     }
     response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                              data=json.dumps(event_organizer),
                              headers=get_headers(token_first))
     logger.info(response.text)
     assert response.status_code == codes.CREATED, 'Status should be Ok, Resource created (201)'
     assert 'Location' in response.headers
     response = response.json()
     assert response['id']
     EventOrganizer.session.commit()
     response = requests.get(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                             headers=auth_header(token_first))
     assert response.status_code == codes.OK
     results = response.json()
     assert 'event_organizers' in results
     assert len(results['event_organizers']) == 1
     match_event_organizer_fields(results['event_organizers'][0])
Exemplo n.º 7
0
    def get_member_id(self):
        """
        - If getTalent user has an account on some social network, like Meetup.com, it will have a "member id" for
            that social network. This "member id" is used to make API subsequent calls to fetch events or RSVPs and
            relevant data for getTalent user from social network website.

        :Example:

            from social_network_service.meetup import Meetup
            sn = Meetup(user_id=1)
            sn.get_member_id()

        - We call this method from connect() of SocialNetworkBase class so that we don't need to get 'member id'
            of getTalent user while making object of some social network class at different places.
            (e.g. creating object of Meetup() in when importing events)

        **See Also**
        .. seealso:: connect() method defined in SocialNetworkBase class inside social_network_service/base.py.
        """
        logger.info('Getting "member id" of %s(user id:%s) using API of %s.' % (self.user.name, self.user.id,
                                                                                self.social_network.name))
        url = get_url(self, SocialNetworkUrls.VALIDATE_TOKEN)
        # Now we have the URL, access token, and header is set too,
        response = http_request('POST', url, headers=self.headers, user_id=self.user.id, app=app)
        if response.ok:
            return response.json().get('id')
        else:
            logger.error('get_member_id: Error:%s.' % response.text)
Exemplo n.º 8
0
def get_events_with_query(token,
                          search=None,
                          social_network_id=None,
                          sort_by=None,
                          sort_type=None,
                          user_id=None):
    url = SocialNetworkApiUrl.EVENTS
    if search:
        url += '?search=%s' % search
    if social_network_id:
        url += '?social_network_id=%s' % social_network_id if url.find('?') == -1 \
            else '&social_network_id=%s' % social_network_id
    if sort_by:
        url += '?sort_by=%s' % sort_by if url.find(
            '?') == -1 else '&sort_by=%s' % sort_by
    if sort_type:
        url += '?sort_type=%s' % sort_type if url.find(
            '?') == -1 else '&sort_type=%s' % sort_type
    if user_id:
        url += '?user_id=%s' % user_id if url.find(
            '?') == -1 else '&user_id=%s' % user_id
    response = requests.get(url, headers=auth_header(token))
    logger.info(response.text)
    assert response.status_code == codes.OK, 'Status should be Ok (200)'
    return response.json()['events']
Exemplo n.º 9
0
    def mark_vendor_deleted(self, events):
        """
        There can be some events in db those exist in getTalent databse but not in vendor db. So we need to mark such
        events as `is_deleted_from_vendor = True`
        :param list events: list of events (dict) retrieved from Meetup / Eventbrite
        """
        member_users = UserSocialNetworkCredential.filter_by_keywords(
            member_id=self.member_id, social_network_id=self.social_network.id)
        members_events = []
        social_network_event_ids = [event['id'] for event in events]
        member_user_ids = [member.user_id for member in member_users]
        events_to_mark_deleted = Event.query.filter(
            Event.user_id.in_(member_user_ids),
            Event.is_deleted_from_vendor == 0,
            Event.social_network_id == self.social_network.id,
            ~Event.social_network_event_id.in_(social_network_event_ids))

        events_marked_deleted = events_to_mark_deleted.update(
            {'is_deleted_from_vendor': 1}, synchronize_session=False)
        db.session.commit()
        # TODO: This commented code will be required for a future feature. Entirely delete unused events from db
        # deleted_event_ids = []
        # for event_obj in missing_events:
        #     try:
        #         event = self.fetch_event(event_obj.social_network_event_id)
        #     except Exception as e:
        #         event = None
        #     if not event or (event and event.get('status') in self.cancelled_status):
        #         deleted_event_ids.append(event_obj.id)
        logger.info(
            '%s events have been marked as vendor deleted. SocialNetwork: %s, UserId: %s'
            % (events_marked_deleted, self.social_network.name, self.user.id))
Exemplo n.º 10
0
    def test_post_with_same_organizer_name(self, token_first, user_first,
                                           test_eventbrite_credentials):
        """
        Send POST request with valid event organizer data and response should be 201 but when
        we will try to create organizer with same name again, API will raise InvalidUsage 400 error.
        """
        name = "organizer_%s" % fake.uuid4(),
        event_organizer = {
            "user_id": user_first['id'],
            "name": name,
            "email": "*****@*****.**",
            "about": "He is a testing engineer"
        }
        response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                                 data=json.dumps(event_organizer),
                                 headers=get_headers(token_first))
        logger.info(response.text)
        assert response.status_code == codes.CREATED, 'Status should be Ok, Resource created (201)'
        assert 'Location' in response.headers
        response = response.json()
        event_organizer_id = response['id']
        assert event_organizer_id > 0

        # Now try to create organizer with same name. It will raise 400 (InvalidUsage)
        response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                                 data=json.dumps(event_organizer),
                                 headers=get_headers(token_first))
        logger.info(response.text)
        assert response.status_code == codes.BAD, 'Status should be 400, InvalidUsage'
        EventOrganizer.session.commit()
        event_organizer = EventOrganizer.get_by_id(event_organizer_id)
        assert event_organizer, 'Event organizer not found in db'
        EventOrganizer.delete(event_organizer.id)
Exemplo n.º 11
0
    def delete(self):
        """
        Deletes multiple social network whose ids are given in list in request data.

        :Example:
            social_network_ids = {
                'ids': [1,2,3]
            }
            headers = {
                        'Authorization': 'Bearer <access_token>',
                        'Content-Type': 'application/json'
                       }
            data = json.dumps(social_network_ids)
            response = requests.post(
                                        API_URL + '/social-network/',
                                        data=data,
                                        headers=headers,
                                    )

        .. Response::

            {
                'message': '3 social networks have been deleted successfully'
            }
        .. HTTP Status::
                    200 (Resource Deleted)
                    207 (Not all deleted)
                    400 (Bad request)
                    500 (Internal Server Error)

        """
        # get event_ids for events to be deleted
        req_data = get_valid_json_data(request)
        social_network_ids = req_data['social_network_ids'] \
            if 'social_network_ids' in req_data and isinstance(
            req_data['social_network_ids'], list) else []
        total_deleted = 0
        total_not_deleted = 0
        if social_network_ids:
            for sn_id in social_network_ids:
                try:
                    if SocialNetwork.get_by_id(sn_id):
                        SocialNetwork.delete(sn_id)
                        total_deleted += 1
                except Exception as e:
                    total_not_deleted += 1
                    logger.info(
                        'Unable to delete social network with ID: %s\nError: %s'
                        % (sn_id, e.message))

        if total_not_deleted:
            return dict(message='Unable to delete %s social networks' %
                        total_not_deleted,
                        deleted=total_deleted,
                        not_deleted=total_not_deleted), codes.MULTI_STATUS
        elif total_deleted:
            return dict(message='%s social networks deleted successfully' %
                        total_deleted)
        raise InvalidUsage('Bad request, include social work ids as list data',
                           error_code=codes.BAD_REQUEST)
Exemplo n.º 12
0
    def test_events_post_with_valid_token(self, token_first, test_event):
        """
        Post event and it should create an event with 201 response
        """
        event_data = test_event

        # Success case
        response = send_post_request(SocialNetworkApiUrl.EVENTS, event_data,
                                     token_first)
        logger.info(response.text)
        assert response.status_code == codes.CREATED, 'Status should be Ok, Resource Created (201)'
        event_id = response.json()['id']
        db.db.session.commit()
        CampaignsTestsHelpers.assert_for_activity(
            event_data['user_id'], Activity.MessageIds.EVENT_CREATE, event_id)
        activities = Activity.get_by_user_id_type_source_id(
            user_id=event_data['user_id'],
            source_id=event_id,
            type_=Activity.MessageIds.EVENT_CREATE)
        data = json.loads(activities.params)
        assert data['event_title'] == event_data['title']

        assert event_id > 0, 'Event id should be a positive number'
        test_event[
            'id'] = event_id  # Add created event id  in test_event so it can be deleted in tear_down
Exemplo n.º 13
0
def test_subscribed_social_network(token_first, user_first, test_credentials):
    """
    Input: We created two test social networks with name SN1 and SN2 and added credentials for SN1
    in UserSocialNetworkCredential Table in is_subscribed_test_data fixture.

    Output: Once we call the /social_networks/ for that user we get a list
    of all social networks and for Now in social_networks API data, these two social networks
    should be according to our expectations.
    'is_subscribed' set to 'true' for SN1 and 'false' for SN2
    """

    response = requests.get(
        SocialNetworkApiUrl.SOCIAL_NETWORKS,
        headers={'Authorization': 'Bearer %s' % token_first})
    logger.info(response.text)
    assert response.status_code == 200
    social_networks = json.loads(response.text)['social_networks']
    assert all(['is_subscribed' in sn for sn in social_networks])
    subscribed_social_networks = filter(lambda sn: sn['is_subscribed'],
                                        social_networks)
    assert len(subscribed_social_networks) >= 1
    subscribed_social_network = filter(
        lambda sn: sn['name'] in ['Eventbrite', 'Meetup'],
        subscribed_social_networks)
    assert len(
        subscribed_social_network
    ) >= 1, 'Subscribed social networks must be Meetup and Eventbrite'
Exemplo n.º 14
0
def schedule_job(url, task_name):
    """
    Schedule a general job that hits Event and RSVP importer endpoint every hour.
    :param url: URL to hit
    :type url: basestring
    :param task_name: task_name of scheduler job
    :type task_name: basestring
    """
    start_datetime = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)

    access_token = User.generate_jw_token()
    headers = {
        'Content-Type': 'application/json',
        'Authorization': access_token
    }

    data = {
        "url": url,
        "run_datetime": start_datetime.strftime(DatetimeUtils.ISO8601_FORMAT),
        "task_type": "one_time",
        "task_name": "Meetup_Event_Importer_Stream",
        'is_jwt_request': True

    }

    logger.info('Checking if {} task already running...'.format(task_name))
    response = requests.post(SchedulerApiUrl.TASKS, headers=headers,
                             data=json.dumps(data))
    if response.ok:
        logger.info('Meetup_Event_Importer_Stream Job scheduled successfully. {}'.format(response.text))
    else:
        logger.error(response.text)
Exemplo n.º 15
0
    def add_venue_to_sn(self, venue_data):
        """
        This function sends a POST request to Eventbrite api to create a venue for event.
        :param dict venue_data: a dictionary containing data required to create a venue
        """

        payload = {
            'venue.name': venue_data['address_line_1'],
            'venue.address.address_1': venue_data['address_line_1'],
            'venue.address.address_2': venue_data.get('address_line_2'),
            'venue.address.region': venue_data['state'],
            'venue.address.city': venue_data['city'],
            'venue.address.postal_code': venue_data.get('zip_code'),
            'venue.address.latitude': venue_data.get('latitude'),
            'venue.address.longitude': venue_data.get('longitude')
        }
        # create url to send post request to create venue
        url = get_url(self, Urls.VENUES)
        response = http_request('POST',
                                url,
                                params=payload,
                                headers=self.headers,
                                user_id=self.user.id)
        json_resp = response.json()
        if response.ok:
            logger.info('|  Venue has been created  |')
            venue_id = json_resp.get('id')
        else:
            raise InternalServerError(
                'ApiError: Unable to create venue for Eventbrite',
                additional_error_info=dict(venue_error=json_resp))

        venue_data['user_id'] = self.user.id
        venue_data['social_network_venue_id'] = venue_id
        return SocialNetworkBase.save_venue(venue_data)
Exemplo n.º 16
0
    def test_delete_with_valid_token(self, token_first,
                                     new_event_in_db_second):
        """
        - Try to delete event data using id, if deleted you expect 200 response
        - Then again try to delete event using same event id and expect 403 response
        """
        event_id = new_event_in_db_second['id']
        response = requests.delete(SocialNetworkApiUrl.EVENT % event_id,
                                   headers=auth_header(token_first))
        logger.info(response.text)
        assert response.status_code == codes.OK, str(response.text)
        response = requests.delete(SocialNetworkApiUrl.EVENT % event_id,
                                   headers=auth_header(token_first))

        # check if event delete activity
        user_id = new_event_in_db_second['user_id']
        db.db.session.commit()
        CampaignsTestsHelpers.assert_for_activity(
            user_id, Activity.MessageIds.EVENT_DELETE, event_id)
        activity = Activity.get_by_user_id_type_source_id(
            user_id=user_id,
            source_id=event_id,
            type_=Activity.MessageIds.EVENT_DELETE)
        assert activity, 'Activity not found'
        data = json.loads(activity.params)
        assert data['event_title'] == new_event_in_db_second['title']

        logger.info(response.text)
        assert response.status_code == codes.FORBIDDEN, 'Unable to delete event as it is not present there (403)'
Exemplo n.º 17
0
def test_social_network_no_auth():
    """
    Send request with invalid token and response should be 401 (Unauthorized)
    """
    response = requests.get(SocialNetworkApiUrl.SOCIAL_NETWORKS,
                            headers={'Authorization': 'some random'})
    logger.info(response.text)
    assert response.status_code == 401
Exemplo n.º 18
0
 def test_post_with_invalid_token(self):
     """
     Send POST request to create venue endpoint with invalid token in header and response should be 401 unauthorized
     """
     response = requests.post(SocialNetworkApiUrl.VENUES,
                              headers=auth_header('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
Exemplo n.º 19
0
 def test_get_with_invalid_token(self):
     """
     Send GET request with invalid token in header and response should be un-authorize access
     """
     response = requests.post(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                              headers=auth_header('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
     assert 'organizers' not in response.json()
Exemplo n.º 20
0
 def test_events_post_with_invalid_token(self):
     """
     Post event using invalid token and response should be 401 (unauthorized user)
     """
     response = send_post_request(SocialNetworkApiUrl.EVENTS, {},
                                  'invalid_token')
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
     assert 'events' not in response.json()
Exemplo n.º 21
0
 def test_get_with_invalid_token(self):
     """
     Send POST request with invalid_token in header and response should be 401 (unauthorized)
     """
     response = requests.post(SocialNetworkApiUrl.VENUES,
                              headers=auth_header('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
     assert 'venues' not in response.json()
Exemplo n.º 22
0
 def test_delete_with_invalid_token(self):
     """
     Send DELETE request with invalid token in header and response should be 401 (unauthorize)
     """
     response = requests.delete(SocialNetworkApiUrl.EVENT_ORGANIZERS,
                                data=json.dumps({'ids': []}),
                                headers=get_headers('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, "Response: {}".format(
         response.text)
Exemplo n.º 23
0
 def test_get_with_invalid_token(self):
     """
     - Try to get events using invalid access_token.
     - Expect 401 (unauthorized) in response
     """
     response = requests.get(SocialNetworkApiUrl.EVENTS,
                             headers=auth_header('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
     assert 'events' not in response.json()
Exemplo n.º 24
0
def create_event(token, event_data, title, start_datetime):
    end_datetime = start_datetime + timedelta(hours=3)
    data = dict(
        title=title,
        start_datetime=start_datetime.strftime(DatetimeUtils.ISO8601_FORMAT),
        end_datetime=end_datetime.strftime(DatetimeUtils.ISO8601_FORMAT))
    event_data.update(data)
    response = send_post_request(SocialNetworkApiUrl.EVENTS, event_data, token)
    logger.info(response.text)
    assert response.status_code == codes.CREATED, response.text
Exemplo n.º 25
0
 def test_delete_with_invalid_token(self):
     """
     Send DELETE request to delete venues endpoint using invalid token in header. response should be
     unauthorized (401)
     """
     response = requests.delete(SocialNetworkApiUrl.VENUES,
                                data=json.dumps({'ids': []}),
                                headers=get_headers('invalid_token'))
     logger.info(response.text)
     assert response.status_code == codes.UNAUTHORIZED, 'It should be unauthorized (401)'
Exemplo n.º 26
0
 def test_get_with_valid_token(self, token_first):
     """
     Send GET request to venues and response should be 200.
     """
     response = requests.get(SocialNetworkApiUrl.VENUES,
                             headers=auth_header(token_first))
     logger.info(response.text)
     assert response.status_code == codes.OK, 'Status should be Ok (200)'
     results = response.json()
     assert 'venues' in results
Exemplo n.º 27
0
    def get_access_and_refresh_token(cls, user_id, social_network, code_to_get_access_token=None, method_type=None,
                                     payload=None, params=None, api_relative_url=None):
        """
        This function is used by Social Network API to save 'access_token' and 'refresh_token' for specific
        social network against a user (current user) by sending an POST call to respective social network API.
        :param user_id: current user id
        :param social_network: social_network in getTalent database
        :param code_to_get_access_token: Code which is exchanged for an access token.
        :param method_type: In case of Eventbrite, need to make a 'POST' call to get access token.
        :param payload: is set inside this method and is passed in super constructor. This is sent in body
                        of HTTP request.
        :param params: dictionary of data to send in the url params.
        :param api_relative_url: This variable is set in this function and is passed in super constructor to make
                                HTTP request.
        :type user_id: int
        :type social_network: common.models.social_network.SocialNetwork
        :type code_to_get_access_token: str
        :type method_type: str
        :type payload: dict
        :type payload: dict
        :type api_relative_url: str
        :return: returns access token and refresh token
        :rtype: tuple (str, str)
        """
        logger.info('Getting "access_token and refresh_token" of user_id:%s using API of %s.'
                    % (user_id, social_network.name))
        url = social_network.auth_url + api_relative_url
        get_token_response = http_request(method_type, url, data=payload, user_id=user_id, params=params)
        try:
            if get_token_response.ok:
                # access token is used to make API calls, this is what we need
                # to make subsequent calls
                try:
                    response = get_token_response.json()
                    access_token = response.get('access_token')
                    # refresh token is used to refresh the access token
                    refresh_token = response.get('refresh_token')
                except ValueError:
                    if 'facebook' in social_network.api_url:
                        # In case of Facebook, access_token is retrieved as follows
                        access_token = \
                            get_token_response.content.split('=')[1].split('&')[0]
                        refresh_token = ''
                    else:
                        raise
                return access_token, refresh_token
            else:
                log_error({'user_id': user_id, 'error': get_token_response.json().get('error')})
                raise SNServerException('Unable to to get access token for current user')

        except:
            logger.exception('get_access_and_refresh_token: user_id: %s, social network: %s(id: %s)'
                             % (user_id, social_network.name, social_network.id))
            raise SNServerException('Unable to create user credentials for current user')
Exemplo n.º 28
0
 def test_event_get_with_valid_token(self, token_second):
     """
     - Get events using valid access_token
     - As the test user is newly created. so, initially there will be no events
     """
     response = requests.get(SocialNetworkApiUrl.EVENTS,
                             headers=auth_header(token_second))
     logger.info(response.text)
     assert response.status_code == codes.OK, 'Status should be Ok (200)'
     events = response.json()['events']
     assert len(
         events) == 0, 'There shouldn\'t be some events for test user'
Exemplo n.º 29
0
 def test_meetup_with_valid_address(self, token_first, meetup_event_data,
                                    test_meetup_credentials):
     """
     Send Post request with meetup_event data and response should be 201 (event created)
     """
     event_data = meetup_event_data
     response = send_post_request(SocialNetworkApiUrl.EVENTS, event_data,
                                  token_first)
     logger.info(response.text)
     assert response.status_code == codes.CREATED, 'Event should be created, address is valid'
     event_id = response.json()['id']
     meetup_event_data['id'] = event_id
Exemplo n.º 30
0
 def test_meetup_with_invalid_address(self, token_first, meetup_event_data,
                                      test_meetup_credentials):
     """
     Send post request with invalid meetup_event data (change venue_id) and response should be 404 with error code
     4065 (Address invalid)
     """
     event_data = meetup_event_data
     event_data['venue_id'] = -1
     response = send_post_request(SocialNetworkApiUrl.EVENTS, event_data,
                                  token_first)
     logger.info(response.text)
     assert response.status_code == codes.NOT_FOUND, 'Venue not Found in database'