Commit d7d8a871805cb2f3301d03a4e01f47fa04b1d341

Authored by Evandro Junior
1 parent 87617f3c

Added plugin directory

plugins/serpro_captcha/Gemfile 0 → 100644
... ... @@ -0,0 +1 @@
  1 +gem 'webmock'
... ...
plugins/serpro_captcha/README.md 0 → 100644
plugins/serpro_captcha/controllers/serpro_captcha_plugin_admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +class SerproCaptchaPluginAdminController < PluginAdminController
  2 +
  3 + append_view_path File.join(File.dirname(__FILE__) + '/../views')
  4 +
  5 + def index
  6 + end
  7 +
  8 + def update
  9 + if @environment.update_attributes(params[:environment])
  10 + session[:notice] = _('Captcha configuration updated successfully.')
  11 + else
  12 + session[:notice] = _('Captcha configuration could not be saved.')
  13 + end
  14 + render :action => 'index'
  15 + end
  16 +
  17 +end
... ...
plugins/serpro_captcha/fixtures/ldap.yml.dist 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +server:
  2 + host: "127.0.0.1"
  3 + port: 389
  4 + account: "uid=ldap_user,,ou=person,dc=noosfero,dc=org"
  5 + account_password: "ldap_pass"
  6 + base_dn: "dc=noosfero,dc=org"
  7 + attr_login: "uid"
  8 + attr_fullname: "cn"
  9 + attr_mail: "mail"
  10 + onthefly_register: true
  11 + filter: ""
  12 + tls: false
  13 +user:
  14 + login: 'valid_ldap_login'
  15 + password: 'valid_ldap_password'
... ...
plugins/serpro_captcha/lib/ext/environment.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +require_dependency 'environment'
  2 +
  3 +class Environment
  4 +
  5 + #Captcha settings
  6 + settings_items :serpro_captcha_plugin, :type => ActiveSupport::HashWithIndifferentAccess, :default => {}
  7 +
  8 +# settings_items :verify_uri, :type => :string, :default => 'http://captcha.servicoscorporativos.serpro.gov.br/captchavalidar/1.0.0/validar'
  9 +# settings_items :serpro_client_id, :type => :string, :default => 'fdbcdc7a0b754ee7ae9d865fda740f17'
  10 +
  11 + attr_accessible :serpro_captcha_plugin_attributes, :serpro_captcha_verify_uri, :serpro_captcha_client_id
  12 +
  13 + def serpro_captcha_plugin_attributes
  14 + self.serpro_captcha_plugin || {}
  15 + end
  16 +
  17 + def serpro_captcha_verify_uri= verify_uri
  18 + self.serpro_captcha_plugin = {} if self.serpro_captcha_plugin.blank?
  19 + self.serpro_captcha_plugin['serpro_captcha_verify_uri'] = verify_uri
  20 + end
  21 +
  22 + def serpro_captcha_verify_uri
  23 + self.serpro_captcha_plugin['serpro_captcha_verify_uri']
  24 + end
  25 +
  26 + def serpro_captcha_client_id= client_id
  27 + self.serpro_captcha_plugin = {} if self.serpro_captcha_plugin.blank?
  28 + self.serpro_captcha_plugin['serpro_captcha_client_id'] = client_id
  29 + end
  30 +
  31 + def serpro_captcha_client_id
  32 + self.serpro_captcha_plugin['serpro_captcha_client_id']
  33 + end
  34 +
  35 +end
... ...
plugins/serpro_captcha/lib/serpro_captcha_plugin.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +class SerproCaptchaPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + _('Serpro\'s captcha plugin')
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("Provides a plugin to Serpro's captcha infrastructure.")
  9 + end
  10 +
  11 + def self.api_mount_points
  12 + [SerproCaptchaPlugin::API ]
  13 + end
  14 +
  15 + def test_captcha(remote_ip, params, environment)
  16 + spv = SerproCaptchaVerification.new
  17 + return spv.verify_serpro_captcha(environment.serpro_captcha_client_id, params[:txtToken_captcha_serpro_gov_br], params[:captcha_text], environment.serpro_captcha_verify_uri)
  18 + end
  19 +
  20 +end
