aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests/test_username_floor.py
blob: 607aacc427d72a38a71111eb9cbd28be90ec0c87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
A username is at least 8 characters — at registration, and nowhere else.

Accounts created under the older 3-character floor must keep signing in, so
the check lives on `RegisterRequest` alone. The client checks the same number
before deriving anything; the two constants are held equal here.
"""

import re
from pathlib import Path

import pytest
from meshbay_hub.api import users

AUTH_PAGE = (Path(__file__).resolve().parents[1]
             / "src" / "meshbay_hub" / "static" / "auth-page.js")
AUTH_KEY = "k" * 44


async def _register(client, username):
    return await client.post("/v1/users/register", json={
        "username": username, "email": "floor@example.com", "auth_key": AUTH_KEY})


@pytest.mark.asyncio
async def test_seven_characters_is_refused(client):
    r = await _register(client, "sevenc7")
    assert r.status_code == 422, r.text


@pytest.mark.asyncio
async def test_surrounding_spaces_do_not_count(client):
    r = await _register(client, "  sevenc7  ")
    assert r.status_code == 422, r.text


@pytest.mark.asyncio
async def test_eight_characters_is_accepted(client):
    r = await _register(client, "eightch8")
    assert r.status_code == 201, r.text


@pytest.mark.asyncio
async def test_an_existing_short_account_still_signs_in(client, monkeypatch):
    """An account made under the old floor is not locked out by the new one."""
    monkeypatch.setattr(users, "USERNAME_MIN_LEN", 3)
    assert (await _register(client, "bob")).status_code == 201
    monkeypatch.setattr(users, "USERNAME_MIN_LEN", 8)

    r = await client.post("/v1/users/login", json={"username": "bob", "auth_key": AUTH_KEY})
    assert r.status_code == 200, r.text


def test_the_client_checks_the_same_floor():
    m = re.search(r"^const USERNAME_MIN_LEN = (\d+);", AUTH_PAGE.read_text(encoding="utf-8"),
                  re.M)
    assert m, "auth-page.js no longer declares USERNAME_MIN_LEN"
    assert int(m.group(1)) == users.USERNAME_MIN_LEN