From c7ad1cc5b4a5d190a060e3ddd9274c3065da6708 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Fri, 13 Jan 2012 23:24:21 +0100 Subject: plugin unit test, closed #499, #500 --- tests/helper/PluginTester.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/helper/PluginTester.py (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py new file mode 100644 index 000000000..997a0923f --- /dev/null +++ b/tests/helper/PluginTester.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- + +from unittest import TestCase +from os.path import abspath +from sys import exc_clear, exc_info +from logging import log, DEBUG +from time import sleep, time + +from Stubs import Thread, Core, noop + +from module.plugins.Hoster import Hoster, Abort, Fail + +def _wait(self): + """ waits the time previously set """ + self.waiting = True + + waittime = self.pyfile.waitUntil - time() + log(DEBUG, "waiting %ss" % waittime) + + if self.wantReconnect: + raise Fail("Would wait for reconnect %ss" % waittime ) + if self.wantReconnect or waittime > 300: + raise Fail("Would wait %ss" % waittime ) + + while self.pyfile.waitUntil > time(): + sleep(1) + if self.pyfile.abort: raise Abort + + self.waiting = False + self.pyfile.setStatus("starting") + +Hoster.wait = _wait + +Hoster.checkForSameFiles = noop + +class PluginTester(TestCase): + + @classmethod + def setUpClass(cls): + cls.core = Core() + + def setUp(self): + self.thread = Thread(self.core) + exc_clear() + + def tearDown(self): + exc = exc_info() + if exc != (None, None, None): + debug = self.thread.writeDebugReport() + log(DEBUG, debug) + # generate attachment + print "\n[[ATTACHMENT|%s]]\n" % abspath(debug) \ No newline at end of file -- cgit v1.2.3 From 069503da5106fd2fcf7fa2d3a8462ab109b44adb Mon Sep 17 00:00:00 2001 From: RaNaN Date: Fri, 13 Jan 2012 23:42:10 +0100 Subject: cosmetic fixes --- tests/helper/PluginTester.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py index 997a0923f..d0c1cdd3c 100644 --- a/tests/helper/PluginTester.py +++ b/tests/helper/PluginTester.py @@ -7,6 +7,7 @@ from logging import log, DEBUG from time import sleep, time from Stubs import Thread, Core, noop +from sys import stderr from module.plugins.Hoster import Hoster, Abort, Fail @@ -49,4 +50,4 @@ class PluginTester(TestCase): debug = self.thread.writeDebugReport() log(DEBUG, debug) # generate attachment - print "\n[[ATTACHMENT|%s]]\n" % abspath(debug) \ No newline at end of file + stderr.write("\n[[ATTACHMENT|%s]]\n" % abspath(debug)) \ No newline at end of file -- cgit v1.2.3 From 828cc89cc9b7a2ecacf98fc73928d988e15f0b98 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 14 Jan 2012 15:49:08 +0100 Subject: captcha and attachments for plugin tester --- tests/helper/PluginTester.py | 118 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 10 deletions(-) (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py index d0c1cdd3c..e0ce8f354 100644 --- a/tests/helper/PluginTester.py +++ b/tests/helper/PluginTester.py @@ -1,14 +1,21 @@ # -*- coding: utf-8 -*- from unittest import TestCase -from os.path import abspath +from os import makedirs, remove +from os.path import exists, join, expanduser +from shutil import move from sys import exc_clear, exc_info from logging import log, DEBUG from time import sleep, time +from random import randint +from glob import glob + +from pycurl import LOW_SPEED_TIME, FORM_FILE +from json import loads from Stubs import Thread, Core, noop -from sys import stderr +from module.network.RequestFactory import getRequest, getURL from module.plugins.Hoster import Hoster, Abort, Fail def _wait(self): @@ -18,10 +25,10 @@ def _wait(self): waittime = self.pyfile.waitUntil - time() log(DEBUG, "waiting %ss" % waittime) - if self.wantReconnect: - raise Fail("Would wait for reconnect %ss" % waittime ) - if self.wantReconnect or waittime > 300: - raise Fail("Would wait %ss" % waittime ) + if self.wantReconnect and waittime > 300: + raise Fail("Would wait for reconnect %ss" % waittime) + elif waittime > 300: + raise Fail("Would wait %ss" % waittime) while self.pyfile.waitUntil > time(): sleep(1) @@ -32,13 +39,106 @@ def _wait(self): Hoster.wait = _wait + +def decryptCaptcha(self, url, get={}, post={}, cookies=False, forceUser=False, imgtype='jpg', + result_type='textual'): + img = self.load(url, get=get, post=post, cookies=cookies) + + id = ("%.2f" % time())[-6:].replace(".", "") + temp_file = open(join("tmp", "tmpCaptcha_%s_%s.%s" % (self.__name__, id, imgtype)), "wb") + temp_file.write(img) + temp_file.close() + + Ocr = self.core.pluginManager.loadClass("captcha", self.__name__) + + if Ocr: + log(DEBUG, "Using tesseract for captcha") + sleep(randint(3000, 5000) / 1000.0) + if self.pyfile.abort: raise Abort + + ocr = Ocr() + result = ocr.get_captcha(temp_file.name) + else: + log(DEBUG, "Using ct for captcha") + # put username and passkey into two lines in ct.conf + conf = join(expanduser("~"), "ct.conf") + if not exists(conf): raise Exception("CaptchaTrader config %s not found." % conf) + f = open(conf, "rb") + req = getRequest() + + #raise timeout threshold + req.c.setopt(LOW_SPEED_TIME, 80) + + try: + json = req.load("http://captchatrader.com/api/submit", + post={"api_key": "9f65e7f381c3af2b076ea680ae96b0b7", + "username": f.readline().strip(), + "password": f.readline().strip(), + "value": (FORM_FILE, temp_file.name), + "type": "file"}, multipart=True) + finally: + f.close() + req.close() + + response = loads(json) + log(DEBUG, str(response)) + result = response[1] + + self.cTask = response[0] + + return result + +Hoster.decryptCaptcha = decryptCaptcha + + +def respond(ticket, value): + conf = join(expanduser("~"), "ct.conf") + f = open(conf, "rb") + try: + getURL("http://captchatrader.com/api/respond", + post={"is_correct": value, + "username": f.readline().strip(), + "password": f.readline().strip(), + "ticket": ticket}) + except Exception, e : + print "CT Exception:", e + log(DEBUG, str(e)) + finally: + f.close() + + + +def invalidCaptcha(self): + log(DEBUG, "Captcha invalid") + if self.cTask: + respond(self.ticket, 0) + +Hoster.invalidCaptcha = invalidCaptcha + +def correctCaptcha(self): + log(DEBUG, "Captcha correct") + if self.cTask: + respond(self.ticket, 1) + +Hoster.correctCaptcha = correctCaptcha + Hoster.checkForSameFiles = noop class PluginTester(TestCase): - @classmethod def setUpClass(cls): cls.core = Core() + name = "tests.%s.%s" % (cls.__name__, cls.__name__) + for f in glob(join(name, "debug_*")): + remove(f) + + # Copy debug report to attachment dir for jenkins + @classmethod + def tearDownClass(cls): + name = "tests.%s.%s" % (cls.__name__, cls.__name__) + if not exists(name): makedirs(name) + for f in glob("debug_*"): + move(f, join(name, f)) def setUp(self): self.thread = Thread(self.core) @@ -48,6 +148,4 @@ class PluginTester(TestCase): exc = exc_info() if exc != (None, None, None): debug = self.thread.writeDebugReport() - log(DEBUG, debug) - # generate attachment - stderr.write("\n[[ATTACHMENT|%s]]\n" % abspath(debug)) \ No newline at end of file + log(DEBUG, debug) \ No newline at end of file -- cgit v1.2.3 From a0615c500c8bce03aaf89156a5f5e5e06873587c Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 14 Jan 2012 16:07:47 +0100 Subject: fixed attachment file path --- tests/helper/PluginTester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py index e0ce8f354..db01a2d06 100644 --- a/tests/helper/PluginTester.py +++ b/tests/helper/PluginTester.py @@ -128,14 +128,14 @@ class PluginTester(TestCase): @classmethod def setUpClass(cls): cls.core = Core() - name = "tests.%s.%s" % (cls.__name__, cls.__name__) + name = "%s.%s" % (cls.__name__, cls.__name__) for f in glob(join(name, "debug_*")): remove(f) # Copy debug report to attachment dir for jenkins @classmethod def tearDownClass(cls): - name = "tests.%s.%s" % (cls.__name__, cls.__name__) + name = "%s.%s" % (cls.__name__, cls.__name__) if not exists(name): makedirs(name) for f in glob("debug_*"): move(f, join(name, f)) -- cgit v1.2.3 From 26227cfe53f8fd4bc1631d8e1b35031f589682dc Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sun, 15 Jan 2012 15:55:19 +0100 Subject: backend + api test case, nicer format for plugin tester --- tests/helper/PluginTester.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py index db01a2d06..b70c0d061 100644 --- a/tests/helper/PluginTester.py +++ b/tests/helper/PluginTester.py @@ -128,14 +128,14 @@ class PluginTester(TestCase): @classmethod def setUpClass(cls): cls.core = Core() - name = "%s.%s" % (cls.__name__, cls.__name__) + name = "%s.%s" % (cls.__module__, cls.__name__) for f in glob(join(name, "debug_*")): remove(f) # Copy debug report to attachment dir for jenkins @classmethod def tearDownClass(cls): - name = "%s.%s" % (cls.__name__, cls.__name__) + name = "%s.%s" % (cls.__module__, cls.__name__) if not exists(name): makedirs(name) for f in glob("debug_*"): move(f, join(name, f)) -- cgit v1.2.3 From aabf168c0ccee7d6d7eacfbadecdca3cfc3956d3 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Mon, 16 Jan 2012 18:02:26 +0100 Subject: little fixes --- tests/helper/PluginTester.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/helper/PluginTester.py') diff --git a/tests/helper/PluginTester.py b/tests/helper/PluginTester.py index b70c0d061..ef61385be 100644 --- a/tests/helper/PluginTester.py +++ b/tests/helper/PluginTester.py @@ -111,14 +111,14 @@ def respond(ticket, value): def invalidCaptcha(self): log(DEBUG, "Captcha invalid") if self.cTask: - respond(self.ticket, 0) + respond(self.cTask, 0) Hoster.invalidCaptcha = invalidCaptcha def correctCaptcha(self): log(DEBUG, "Captcha correct") if self.cTask: - respond(self.ticket, 1) + respond(self.cTask, 1) Hoster.correctCaptcha = correctCaptcha @@ -148,4 +148,4 @@ class PluginTester(TestCase): exc = exc_info() if exc != (None, None, None): debug = self.thread.writeDebugReport() - log(DEBUG, debug) \ No newline at end of file + log(DEBUG, debug) -- cgit v1.2.3