diff --git a/app/api/views/alias_options.py b/app/api/views/alias_options.py index 3312922a..1dc40d20 100644 --- a/app/api/views/alias_options.py +++ b/app/api/views/alias_options.py @@ -1,3 +1,4 @@ +import tldextract from flask import jsonify, request, g from sqlalchemy import desc @@ -62,12 +63,10 @@ def options_v4(): if hostname: # keep only the domain name of hostname, ignore TLD and subdomain # for ex www.groupon.com -> groupon - domain_name = hostname - if "." in hostname: - parts = hostname.split(".") - domain_name = parts[-2] - domain_name = convert_to_id(domain_name) - ret["prefix_suggestion"] = domain_name + ext = tldextract.extract(hostname) + prefix_suggestion = ext.domain + prefix_suggestion = convert_to_id(prefix_suggestion) + ret["prefix_suggestion"] = prefix_suggestion suffixes = get_available_suffixes(user) @@ -133,12 +132,10 @@ def options_v5(): if hostname: # keep only the domain name of hostname, ignore TLD and subdomain # for ex www.groupon.com -> groupon - domain_name = hostname - if "." in hostname: - parts = hostname.split(".") - domain_name = parts[-2] - domain_name = convert_to_id(domain_name) - ret["prefix_suggestion"] = domain_name + ext = tldextract.extract(hostname) + prefix_suggestion = ext.domain + prefix_suggestion = convert_to_id(prefix_suggestion) + ret["prefix_suggestion"] = prefix_suggestion suffixes = get_available_suffixes(user) diff --git a/tests/api/test_alias_options.py b/tests/api/test_alias_options.py index 81239c79..f5e32525 100644 --- a/tests/api/test_alias_options.py +++ b/tests/api/test_alias_options.py @@ -1,5 +1,3 @@ -import json - from flask import url_for from app.db import Session @@ -132,10 +130,15 @@ def test_different_scenarios_v5(flask_client): "/api/v5/alias/options?hostname=www.test.com", headers={"Authentication": api_key.code}, ) - print(json.dumps(r.json, indent=2)) - assert r.json["prefix_suggestion"] == "test" + # <<< with hostname with 2 parts TLD, for example wwww.numberoneshoes.co.nz >>> + r = flask_client.get( + "/api/v5/alias/options?hostname=wwww.numberoneshoes.co.nz", + headers={"Authentication": api_key.code}, + ) + assert r.json["prefix_suggestion"] == "numberoneshoes" + # <<< with recommendation >>> alias = Alias.create_new(user, prefix="test") Session.commit()