... ...
plugins/serpro_captcha/lib/serpro_captcha_verification.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +class SerproCaptchaVerification
  2 +
  3 + # return true or a hash with the error
  4 + # :user_message, :status, :log_message, :javascript_console_message
  5 + def verify_serpro_captcha(client_id, token, captcha_text, verify_uri)
  6 + msg_icve = _('Internal captcha validation error')
  7 + msg_esca = 'Environment serpro_captcha_plugin_attributes'
  8 + return hash_error(msg_icve, 500, nil, "#{msg_esca} verify_uri not defined") if verify_uri.nil?
  9 + return hash_error(msg_icve, 500, nil, "#{msg_esca} client_id not defined") if client_id.nil?
  10 + return hash_error(_("Error processing token validation"), 500, nil, _("Missing Serpro's Captcha token")) unless token
  11 + return hash_error(_('Captcha text has not been filled'), 403) unless captcha_text
  12 + uri = URI(verify_uri)
  13 + http = Net::HTTP.new(uri.host, uri.port)
  14 + request = Net::HTTP::Post.new(uri.path)
  15 + verify_string = "#{client_id}&#{token}&#{captcha_text}"
  16 + request.body = verify_string
  17 + begin
  18 + body = http.request(request).body
  19 + rescue Exception => e
  20 + return hash_error(_('Internal captcha validation error'), 500, nil, "Serpro captcha error: #{e.message}")
  21 + end
  22 + return true if body == '1'
  23 + return hash_error(_("Internal captcha validation error"), 500, body, "Unable to reach Serpro's Captcha validation service") if body == "Activity timed out"
  24 + return hash_error(_("Wrong captcha text, please try again"), 403) if body == 0
  25 + return hash_error(_("Serpro's captcha token not found"), 500) if body == 2
  26 + return hash_error(_("No data sent to validation server or other serious problem"), 500) if body == -1
  27 + #Catches all errors at the end
  28 + return hash_error(_("Internal captcha validation error"), 500, nil, "Error validating Serpro's captcha #{body}")
  29 + end
  30 +
  31 + def hash_error(user_message, status, log_message=nil, javascript_console_message=nil)
  32 + {user_message: user_message, status: status, log_message: log_message, javascript_console_message: javascript_console_message}
  33 + end
  34 +
  35 +end
... ...
plugins/serpro_captcha/po/de/ldap.po 0 → 100644
... ... @@ -0,0 +1,99 @@
  1 +# German translation of noosfero.
  2 +# Copyright (C) 2009-2013 Josef Spillner
  3 +# Copyright (C) 2009, 2011 Ronny Kursawe
  4 +# This file is distributed under the same license as the noosfero package.
  5 +# Josef Spillner <josef.spillner@tu-dresden.de>, 2009.
  6 +#
  7 +msgid ""
  8 +msgstr ""
  9 +"Project-Id-Version: 1.2~rc2-23-g29aba34\n"
  10 +"POT-Creation-Date: 2015-08-06 18:47-0300\n"
  11 +"PO-Revision-Date: 2014-12-12 14:23+0200\n"
  12 +"Last-Translator: Michal Čihař <michal@cihar.com>\n"
  13 +"Language-Team: German <https://hosted.weblate.org/projects/noosfero/noosfero/"
  14 +"de/>\n"
  15 +"Language: de\n"
  16 +"MIME-Version: 1.0\n"
  17 +"Content-Type: text/plain; charset=UTF-8\n"
  18 +"Content-Transfer-Encoding: 8bit\n"
  19 +"Plural-Forms: nplurals=2; plural=n != 1;\n"
  20 +"X-Generator: Weblate 2.2-dev\n"
  21 +
  22 +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11
  23 +#, fuzzy
  24 +msgid "A plugin that add ldap support."
  25 +msgstr "Ein Plugin, welches dies und jenes tut."
  26 +
  27 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10
  28 +#, fuzzy
  29 +msgid "Ldap configuration updated successfully."
  30 +msgstr "Optionen erfolgreich aktualisiert."
  31 +
  32 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12
  33 +#, fuzzy
  34 +msgid "Ldap configuration could not be saved."
  35 +msgstr "Die Konfiguration konnte nicht gespeichert werden"
  36 +
  37 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1
  38 +#, fuzzy
  39 +msgid "Ldap Management"
  40 +msgstr "Inhalt verwalten"
  41 +
  42 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8
  43 +msgid "Value"
  44 +msgstr "Wert"
  45 +
  46 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11
  47 +msgid "Host"
  48 +msgstr "Rechner"
  49 +
  50 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15
  51 +msgid "Port"
  52 +msgstr ""
  53 +
  54 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19
  55 +#, fuzzy
  56 +msgid "Account"
  57 +msgstr "Preisermäßigung"
  58 +
  59 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23
  60 +#, fuzzy
  61 +msgid "Account Password"
  62 +msgstr "Derzeitiges Passwort"
  63 +
  64 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27
  65 +msgid "Base DN"
  66 +msgstr ""
  67 +
  68 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31
  69 +#, fuzzy
  70 +msgid "LDAP Filter"
  71 +msgstr "Filter"
  72 +
  73 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35
  74 +#, fuzzy
  75 +msgid "On the fly creation"
  76 +msgstr "Im letzten Monat"
  77 +
  78 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39
  79 +msgid "LDAPS"
  80 +msgstr ""
  81 +
  82 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46
  83 +msgid "Attributes"
  84 +msgstr ""
  85 +
  86 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53
  87 +#, fuzzy
  88 +msgid "Fullname"
  89 +msgstr "Vollständiger Name"
  90 +
  91 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57
  92 +#, fuzzy
  93 +msgid "Mail"
  94 +msgstr "E-Mail"
  95 +
  96 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65
  97 +#, fuzzy
  98 +msgid "Back to plugins administration panel"
  99 +msgstr "Zurück zum Adminfeld"
