| 1 |
"""Tests for the 'permissions' plugin.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import platform |
| 5 |
from unittest.mock import Mock, patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from beets.test.helper import AsIsImporterMixin, ImportHelper, PluginMixin |
| 10 |
from beetsplug.permissions import ( |
| 11 |
check_permissions, |
| 12 |
convert_perm, |
| 13 |
dirs_in_library, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class TestPermissionsPlugin(AsIsImporterMixin, PluginMixin, ImportHelper): |
| 18 |
plugin = "permissions" |
| 19 |
|
| 20 |
def setup_beets(self): |
| 21 |
super().setup_beets() |
| 22 |
self.config["permissions"] = {"file": "777", "dir": "777"} |
| 23 |
|
| 24 |
def test_permissions_on_album_imported(self): |
| 25 |
self.import_and_check_permissions() |
| 26 |
|
| 27 |
def test_permissions_on_item_imported(self): |
| 28 |
self.config["import"]["singletons"] = True |
| 29 |
self.import_and_check_permissions() |
| 30 |
|
| 31 |
def import_and_check_permissions(self): |
| 32 |
if platform.system() == "Windows": |
| 33 |
pytest.skip("permissions not available on Windows") |
| 34 |
|
| 35 |
track_file = self.import_path / "album" / "track_1.mp3" |
| 36 |
assert track_file.stat().st_mode & 0o777 != 511 |
| 37 |
|
| 38 |
self.run_asis_importer() |
| 39 |
item = self.lib.items().get() |
| 40 |
|
| 41 |
paths = (item.path, *dirs_in_library(self.lib.directory, item.path)) |
| 42 |
for path in paths: |
| 43 |
assert os.stat(path).st_mode & 0o777 == 511 |
| 44 |
|
| 45 |
def test_convert_perm_from_string(self): |
| 46 |
assert convert_perm("10") == 8 |
| 47 |
|
| 48 |
def test_convert_perm_from_int(self): |
| 49 |
assert convert_perm(10) == 8 |
| 50 |
|
| 51 |
def test_permissions_on_set_art(self): |
| 52 |
self.do_set_art(True) |
| 53 |
|
| 54 |
@patch("os.chmod", Mock()) |
| 55 |
def test_failing_permissions_on_set_art(self): |
| 56 |
self.do_set_art(False) |
| 57 |
|
| 58 |
def do_set_art(self, expect_success): |
| 59 |
if platform.system() == "Windows": |
| 60 |
pytest.skip("permissions not available on Windows") |
| 61 |
self.run_asis_importer() |
| 62 |
album = self.lib.albums().get() |
| 63 |
artpath = self.temp_path / "cover.jpg" |
| 64 |
artpath.touch() |
| 65 |
album.set_art(artpath) |
| 66 |
assert expect_success == check_permissions(album.artpath, 0o777) |