forked from fleXible/NotifyPlex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
145 lines (129 loc) · 5 KB
/
tests.py
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
#!/usr/bin/env python3
#
# Copyright (C) 2024 Denis <denis@nzbget.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
from os.path import dirname
import os
import subprocess
import unittest
import http.server
import threading
POSTPROCESS_SUCCESS=93
POSTPROCESS_NONE=95
POSTPROCESS_ERROR=94
root = dirname(__file__)
plex_host = 'localhost'
plex_port = 4444
host = '127.0.0.1'
username = 'TestUser'
password = 'TestPassword'
port = '6789'
class MockServerRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/xml')
self.end_headers()
response_content = (b'<?xml version="1.0" encoding="UTF-8"?>' +
b'<MediaContainer size="3" allowSync="0" title1="Plex Library">' +
b'<Directory allowSync="1" art="/:/resources/movie-fanart.jpg" ' +
b'composite="/library/sections/2/composite/1709800247" filters="1" refreshing="0" '+
b'thumb="/:/resources/movie.png" key="2" type="movie" title="Movies" agent="tv.plex.agents.movie" ' +
b'scanner="Plex Movie" language="en-US" uuid="d495999b-6b8c-4676-9c1c-78e61175f0f5" ' +
b'updatedAt="1709711552" createdAt="1709705968" scannedAt="1709800247" content="1" directory="1" contentChangedAt="1691" hidden="0">' +
b'<Location id="10" path="D:\Movies" /></Directory>' +
b'<Directory allowSync="1" art="/:/resources/show-fanart.jpg" ' +
b'composite="/library/sections/3/composite/1709799889" filters="1" refreshing="0" ' +
b'thumb="/:/resources/show.png" key="3" type="show" title="Series" agent="tv.plex.agents.series" scanner="Plex TV Series" ' +
b'language="en-US" uuid="b6df6bac-25d5-448b-9d81-a1ffa1d4db19" updatedAt="1709711531" createdAt="170">' +
b'</Directory>' +
b'</MediaContainer>')
self.wfile.write(response_content)
def get_python():
if os.name == 'nt':
return 'python'
return 'python3'
def run_script():
sys.stdout.flush()
proc = subprocess.Popen([get_python(), root + '/main.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ.copy())
out, err = proc.communicate()
proc.pid
ret_code = proc.returncode
return (out.decode(), int(ret_code), err.decode())
def set_defaults_env():
# NZBGet global options
os.environ['NZBOP_CONTROLPORT'] = port
os.environ['NZBOP_CONTROLIP'] = host
os.environ['NZBOP_CONTROLUSERNAME'] = username
os.environ['NZBOP_CONTROLPASSWORD'] = password
os.environ['NZBPP_TOTALSTATUS'] = 'SUCCESS'
os.environ['NZBOP_VERSION'] = '23.0'
os.environ['NZBPP_CATEGORY'] = 'Movies'
# script options
os.environ['NZBPO_SILENTFAILURE'] = 'no'
os.environ['NZBPO_REFRESHLIBRARY'] = 'no'
os.environ['NZBPO_PLEXUSER'] = 'user'
os.environ['NZBPO_PLEXPASSWORD'] = 'password'
os.environ['NZBPO_MOVIESCAT'] = 'Movies'
os.environ['NZBPO_PLEXHOST'] = plex_host + ':' + str(plex_port)
os.environ['NZBPO_REFRESHMODE'] = 'Both'
os.environ['NZBPO_TVCAT'] = 'TV'
os.environ['NZBPO_PLEXAUTHTOKEN'] = 'Token'
os.environ['NZBPO_CUSTOMPLEXSECTION'] = 'Series'
os.environ['NZBPP_NZBNAME'] = 'NzbName'
os.environ['NZBPP_STATUS'] = 'SUCCESS/'
class Tests(unittest.TestCase):
def test_refresh_auto(self):
set_defaults_env()
os.environ['NZBPO_REFRESHLIBRARY'] = 'yes'
os.environ['NZBPO_REFRESHMODE'] = 'Auto'
server = http.server.HTTPServer((plex_host, plex_port), MockServerRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
[_, code, _] = run_script()
server.shutdown()
server.server_close()
thread.join()
self.assertEqual(code, POSTPROCESS_SUCCESS)
def test_refresh_custom(self):
set_defaults_env()
os.environ['NZBPO_REFRESHLIBRARY'] = 'yes'
os.environ['NZBPO_REFRESHMODE'] = 'Custom'
server = http.server.HTTPServer((plex_host, plex_port), MockServerRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
[_, code, _] = run_script()
server.shutdown()
server.server_close()
thread.join()
self.assertEqual(code, POSTPROCESS_SUCCESS)
def test_refresh_both(self):
set_defaults_env()
os.environ['NZBPO_REFRESHLIBRARY'] = 'yes'
os.environ['NZBPO_REFRESHMODE'] = 'Both'
server = http.server.HTTPServer((plex_host, plex_port), MockServerRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
[_, code, _] = run_script()
server.shutdown()
server.server_close()
thread.join()
self.assertEqual(code, POSTPROCESS_SUCCESS)
if __name__ == '__main__':
unittest.main()