From e82cdd812d05425f0be349b8cce86837ae8fc82d Mon Sep 17 00:00:00 2001 From: dnzbk Date: Tue, 27 Feb 2024 19:32:09 +0300 Subject: [PATCH] Add: tests --- .github/workflows/tests.yml | 15 +++++ .gitignore | 1 + test_data/with_password_123.rar | Bin 0 -> 134 bytes test_data/without_password.rar | Bin 0 -> 69 bytes tests.py | 100 ++++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 test_data/with_password_123.rar create mode 100644 test_data/without_password.rar create mode 100644 tests.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..deb528f --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,15 @@ +name: tests + +on: + push: + branches: + - feature/* + - main + +jobs: + tests: + uses: nzbgetcom/nzbget-extensions/.github/workflows/python-tests.yml@main + with: + python-versions: "3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12" + supported-python-versions: "3.8 3.9 3.10 3.11 3.12" + test-script: tests.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9a5aec --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/test_data/with_password_123.rar b/test_data/with_password_123.rar new file mode 100644 index 0000000000000000000000000000000000000000..122229c000fdeb9d0d56e59d5e990ad83c408ec1 GIT binary patch literal 134 zcmV;10D1pXVR9iF2LS*xlC|Xu0R;vC1pxtofB+LB0q9Z!13Zub1b_e_J7d#wfCB&p zUoLcbbT9z`0}tY1J1KrV`p9`MjG~C)SKDwi&2VCa{Bg?~l=9k2o%Vy*7mf9&55#<9 oY48dI0wLmX%ZX{)0d`-Q3cHY%hW9zFSpZ6qu^o3-Q3C}80H7N&?*IS* literal 0 HcmV?d00001 diff --git a/test_data/without_password.rar b/test_data/without_password.rar new file mode 100644 index 0000000000000000000000000000000000000000..00c3567a713fbee29277203ee090dd60544cd5e7 GIT binary patch literal 69 zcmWGaEK-zWXJjy*wDl<$BP$yNDS5i^J X#muDmsNi&G<}F6q@~}W=Ru%>TIlB;| literal 0 HcmV?d00001 diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..a93e980 --- /dev/null +++ b/tests.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +# +# Test for FakeDetector queue/post-processing script for NZBGet. +# +# Copyright (C) 2024 Denis +# +# 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 . +# + +import sys +from os.path import dirname +import os +import subprocess +import unittest +import shutil + +POSTPROCESS_SUCCESS=93 +POSTPROCESS_NONE=95 +POSTPROCESS_ERROR=94 + +unrar = os.environ.get('unrar', 'unrar') + +root_dir = dirname(__file__) +test_data_dir = root_dir + '/test_data/' +tmp_dir = root_dir + '/tmp/' +with_password_rar = 'with_password_123.rar' +without_password_rar = 'without_password.rar' + +host = '127.0.0.1' +username = 'TestUser' +password = 'TestPassword' +port = '6789' + +def get_python(): + if os.name == 'nt': + return 'python' + return 'python3' + +def run_script(): + sys.stdout.flush() + proc = subprocess.Popen([get_python(), root_dir + '/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['NZBNA_EVENT'] = 'FILE_DOWNLOADED' + os.environ['NZBPP_DIRECTORY'] = tmp_dir + os.environ['NZBOP_ARTICLECACHE'] = '8' + os.environ['NZBPO_PASSACTION'] = 'PASSACTION' + os.environ['NZBOP_CONTROLPORT'] = port + os.environ['NZBOP_CONTROLIP'] = host + os.environ['NZBOP_CONTROLUSERNAME'] = username + os.environ['NZBOP_CONTROLPASSWORD'] = password + os.environ['NZBPR_PASSWORDDETECTOR_HASPASSWORD'] = 'no' + + # script options + os.environ['NZBNA_CATEGORY'] = 'Movies' + os.environ['NZBNA_DIRECTORY'] = tmp_dir + os.environ['NZBNA_NZBNAME'] = 'TestNZB' + os.environ['NZBPR_FAKEDETECTOR_SORTED'] = 'yes' + os.environ['NZBOP_TEMPDIR'] = tmp_dir + os.environ['NZBOP_UNRARCMD'] = unrar + +class Tests(unittest.TestCase): + + def test_without_password(self): + os.mkdir(tmp_dir) + set_defaults_env() + shutil.copyfile(test_data_dir + without_password_rar, tmp_dir + without_password_rar) + os.environ['NZBNA_NZBID'] = without_password_rar + [out, _, _] = run_script() + self.assertTrue('Password found in' not in out) + + def test_with_password(self): + os.mkdir(tmp_dir) + set_defaults_env() + shutil.copyfile(test_data_dir + with_password_rar, tmp_dir + with_password_rar) + os.environ['NZBNA_NZBID'] = with_password_rar + [out, _, _] = run_script() + self.assertTrue('Password found in' in out) + + def __del__(self): + shutil.rmtree(tmp_dir) + +if __name__ == '__main__': + unittest.main()