Example #1
0
exports['toHTML() https with no error'] = function(test) {
    var recaptcha = new Recaptcha('PUBLIC', 'PRIVATE', true);
    var script_src = 'https://www.google.com/recaptcha/api/challenge?k=PUBLIC';
    var noscript_src = 'https://www.google.com/recaptcha/api/noscript?k=PUBLIC';

    var expected_html = '<script type="text/javascript" src="' + script_src + '">' +
                        '</script><noscript><iframe src="' + noscript_src + '" ' +
                        'height="300" width="500" frameborder="0"></iframe><br>' +
                        '<textarea name="recaptcha_challenge_field" rows="3" cols="40">' +
                        '</textarea><input type="hidden" name="recaptcha_response_field" ' +
                        'value="manual_challenge"></noscript>';

    test.strictEqual(recaptcha.toHTML(), expected_html, 'toHTML() https returns expected HTML');
    test.done();
};
Example #2
0
exports['verify() with no data'] = function(test) {
    var recaptcha = new Recaptcha('PUBLIC', 'PRIVATE');
    var create_client_called = false;

    // We shouldn't need to contact Recaptcha to know this is invalid.
    http.request = function(options, callback) {
        create_client_called = true;
    };

    recaptcha.verify(function(error_code) {
        test.strictEqual(error_code, 'verify-params-incorrect');
        test.strictEqual(recaptcha.error_code, 'verify-params-incorrect');

        // Ensure that http.request() was never called.
        test.strictEqual(create_client_called, false);

        test.done();
    });
};
Example #3
0
exports['verify() with blank response'] = function(test) {
    var data = {
        remoteip: '127.0.0.1',
        challenge: 'challenge',
        response: ''
    };
    var recaptcha = new Recaptcha('PUBLIC', 'PRIVATE', data);
    var create_client_called = false;

    // We shouldn't need to contact Recaptcha to know this is invalid.
    http.request = function(options, callback) {
        create_client_called = true;
    };

    recaptcha.verify(function(error_code) {
        test.strictEqual(error_code, 'incorrect-captcha-sol');
        test.strictEqual(recaptcha.error_code, 'incorrect-captcha-sol');

        // Ensure that http.request() was never called.
        test.strictEqual(create_client_called, false);

        test.done();
    });
};
Example #4
0
exports['verify() with good data'] = function(test) {
    var data = {
        remoteip:  '127.0.0.1',
        challenge: 'challenge',
        response:  'good_response'
    };
    var recaptcha = new Recaptcha('PUBLIC', 'PRIVATE', data);

    var data_with_pk = {
        remoteip:   data['remoteip'],
        challenge:  data['challenge'],
        response:   data['response'],
        privatekey: 'PRIVATE'
    };

    var response_data = 'true';
    var data_qs = querystring.stringify(data_with_pk);
    var end_called = false;
    var write_called = false;

    // Stub out communication with Recaptcha.
    var fake_client = {};
    var fake_request = new events.EventEmitter();
    var fake_response = new events.EventEmitter();

    http.request = function(options, callback) {
        test.deepEqual(options, {
            host: 'www.google.com',
            path: '/recaptcha/api/verify',
            port: 80,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': data_qs.length
            }
        }, 'options for request() call are correct');

        callback(fake_response);
        return fake_request;
    };
    fake_request.write = function(data, encoding) {
        test.strictEqual(data, data_qs, 'data correct in request.write() call');
        test.strictEqual(encoding, 'utf8', 'encoding correct in request.write() call');
        write_called = true;
    };
    fake_request.end = function() { end_called = true; };

    // Check callback values for verify.
    recaptcha.verify(function(error_code) {
        test.strictEqual(error_code, undefined, 'error_code is undefined');
        test.strictEqual(recaptcha.error_code, undefined, 'recaptcha.error_code is undefined');

        // Make sure that request.write() and request.end() were called.
        test.strictEqual(write_called, true, 'request.write() was called');
        test.strictEqual(end_called, true, 'request.end() was called');

        test.done();
    });

    // Emit the signals to mimic getting data from Recaptcha.
    fake_request.emit('response', fake_response);
    fake_response.emit('data', response_data);
    fake_response.emit('end');
};