Unit testing captcha in yii - unit-testing

How to unit test a form with captcha?
I get this error:
Fatal error: Call to a member function createAction() on a non-object in framework/validators/CCaptchaValidator.php on line 65
How can I fix this error?
I have tried the code below:
$c = Yii::app()->createController('module/action');
Yii::app()->controller = $c[0];
$captcha = new CCaptchaAction(Yii::app()->controller, 'captcha');
$model->captcha = $captcha->getVerifyCode();
but I get the error below:
Fatal error: Call to a member function getUniqueId() on a non-object in C:\xampp\htdocs\yii\framework\web\widgets\captcha\CCaptchaAction.php on line 221
any ideas?

i fix this error by ignoring captcha rule and give it a custom value and it works!
i remove this part of rules in my form:
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements())

Related

Python, Passing --junitxml to pytest.main()

I am calling my tests as pytest.main(["--junitxml" , args.resultXml, "--someargument", path]).
This is my conftest.py:
parser.addoption(
'--someargument',
dest="someArgument",
help="Path to someargument")
parser.addoption(
'--junitxml',
action="store",
dest="resultXml",
default = "resultXML.xml",
help="Path to XML file")
However I am getting this error:
argparse.ArgumentError: argument --junitxml/--junit-xml: conflicting option string(s): --junitxml
Is there something that I am doing wrong?

I m using python 2.7. Facing issue with 'END' keyword

The following print statement was executed-
print ('Hello',end='\t')
but found following error:
Output:
File "Control_Statements.py", line 124
print ('Hello',end='\t')
^
SyntaxError:
invalid syntax
-

2 arguments missing but method works

Could you help me understand what is going on here. The question is about the error in the traceback. The failure is just as the illustration. And what I would like to illustrate that the function works.
Well, I was told that 2 positional arguments: 'view_instance' and 'address' are missing.
But the method really has taken those 2 positional arguments and worked happily till its logical end. In the interactive playing I show that I can catch the arguments transmitted.
Why does error appear? Thank you in advance for your help.
ADDED LATER:
Well, this seems to be because of the 'test_' beginning of the function.
Without "test" it works (def anonymous_user_redirected_to_login_page(self, view_instance, address):).
/photoarchive/general/tests.py
class GeneralTest(TestCase):
def test_anonymous_user_redirected_to_login_page(self, view_instance, address):
pdb.set_trace()
request = RequestFactory().get(address)
request.user = AnonymousUser()
response = view_instance(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/accounts/login/')
def test_anonymous_user_from_home_page_redirected_to_login_page(self):
view_instance = HomePageView.as_view()
address = '/'
self.test_anonymous_user_redirected_to_login_page(view_instance, address)
Traceback
(photoarchive) michael#michael:~/workspace/photoarchive/photoarchive$ python manage.py test general
Creating test database for alias 'default'...
FE
======================================================================
ERROR: test_anonymous_user_redirected_to_login_page (general.tests.GeneralTest)
----------------------------------------------------------------------
TypeError: test_anonymous_user_redirected_to_login_page() missing 2 required positional arguments: 'view_instance' and 'address'
======================================================================
FAIL: test_anonymous_user_from_home_page_redirected_to_login_page (general.tests.GeneralTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 29, in test_anonymous_user_from_home_page_redirected_to_login_page
self.test_anonymous_user_redirected_to_login_page(view_instance, address)
File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 23, in test_anonymous_user_redirected_to_login_page
self.assertEqual(response.status_code, 302)
AssertionError: 200 != 302
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1, errors=1)
Destroying test database for alias 'default'...
Interactive playing:
(photoarchive) michael#michael:~/workspace/photoarchive/photoarchive$ python manage.py test general
Creating test database for alias 'default'...
> /home/michael/workspace/photoarchive/photoarchive/general/tests.py(20)test_anonymous_user_redirected_to_login_page()
-> request = RequestFactory().get(address)
(Pdb) view_instance
<function HomePageView at 0x7faa0f76fea0>
(Pdb) address
'/'
(Pdb)
test_anonymous_user_redirected_to_login_page() method is treated by unittest framework as a test method, because its name starts with test. The framework tries to execute it, but is not passing any arguments to it (test methods don't normally take any arguments). However, the method requires them, hence the error.
If this method is only a helper method to be called from the other method, name it so that it doesn't start with test, e.g. _test_anonymous_user_redirected_to_login_page().
Note that the traceback is not related to this problem. The traceback simply shows where the other test method failed at an assertion. That is, the other test method runs correctly (both in unittest run and in your interactive session).

Exception when word is missing in the dictionary

I tried the demo app, it works fine but when I tried to integrate my own .jsgf grammar file it is giving error when I tried same thing using .lm file for the same dictionary it is working fine .
I referred to PocketSphinx android demo runtime exception link but it didn't help me out
Following is the code snippet:
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
File modelsDir = new File(assetsDir, "models");
mRecognizer = defaultSetup()
.setAcousticModel(new File(assetsDir, "en-us-ptm"))
.setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
.setRawLogDir(assetsDir)
.setKeywordThreshold(1e-10f)
.setFloat("-beam", 1e-30f)
.setBoolean("-allphone_ci", true)
.getRecognizer();
mRecognizer.addListener(this);
File languageModel = new File(assetsDir, "commands_gram.gram");
mRecognizer.addGrammarSearch(COMMANDS, languageModel);
}
Following is the error
E/cmusphinx: ERROR: "fsg_search.c", line 141: The word 'next' is missing in the dictionary
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Decoder_setJsgfFile returned -1
at edu.cmu.pocketsphinx.PocketSphinxJNI.Decoder_setJsgfFile(Native Method)
at edu.cmu.pocketsphinx.Decoder.setJsgfFile(Decoder.java:139)
at edu.cmu.pocketsphinx.SpeechRecognizer.addGrammarSearch(SpeechRecognizer.java:234)
at com.package.ui.SphinxSpeechRecognizerActivity.setupRecognizer(SphinxSpeechRecognizerActivity.java:284)
at com.package.ui.SphinxSpeechRecognizerActivity.access$400(SphinxSpeechRecognizerActivity.java:29)
at com.package.ui.SphinxSpeechRecognizerActivity$3.doInBackground(SphinxSpeechRecognizerActivity.java:239)
at com.package.ui.SphinxSpeechRecognizerActivity$3.doInBackground(SphinxSpeechRecognizerActivity.java:233)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818) 
Also I realized that any .jsgf grammar file I am using it is not detecting the last word from the dictionary. before it was working fine after I changed to my own .jsgf grammar file it is giving exception
This line:
E/cmusphinx: ERROR: "fsg_search.c", line 141: The word 'next' is missing in the dictionary
tells you need to add the word 'next' to the dictionary. You used this word in your JSGF grammar, but it was missing in the dictionary. Words are case-sensitive, so you need to make sure you use proper case.
I maybe know your problem.
Your have to make the grammar in correct form,like your space and tab.
I suggest you checking your grammar for block or tab.
Sorry my poor English.

