File size: 2,452 Bytes
bed4e38
ff8c636
 
bed4e38
754345f
 
 
 
 
 
bed4e38
 
ff8c636
 
 
 
bed4e38
 
 
 
 
754345f
 
 
 
 
 
 
 
 
bed4e38
 
 
 
ff8c636
 
bed4e38
 
 
 
754345f
 
 
 
 
 
bed4e38
 
895c557
 
bed4e38
754345f
 
 
 
 
 
bed4e38
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
from agent.core.hf_access import is_billing_error, jobs_access_from_whoami


def test_personal_user_lists_username_namespace():
    access = jobs_access_from_whoami(
        {
            "name": "alice",
            "orgs": [],
        }
    )
    assert access.username == "alice"
    assert access.org_names == []
    assert access.eligible_namespaces == ["alice"]
    assert access.default_namespace == "alice"


def test_user_with_orgs_lists_all_namespaces_regardless_of_plan():
    # Plan/tier is ignored — credits live on the namespace itself, so any
    # org the user belongs to is eligible.  We sort orgs alphabetically and
    # always put the personal namespace first so the picker default is the
    # user's own account.
    access = jobs_access_from_whoami(
        {
            "name": "alice",
            "orgs": [
                {"name": "team-a", "plan": "team"},
                {"name": "oss-friends", "plan": "free"},
            ],
        }
    )
    assert access.username == "alice"
    assert access.org_names == ["oss-friends", "team-a"]
    assert access.eligible_namespaces == ["alice", "oss-friends", "team-a"]
    assert access.default_namespace == "alice"


def test_free_user_without_org_still_eligible_under_personal_namespace():
    # Pro is no longer required — the user is offered their personal
    # namespace; whether they actually have credits is decided at job
    # creation time when HF returns a 402 / billing error.
    access = jobs_access_from_whoami(
        {
            "name": "alice",
            "orgs": [],
        }
    )
    assert access.eligible_namespaces == ["alice"]
    assert access.default_namespace == "alice"


def test_org_only_token_falls_back_to_first_org():
    access = jobs_access_from_whoami(
        {
            "name": None,
            "orgs": [{"name": "team-a"}, {"name": "team-b"}],
        }
    )
    assert access.username is None
    assert access.eligible_namespaces == ["team-a", "team-b"]
    assert access.default_namespace == "team-a"


def test_is_billing_error_detects_402_and_credit_phrasing():
    assert is_billing_error("402 Payment Required")
    assert is_billing_error("Insufficient credits on namespace foo")
    assert is_billing_error("This namespace requires credits to run jobs")
    assert is_billing_error("Out of credit, please add billing")
    assert not is_billing_error("Internal server error")
    assert not is_billing_error("")