def test_of_multiple_urls(self): # test for multiple URLs test_result = search_urls_in_text(TEST_DATA['multiple_urls']) assert len(test_result) == 3 assert test_result[0] in TEST_DATA['multiple_urls'] assert test_result[1] in TEST_DATA['multiple_urls'] assert test_result[2] in TEST_DATA['multiple_urls']
def test_of_multiple_urls(self): # test for multiple URLs test_result = search_urls_in_text(TEST_DATA['multiple_urls']) assert len(test_result) == 3 self._assert_validate_url_format(test_result[0]) self._assert_validate_url_format(test_result[1]) self._assert_validate_url_format(test_result[2])
def validate_urls_in_body_text(text): """ This function validates the URLs present in SMS body text. It first check if they are in valid format, then it makes HTTP GET call to that URL to verify the URL is live. :param text: :return: """ raise_if_not_instance_of(text, basestring) urls = search_urls_in_text(text) invalid_urls = [] for url in urls: try: validate_url_format(url) if not validate_url_by_http_request(url): invalid_urls.append(url) except InvalidUsage: invalid_urls.append(url) return invalid_urls
def process_urls_in_sms_body_text(self, candidate_id): """ We use "url_conversion" table fields: 1- 'destination_url' as URL provided by recruiter 2- 'source_url' as URL to redirect candidate to our app - Once we have the body text of SMS campaign provided by recruiter(user), We check if it contains any URL in it. If it has any link, we do the following: 1- Save that URL in db table "url_conversion" as destination_url with empty source_url. 2- Create a URL (using id of url_conversion record created in step 1) to redirect candidate to our app and save that as source_url (for the same database record we created in step 1). This source_url looks like http://127.0.0.1:8012/v1/redirect/1 3- Convert the source_url into shortened URL using Google's shorten URL API. 4- Replace the link in original body text with the shortened URL (which we created in step 2) 5- Set the updated body text in self.transform_body_text Otherwise we save the body text in modified_body_text - This method is called from send() method of class SmsCampaignBase inside sms_campaign_service/sms_campaign_base.py. :param candidate_id: id of Candidate :type candidate_id: int | long :exception: GoogleShortenUrlAPIError :return: list of URL conversion records :rtype: list **See Also** .. see also:: send() method in SmsCampaignBase class. """ raise_if_dict_values_are_not_int_or_long( dict(candidate_id=candidate_id)) logger.debug( 'process_urls_in_sms_body_text: Processing any ' 'link present in body_text for ' 'SMS Campaign(id:%s) and Candidate(id:%s). (User(id:%s))' % (self.campaign.id, candidate_id, self.user.id)) urls_in_body_text = search_urls_in_text(self.campaign.body_text) short_urls = [] url_conversion_ids = [] for url in urls_in_body_text: validate_url_format(url) # We have only one link in body text which needs to shortened. url_conversion_id = self.create_or_update_url_conversion( destination_url=url, source_url='') # URL to redirect candidates to our end point app_redirect_url = SmsCampaignApiUrl.REDIRECT if app.config[TalentConfigKeys.ENV_KEY] == TalentEnvs.DEV: app_redirect_url = replace_localhost_with_ngrok( SmsCampaignApiUrl.REDIRECT) # redirect URL looks like (for prod) # http://sms-campaing-service.gettalent.com/redirect/1 redirect_url = str(app_redirect_url % url_conversion_id) # sign the redirect URL long_url = CampaignUtils.sign_redirect_url( redirect_url, datetime.utcnow() + relativedelta(years=+1)) # long_url looks like (for prod) # http://sms-campaing-service.gettalent.com/v1/redirect/1052?valid_until=1453990099.0 # &auth_user=no_user&extra=&signature=cWQ43J%2BkYetfmE2KmR85%2BLmvuIw%3D # Use Google's API to shorten the long URL short_url, error = url_conversion(long_url) logger.info("url_conversion: Long URL was: %s" % long_url) logger.info("url_conversion: Shortened URL is: %s" % short_url) if error: raise GoogleShortenUrlAPIError(error) short_urls.append(short_url) url_conversion_ids.append(url_conversion_id) if CampaignUtils.IS_DEV: # update the 'source_url' in "url_conversion" record. # Source URL should not be saved in database. But we have tests written # for Redirection endpoint. That's why in case of DEV, I am saving source URL here. self.create_or_update_url_conversion( url_conversion_id=url_conversion_id, source_url=long_url) updated_text_body = self.transform_body_text(urls_in_body_text, short_urls) return updated_text_body, url_conversion_ids
def test_of_ftp_url(self): # test of ftp URL test_result = search_urls_in_text(TEST_DATA['ftp_url']) assert len(test_result) == 2 assert test_result[0] in TEST_DATA['ftp_url'] assert test_result[1] in TEST_DATA['ftp_url']
def test_of_www_url(self): # test of www URL test_result = search_urls_in_text(TEST_DATA['www_url']) assert len(test_result) == 1 assert test_result[0] in TEST_DATA['www_url']
def test_of_https_url(self): # test of https URL test_result = search_urls_in_text(TEST_DATA['https_url']) assert len(test_result) == 1 assert test_result[0] in TEST_DATA['https_url']
def test_with_keywords(self): # test string with valid URLs keywords like http, https, www. assert len(search_urls_in_text(TEST_DATA['with_keywords'])) == 0
def test_with_no_url(self): # test with string having no URL assert len(search_urls_in_text(TEST_DATA['no_url'])) == 0
def test_with_empty_string(self): # test empty string test_string = '' assert len(search_urls_in_text(test_string)) == 0
def test_of_ftp_url(self): # test of ftp URL test_result = search_urls_in_text(TEST_DATA['ftp_url']) assert len(test_result) == 2 self._assert_validate_url_format(test_result[0]) self._assert_validate_url_format(test_result[1])
def test_of_www_url(self): # test of www URL test_result = search_urls_in_text(TEST_DATA['www_url']) assert len(test_result) == 1 self._assert_validate_url_format(test_result[0])