Skip to content

Commit

Permalink
Fixed captcha tests and bugs. Fixed 'find' to detect count (:first/:a…
Browse files Browse the repository at this point in the history
…ll) from query.
  • Loading branch information
gaspard committed May 31, 2011
1 parent e6d4eee commit 0d7e0c3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/views/comments/_li.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= link_to_remote(_('btn_publish'), :url=> publish_comment_path(:id=>li[:id], :bin=>@admin), :method => :put) if li[:status] > Zena::Status::Pub %>
</span>
<span class='date'><%= long_date(li.created_at) + " " + short_time(li.created_at) %></span>
<span class='sign'><%= li.author_name || li.author.login %></span>
<span class='sign'><%= li.author_name %></span>
<span class='title'><%= make_link(:node => li.discussion.node, :title => "#{li.discussion.node.title}#{li.title.blank? ? '' : " / #{li.title}"}") %></span>
</div>
<div class='body'>
Expand Down
35 changes: 25 additions & 10 deletions bricks/captcha/lib/bricks/captcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ module ViewMethods

# Overwrite mail_hide to avoid MH_PUB, MH_PRIV globals
def mail_hide(address, options={})
contents = options[:contents] || truncate(address, :length => 10)
pub_key = options[:mh_pub] || MH_PUB
priv_key = options[:mh_priv] || MH_PRIV
k = ReCaptcha::MHClient.new(pub_key, priv_key)
enciphered = k.encrypt(address)
uri = "http://mailhide.recaptcha.net/d?k=#{pub_key}&c=#{enciphered}"
%{<a href="#{uri}" onclick="window.open('#{uri}', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="#{_('Reveal this e-mail address')}">#{contents}</a>}
show = options[:show] || truncate(address, :length => 10)
pub_key = options[:mh_pub] || current_site.prop['mail_hide_pub']
priv_key = options[:mh_priv] || current_site.prop['mail_hide_priv']
k = ReCaptcha::MHClient.new(pub_key, priv_key, address)
uri = "http://www.google.com/recaptcha/mailhide/d?k=#{pub_key}&c=#{k.crypted_address}"
%Q{<a href="#{uri}" onclick="window.open('#{uri}', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="#{_('Reveal this e-mail address')}">#{show}</a>}
end

# Rewrite get_captcha to avoid writing priv/pub keys in templates
def get_captcha(options={})
pub_key = options[:rcc_pub] || current_site.prop['recaptcha_pub']
priv_key = options[:rcc_priv] || current_site.prop['recaptcha_priv']
k = ReCaptcha::Client.new(pub_key, priv_key, options[:ssl])
r = k.get_challenge(session[:rcc_err] || '', options)
session[:rcc_err]=''
r
end

def email_asset(opts)
Expand All @@ -41,20 +50,26 @@ def email_asset(opts)
module ZafuMethods
def r_captcha
return parser_error("recaptcha keys not set") unless current_site.prop['recaptcha_pub'] && current_site.prop['recaptcha_priv']
res = "<%= get_captcha(:rcc_pub => #{visitor.site.prop['recaptcha_pub'].inspect}, :rcc_priv => #{visitor.site.prop['recaptcha_priv'].inspect}#{get_recaptcha_params}) %>"
res = "<%= get_captcha(:ssl => #{@params[:ssl] == 'true' ? 'true' : 'false'}#{get_recaptcha_params}) %>"
res += expand_with
"<% if visitor.is_anon? %>#{@markup.wrap(res)}<% end %>"
end

def r_mail_hide
if code = get_attribute_or_eval
return parser_error("Argument to mail_hide should be a String (found #{code.klass}).") unless code.klass <= String
"<%= visitor.is_anon? ? mail_hide(#{code},:mh_pub => #{visitor.site.prop['mail_hide_pub'].inspect}, :mh_priv => #{visitor.site.prop['mail_hide_priv'].inspect}#{get_recaptcha_params}) : #{code} %>"
if show = @params[:show]
show = RubyLess.translate_string(self, show)
end
"<%= visitor.is_anon? ? mail_hide(#{code}#{get_recaptcha_params(show)}) : #{code} %>"
end
end

def get_recaptcha_params
def get_recaptcha_params(mh_show = nil)
res = ", :options => {"
if mh_show
res << ":show => #{mh_show},"
end
res << ":theme => #{(@params[:theme] || 'red').inspect}"
res << ", :lang => #{(@params[:lang] || helper.send(:lang)).inspect}"
res << ", :tabindex => #{(@params[:tabindex] || 0).to_i}}"
Expand Down
2 changes: 1 addition & 1 deletion bricks/tags/zena/test/zafu/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ tag_names:

checkbox_tagged:
src: "<r:input type='checkbox' name='tagged[blue]'/>"
res: "<input type='checkbox' name='node[tagged][blue]' value='blue' checked='checked'/>"
res: "<input type='hidden' name='node[tagged][blue]' value=''/><input type='checkbox' name='node[tagged][blue]' value='blue' checked='checked'/>"
23 changes: 17 additions & 6 deletions lib/zena/status.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
module Zena
module Status
Red = 70
# Redaction (initial state of an element)
Red = 70
# Proposed along with another node (images/documents in the node)
PropWith = 65
Prop = 60
Pub = 50
Rep = 20
Rem = 10
Del = 0
# Proposed for publication
Prop = 60
# Published
Pub = 50
# Replaced (a new publication takes over)
Rep = 20
# Removed (unpublished by hand)
Rem = 10
# Deleted (not used)
Del = 0

def self.[](key)
self.const_get(key.to_s.camelize)
end
end # Status
end # Zena
5 changes: 4 additions & 1 deletion lib/zena/use/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ def r_query

# Pre-processing of the 'find("...")' method.
def get_type_for_find(string)
finder = build_finder(:first, string, {})
finder = build_finder(get_count(string, {}), string, {})
TypedString.new(finder.delete(:method), finder)
rescue ::QueryBuilder::Error => err
out parser_error(err.message)
nil
end

# Pre-processing of the 'count("...")' method.
Expand Down
6 changes: 5 additions & 1 deletion test/integration/zafu_compiler/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ bad_iconv:

add_bad_klass:
src: "<ol do='pages'><li do='each' do='link'/><li do='add' klass='page'/></ol>"
res: "!/backtrace/"
res: "!/backtrace/"

error_in_find_with_rubyless:
src: "<b do='find(\"images in in site limit 2\")' do='each' join=',' do='title'/>"
res: "/Syntax error near \"in site/"
6 changes: 5 additions & 1 deletion test/integration/zafu_compiler/query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,8 @@ query_errors:
context:
c: 'badaboum'
src: "<r:query default='nodes' select='nodes in #{params[:c]}'><r:elsif test='query_errors' do='query_errors'/></r:query>"
res: "<span class='query'>nodes in badaboum</span> <span class='error'>Invalid scope 'badaboum'.</span>"
res: "<span class='query'>nodes in badaboum</span> <span class='error'>Invalid scope 'badaboum'.</span>"

find_with_rubyless:
src: "<b do='find(\"images in site limit 2\")' do='each' join=',' do='title'/>"
res: "<b>Autumn Tree,bird</b>"
2 changes: 1 addition & 1 deletion test/unit/zena/use/zazen_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,6 @@ def test_mail_hide
login(:lion)
assert current_site.update_attributes(:mail_hide_priv => '1234', :mail_hide_pub => '3456')
@node = secure!(Node) { nodes(:status) }
assert_match %r{<a href.*mailhide.recaptcha.net/d\?k=3456&.*window.open}m, zazen("This is an email [email]bob@example.com[/email].")
assert_match %r{<a href.*google.com/recaptcha/mailhide/d\?k=3456&.*window.open}m, zazen("This is an email [email]bob@example.com[/email].")
end
end
3 changes: 2 additions & 1 deletion zena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Gaspard Bucher"]
s.date = %q{2011-05-31}
s.date = %q{2011-06-01}
s.default_executable = %q{zena}
s.description = %q{zena is a Ruby on Rails CMS (content managment system) with a focus on usability, ease of customization and web 2.0 goodness (application like behaviour).}
s.email = %q{gaspard@teti.ch}
Expand Down Expand Up @@ -547,6 +547,7 @@ Gem::Specification.new do |s|
"lib/zena/remote/serializable_array.rb",
"lib/zena/routes.rb",
"lib/zena/site_worker.rb",
"lib/zena/status.rb",
"lib/zena/test_controller.rb",
"lib/zena/unit/test_case.rb",
"lib/zena/use.rb",
Expand Down

0 comments on commit 0d7e0c3

Please sign in to comment.