... ...
plugins/serpro_captcha/po/ldap.pot 0 → 100644
... ... @@ -0,0 +1,86 @@
  1 +# SOME DESCRIPTIVE TITLE.
  2 +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
  3 +# This file is distributed under the same license as the PACKAGE package.
  4 +# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
  5 +#
  6 +#, fuzzy
  7 +msgid ""
  8 +msgstr ""
  9 +"Project-Id-Version: 1.2~rc2-23-g29aba34\n"
  10 +"POT-Creation-Date: 2015-08-06 18:47-0300\n"
  11 +"PO-Revision-Date: 2015-08-06 17:21-0300\n"
  12 +"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
  13 +"Language-Team: LANGUAGE <LL@li.org>\n"
  14 +"Language: \n"
  15 +"MIME-Version: 1.0\n"
  16 +"Content-Type: text/plain; charset=UTF-8\n"
  17 +"Content-Transfer-Encoding: 8bit\n"
  18 +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
  19 +
  20 +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11
  21 +msgid "A plugin that add ldap support."
  22 +msgstr ""
  23 +
  24 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10
  25 +msgid "Ldap configuration updated successfully."
  26 +msgstr ""
  27 +
  28 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12
  29 +msgid "Ldap configuration could not be saved."
  30 +msgstr ""
  31 +
  32 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1
  33 +msgid "Ldap Management"
  34 +msgstr ""
  35 +
  36 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8
  37 +msgid "Value"
  38 +msgstr ""
  39 +
  40 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11
  41 +msgid "Host"
  42 +msgstr ""
  43 +
  44 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15
  45 +msgid "Port"
  46 +msgstr ""
  47 +
  48 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19
  49 +msgid "Account"
  50 +msgstr ""
  51 +
  52 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23
  53 +msgid "Account Password"
  54 +msgstr ""
  55 +
  56 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27
  57 +msgid "Base DN"
  58 +msgstr ""
  59 +
  60 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31
  61 +msgid "LDAP Filter"
  62 +msgstr ""
  63 +
  64 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35
  65 +msgid "On the fly creation"
  66 +msgstr ""
  67 +
  68 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39
  69 +msgid "LDAPS"
  70 +msgstr ""
  71 +
  72 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46
  73 +msgid "Attributes"
  74 +msgstr ""
  75 +
  76 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53
  77 +msgid "Fullname"
  78 +msgstr ""
  79 +
  80 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57
  81 +msgid "Mail"
  82 +msgstr ""
  83 +
  84 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65
  85 +msgid "Back to plugins administration panel"
  86 +msgstr ""
... ...
plugins/serpro_captcha/po/pt/ldap.po 0 → 100644
... ... @@ -0,0 +1,93 @@
  1 +# translation of noosfero.po to
  2 +# Krishnamurti Lelis Lima Vieira Nunes <krishna@colivre.coop.br>, 2007.
  3 +# noosfero - Brazilian Portuguese translation
  4 +# Copyright (C) 2007,
  5 +# Forum Brasileiro de Economia Solidaria <http://www.fbes.org.br/>
  6 +# Copyright (C) 2007,
  7 +# Ynternet.org Foundation <http://www.ynternet.org/>
  8 +# This file is distributed under the same license as noosfero itself.
  9 +# Joenio Costa <joenio@colivre.coop.br>, 2008.
  10 +#
  11 +#
  12 +msgid ""
  13 +msgstr ""
  14 +"Project-Id-Version: 1.2~rc2-23-g29aba34\n"
  15 +"POT-Creation-Date: 2015-08-06 18:47-0300\n"
  16 +"PO-Revision-Date: 2014-12-18 18:40-0200\n"
  17 +"Last-Translator: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com>\n"
  18 +"Language-Team: Portuguese <https://hosted.weblate.org/projects/noosfero/"
  19 +"noosfero/pt/>\n"
  20 +"Language: pt\n"
  21 +"MIME-Version: 1.0\n"
  22 +"Content-Type: text/plain; charset=UTF-8\n"
  23 +"Content-Transfer-Encoding: 8bit\n"
  24 +"Plural-Forms: nplurals=2; plural=n != 1;\n"
  25 +"X-Generator: Weblate 2.0\n"
  26 +
  27 +#: plugins/ldap/lib/serpro_captcha_plugin.rb:11
  28 +msgid "A plugin that add ldap support."
  29 +msgstr "Um plugin que adiciona suporte a ldap."
  30 +
  31 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:10
  32 +msgid "Ldap configuration updated successfully."
  33 +msgstr "Configuração do Ldap atualizada com sucesso."
  34 +
  35 +#: plugins/ldap/controllers/serpro_captcha_plugin_admin_controller.rb:12
  36 +msgid "Ldap configuration could not be saved."
  37 +msgstr "Configuração do Ldap não pode ser salva."
  38 +
  39 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:1
  40 +msgid "Ldap Management"
  41 +msgstr "Gerenciamento do Ldap"
  42 +
  43 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:8
  44 +msgid "Value"
  45 +msgstr "Valor"
  46 +
  47 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:11
  48 +msgid "Host"
  49 +msgstr "Host"
  50 +
  51 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:15
  52 +msgid "Port"
  53 +msgstr "Porta"
  54 +
  55 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:19
  56 +msgid "Account"
  57 +msgstr "Conta"
  58 +
  59 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:23
  60 +msgid "Account Password"
  61 +msgstr "Senha da conta"
  62 +
  63 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:27
  64 +msgid "Base DN"
  65 +msgstr "DN Base"
  66 +
  67 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:31
  68 +msgid "LDAP Filter"
  69 +msgstr "Filtro LDAP"
  70 +
  71 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:35
  72 +msgid "On the fly creation"
  73 +msgstr "Criação sob-demanda"
  74 +
  75 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:39
  76 +msgid "LDAPS"
  77 +msgstr "LDAPS"
  78 +
  79 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:46
  80 +msgid "Attributes"
  81 +msgstr "Atributos"
  82 +
  83 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:53
  84 +msgid "Fullname"
  85 +msgstr "Nome completo"
  86 +
  87 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:57
  88 +msgid "Mail"
  89 +msgstr "Mail"
  90 +
  91 +#: plugins/ldap/views/serpro_captcha_plugin_admin/index.html.erb:65
  92 +msgid "Back to plugins administration panel"
  93 +msgstr "Voltar ao painel de administração"
