diff --git a/plugins/serpro_captcha/Gemfile b/plugins/serpro_captcha/Gemfile new file mode 100644 index 0000000..6b9ce46 --- /dev/null +++ b/plugins/serpro_captcha/Gemfile @@ -0,0 +1 @@ +gem 'webmock' diff --git a/plugins/serpro_captcha/README.md b/plugins/serpro_captcha/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/plugins/serpro_captcha/README.md diff --git a/plugins/serpro_captcha/controllers/serpro_captcha_plugin_admin_controller.rb b/plugins/serpro_captcha/controllers/serpro_captcha_plugin_admin_controller.rb new file mode 100644 index 0000000..a8dddab --- /dev/null +++ b/plugins/serpro_captcha/controllers/serpro_captcha_plugin_admin_controller.rb @@ -0,0 +1,17 @@ +class SerproCaptchaPluginAdminController < PluginAdminController + + append_view_path File.join(File.dirname(__FILE__) + '/../views') + + def index + end + + def update + if @environment.update_attributes(params[:environment]) + session[:notice] = _('Captcha configuration updated successfully.') + else + session[:notice] = _('Captcha configuration could not be saved.') + end + render :action => 'index' + end + +end diff --git a/plugins/serpro_captcha/fixtures/ldap.yml.dist b/plugins/serpro_captcha/fixtures/ldap.yml.dist new file mode 100644 index 0000000..720e80c --- /dev/null +++ b/plugins/serpro_captcha/fixtures/ldap.yml.dist @@ -0,0 +1,15 @@ +server: + host: "127.0.0.1" + port: 389 + account: "uid=ldap_user,,ou=person,dc=noosfero,dc=org" + account_password: "ldap_pass" + base_dn: "dc=noosfero,dc=org" + attr_login: "uid" + attr_fullname: "cn" + attr_mail: "mail" + onthefly_register: true + filter: "" + tls: false +user: + login: 'valid_ldap_login' + password: 'valid_ldap_password' diff --git a/plugins/serpro_captcha/lib/ext/environment.rb b/plugins/serpro_captcha/lib/ext/environment.rb new file mode 100644 index 0000000..6b1f346 --- /dev/null +++ b/plugins/serpro_captcha/lib/ext/environment.rb @@ -0,0 +1,35 @@ +require_dependency 'environment' + +class Environment + + #Captcha settings + settings_items :serpro_captcha_plugin, :type => ActiveSupport::HashWithIndifferentAccess, :default => {} + +# settings_items :verify_uri, :type => :string, :default => 'http://captcha.servicoscorporativos.serpro.gov.br/captchavalidar/1.0.0/validar' +# settings_items :serpro_client_id, :type => :string, :default => 'fdbcdc7a0b754ee7ae9d865fda740f17' + + attr_accessible :serpro_captcha_plugin_attributes, :serpro_captcha_verify_uri, :serpro_captcha_client_id + + def serpro_captcha_plugin_attributes + self.serpro_captcha_plugin || {} + end + + def serpro_captcha_verify_uri= verify_uri + self.serpro_captcha_plugin = {} if self.serpro_captcha_plugin.blank? + self.serpro_captcha_plugin['serpro_captcha_verify_uri'] = verify_uri + end + + def serpro_captcha_verify_uri + self.serpro_captcha_plugin['serpro_captcha_verify_uri'] + end + + def serpro_captcha_client_id= client_id + self.serpro_captcha_plugin = {} if self.serpro_captcha_plugin.blank? + self.serpro_captcha_plugin['serpro_captcha_client_id'] = client_id + end + + def serpro_captcha_client_id + self.serpro_captcha_plugin['serpro_captcha_client_id'] + end + +end diff --git a/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb b/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb new file mode 100644 index 0000000..5212779 --- /dev/null +++ b/plugins/serpro_captcha/lib/serpro_captcha_plugin.rb @@ -0,0 +1,20 @@ +class SerproCaptchaPlugin < Noosfero::Plugin + + def self.plugin_name + _('Serpro\'s captcha plugin') + end + + def self.plugin_description + _("Provides a plugin to Serpro's captcha infrastructure.") + end + + def self.api_mount_points + [SerproCaptchaPlugin::API ] + end + + def test_captcha(remote_ip, params, environment) + spv = SerproCaptchaVerification.new + return spv.verify_serpro_captcha(environment.serpro_captcha_client_id, params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], environment.serpro_captcha_verify_uri) + end + +end diff --git a/plugins/serpro_captcha/lib/serpro_captcha_verification.rb b/plugins/serpro_captcha/lib/serpro_captcha_verification.rb new file mode 100644 index 0000000..6a7d1a5 --- /dev/null +++ b/plugins/serpro_captcha/lib/serpro_captcha_verification.rb @@ -0,0 +1,35 @@ +class SerproCaptchaVerification + + # return true or a hash with the error + # :user_message, :status, :log_message, :javascript_console_message + def verify_serpro_captcha(client_id, token, captcha_text, verify_uri) + msg_icve = _('Internal captcha validation error') + msg_esca = 'Environment serpro_captcha_plugin_attributes' + return hash_error(msg_icve, 500, nil, "#{msg_esca} verify_uri not defined") if verify_uri.nil? + return hash_error(msg_icve, 500, nil, "#{msg_esca} client_id not defined") if client_id.nil? + return hash_error(_("Error processing token validation"), 500, nil, _("Missing Serpro's Captcha token")) unless token + return hash_error(_('Captcha text has not been filled'), 403) unless captcha_text + uri = URI(verify_uri) + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Post.new(uri.path) + verify_string = "#{client_id}&#{token}&#{captcha_text}" + request.body = verify_string + begin + body = http.request(request).body + rescue Exception => e + return hash_error(_('Internal captcha validation error'), 500, nil, "Serpro captcha error: #{e.message}") + end + return true if body == '1' + return hash_error(_("Internal captcha validation error"), 500, body, "Unable to reach Serpro's Captcha validation service") if body == "Activity timed out" + return hash_error(_("Wrong captcha text, please try again"), 403) if body == 0 + return hash_error(_("Serpro's captcha token not found"), 500) if body == 2 + return hash_error(_("No data sent to validation server or other serious problem"), 500) if body == -1 + #Catches all errors at the end + return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha #{body}") + end + + def hash_error(user_message, status, log_message=nil, javascript_console_message=nil) + {user_message: user_message, status: status, log_message: log_message, javascript_console_message: javascript_console_message} + end + +end diff --git a/plugins/serpro_captcha/po/de/ldap.po b/plugins/serpro_captcha/po/de/ldap.po new file mode 100644 index 0000000..cd92fcc --- /dev/null +++ b/plugins/serpro_captcha/po/de/ldap.po @@ -0,0 +1,99 @@ +# German translation of noosfero. +# Copyright (C) 2009-2013 Josef Spillner +# Copyright (C) 2009, 2011 Ronny Kursawe +# This file is distributed under the same license as the noosfero package. +# Josef Spillner , 2009. +# +msgid "" +msgstr "" +"Project-Id-Version: 1.2~rc2-23-g29aba34\n" +"POT-Creation-Date: 2015-08-06 18:47-0300\n" +"PO-Revision-Date: 2014-12-12 14:23+0200\n" +"Last-Translator: Michal Čihař \n" +"Language-Team: German \n" +"Language: de\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 2.2-dev\n" + +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11 +#, fuzzy +msgid "A plugin that add ldap support." +msgstr "Ein Plugin, welches dies und jenes tut." + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10 +#, fuzzy +msgid "Ldap configuration updated successfully." +msgstr "Optionen erfolgreich aktualisiert." + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12 +#, fuzzy +msgid "Ldap configuration could not be saved." +msgstr "Die Konfiguration konnte nicht gespeichert werden" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1 +#, fuzzy +msgid "Ldap Management" +msgstr "Inhalt verwalten" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8 +msgid "Value" +msgstr "Wert" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11 +msgid "Host" +msgstr "Rechner" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15 +msgid "Port" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19 +#, fuzzy +msgid "Account" +msgstr "Preisermäßigung" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23 +#, fuzzy +msgid "Account Password" +msgstr "Derzeitiges Passwort" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27 +msgid "Base DN" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31 +#, fuzzy +msgid "LDAP Filter" +msgstr "Filter" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35 +#, fuzzy +msgid "On the fly creation" +msgstr "Im letzten Monat" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39 +msgid "LDAPS" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46 +msgid "Attributes" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53 +#, fuzzy +msgid "Fullname" +msgstr "Vollständiger Name" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57 +#, fuzzy +msgid "Mail" +msgstr "E-Mail" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65 +#, fuzzy +msgid "Back to plugins administration panel" +msgstr "Zurück zum Adminfeld" diff --git a/plugins/serpro_captcha/po/ldap.pot b/plugins/serpro_captcha/po/ldap.pot new file mode 100644 index 0000000..812412e --- /dev/null +++ b/plugins/serpro_captcha/po/ldap.pot @@ -0,0 +1,86 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 1.2~rc2-23-g29aba34\n" +"POT-Creation-Date: 2015-08-06 18:47-0300\n" +"PO-Revision-Date: 2015-08-06 17:21-0300\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11 +msgid "A plugin that add ldap support." +msgstr "" + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10 +msgid "Ldap configuration updated successfully." +msgstr "" + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12 +msgid "Ldap configuration could not be saved." +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1 +msgid "Ldap Management" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8 +msgid "Value" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11 +msgid "Host" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15 +msgid "Port" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19 +msgid "Account" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23 +msgid "Account Password" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27 +msgid "Base DN" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31 +msgid "LDAP Filter" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35 +msgid "On the fly creation" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39 +msgid "LDAPS" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46 +msgid "Attributes" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53 +msgid "Fullname" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57 +msgid "Mail" +msgstr "" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65 +msgid "Back to plugins administration panel" +msgstr "" diff --git a/plugins/serpro_captcha/po/pt/ldap.po b/plugins/serpro_captcha/po/pt/ldap.po new file mode 100644 index 0000000..8561a70 --- /dev/null +++ b/plugins/serpro_captcha/po/pt/ldap.po @@ -0,0 +1,93 @@ +# translation of noosfero.po to +# Krishnamurti Lelis Lima Vieira Nunes , 2007. +# noosfero - Brazilian Portuguese translation +# Copyright (C) 2007, +# Forum Brasileiro de Economia Solidaria +# Copyright (C) 2007, +# Ynternet.org Foundation +# This file is distributed under the same license as noosfero itself. +# Joenio Costa , 2008. +# +# +msgid "" +msgstr "" +"Project-Id-Version: 1.2~rc2-23-g29aba34\n" +"POT-Creation-Date: 2015-08-06 18:47-0300\n" +"PO-Revision-Date: 2014-12-18 18:40-0200\n" +"Last-Translator: Luciano Prestes Cavalcanti \n" +"Language-Team: Portuguese \n" +"Language: pt\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 2.0\n" + +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11 +msgid "A plugin that add ldap support." +msgstr "Um plugin que adiciona suporte a ldap." + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10 +msgid "Ldap configuration updated successfully." +msgstr "Configuração do Ldap atualizada com sucesso." + +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12 +msgid "Ldap configuration could not be saved." +msgstr "Configuração do Ldap não pode ser salva." + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1 +msgid "Ldap Management" +msgstr "Gerenciamento do Ldap" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8 +msgid "Value" +msgstr "Valor" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11 +msgid "Host" +msgstr "Host" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15 +msgid "Port" +msgstr "Porta" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19 +msgid "Account" +msgstr "Conta" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23 +msgid "Account Password" +msgstr "Senha da conta" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27 +msgid "Base DN" +msgstr "DN Base" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31 +msgid "LDAP Filter" +msgstr "Filtro LDAP" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35 +msgid "On the fly creation" +msgstr "Criação sob-demanda" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39 +msgid "LDAPS" +msgstr "LDAPS" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46 +msgid "Attributes" +msgstr "Atributos" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53 +msgid "Fullname" +msgstr "Nome completo" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57 +msgid "Mail" +msgstr "Mail" + +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65 +msgid "Back to plugins administration panel" +msgstr "Voltar ao painel de administração" diff --git a/plugins/serpro_captcha/test/functional/account_controller_plugin_test.rb b/plugins/serpro_captcha/test/functional/account_controller_plugin_test.rb new file mode 100644 index 0000000..1b02f44 --- /dev/null +++ b/plugins/serpro_captcha/test/functional/account_controller_plugin_test.rb @@ -0,0 +1,86 @@ +# require File.dirname(__FILE__) + '/../test_helper' +# +# # Re-raise errors caught by the controller. +# class AccountController; def rescue_action(e) raise e end; end +# +# class AccountControllerPluginTest < ActionController::TestCase +# +# def setup +# @controller = AccountController.new +# @request = ActionController::TestRequest.new +# @response = ActionController::TestResponse.new +# +# @environment = Environment.default +# @environment.enabled_plugins = ['SerproCaptchaPlugin'] +# @ldap_config = load_ldap_config +# @environment.serpro_captcha_plugin= @ldap_config['server'] unless @ldap_config.nil? +# @environment.save! +# end +# +# should 'not authenticate user if its not a local user or a ldap user' do +# post :login, :user => {:login => 'someuser', :password => 'somepass'} +# assert_nil session[:user] +# end +# +# should 'diplay not logged message if the user is not a local user or a ldap user' do +# post :login, :user => {:login => 'someuser', :password => 'somepass'} +# assert_equal 'Incorrect username or password', session[:notice] +# end +# +# should 'authenticate user if its a local user but is not a ldap user' do +# user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') +# user.activate +# post :login, :user => {:login => 'testuser', :password => 'test'} +# assert session[:user] +# end +# +# should 'display required fields on user login' do +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}} +# @environment.save +# get :login +# assert_tag(:input, :attributes => {:id => 'profile_data_contact_phone'}) +# end +# +# if ldap_configured? +# +# should 'authenticate an existing noosfero user with ldap and loggin' do +# user = create_user(@ldap_config['user']['login'], :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test') +# user.activate +# count = User.count +# post :login, :user => @ldap_config['user'] +# assert session[:user] +# assert_equal count, User.count +# end +# +# should 'login and create a new noosfero user if ldap authentication works properly' do +# count = User.count +# post :login, :user => @ldap_config['user'] +# assert session[:user] +# assert_equal count + 1, User.count +# end +# +# should 'login on ldap if required fields are defined' do +# count = User.count +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}} +# @environment.save +# post :login, :user => @ldap_config['user'], :profile_data => {:contact_phone => '11111111'} +# assert session[:user] +# end +# +# should 'not login on ldap if required fields are not defined' do +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}} +# @environment.save +# post :login, :user => @ldap_config['user'] +# assert_nil session[:user] +# end +# +# should 'authenticate user if its not a local user but is a ldap user' do +# post :login, :user => @ldap_config['user'] +# assert session[:user] +# end +# +# else +# puts LDAP_SERVER_ERROR_MESSAGE +# end +# +# end diff --git a/plugins/serpro_captcha/test/functional/serpro_captcha_test.rb b/plugins/serpro_captcha/test/functional/serpro_captcha_test.rb new file mode 100644 index 0000000..3860b36 --- /dev/null +++ b/plugins/serpro_captcha/test/functional/serpro_captcha_test.rb @@ -0,0 +1,29 @@ +# require File.dirname(__FILE__) + '/../../../../test/test_helper' +# require File.dirname(__FILE__) + '/../../controllers/serpro_captcha_plugin_admin_controller' +# +# # Re-raise errors caught by the controller. +# class SerproCaptchaPluginAdminController; def rescue_action(e) raise e end; end +# +# class SerproCaptchaPluginAdminControllerTest < ActionController::TestCase +# +# def setup +# @environment = Environment.default +# user_login = create_admin_user(@environment) +# login_as(user_login) +# @admin = User[user_login].person +# @environment.enabled_plugins = ['SerproCaptchaPlugin'] +# @environment.serpro_captcha_plugin_host="http://somehost" +# @environment.save! +# end +# +# should 'detected error, Name or service not known, for Serpro captcha communication' do +# environment = Environment.default +# environment.serpro_captcha_verify_uri = 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate' +# environment.serpro_captcha_client_id = '000000000000' +# environment.save! +# params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com", +# :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'} +# post "/api/v1/register?#{params.to_query}" +# message = JSON.parse(last_response.body)['javascript_console_message'] +# assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", message +# end diff --git a/plugins/serpro_captcha/test/test_helper.rb b/plugins/serpro_captcha/test/test_helper.rb new file mode 100644 index 0000000..481e398 --- /dev/null +++ b/plugins/serpro_captcha/test/test_helper.rb @@ -0,0 +1,86 @@ +require "#{Rails.root}/lib/noosfero/api/helpers" + +class ActiveSupport::TestCase + + include Rack::Test::Methods + + def app + Noosfero::API::API + end + + def pass_captcha + stub_request(:post, "http://www.somecompany.com:443/validate"). + with(:body => "323232&642646&44641441", + :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => "1", :headers => {'Content-Length' => 1}) + spv = SerproCaptchaVerification.new + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) + end + + def fail_captcha + stub_request(:post, "http://www.somecompany.com:443/validate"). + with(:body => "323232&642646&44641441", + :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => "2", :headers => {'Content-Length' => 1}) + spv = SerproCaptchaVerification.new + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) + end + + def login_with_captcha + json = do_login_captcha_from_api + @private_token = json["private_token"] + @params = { "private_token" => @private_token} + json + end + + ## Performs a login using the session.rb but mocking the + ## real HTTP request to validate the captcha. + def do_login_captcha_from_api + # Request mocking + #Net::HTTP::Post Mock + request = mock + #Net::HTTP Mock + http = mock + uri = URI(environment.api_captcha_settings[:verify_uri]) + Net::HTTP.expects(:new).with(uri.host, uri.port).returns(http) + Net::HTTP::Post.expects(:new).with(uri.path).returns(request) + + # Captcha required codes + request.stubs(:body=).with("0000000000000000&4324343&4030320") + http.stubs(:request).with(request).returns(http) + + # Captcha validation success !! + http.stubs(:body).returns("1") + + params = {:txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'} + post "#{@url}#{params.to_query}" + json = JSON.parse(last_response.body) + json + end + + def login_api + @environment = Environment.default + @user = User.create!(:login => 'testapi', :password => 'testapi', :password_confirmation => 'testapi', :email => 'test@test.org', :environment => @environment) + @user.activate + @person = @user.person + + post "/api/v1/login?login=testapi&password=testapi" + json = JSON.parse(last_response.body) + @private_token = json["private_token"] + unless @private_token + @user.generate_private_token! + @private_token = @user.private_token + end + + @params = {:private_token => @private_token} + end + attr_accessor :private_token, :user, :person, :params, :environment + + private + + def json_response_ids(kind) + json = JSON.parse(last_response.body) + json[kind.to_s].map {|c| c['id']} + end + +end diff --git a/plugins/serpro_captcha/test/unit/ext/environment_test.rb b/plugins/serpro_captcha/test/unit/ext/environment_test.rb new file mode 100644 index 0000000..3d65cc4 --- /dev/null +++ b/plugins/serpro_captcha/test/unit/ext/environment_test.rb @@ -0,0 +1,186 @@ +# require File.dirname(__FILE__) + '/../../../../../test/test_helper' +# +# class EnvironmentTest < ActiveSupport::TestCase +# +# def setup +# @enviroment = Environment.default +# end +# +# should 'have serpro_captcha_plugin variable defined' do +# assert_equal Hash, @enviroment.serpro_captcha_plugin.class +# end +# +# should 'return an empty hash by default on serpro_captcha_plugin_attributes method' do +# assert_equal Hash.new, @enviroment.serpro_captcha_plugin_attributes +# end +# +# should 'serpro_captcha_plugin_host= define the ldap host' do +# host = "http://something" +# @enviroment.serpro_captcha_plugin_host= host +# assert_equal host, @enviroment.serpro_captcha_plugin['host'] +# end +# +# should 'serpro_captcha_plugin_host return the defined ldap host' do +# host = "http://something" +# @enviroment.serpro_captcha_plugin_host= host +# assert_equal host, @enviroment.serpro_captcha_plugin_host +# end +# +# should 'serpro_captcha_plugin_port= define the ldap port' do +# value = 255 +# @enviroment.serpro_captcha_plugin_port= value +# assert_equal value, @enviroment.serpro_captcha_plugin['port'] +# end +# +# should 'serpro_captcha_plugin_port return the defined ldap port' do +# value = 255 +# @enviroment.serpro_captcha_plugin_port= value +# assert_equal value, @enviroment.serpro_captcha_plugin_port +# end +# +# should 'default serpro_captcha_plugin_port be 389' do +# assert_equal 389, @enviroment.serpro_captcha_plugin_port +# end +# +# should 'serpro_captcha_plugin_account= define the ldap acccount' do +# value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br' +# @enviroment.serpro_captcha_plugin_account= value +# assert_equal value, @enviroment.serpro_captcha_plugin['account'] +# end +# +# should 'serpro_captcha_plugin_account return the defined ldap account' do +# value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br' +# @enviroment.serpro_captcha_plugin_account= value +# assert_equal value, @enviroment.serpro_captcha_plugin_account +# end +# +# should 'serpro_captcha_plugin_account_password= define the ldap acccount_password' do +# value = 'password' +# @enviroment.serpro_captcha_plugin_account_password= value +# assert_equal value, @enviroment.serpro_captcha_plugin['account_password'] +# end +# +# should 'serpro_captcha_plugin_account_password return the defined ldap account password' do +# value = 'password' +# @enviroment.serpro_captcha_plugin_account_password= value +# assert_equal value, @enviroment.serpro_captcha_plugin_account_password +# end +# +# should 'serpro_captcha_plugin_base_dn= define the ldap base_dn' do +# value = 'dc=company,dc=com,dc=br' +# @enviroment.serpro_captcha_plugin_base_dn= value +# assert_equal value, @enviroment.serpro_captcha_plugin['base_dn'] +# end +# +# should 'serpro_captcha_plugin_base_dn return the defined ldap base_dn' do +# value = 'dc=company,dc=com,dc=br' +# @enviroment.serpro_captcha_plugin_base_dn= value +# assert_equal value, @enviroment.serpro_captcha_plugin_base_dn +# end +# +# should 'serpro_captcha_plugin_attr_login= define the ldap attr_login' do +# value = 'uid' +# @enviroment.serpro_captcha_plugin_attr_login= value +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_login'] +# end +# +# should 'serpro_captcha_plugin_attr_login return the defined ldap attr_login' do +# value = 'uid' +# @enviroment.serpro_captcha_plugin_attr_login= value +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_login +# end +# +# should 'serpro_captcha_plugin_attr_fullname= define the ldap attr_fullname' do +# value = 'Noosfero System' +# @enviroment.serpro_captcha_plugin_attr_fullname= value +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_fullname'] +# end +# +# should 'serpro_captcha_plugin_attr_fullname return the defined ldap attr_fullname' do +# value = 'uid' +# @enviroment.serpro_captcha_plugin_attr_fullname= value +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_fullname +# end +# +# +# should 'serpro_captcha_plugin_attr_mail= define the ldap attr_mail' do +# value = 'test@noosfero.com' +# @enviroment.serpro_captcha_plugin_attr_mail= value +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_mail'] +# end +# +# should 'serpro_captcha_plugin_attr_mail return the defined ldap attr_mail' do +# value = 'test@noosfero.com' +# @enviroment.serpro_captcha_plugin_attr_mail= value +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_mail +# end +# +# should 'serpro_captcha_plugin_onthefly_register= define the ldap onthefly_register' do +# value = '1' +# @enviroment.serpro_captcha_plugin_onthefly_register= value +# assert @enviroment.serpro_captcha_plugin['onthefly_register'] +# end +# +# should 'serpro_captcha_plugin_onthefly_register return true if ldap onthefly_register variable is defined as true' do +# value = '1' +# @enviroment.serpro_captcha_plugin_onthefly_register= value +# assert @enviroment.serpro_captcha_plugin_onthefly_register +# end +# +# should 'serpro_captcha_plugin_onthefly_register return false if ldap onthefly_register variable is defined as false' do +# value = '0' +# @enviroment.serpro_captcha_plugin_onthefly_register= value +# refute @enviroment.serpro_captcha_plugin_onthefly_register +# end +# +# should 'serpro_captcha_plugin_filter= define the ldap filter' do +# value = 'test' +# @enviroment.serpro_captcha_plugin_filter= value +# assert_equal value, @enviroment.serpro_captcha_plugin['filter'] +# end +# +# should 'serpro_captcha_plugin_filter return the defined ldap filter' do +# value = 'test' +# @enviroment.serpro_captcha_plugin_filter= value +# assert_equal value, @enviroment.serpro_captcha_plugin_filter +# end +# +# should 'serpro_captcha_plugin_tls= define the ldap tls' do +# value = '1' +# @enviroment.serpro_captcha_plugin_tls= value +# assert @enviroment.serpro_captcha_plugin['tls'] +# end +# +# should 'tls return true if ldap tls variable is defined as true' do +# value = '1' +# @enviroment.serpro_captcha_plugin_tls= value +# assert @enviroment.serpro_captcha_plugin_tls +# end +# +# should 'tls return false if ldap tls variable is defined as false' do +# value = '0' +# @enviroment.serpro_captcha_plugin_tls= value +# refute @enviroment.serpro_captcha_plugin_tls +# end +# +# should 'validates presence of host' do +# @enviroment.serpro_captcha_plugin= {:port => 3000} +# @enviroment.valid? +# +# assert @enviroment.errors.include?(:serpro_captcha_plugin_host) +# +# @enviroment.serpro_captcha_plugin_host= "http://somehost.com" +# @enviroment.valid? +# refute @enviroment.errors.include?(:serpro_captcha_plugin_host) +# end +# +# should 'validates presence of host only if some ldap configuration is defined' do +# @enviroment.valid? +# refute @enviroment.errors.include?(:serpro_captcha_plugin_host) +# +# @enviroment.serpro_captcha_plugin= {:port => 3000} +# @enviroment.valid? +# assert @enviroment.errors.include?(:serpro_captcha_plugin_host) +# end +# +# end diff --git a/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb b/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb new file mode 100644 index 0000000..5849f4e --- /dev/null +++ b/plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb @@ -0,0 +1,60 @@ +require 'webmock' +include WebMock::API +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require_relative '../test_helper' + +class SerproCaptchaVerificationTest < ActiveSupport::TestCase + + def setup + @environment = Environment.default + @environment.enabled_plugins = ['SerproCaptchaPlugin'] + @environment.serpro_captcha_verify_uri='https://www.somecompany.com/validate' + @environment.serpro_captcha_client_id='323232' + @environment.save! + end + + should 'register a user when there are no enabled captcha pluging' do + @environment.enabled_plugins = [] + Environment.default.enable('skip_new_user_email_confirmation') + params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } + post "/api/v1/register?#{params.to_query}" + assert_equal 201, last_response.status + json = JSON.parse(last_response.body) + assert User['newuserapi'].activated? + assert json['activated'] + assert json['private_token'].present? + end + + # should 'not register a user if captcha fails' do + # fail_captcha + # Environment.default.enable('skip_new_user_email_confirmation') + # params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" } + # post "/api/v1/register?#{params.to_query}" + # assert_equal 201, last_response.status + # json = JSON.parse(last_response.body) + # refute User['newuserapi'].activated? + # refute !json['activated'] + # refute !json['private_token'].present? + # end + + should 'verify_serpro_captcha' do + pass_captcha + spv = SerproCaptchaVerification.new + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri) + end + + should 'fail captcha if user has not filled Serpro\' captcha text' do + pass_captcha + spv = SerproCaptchaVerification.new + hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', nil, @environment.serpro_captcha_verify_uri) + assert hash[:user_message], _('Captcha text has not been filled') + end + + should 'fail captcha if Serpro\' captcha token has not been sent' do + pass_captcha + spv = SerproCaptchaVerification.new + hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, nil, '76876846', @environment.serpro_captcha_verify_uri) + assert hash[:javascript_console_message], _("Missing Serpro's Captcha token") + end + +end diff --git a/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb b/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb new file mode 100644 index 0000000..e5de360 --- /dev/null +++ b/plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb @@ -0,0 +1,27 @@ +

