test_models.py 3.54 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
2
3
import pytest
from django import db

4
5
from funkwhale_api.federation import models

Eliot Berriot's avatar
Eliot Berriot committed
6
7

def test_cannot_duplicate_actor(factories):
Eliot Berriot's avatar
Eliot Berriot committed
8
    actor = factories["federation.Actor"]()
Eliot Berriot's avatar
Eliot Berriot committed
9
10

    with pytest.raises(db.IntegrityError):
Eliot Berriot's avatar
Eliot Berriot committed
11
12
        factories["federation.Actor"](
            domain=actor.domain, preferred_username=actor.preferred_username
Eliot Berriot's avatar
Eliot Berriot committed
13
14
15
16
        )


def test_cannot_duplicate_follow(factories):
Eliot Berriot's avatar
Eliot Berriot committed
17
    follow = factories["federation.Follow"]()
Eliot Berriot's avatar
Eliot Berriot committed
18
19

    with pytest.raises(db.IntegrityError):
Eliot Berriot's avatar
Eliot Berriot committed
20
        factories["federation.Follow"](target=follow.target, actor=follow.actor)
Eliot Berriot's avatar
Eliot Berriot committed
21

22

Eliot Berriot's avatar
Eliot Berriot committed
23
def test_follow_federation_url(factories):
Eliot Berriot's avatar
Eliot Berriot committed
24
    follow = factories["federation.Follow"](local=True)
25
26
27
28
29
30
31
    expected = "{}#follows/{}".format(follow.actor.fid, follow.uuid)

    assert follow.get_federation_id() == expected


def test_actor_get_quota(factories):
    library = factories["music.Library"]()
Eliot Berriot's avatar
Eliot Berriot committed
32
    factories["music.Upload"](
33
34
35
36
37
        library=library,
        import_status="pending",
        audio_file__from_path=None,
        audio_file__data=b"a",
    )
Eliot Berriot's avatar
Eliot Berriot committed
38
    factories["music.Upload"](
39
40
41
42
43
        library=library,
        import_status="skipped",
        audio_file__from_path=None,
        audio_file__data=b"aa",
    )
Eliot Berriot's avatar
Eliot Berriot committed
44
    factories["music.Upload"](
45
46
47
48
49
        library=library,
        import_status="errored",
        audio_file__from_path=None,
        audio_file__data=b"aaa",
    )
Eliot Berriot's avatar
Eliot Berriot committed
50
    factories["music.Upload"](
51
52
53
54
55
        library=library,
        import_status="finished",
        audio_file__from_path=None,
        audio_file__data=b"aaaa",
    )
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

    # this one is imported in place and don't count
    factories["music.Upload"](
        library=library,
        import_status="finished",
        source="file://test",
        audio_file=None,
        size=42,
    )
    # this one is imported in place but count because there is a mapped file
    factories["music.Upload"](
        library=library,
        import_status="finished",
        source="file://test2",
        audio_file__from_path=None,
        audio_file__data=b"aaaa",
    )

    expected = {"total": 14, "pending": 1, "skipped": 2, "errored": 3, "finished": 8}
75
76

    assert library.actor.get_current_usage() == expected
77
78
79
80
81
82
83
84
85
86
87
88
89


@pytest.mark.parametrize(
    "value, expected",
    [
        ("Domain.com", "domain.com"),
        ("hello-WORLD.com", "hello-world.com"),
        ("posés.com", "posés.com"),
    ],
)
def test_domain_name_saved_properly(value, expected, factories):
    domain = factories["federation.Domain"](name=value)
    assert domain.name == expected
90
91
92
93
94
95
96
97


def test_external_domains(factories, settings):
    d1 = factories["federation.Domain"]()
    d2 = factories["federation.Domain"]()
    settings.FEDERATION_HOSTNAME = d1.pk

    assert list(models.Domain.objects.external()) == [d2]
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117


def test_domain_stats(factories):
    expected = {
        "actors": 0,
        "libraries": 0,
        "tracks": 0,
        "albums": 0,
        "uploads": 0,
        "artists": 0,
        "outbox_activities": 0,
        "received_library_follows": 0,
        "emitted_library_follows": 0,
        "media_total_size": 0,
        "media_downloaded_size": 0,
    }

    domain = factories["federation.Domain"]()

    assert domain.get_stats() == expected
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


def test_actor_stats(factories):
    expected = {
        "libraries": 0,
        "tracks": 0,
        "albums": 0,
        "uploads": 0,
        "artists": 0,
        "outbox_activities": 0,
        "received_library_follows": 0,
        "emitted_library_follows": 0,
        "media_total_size": 0,
        "media_downloaded_size": 0,
    }

    actor = factories["federation.Actor"]()

    assert actor.get_stats() == expected