File size: 3,347 Bytes
f394214
 
61f83d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f394214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
import asyncio

from bson import ObjectId

from app.services import shared_devices
from app.services.shared_devices import parse_device_ids, user_scope_filter, with_shared_devices

USER = {"_id": ObjectId("65dec42fbb74c16d06c15b57")}


def test_parse_device_ids_handles_blanks_and_spaces():
    assert parse_device_ids("") == []
    assert parse_device_ids(" ESP32_21E5F0 , PLAYBACK_DEVICE_REC ,,") == ["ESP32_21E5F0", "PLAYBACK_DEVICE_REC"]


def test_with_shared_devices_keeps_own_devices_first_without_duplicates(monkeypatch):
    monkeypatch.setattr(shared_devices, "shared_device_ids", lambda: ["ESP32_21E5F0", "PLAYBACK_DEVICE_REC"])
    assert with_shared_devices(["MY_DEVICE", "ESP32_21E5F0"]) == ["MY_DEVICE", "ESP32_21E5F0", "PLAYBACK_DEVICE_REC"]
    assert with_shared_devices([]) == ["ESP32_21E5F0", "PLAYBACK_DEVICE_REC"]


def test_user_scope_filter_includes_shared_devices(monkeypatch):
    monkeypatch.setattr(shared_devices, "shared_device_ids", lambda: ["ESP32_21E5F0"])
    assert user_scope_filter(USER) == {
        "$or": [{"user_id": "65dec42fbb74c16d06c15b57"}, {"device_id": {"$in": ["ESP32_21E5F0"]}}]
    }


def test_user_scope_filter_is_unchanged_without_shared_devices(monkeypatch):
    monkeypatch.setattr(shared_devices, "shared_device_ids", lambda: [])
    assert user_scope_filter(USER) == {"user_id": "65dec42fbb74c16d06c15b57"}
    assert with_shared_devices(["MY_DEVICE"]) == ["MY_DEVICE"]


class FakeVitals:
    def __init__(self, device_ids):
        self.device_ids = device_ids

    async def distinct(self, field):
        assert field == "device_id"
        return list(self.device_ids)


class FakeDb:
    def __init__(self, device_ids):
        self.vitals = FakeVitals(device_ids)


UPLOADED = ["PLAYBACK_PTBXL", "ESP32_NEW001", "ESP32_21E5F0", "PLAYBACK_DEVICE_REC", "MY_DEVICE"]


def test_star_is_not_a_device_id(monkeypatch):
    monkeypatch.setattr(shared_devices.settings, "SHARED_DEVICE_IDS", "ESP32_21E5F0, *")
    assert shared_devices.shared_device_ids() == ["ESP32_21E5F0"]
    assert shared_devices.all_devices_shared()
    monkeypatch.setattr(shared_devices.settings, "SHARED_DEVICE_IDS", "ESP32_21E5F0")
    assert not shared_devices.all_devices_shared()


def test_star_adds_every_uploading_device_except_playback(monkeypatch):
    monkeypatch.setattr(shared_devices.settings, "SHARED_DEVICE_IDS", "ESP32_21E5F0,PLAYBACK_DEVICE_REC,*")
    ids = asyncio.run(shared_devices.visible_device_ids(FakeDb(UPLOADED), ["MY_DEVICE"]))
    # own first, then named shared devices (playback allowed when named), then other uploaders
    assert ids == ["MY_DEVICE", "ESP32_21E5F0", "PLAYBACK_DEVICE_REC", "ESP32_NEW001"]
    assert "PLAYBACK_PTBXL" not in ids


def test_without_star_only_named_devices_are_added(monkeypatch):
    monkeypatch.setattr(shared_devices.settings, "SHARED_DEVICE_IDS", "ESP32_21E5F0")
    ids = asyncio.run(shared_devices.visible_device_ids(FakeDb(UPLOADED), []))
    assert ids == ["ESP32_21E5F0"]


def test_star_scope_covers_the_visible_device_list(monkeypatch):
    monkeypatch.setattr(shared_devices.settings, "SHARED_DEVICE_IDS", "*")
    user = dict(USER, device_ids=["ESP32_21E5F0", "ESP32_NEW001"])
    assert user_scope_filter(user) == {
        "$or": [{"user_id": "65dec42fbb74c16d06c15b57"}, {"device_id": {"$in": ["ESP32_21E5F0", "ESP32_NEW001"]}}]
    }