... ...
plugins/serpro_captcha/test/functional/account_controller_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,86 @@
  1 +# require File.dirname(__FILE__) + '/../test_helper'
  2 +#
  3 +# # Re-raise errors caught by the controller.
  4 +# class AccountController; def rescue_action(e) raise e end; end
  5 +#
  6 +# class AccountControllerPluginTest < ActionController::TestCase
  7 +#
  8 +# def setup
  9 +# @controller = AccountController.new
  10 +# @request = ActionController::TestRequest.new
  11 +# @response = ActionController::TestResponse.new
  12 +#
  13 +# @environment = Environment.default
  14 +# @environment.enabled_plugins = ['SerproCaptchaPlugin']
  15 +# @ldap_config = load_ldap_config
  16 +# @environment.serpro_captcha_plugin= @ldap_config['server'] unless @ldap_config.nil?
  17 +# @environment.save!
  18 +# end
  19 +#
  20 +# should 'not authenticate user if its not a local user or a ldap user' do
  21 +# post :login, :user => {:login => 'someuser', :password => 'somepass'}
  22 +# assert_nil session[:user]
  23 +# end
  24 +#
  25 +# should 'diplay not logged message if the user is not a local user or a ldap user' do
  26 +# post :login, :user => {:login => 'someuser', :password => 'somepass'}
  27 +# assert_equal 'Incorrect username or password', session[:notice]
  28 +# end
  29 +#
  30 +# should 'authenticate user if its a local user but is not a ldap user' do
  31 +# user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
  32 +# user.activate
  33 +# post :login, :user => {:login => 'testuser', :password => 'test'}
  34 +# assert session[:user]
  35 +# end
  36 +#
  37 +# should 'display required fields on user login' do
  38 +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}}
  39 +# @environment.save
  40 +# get :login
  41 +# assert_tag(:input, :attributes => {:id => 'profile_data_contact_phone'})
  42 +# end
  43 +#
  44 +# if ldap_configured?
  45 +#
  46 +# should 'authenticate an existing noosfero user with ldap and loggin' do
  47 +# user = create_user(@ldap_config['user']['login'], :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
  48 +# user.activate
  49 +# count = User.count
  50 +# post :login, :user => @ldap_config['user']
  51 +# assert session[:user]
  52 +# assert_equal count, User.count
  53 +# end
  54 +#
  55 +# should 'login and create a new noosfero user if ldap authentication works properly' do
  56 +# count = User.count
  57 +# post :login, :user => @ldap_config['user']
  58 +# assert session[:user]
  59 +# assert_equal count + 1, User.count
  60 +# end
  61 +#
  62 +# should 'login on ldap if required fields are defined' do
  63 +# count = User.count
  64 +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}}
  65 +# @environment.save
  66 +# post :login, :user => @ldap_config['user'], :profile_data => {:contact_phone => '11111111'}
  67 +# assert session[:user]
  68 +# end
  69 +#
  70 +# should 'not login on ldap if required fields are not defined' do
  71 +# @environment.custom_person_fields = {"contact_phone"=>{"required"=>"true", "signup"=>"false", "active"=>"true"}}
  72 +# @environment.save
  73 +# post :login, :user => @ldap_config['user']
  74 +# assert_nil session[:user]
  75 +# end
  76 +#
  77 +# should 'authenticate user if its not a local user but is a ldap user' do
  78 +# post :login, :user => @ldap_config['user']
  79 +# assert session[:user]
  80 +# end
  81 +#
  82 +# else
  83 +# puts LDAP_SERVER_ERROR_MESSAGE
  84 +# end
  85 +#
  86 +# end
