Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Keep source on documents read off term pages; tighten the username reservation

Claude567fe8aa0df2
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index 7f039f4..bee6d45 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -128,10 +128,12 @@ def register(request, registration: Registration):
raise InvalidRequest("Email already registered")
username = registration.username or generate_username()
- username_taken = MwmblUser.objects.filter(username=registration.username).exists()
- # Reserved even before migration 0049 makes the user, so nobody can take it first and fail it.
- if registration.username and (username_taken or registration.username == SEED_CRAWLER_USERNAME):
- raise InvalidRequest("Username already exists")
+ if registration.username:
+ username_taken = MwmblUser.objects.filter(username=registration.username).exists()
+ # Reserved even before migration 0049 makes the user, so nobody can take it first and
+ # fail it - and in any case, as allauth's blacklist is, so nobody can pass for it.
+ if username_taken or registration.username.lower() == SEED_CRAWLER_USERNAME:
+ raise InvalidRequest("Username already exists")
user = MwmblUser(username=username, email=registration.email)
user.set_password(registration.password)
diff --git a/mwmbl/tinysearchengine/rank.py b/mwmbl/tinysearchengine/rank.py
index d1fd7cf..3aac63a 100644
--- a/mwmbl/tinysearchengine/rank.py
+++ b/mwmbl/tinysearchengine/rank.py
@@ -411,6 +411,7 @@ class Ranker:
result.score,
result.term,
remove_curate_state(result.state),
+ source=result.source,
)
for result in items_wrong_state
]
diff --git a/test/test_rank.py b/test/test_rank.py
index be14d81..4b3ed70 100644
--- a/test/test_rank.py
+++ b/test/test_rank.py
@@ -8,7 +8,7 @@ from requests.exceptions import RetryError
from urllib3.exceptions import MaxRetryError, ResponseError
from mwmbl.tinysearchengine import rank
-from mwmbl.tinysearchengine.indexer import Document
+from mwmbl.tinysearchengine.indexer import Document, DocumentSource
from mwmbl.tinysearchengine.ltr_rank import LTRRanker
from mwmbl.tinysearchengine.rank import (
NUM_WIKI_RESULTS,
@@ -84,6 +84,25 @@ def test_search_retrieved_ranks_like_search_without_external_search():
assert ranker.external_search_calls == []
+def test_a_staan_copy_from_the_index_keeps_its_source():
+ """Read off a term's page rather than the curation term's, as it is for any query other
+ than the one it was stored under, it still goes out labelled as Staan's."""
+ staan_copy = Document(
+ "Tokio async runtime",
+ "https://tokio.rs/",
+ "An async runtime for Rust.",
+ 1.0,
+ term="tokio",
+ source=DocumentSource.STAAN_INDEXED,
+ )
+ ranker = _TrackingRanker()
+ ranker.tiny_index.retrieve.side_effect = lambda term: [staan_copy] if term == "tokio" else []
+
+ (result,) = ranker.search("async tokio ", [], use_external_search=False)
+
+ assert result.source == DocumentSource.STAAN_INDEXED
+
+
def _make_retry_error(status_code: int) -> RetryError:
reason = ResponseError(f"too many {status_code} error responses")
max_retry_error = MaxRetryError(
diff --git a/test/test_system_users.py b/test/test_system_users.py
index 914414c..55f90c0 100644
--- a/test/test_system_users.py
+++ b/test/test_system_users.py
@@ -35,6 +35,19 @@ def test_the_seed_crawlers_username_cannot_be_registered(client):
assert not User.objects.filter(username=SEED_CRAWLER_USERNAME).exists()
+def test_a_lookalike_of_the_seed_crawlers_username_cannot_be_registered(client):
+ response = client.post(
+ "/api/v1/platform/register",
+ data=json.dumps(
+ {"email": "[email protected]", "password": "correctpassword", "username": SEED_CRAWLER_USERNAME.title()}
+ ),
+ content_type="application/json",
+ )
+
+ assert response.status_code == 400
+
+
def test_allauth_refuses_the_seed_crawlers_username():
"""Settings cannot import SEED_CRAWLER_USERNAME, so they repeat it."""
assert settings.ACCOUNT_USERNAME_BLACKLIST == [SEED_CRAWLER_USERNAME]