<%= _("Serpro's Captcha Management") %>

+ +<%= labelled_form_for(:environment, :url => {:action => 'update'}) do |f| %> + + + + + + + + + + + + + + +
<%= c_('Configuration') %><%= _('Value') %>
<%= _('Host') %><%= text_field :environment, :serpro_captcha_verify_uri %>
<%= _('Port') %><%= text_field :environment, :serpro_captcha_client_id %>
+ +
+ <% button_bar do %> + <%= submit_button('save', c_('Save changes')) %> + <%= button :back, _('Back to plugins administration panel'), :controller => 'plugins' %> + <% end %> +
+ +<% end %> diff --git a/plugins/serpro_captcha/views/serpro_captcha_plugin_admin/index.html.erb b/plugins/serpro_captcha/views/serpro_captcha_plugin_admin/index.html.erb new file mode 100644 index 0000000..07f01fd --- /dev/null +++ b/plugins/serpro_captcha/views/serpro_captcha_plugin_admin/index.html.erb @@ -0,0 +1,28 @@ +

<%= _("Serpro's Captcha Management") %>

+ +<%= labelled_form_for(:environment, :url => {:action => 'update'}) do |f| %> + + + + + + + + + + + + + + +
<%= c_('Configuration') %><%= _('Value') %>
<%= _('Verify URI') %><%= text_field :environment, :serpro_captcha_verify_uri %>
<%= _('Client Id') %><%= text_field :environment, :serpro_captcha_client_id %>
+ + +
+ <% button_bar do %> + <%= submit_button('save', c_('Save changes')) %> + <%= button :back, _('Back to plugins administration panel'), :controller => 'plugins' %> + <% end %> +
+ +<% end %> -- libgit2 0.21.2