File: test_embyupdate.py

package info (click to toggle)
beets 1.6.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,652 kB
  • sloc: python: 39,201; javascript: 7,997; xml: 334; sh: 247; makefile: 117
file content (231 lines) | stat: -rw-r--r-- 10,049 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from test.helper import TestHelper
from beetsplug import embyupdate
import unittest
import responses


class EmbyUpdateTest(unittest.TestCase, TestHelper):
    def setUp(self):
        self.setup_beets()
        self.load_plugins('embyupdate')

        self.config['emby'] = {
            'host': 'localhost',
            'port': 8096,
            'username': 'username',
            'password': 'password'
        }

    def tearDown(self):
        self.teardown_beets()
        self.unload_plugins()

    def test_api_url_only_name(self):
        self.assertEqual(
            embyupdate.api_url(self.config['emby']['host'].get(),
                               self.config['emby']['port'].get(),
                               '/Library/Refresh'),
            'http://localhost:8096/Library/Refresh?format=json'
        )

    def test_api_url_http(self):
        self.assertEqual(
            embyupdate.api_url('http://localhost',
                               self.config['emby']['port'].get(),
                               '/Library/Refresh'),
            'http://localhost:8096/Library/Refresh?format=json'
        )

    def test_api_url_https(self):
        self.assertEqual(
            embyupdate.api_url('https://localhost',
                               self.config['emby']['port'].get(),
                               '/Library/Refresh'),
            'https://localhost:8096/Library/Refresh?format=json'
        )

    def test_password_data(self):
        self.assertEqual(
            embyupdate.password_data(self.config['emby']['username'].get(),
                                     self.config['emby']['password'].get()),
            {
                'username': 'username',
                'password': '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8',
                'passwordMd5': '5f4dcc3b5aa765d61d8327deb882cf99'
            }
        )

    def test_create_header_no_token(self):
        self.assertEqual(
            embyupdate.create_headers('e8837bc1-ad67-520e-8cd2-f629e3155721'),
            {
                'x-emby-authorization': (
                    'MediaBrowser '
                    'UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", '
                    'Client="other", '
                    'Device="beets", '
                    'DeviceId="beets", '
                    'Version="0.0.0"'
                )
            }
        )

    def test_create_header_with_token(self):
        self.assertEqual(
            embyupdate.create_headers('e8837bc1-ad67-520e-8cd2-f629e3155721',
                                      token='abc123'),
            {
                'x-emby-authorization': (
                    'MediaBrowser '
                    'UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", '
                    'Client="other", '
                    'Device="beets", '
                    'DeviceId="beets", '
                    'Version="0.0.0"'
                ),
                'x-mediabrowser-token': 'abc123'
            }
        )

    @responses.activate
    def test_get_token(self):
        body = ('{"User":{"Name":"username", '
                '"ServerId":"1efa5077976bfa92bc71652404f646ec",'
                '"Id":"2ec276a2642e54a19b612b9418a8bd3b","HasPassword":true,'
                '"HasConfiguredPassword":true,'
                '"HasConfiguredEasyPassword":false,'
                '"LastLoginDate":"2015-11-09T08:35:03.6357440Z",'
                '"LastActivityDate":"2015-11-09T08:35:03.6665060Z",'
                '"Configuration":{"AudioLanguagePreference":"",'
                '"PlayDefaultAudioTrack":true,"SubtitleLanguagePreference":"",'
                '"DisplayMissingEpisodes":false,'
                '"DisplayUnairedEpisodes":false,'
                '"GroupMoviesIntoBoxSets":false,'
                '"DisplayChannelsWithinViews":[],'
                '"ExcludeFoldersFromGrouping":[],"GroupedFolders":[],'
                '"SubtitleMode":"Default","DisplayCollectionsView":true,'
                '"DisplayFoldersView":false,"EnableLocalPassword":false,'
                '"OrderedViews":[],"IncludeTrailersInSuggestions":true,'
                '"EnableCinemaMode":true,"LatestItemsExcludes":[],'
                '"PlainFolderViews":[],"HidePlayedInLatest":true,'
                '"DisplayChannelsInline":false},'
                '"Policy":{"IsAdministrator":true,"IsHidden":false,'
                '"IsDisabled":false,"BlockedTags":[],'
                '"EnableUserPreferenceAccess":true,"AccessSchedules":[],'
                '"BlockUnratedItems":[],'
                '"EnableRemoteControlOfOtherUsers":false,'
                '"EnableSharedDeviceControl":true,'
                '"EnableLiveTvManagement":true,"EnableLiveTvAccess":true,'
                '"EnableMediaPlayback":true,'
                '"EnableAudioPlaybackTranscoding":true,'
                '"EnableVideoPlaybackTranscoding":true,'
                '"EnableContentDeletion":false,'
                '"EnableContentDownloading":true,"EnableSync":true,'
                '"EnableSyncTranscoding":true,"EnabledDevices":[],'
                '"EnableAllDevices":true,"EnabledChannels":[],'
                '"EnableAllChannels":true,"EnabledFolders":[],'
                '"EnableAllFolders":true,"InvalidLoginAttemptCount":0,'
                '"EnablePublicSharing":true}},'
                '"SessionInfo":{"SupportedCommands":[],'
                '"QueueableMediaTypes":[],"PlayableMediaTypes":[],'
                '"Id":"89f3b33f8b3a56af22088733ad1d76b3",'
                '"UserId":"2ec276a2642e54a19b612b9418a8bd3b",'
                '"UserName":"username","AdditionalUsers":[],'
                '"ApplicationVersion":"Unknown version",'
                '"Client":"Unknown app",'
                '"LastActivityDate":"2015-11-09T08:35:03.6665060Z",'
                '"DeviceName":"Unknown device","DeviceId":"Unknown device id",'
                '"SupportsRemoteControl":false,"PlayState":{"CanSeek":false,'
                '"IsPaused":false,"IsMuted":false,"RepeatMode":"RepeatNone"}},'
                '"AccessToken":"4b19180cf02748f7b95c7e8e76562fc8",'
                '"ServerId":"1efa5077976bfa92bc71652404f646ec"}')

        responses.add(responses.POST,
                      ('http://localhost:8096'
                       '/Users/AuthenticateByName'),
                      body=body,
                      status=200,
                      content_type='application/json')

        headers = {
            'x-emby-authorization': (
                'MediaBrowser '
                'UserId="e8837bc1-ad67-520e-8cd2-f629e3155721", '
                'Client="other", '
                'Device="beets", '
                'DeviceId="beets", '
                'Version="0.0.0"'
            )
        }

        auth_data = {
            'username': 'username',
            'password': '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8',
            'passwordMd5': '5f4dcc3b5aa765d61d8327deb882cf99'
        }

        self.assertEqual(
            embyupdate.get_token('http://localhost', 8096, headers, auth_data),
            '4b19180cf02748f7b95c7e8e76562fc8')

    @responses.activate
    def test_get_user(self):
        body = ('[{"Name":"username",'
                '"ServerId":"1efa5077976bfa92bc71652404f646ec",'
                '"Id":"2ec276a2642e54a19b612b9418a8bd3b","HasPassword":true,'
                '"HasConfiguredPassword":true,'
                '"HasConfiguredEasyPassword":false,'
                '"LastLoginDate":"2015-11-09T08:35:03.6357440Z",'
                '"LastActivityDate":"2015-11-09T08:42:39.3693220Z",'
                '"Configuration":{"AudioLanguagePreference":"",'
                '"PlayDefaultAudioTrack":true,"SubtitleLanguagePreference":"",'
                '"DisplayMissingEpisodes":false,'
                '"DisplayUnairedEpisodes":false,'
                '"GroupMoviesIntoBoxSets":false,'
                '"DisplayChannelsWithinViews":[],'
                '"ExcludeFoldersFromGrouping":[],"GroupedFolders":[],'
                '"SubtitleMode":"Default","DisplayCollectionsView":true,'
                '"DisplayFoldersView":false,"EnableLocalPassword":false,'
                '"OrderedViews":[],"IncludeTrailersInSuggestions":true,'
                '"EnableCinemaMode":true,"LatestItemsExcludes":[],'
                '"PlainFolderViews":[],"HidePlayedInLatest":true,'
                '"DisplayChannelsInline":false},'
                '"Policy":{"IsAdministrator":true,"IsHidden":false,'
                '"IsDisabled":false,"BlockedTags":[],'
                '"EnableUserPreferenceAccess":true,"AccessSchedules":[],'
                '"BlockUnratedItems":[],'
                '"EnableRemoteControlOfOtherUsers":false,'
                '"EnableSharedDeviceControl":true,'
                '"EnableLiveTvManagement":true,"EnableLiveTvAccess":true,'
                '"EnableMediaPlayback":true,'
                '"EnableAudioPlaybackTranscoding":true,'
                '"EnableVideoPlaybackTranscoding":true,'
                '"EnableContentDeletion":false,'
                '"EnableContentDownloading":true,'
                '"EnableSync":true,"EnableSyncTranscoding":true,'
                '"EnabledDevices":[],"EnableAllDevices":true,'
                '"EnabledChannels":[],"EnableAllChannels":true,'
                '"EnabledFolders":[],"EnableAllFolders":true,'
                '"InvalidLoginAttemptCount":0,"EnablePublicSharing":true}}]')

        responses.add(responses.GET,
                      'http://localhost:8096/Users/Public',
                      body=body,
                      status=200,
                      content_type='application/json')

        response = embyupdate.get_user('http://localhost', 8096, 'username')

        self.assertEqual(response[0]['Id'],
                         '2ec276a2642e54a19b612b9418a8bd3b')

        self.assertEqual(response[0]['Name'],
                         'username')


def suite():
    return unittest.TestLoader().loadTestsFromName(__name__)


if __name__ == '__main__':
    unittest.main(defaultTest='suite')