... ...
plugins/serpro_captcha/test/functional/serpro_captcha_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +# require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +# require File.dirname(__FILE__) + '/../../controllers/serpro_captcha_plugin_admin_controller'
  3 +#
  4 +# # Re-raise errors caught by the controller.
  5 +# class SerproCaptchaPluginAdminController; def rescue_action(e) raise e end; end
  6 +#
  7 +# class SerproCaptchaPluginAdminControllerTest < ActionController::TestCase
  8 +#
  9 +# def setup
  10 +# @environment = Environment.default
  11 +# user_login = create_admin_user(@environment)
  12 +# login_as(user_login)
  13 +# @admin = User[user_login].person
  14 +# @environment.enabled_plugins = ['SerproCaptchaPlugin']
  15 +# @environment.serpro_captcha_plugin_host="http://somehost"
  16 +# @environment.save!
  17 +# end
  18 +#
  19 +# should 'detected error, Name or service not known, for Serpro captcha communication' do
  20 +# environment = Environment.default
  21 +# environment.serpro_captcha_verify_uri = 'http://someserverthatdoesnotexist.mycompanythatdoesnotexist.com/validate'
  22 +# environment.serpro_captcha_client_id = '000000000000'
  23 +# environment.save!
  24 +# params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com",
  25 +# :txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'}
  26 +# post "/api/v1/register?#{params.to_query}"
  27 +# message = JSON.parse(last_response.body)['javascript_console_message']
  28 +# assert_equal "Serpro captcha error: getaddrinfo: Name or service not known", message
  29 +# end
... ...
plugins/serpro_captcha/test/test_helper.rb 0 → 100644
... ... @@ -0,0 +1,86 @@
  1 +require "#{Rails.root}/lib/noosfero/api/helpers"
  2 +
  3 +class ActiveSupport::TestCase
  4 +
  5 + include Rack::Test::Methods
  6 +
  7 + def app
  8 + Noosfero::API::API
  9 + end
  10 +
  11 + def pass_captcha
  12 + stub_request(:post, "http://www.somecompany.com:443/validate").
  13 + with(:body => "323232&642646&44641441",
  14 + :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
  15 + to_return(:status => 200, :body => "1", :headers => {'Content-Length' => 1})
  16 + spv = SerproCaptchaVerification.new
  17 + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri)
  18 + end
  19 +
  20 + def fail_captcha
  21 + stub_request(:post, "http://www.somecompany.com:443/validate").
  22 + with(:body => "323232&642646&44641441",
  23 + :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
  24 + to_return(:status => 200, :body => "2", :headers => {'Content-Length' => 1})
  25 + spv = SerproCaptchaVerification.new
  26 + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri)
  27 + end
  28 +
  29 + def login_with_captcha
  30 + json = do_login_captcha_from_api
  31 + @private_token = json["private_token"]
  32 + @params = { "private_token" => @private_token}
  33 + json
  34 + end
  35 +
  36 + ## Performs a login using the session.rb but mocking the
  37 + ## real HTTP request to validate the captcha.
  38 + def do_login_captcha_from_api
  39 + # Request mocking
  40 + #Net::HTTP::Post Mock
  41 + request = mock
  42 + #Net::HTTP Mock
  43 + http = mock
  44 + uri = URI(environment.api_captcha_settings[:verify_uri])
  45 + Net::HTTP.expects(:new).with(uri.host, uri.port).returns(http)
  46 + Net::HTTP::Post.expects(:new).with(uri.path).returns(request)
  47 +
  48 + # Captcha required codes
  49 + request.stubs(:body=).with("0000000000000000&4324343&4030320")
  50 + http.stubs(:request).with(request).returns(http)
  51 +
  52 + # Captcha validation success !!
  53 + http.stubs(:body).returns("1")
  54 +
  55 + params = {:txtToken_captcha_serpro_gov_br => '4324343', :captcha_text => '4030320'}
  56 + post "#{@url}#{params.to_query}"
  57 + json = JSON.parse(last_response.body)
  58 + json
  59 + end
  60 +
  61 + def login_api
  62 + @environment = Environment.default
  63 + @user = User.create!(:login => 'testapi', :password => 'testapi', :password_confirmation => 'testapi', :email => 'test@test.org', :environment => @environment)
  64 + @user.activate
  65 + @person = @user.person
  66 +
  67 + post "/api/v1/login?login=testapi&password=testapi"
  68 + json = JSON.parse(last_response.body)
  69 + @private_token = json["private_token"]
  70 + unless @private_token
  71 + @user.generate_private_token!
  72 + @private_token = @user.private_token
  73 + end
  74 +
  75 + @params = {:private_token => @private_token}
  76 + end
  77 + attr_accessor :private_token, :user, :person, :params, :environment
  78 +
  79 + private
  80 +
  81 + def json_response_ids(kind)
  82 + json = JSON.parse(last_response.body)
  83 + json[kind.to_s].map {|c| c['id']}
  84 + end
  85 +
  86 +end
