def test_match_keyword(self, mock_goodie): fake = Faker() mocked_keyword = fake.domain_word() # Mock Goodies response mock_goodie.return_value = [{'good_keyword': mocked_keyword}] info("Created mock Goodie with keyword: {}".format(mocked_keyword)) domain = "{}x.com".format(mocked_keyword) info("Requested domain - {}".format(domain)) l = match_keyword(domain) assert_type(l, tuple, "Check if proper tuple is returned") assert_true(l[0], "Check if keyword matches domain") assert_equal(l[1], mocked_keyword, "Check if returned keyword is equal to mocked one")
def test_not_match_keyword(self, mock_goodie): fake = Faker() mocked_keyword = fake.domain_word() # Mock Goodies response mock_goodie.return_value = [{'good_keyword': mocked_keyword}] info("Created mock Goodie with keyword: {}".format(mocked_keyword)) while 1: mocked_domain = fake.domain_name() if mocked_keyword in mocked_domain: continue else: break info("Requested domain - {}".format(mocked_domain)) l = match_keyword(mocked_domain) assert_type(l, tuple, "Check if proper tuple is returned") assert_false(l[0], "Check if keyword does not match domain") assert_none(l[1], "Check if returned keyword is equal to None")
def get_keyword_details(url_body): try: jsonschema.validate(url_body, details_url_schema) except jsonschema.exceptions.ValidationError as exc: raise BadRequest(exc.message) domain = url_to_domain(url_body.get('url')) _, keyword = keywords.match_keyword(domain) if not keyword: return _no_data_response() response_text = { "details": { "matched_keyword": keyword } } return Response(json.dumps( response_text, default=_default_json_model ), 200, mimetype="application/json")
def create_baddie(domain): _, ip_id = add_ip(domain) _, crt_id = add_cert(domain) good_keywords = [k['good_keyword'] for k in Goodies.get_all_goodies()] domain_phrases = domain.split('.') _, _, lev_matched_keyword = lev.levenstein_check(good_keywords, domain_phrases) min_lev_distance = 0 lev_distance = 0 if lev_matched_keyword: for phrase in domain_phrases: lev_distance = lev.calculate_levenstein(lev_matched_keyword, phrase) if 3 > min_lev_distance > lev_distance: min_lev_distance = lev_distance if not lev_matched_keyword: lev_matched_keyword = '' _, contained_matched_keyword = match_keyword(domain) if not contained_matched_keyword: contained_matched_keyword = '' entropy = ent.get_entropy(domain) return add_baddie(domain, ip_id[1], crt_id[1], lev_distance, lev_matched_keyword, contained_matched_keyword, entropy)
def verify_keyword_match(domain): verdict, _ = match_keyword(domain) return verdict