pxssh.pxssh() is producing error in python

I got the following errors when I run pxssh.pxssh() in python. Please let me know what I am missing here.
Exception AttributeError: "'pxssh' object has no attribute 'closed'" in <bound method pxssh.__del__ of <pexpect.pxssh.pxssh object at 0x10d98e910>> ignored
.........
File "/Users/any_user/system/somelibrary_lib.py", line 377, in login
ssh = pxssh.pxssh(maxread=read_buffer, ignore_sighup=False)
TypeError: __init__() got an unexpected keyword argument 'ignore_sighup'
.........
I faced the same problem. the solution was like this :
s = pxssh.pxssh(timeout=time_out, maxread=2000000)
s.SSH_OPTS += " -o StrictHostKeyChecking=no"
s.SSH_OPTS += " -o UserKnownHostsFile=/dev/null"
You should add this two options
I faced the same problem. Solution I found was to use SSH_OPTS property of pxssh object instead of options ctor (__init__) argument.
So my code looks like this:
s = pxssh.pxssh()
s.SSH_OPTS += " -o StrictHostKeyChecking=no"
s.force_password = True
s.login(ip, user, passwd)
It doesn't throw AttributeError exception on module initialization and TypeError too.
But it still doesn't work for me. If the remote server has MOTD or some shell initialization on startup, it would break the logic of pxssh. I made the following trick:
s.login(ip, user, passwd, original_prompt = "Last login:")
Shell writes on each successful login line like Last login: %date% from %ip%. I used it to verify successful logon. Now, using s you can execute remote commands.
Original (default) value for original_prompt is r"[#$]". May be you need somehow to combine them for correct pxssh work. May be you need to add > to original_prompt if you are connecting to SQL shell or Cisco devices.