... ...
plugins/serpro_captcha/test/unit/ext/environment_test.rb 0 → 100644
... ... @@ -0,0 +1,186 @@
  1 +# require File.dirname(__FILE__) + '/../../../../../test/test_helper'
  2 +#
  3 +# class EnvironmentTest < ActiveSupport::TestCase
  4 +#
  5 +# def setup
  6 +# @enviroment = Environment.default
  7 +# end
  8 +#
  9 +# should 'have serpro_captcha_plugin variable defined' do
  10 +# assert_equal Hash, @enviroment.serpro_captcha_plugin.class
  11 +# end
  12 +#
  13 +# should 'return an empty hash by default on serpro_captcha_plugin_attributes method' do
  14 +# assert_equal Hash.new, @enviroment.serpro_captcha_plugin_attributes
  15 +# end
  16 +#
  17 +# should 'serpro_captcha_plugin_host= define the ldap host' do
  18 +# host = "http://something"
  19 +# @enviroment.serpro_captcha_plugin_host= host
  20 +# assert_equal host, @enviroment.serpro_captcha_plugin['host']
  21 +# end
  22 +#
  23 +# should 'serpro_captcha_plugin_host return the defined ldap host' do
  24 +# host = "http://something"
  25 +# @enviroment.serpro_captcha_plugin_host= host
  26 +# assert_equal host, @enviroment.serpro_captcha_plugin_host
  27 +# end
  28 +#
  29 +# should 'serpro_captcha_plugin_port= define the ldap port' do
  30 +# value = 255
  31 +# @enviroment.serpro_captcha_plugin_port= value
  32 +# assert_equal value, @enviroment.serpro_captcha_plugin['port']
  33 +# end
  34 +#
  35 +# should 'serpro_captcha_plugin_port return the defined ldap port' do
  36 +# value = 255
  37 +# @enviroment.serpro_captcha_plugin_port= value
  38 +# assert_equal value, @enviroment.serpro_captcha_plugin_port
  39 +# end
  40 +#
  41 +# should 'default serpro_captcha_plugin_port be 389' do
  42 +# assert_equal 389, @enviroment.serpro_captcha_plugin_port
  43 +# end
  44 +#
  45 +# should 'serpro_captcha_plugin_account= define the ldap acccount' do
  46 +# value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br'
  47 +# @enviroment.serpro_captcha_plugin_account= value
  48 +# assert_equal value, @enviroment.serpro_captcha_plugin['account']
  49 +# end
  50 +#
  51 +# should 'serpro_captcha_plugin_account return the defined ldap account' do
  52 +# value = 'uid=sector,ou=Service,ou=corp,dc=company,dc=com,dc=br'
  53 +# @enviroment.serpro_captcha_plugin_account= value
  54 +# assert_equal value, @enviroment.serpro_captcha_plugin_account
  55 +# end
  56 +#
  57 +# should 'serpro_captcha_plugin_account_password= define the ldap acccount_password' do
  58 +# value = 'password'
  59 +# @enviroment.serpro_captcha_plugin_account_password= value
  60 +# assert_equal value, @enviroment.serpro_captcha_plugin['account_password']
  61 +# end
  62 +#
  63 +# should 'serpro_captcha_plugin_account_password return the defined ldap account password' do
  64 +# value = 'password'
  65 +# @enviroment.serpro_captcha_plugin_account_password= value
  66 +# assert_equal value, @enviroment.serpro_captcha_plugin_account_password
  67 +# end
  68 +#
  69 +# should 'serpro_captcha_plugin_base_dn= define the ldap base_dn' do
  70 +# value = 'dc=company,dc=com,dc=br'
  71 +# @enviroment.serpro_captcha_plugin_base_dn= value
  72 +# assert_equal value, @enviroment.serpro_captcha_plugin['base_dn']
  73 +# end
  74 +#
  75 +# should 'serpro_captcha_plugin_base_dn return the defined ldap base_dn' do
  76 +# value = 'dc=company,dc=com,dc=br'
  77 +# @enviroment.serpro_captcha_plugin_base_dn= value
  78 +# assert_equal value, @enviroment.serpro_captcha_plugin_base_dn
  79 +# end
  80 +#
  81 +# should 'serpro_captcha_plugin_attr_login= define the ldap attr_login' do
  82 +# value = 'uid'
  83 +# @enviroment.serpro_captcha_plugin_attr_login= value
  84 +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_login']
  85 +# end
  86 +#
  87 +# should 'serpro_captcha_plugin_attr_login return the defined ldap attr_login' do
  88 +# value = 'uid'
  89 +# @enviroment.serpro_captcha_plugin_attr_login= value
  90 +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_login
  91 +# end
  92 +#
  93 +# should 'serpro_captcha_plugin_attr_fullname= define the ldap attr_fullname' do
  94 +# value = 'Noosfero System'
  95 +# @enviroment.serpro_captcha_plugin_attr_fullname= value
  96 +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_fullname']
  97 +# end
  98 +#
  99 +# should 'serpro_captcha_plugin_attr_fullname return the defined ldap attr_fullname' do
  100 +# value = 'uid'
  101 +# @enviroment.serpro_captcha_plugin_attr_fullname= value
  102 +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_fullname
  103 +# end
  104 +#
  105 +#
  106 +# should 'serpro_captcha_plugin_attr_mail= define the ldap attr_mail' do
  107 +# value = 'test@noosfero.com'
  108 +# @enviroment.serpro_captcha_plugin_attr_mail= value
  109 +# assert_equal value, @enviroment.serpro_captcha_plugin['attr_mail']
  110 +# end
  111 +#
  112 +# should 'serpro_captcha_plugin_attr_mail return the defined ldap attr_mail' do
  113 +# value = 'test@noosfero.com'
  114 +# @enviroment.serpro_captcha_plugin_attr_mail= value
  115 +# assert_equal value, @enviroment.serpro_captcha_plugin_attr_mail
  116 +# end
  117 +#
  118 +# should 'serpro_captcha_plugin_onthefly_register= define the ldap onthefly_register' do
  119 +# value = '1'
  120 +# @enviroment.serpro_captcha_plugin_onthefly_register= value
  121 +# assert @enviroment.serpro_captcha_plugin['onthefly_register']
  122 +# end
  123 +#
  124 +# should 'serpro_captcha_plugin_onthefly_register return true if ldap onthefly_register variable is defined as true' do
  125 +# value = '1'
  126 +# @enviroment.serpro_captcha_plugin_onthefly_register= value
  127 +# assert @enviroment.serpro_captcha_plugin_onthefly_register
  128 +# end
  129 +#
  130 +# should 'serpro_captcha_plugin_onthefly_register return false if ldap onthefly_register variable is defined as false' do
  131 +# value = '0'
  132 +# @enviroment.serpro_captcha_plugin_onthefly_register= value
  133 +# refute @enviroment.serpro_captcha_plugin_onthefly_register
  134 +# end
  135 +#
  136 +# should 'serpro_captcha_plugin_filter= define the ldap filter' do
  137 +# value = 'test'
  138 +# @enviroment.serpro_captcha_plugin_filter= value
  139 +# assert_equal value, @enviroment.serpro_captcha_plugin['filter']
  140 +# end
  141 +#
  142 +# should 'serpro_captcha_plugin_filter return the defined ldap filter' do
  143 +# value = 'test'
  144 +# @enviroment.serpro_captcha_plugin_filter= value
  145 +# assert_equal value, @enviroment.serpro_captcha_plugin_filter
  146 +# end
  147 +#
  148 +# should 'serpro_captcha_plugin_tls= define the ldap tls' do
  149 +# value = '1'
  150 +# @enviroment.serpro_captcha_plugin_tls= value
  151 +# assert @enviroment.serpro_captcha_plugin['tls']
  152 +# end
  153 +#
  154 +# should 'tls return true if ldap tls variable is defined as true' do
  155 +# value = '1'
  156 +# @enviroment.serpro_captcha_plugin_tls= value
  157 +# assert @enviroment.serpro_captcha_plugin_tls
  158 +# end
  159 +#
  160 +# should 'tls return false if ldap tls variable is defined as false' do
  161 +# value = '0'
  162 +# @enviroment.serpro_captcha_plugin_tls= value
  163 +# refute @enviroment.serpro_captcha_plugin_tls
  164 +# end
  165 +#
  166 +# should 'validates presence of host' do
  167 +# @enviroment.serpro_captcha_plugin= {:port => 3000}
  168 +# @enviroment.valid?
  169 +#
  170 +# assert @enviroment.errors.include?(:serpro_captcha_plugin_host)
  171 +#
  172 +# @enviroment.serpro_captcha_plugin_host= "http://somehost.com"
  173 +# @enviroment.valid?
  174 +# refute @enviroment.errors.include?(:serpro_captcha_plugin_host)
  175 +# end
  176 +#
  177 +# should 'validates presence of host only if some ldap configuration is defined' do
  178 +# @enviroment.valid?
  179 +# refute @enviroment.errors.include?(:serpro_captcha_plugin_host)
  180 +#
  181 +# @enviroment.serpro_captcha_plugin= {:port => 3000}
  182 +# @enviroment.valid?
  183 +# assert @enviroment.errors.include?(:serpro_captcha_plugin_host)
  184 +# end
  185 +#
  186 +# end
... ...
plugins/serpro_captcha/test/unit/serpro_captcha_verification_test.rb 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +require 'webmock'
  2 +include WebMock::API
  3 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  4 +require_relative '../test_helper'
  5 +
  6 +class SerproCaptchaVerificationTest < ActiveSupport::TestCase
  7 +
  8 + def setup
  9 + @environment = Environment.default
  10 + @environment.enabled_plugins = ['SerproCaptchaPlugin']
  11 + @environment.serpro_captcha_verify_uri='https://www.somecompany.com/validate'
  12 + @environment.serpro_captcha_client_id='323232'
  13 + @environment.save!
  14 + end
  15 +
  16 + should 'register a user when there are no enabled captcha pluging' do
  17 + @environment.enabled_plugins = []
  18 + Environment.default.enable('skip_new_user_email_confirmation')
  19 + params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" }
  20 + post "/api/v1/register?#{params.to_query}"
  21 + assert_equal 201, last_response.status
  22 + json = JSON.parse(last_response.body)
  23 + assert User['newuserapi'].activated?
  24 + assert json['activated']
  25 + assert json['private_token'].present?
  26 + end
  27 +
  28 + # should 'not register a user if captcha fails' do
  29 + # fail_captcha
  30 + # Environment.default.enable('skip_new_user_email_confirmation')
  31 + # params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" }
  32 + # post "/api/v1/register?#{params.to_query}"
  33 + # assert_equal 201, last_response.status
  34 + # json = JSON.parse(last_response.body)
  35 + # refute User['newuserapi'].activated?
  36 + # refute !json['activated']
  37 + # refute !json['private_token'].present?
  38 + # end
  39 +
  40 + should 'verify_serpro_captcha' do
  41 + pass_captcha
  42 + spv = SerproCaptchaVerification.new
  43 + assert spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', '44641441', @environment.serpro_captcha_verify_uri)
  44 + end
  45 +
  46 + should 'fail captcha if user has not filled Serpro\' captcha text' do
  47 + pass_captcha
  48 + spv = SerproCaptchaVerification.new
  49 + hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, '642646', nil, @environment.serpro_captcha_verify_uri)
  50 + assert hash[:user_message], _('Captcha text has not been filled')
  51 + end
  52 +
  53 + should 'fail captcha if Serpro\' captcha token has not been sent' do
  54 + pass_captcha
  55 + spv = SerproCaptchaVerification.new
  56 + hash = spv.verify_serpro_captcha(@environment.serpro_captcha_client_id, nil, '76876846', @environment.serpro_captcha_verify_uri)
  57 + assert hash[:javascript_console_message], _("Missing Serpro's Captcha token")
  58 + end
  59 +
  60 +end
... ...
plugins/serpro_captcha/views/ldap_plugin_admin/index.html.erb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +<h1><%= _("Serpro's Captcha Management") %> </h1>
  2 +
  3 +<%= labelled_form_for(:environment, :url => {:action => 'update'}) do |f| %>
  4 +
  5 +<table>
  6 + <tr>
  7 + <th><%= c_('Configuration') %></th>
  8 + <th><%= _('Value') %></th>
  9 + </tr>
  10 + <tr>
  11 + <td><%= _('Host') %></td>
  12 + <td><%= text_field :environment, :serpro_captcha_verify_uri %></td>
  13 + </tr>
  14 + <tr>
  15 + <td><%= _('Port') %></td>
  16 + <td><%= text_field :environment, :serpro_captcha_client_id %></td>
  17 + </tr>
  18 +</table>
  19 +
  20 +<div>
  21 + <% button_bar do %>
  22 + <%= submit_button('save', c_('Save changes')) %>
  23 + <%= button :back, _('Back to plugins administration panel'), :controller => 'plugins' %>
  24 + <% end %>
  25 +</div>
  26 +
  27 +<% end %>
... ...
plugins/serpro_captcha/views/serpro_captcha_plugin_admin/index.html.erb 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +<h1><%= _("Serpro's Captcha Management") %> </h1>
  2 +
  3 +<%= labelled_form_for(:environment, :url => {:action => 'update'}) do |f| %>
  4 +
  5 +<table>
  6 + <tr>
  7 + <th><%= c_('Configuration') %></th>
  8 + <th><%= _('Value') %></th>
  9 + </tr>
  10 + <tr>
  11 + <td><%= _('Verify URI') %></td>
  12 + <td><%= text_field :environment, :serpro_captcha_verify_uri %></td>
  13 + </tr>
  14 + <tr>
  15 + <td><%= _('Client Id') %></td>
  16 + <td><%= text_field :environment, :serpro_captcha_client_id %></td>
  17 + </tr>
  18 +</table>
  19 +
  20 +
  21 +<div>
  22 + <% button_bar do %>
  23 + <%= submit_button('save', c_('Save changes')) %>
  24 + <%= button :back, _('Back to plugins administration panel'), :controller => 'plugins' %>
  25 + <% end %>
  26 +</div>
  27 +
  28 +<% end %>
... ...