Commit fad30e3149982898dc817a21f4f2cd80ce59f179

Authored by Larissa Reis
2 parents 56f774e6 00d5c1ac

invite-members: Merge rails3 stoa plugin

app/controllers/public/invite_controller.rb
... ... @@ -4,8 +4,9 @@ class InviteController < PublicController
4 4 before_filter :login_required
5 5 before_filter :check_permissions_to_invite
6 6  
7   - def select_address_book
  7 + def invite_friends
8 8 @import_from = params[:import_from] || "manual"
  9 + @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
9 10 if request.post?
10 11 contact_list = ContactList.create
11 12 Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual'
... ... @@ -22,7 +23,7 @@ class InviteController < PublicController
22 23 webmail_import_addresses = params[:webmail_import_addresses]
23 24 contacts_to_invite = Invitation.join_contacts(manual_import_addresses, webmail_import_addresses)
24 25 if !contacts_to_invite.empty?
25   - Delayed::Job.enqueue InvitationJob.new(current_user.person.id, contacts_to_invite, params[:mail_template], profile.id, @contact_list.id, locale)
  26 + Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, params[:mail_template], profile.id, @contact_list.id, locale)
26 27 session[:notice] = _('Your invitations are being sent.')
27 28 if profile.person?
28 29 redirect_to :controller => 'profile', :action => 'friends'
... ... @@ -52,14 +53,42 @@ class InviteController < PublicController
52 53 def cancel_fetching_emails
53 54 contact_list = ContactList.find(params[:contact_list])
54 55 contact_list.destroy
55   - redirect_to :action => 'select_address_book'
  56 + redirect_to :action => 'invite_friends'
  57 + end
  58 +
  59 + def invite_registered_friend
  60 + contacts_to_invite = params['q'].split(',')
  61 + if !contacts_to_invite.empty? && request.post?
  62 + Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, '', profile.id, nil, locale)
  63 + session[:notice] = _('Your invitations are being sent.')
  64 + if profile.person?
  65 + redirect_to :controller => 'profile', :action => 'friends'
  66 + else
  67 + redirect_to :controller => 'profile', :action => 'members'
  68 + end
  69 + return
  70 + else
  71 + redirect_to :action => 'invite_friends'
  72 + session[:notice] = _('Please enter a valid profile.')
  73 + end
  74 + end
  75 +
  76 + include InviteHelper
  77 + helper :invite
  78 +
  79 + def search_friend
  80 + fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }
  81 + values = ["%#{params['q']}%"] * fields.count
  82 + render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]).
  83 + select {|person| !profile.members.include?(person) }.
  84 + map {|person| {:id => person.id, :name => person.name} }.to_json
56 85 end
57 86  
58 87 protected
59 88  
60 89 def check_permissions_to_invite
61   - if profile.person? and !current_user.person.has_permission?(:manage_friends, profile) or
62   - profile.community? and !current_user.person.has_permission?(:invite_members, profile)
  90 + if profile.person? and !user.has_permission?(:manage_friends, profile) or
  91 + profile.community? and !user.has_permission?(:invite_members, profile)
63 92 render_access_denied
64 93 end
65 94 end
... ...
app/helpers/invite_helper.rb
1 1 module InviteHelper
  2 + def plugins_options
  3 + @plugins.dispatch(:search_friend_fields)
  4 + end
  5 +
  6 + def search_friend_fields
  7 + labels = [
  8 + _('Name'),
  9 + _('Username'),
  10 + _('Email'),
  11 + ] + plugins_options.map { |options| options[:name] }
  12 +
  13 + last = labels.pop
  14 + label = labels.join(', ')
  15 + "#{label} #{_('or')} #{last}"
  16 + end
2 17 end
... ...
app/models/invitation.rb
... ... @@ -51,7 +51,10 @@ class Invitation < Task
51 51 next if contact_to_invite == _("Firstname Lastname <friend@email.com>")
52 52  
53 53 contact_to_invite.strip!
54   - if match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT)
  54 + find_by_profile_id = false
  55 + if contact_to_invite.match(/^\d*$/)
  56 + find_by_profile_id = true
  57 + elsif match = contact_to_invite.match(/(.*)<(.*)>/) and match[2].match(Noosfero::Constants::EMAIL_FORMAT)
55 58 friend_name = match[1].strip
56 59 friend_email = match[2]
57 60 elsif match = contact_to_invite.strip.match(Noosfero::Constants::EMAIL_FORMAT)
... ... @@ -61,19 +64,23 @@ class Invitation &lt; Task
61 64 next
62 65 end
63 66  
64   - user = User.find_by_email(friend_email)
  67 + begin
  68 + user = find_by_profile_id ? Person.find_by_id(contact_to_invite).user : User.find_by_email(friend_email)
  69 + rescue
  70 + user = nil
  71 + end
65 72  
66   - task_args = if user.nil?
  73 + task_args = if user.nil? && !find_by_profile_id
67 74 {:person => person, :friend_name => friend_name, :friend_email => friend_email, :message => message}
68   - elsif !user.person.is_a_friend?(person)
  75 + elsif user.present? && !(user.person.is_a_friend?(person) && profile.person?)
69 76 {:person => person, :target => user.person}
70 77 end
71 78  
72 79 if !task_args.nil?
73 80 if profile.person?
74   - InviteFriend.create(task_args)
  81 + InviteFriend.create(task_args) if !user || user.person.tasks.pending.of("InviteFriend").find(:all, :conditions => {:requestor_id => person.id, :target_id => user.person.id}).blank?
75 82 elsif profile.community?
76   - InviteMember.create(task_args.merge(:community_id => profile.id))
  83 + InviteMember.create(task_args.merge(:community_id => profile.id)) if !user || user.person.tasks.pending.of("InviteMember").find(:all, :conditions => {:requestor_id => person.id}).select { |t| t.data[:community_id] == profile.id }.blank?
77 84 else
78 85 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s
79 86 end
... ...
app/models/invite_member.rb
... ... @@ -12,7 +12,7 @@ class InviteMember &lt; Invitation
12 12 end
13 13  
14 14 def perform
15   - community.add_member(friend)
  15 + community.add_member(friend) and friend.tasks.pending.of("InviteMember").select { |t| t.data[:community_id] == community_id }.each { |invite| invite.cancel }
16 16 end
17 17  
18 18 def title
... ... @@ -39,6 +39,14 @@ class InviteMember &lt; Invitation
39 39 _('%{requestor} invited you to join %{community}.') % {:requestor => requestor.name, :community => community.name}
40 40 end
41 41  
  42 + def target_notification_message
  43 + if friend
  44 + _('%{requestor} is inviting you to join "%{community}" on %{system}.') % { :system => target.environment.name, :requestor => requestor.name, :community => community.name }
  45 + else
  46 + super
  47 + end
  48 + end
  49 +
42 50 def expanded_message
43 51 super.gsub /<community>/, community.name
44 52 end
... ...
app/views/friends/index.html.erb
... ... @@ -15,7 +15,7 @@
15 15 <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %>
16 16 <%= button(:search, _('Find people'), :controller => 'search', :action => 'assets', :asset => 'people') %>
17 17 <% unless @plugins.dispatch(:remove_invite_friends_button).include?(true) %>
18   - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %>
  18 + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %>
19 19 <% end %>
20 20 <% end %>
21 21 <% end %>
... ... @@ -46,7 +46,7 @@
46 46 <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %>
47 47 <%= button(:search, _('Find people'), :controller => 'search', :action => 'assets', :asset => 'people') %>
48 48 <% unless @plugins.dispatch(:remove_invite_friends_button).include?(true) %>
49   - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %>
  49 + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %>
50 50 <% end %>
51 51 <% end %>
52 52 <% end %>
... ...
app/views/invite/_personalize_invitation_mail.html.erb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<br/>
  2 +
  3 +<%= link_to_function(_('Personalize invitation mail'), nil) do |page|
  4 + page['invitation-mail_template'].show
  5 +end %>
  6 +
  7 +<div id='invitation-mail_template' style='display:none'>
  8 + <%= h _("Now enter an invitation message. You must keep the <url> code in your invitation text. When your friends receive the invitation e-mail, <url> will be replaced by a link that they need to click to activate their account. <user> and <friend> codes will be replaced by your name and friend name, but they are optional.") %>
  9 + <%= labelled_form_field(_('Invitation text:'), text_area_tag(:mail_template, mail_template, :cols => 72, :rows => 8)) %>
  10 +</div>
... ...
app/views/invite/_select_address_book.html.erb 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +<% header ||='h2' %>
  2 +<<%= header %>><%= _('Step 1 of 2: Select address book') %></<%= header %>>
  3 +
  4 +<% form_tag do %>
  5 +
  6 + <%= [
  7 + radio_button_tag(:import_from, "manual", @import_from == "manual", :onclick => 'hide_invite_friend_login_password()') + content_tag('label', _('Manually (empty field)'), :for => "import_from_manual"),
  8 + radio_button_tag(:import_from, "gmail", @import_from == "gmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Gmail', :for => 'import_from_gmail'),
  9 + radio_button_tag(:import_from, "yahoo", @import_from == "yahoo", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Yahoo', :for => "import_from_yahoo"),
  10 + radio_button_tag(:import_from, "hotmail", @import_from == "hotmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Hotmail', :for => "import_from_hotmail")
  11 + ].join("\n<br/>\n") %>
  12 +
  13 + <script type="text/javascript">
  14 + function hide_invite_friend_login_password() {
  15 + $('invite-friends-login-password').hide();
  16 + }
  17 + function show_invite_friend_login_password(option) {
  18 + if (option == 'hotmail') {
  19 + $('hotmail_username_tip').show();
  20 + } else {
  21 + $('hotmail_username_tip').hide();
  22 + }
  23 + $('invite-friends-login-password').show();
  24 + $('login').focus();
  25 + }
  26 + </script>
  27 + <div id='invite-friends-login-password' <%= "style='display: none;'" if (@import_from == 'manual') %>>
  28 + <div id='hotmail_username_tip'>
  29 + <%= ui_icon('ui-icon-alert') %>
  30 + <%= _('Please type your username in the format yourname@example.com') %>
  31 + </div>
  32 +
  33 + <%= labelled_form_field(_("Username") + ":", text_field_tag(:login, @login)) %>
  34 + <%= labelled_form_field(_("Password") + ":", password_field_tag(:password)) %>
  35 + </div>
  36 +
  37 + <% button_bar do %>
  38 + <%= submit_button(:forward, _("Next")) %>
  39 + <% end %>
  40 + <p><%= _("We won't store your password or contact anyone without your permission.") %></p>
  41 +<% end %>
... ...
app/views/invite/invite_friends.html.erb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +<% if profile.person? %>
  2 + <h1><%= _('Invite your friends') %></h1>
  3 +<% else %>
  4 + <h1><%= _('Invite your friends to join %s') % profile.name %></h1>
  5 +<% end %>
  6 +
  7 +<% unless profile.person? %>
  8 + <h2><%= _('Invite other registered users') %></h2>
  9 + <p><%= _('You can search for user profiles and invite them to join this community.') %></p>
  10 + <% form_tag :action => 'invite_registered_friend' do %>
  11 + <% search_action = url_for(:action => 'search_friend') %>
  12 + <%= token_input_field_tag(:q, 'search-friends', search_action,
  13 + { :hint_text => _('Type in your friend\'s ') + search_friend_fields,
  14 + :focus => false }) %>
  15 +
  16 + <% button_bar do %>
  17 + <%= submit_button('save', _('Invite'))%>
  18 + <%= button('cancel', _('Cancel'), profile.url)%>
  19 + <% end %>
  20 + <% end %>
  21 +
  22 + <br />
  23 + <h2><%= _('Invite people from my e-mail contacts') %></h2>
  24 + <% header = 'h3' %>
  25 +
  26 +<% end %>
  27 +
  28 +<%= render :partial => 'invite/select_address_book', :locals => {:header => header} %>
  29 +
  30 +<div id="loadingScreen"></div>
... ...
app/views/invite/select_address_book.html.erb
... ... @@ -1,51 +0,0 @@
1   -<% if profile.person? %>
2   - <h1><%= _('Invite your friends') %></h1>
3   -<% else %>
4   - <h1><%= _('Invite your friends to join %s') % profile.name %></h1>
5   -<% end %>
6   -
7   -<h2><%= _('Step 1 of 2: Select address book') %></h2>
8   -
9   -<%= form_tag do %>
10   -
11   - <%= [
12   - radio_button_tag(:import_from, "manual", @import_from == "manual", :onclick => 'hide_invite_friend_login_password()') + content_tag('label', _('Manually (empty field)'), :for => "import_from_manual"),
13   - radio_button_tag(:import_from, "gmail", @import_from == "gmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Gmail', :for => 'import_from_gmail'),
14   - radio_button_tag(:import_from, "yahoo", @import_from == "yahoo", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Yahoo', :for => "import_from_yahoo"),
15   - radio_button_tag(:import_from, "hotmail", @import_from == "hotmail", :onclick => 'show_invite_friend_login_password(this.value)') + content_tag('label', 'Hotmail', :for => "import_from_hotmail")
16   - ].join("\n<br/>\n") %>
17   -
18   - <script type="text/javascript">
19   - function hide_invite_friend_login_password() {
20   - $('invite-friends-login-password').hide();
21   - }
22   - function show_invite_friend_login_password(option) {
23   - if (option == 'hotmail') {
24   - $('hotmail_username_tip').show();
25   - } else {
26   - $('hotmail_username_tip').hide();
27   - }
28   - $('invite-friends-login-password').show();
29   - $('login').focus();
30   - }
31   - </script>
32   - <div id='invite-friends-login-password' <%= "style='display: none;'" if (@import_from == 'manual') %>>
33   - <div id='hotmail_username_tip'>
34   - <%= ui_icon('ui-icon-alert') %>
35   - <%= _('Please type your username in the format yourname@example.com') %>
36   - </div>
37   -
38   - <%= labelled_form_field(_("Username") + ":", text_field_tag(:login, @login)) %>
39   - <%= labelled_form_field(_("Password") + ":", password_field_tag(:password)) %>
40   - </div>
41   -
42   - <% button_bar do %>
43   - <%= submit_button(:forward, _("Next")) %>
44   - <% end %>
45   - <p><%= _("We won't store your password or contact anyone without your permission.") %></p>
46   -<% end %>
47   -
48   -<div id="loadingScreen"></div>
49   -
50   -
51   -
app/views/invite/select_friends.html.erb
... ... @@ -9,7 +9,7 @@
9 9  
10 10 <h2><%= _('Step 2 of 2: Selecting Friends') %></h2>
11 11  
12   -<%= button(:back, _('Back'), { :action => 'select_address_book' }, :id => 'invitation_back_button') %>
  12 +<%= button(:back, _('Back'), { :action => 'invite_friends' }, :id => 'invitation_back_button') %>
13 13  
14 14 <p>
15 15 <%= _('Indicate which friends you want to invite.') %>
... ... @@ -30,16 +30,7 @@
30 30 </div>
31 31 <% end -%>
32 32  
33   - <br/>
34   -
35   - <%= link_to_function(_('Personalize invitation mail'), nil) do |page|
36   - page['invitation-mail_template'].show
37   - end %>
38   -
39   - <div id='invitation-mail_template' style='display:none'>
40   - <%= h _("Now enter an invitation message. You must keep the <url> code in your invitation text. When your friends receive the invitation e-mail, <url> will be replaced by a link that they need to click to activate their account. <user> and <friend> codes will be replaced by your name and friend name, but they are optional.") %>
41   - <%= labelled_form_field(_('Invitation text:'), text_area_tag(:mail_template, @mail_template, :cols => 72, :rows => 8)) %>
42   - </div>
  33 + <%= render :partial => 'invite/personalize_invitation_mail', :locals => {:mail_template => @mail_template } %>
43 34  
44 35 <% button_bar do %>
45 36 <%= submit_button(:ok, _("Invite my friends!")) %>
... ...
app/views/profile/friends.html.erb
... ... @@ -18,7 +18,7 @@
18 18 <%= button :back, _('Go back'), { :controller => 'profile' } %>
19 19 <% if user == profile %>
20 20 <%= button :edit, _('Manage my friends'), :controller => 'friends', :action => 'index', :profile => profile.identifier %>
21   - <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'select_address_book') %>
  21 + <%= button(:search, _('Invite people from my e-mail contacts'), :controller => 'invite', :action => 'invite_friends') %>
22 22 <% end %>
23 23 <% end %>
24 24  
... ...
app/views/profile/members.html.erb
... ... @@ -18,7 +18,7 @@
18 18 <%= button :back, _('Go back'), { :controller => 'profile' } %>
19 19 <% if profile.community? and user %>
20 20 <% if user.has_permission?(:invite_members, profile) %>
21   - <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'select_address_book' %>
  21 + <%= button :search, _('Invite your friends to join %s') % profile.name, :controller => 'invite', :action => 'invite_friends' %>
22 22 <% end %>
23 23 <% if user.has_permission?(:send_mail_to_members, profile) %>
24 24 <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %>
... ...
app/views/profile_members/_index_buttons.html.erb
... ... @@ -2,7 +2,7 @@
2 2 <%= button :back, _('Back'), :controller => 'profile_editor' %>
3 3 <%= button :add, _('Add members'), :action => 'add_members' if profile.enterprise? %>
4 4 <% if profile.community? and user.has_permission?(:invite_members, profile) %>
5   - <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'select_address_book' %>
  5 + <%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'invite_friends' %>
6 6 <% end %>
7 7 <% if profile.community? and user.has_permission?(:send_mail_to_members, profile) %>
8 8 <%= button :send, _('Send e-mail to members'), :controller => 'profile', :action => 'send_mail' %>
... ...
config/routes.rb
... ... @@ -67,7 +67,7 @@ Noosfero::Application.routes.draw do
67 67 match 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/, :as => :catalog
68 68  
69 69 # invite
70   - match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'select_address_book', :profile => /#{Noosfero.identifier_format}/
  70 + match 'profile/:profile/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => /#{Noosfero.identifier_format}/
71 71 match 'profile/:profile/invite/:action', :controller => 'invite', :profile => /#{Noosfero.identifier_format}/
72 72  
73 73 # feeds per tag
... ...
lib/noosfero/plugin.rb
... ... @@ -513,6 +513,12 @@ class Noosfero::Plugin
513 513 nil
514 514 end
515 515  
  516 + # -> Adds aditional fields for invite_friends search
  517 + # returns = [{:field => 'field1', :name => 'field 1 name'}, {...}]
  518 + def search_friend_fields
  519 + nil
  520 + end
  521 +
516 522 # -> Adds additional blocks to profiles and environments.
517 523 # Your plugin must implements a class method called 'extra_blocks'
518 524 # that returns a hash with the following syntax.
... ...
plugins/stoa/lib/stoa_plugin.rb
... ... @@ -110,7 +110,7 @@ class StoaPlugin &lt; Noosfero::Plugin
110 110 { :title => _('Invite friends'),
111 111 :icon => 'invite-friends',
112 112 :url => {:controller => 'invite',
113   - :action => 'select_address_book'} } if context.send(:user) && context.send(:user).usp_id.present?
  113 + :action => 'invite_friends'} } if context.send(:user) && context.send(:user).usp_id.present?
114 114 end
115 115  
116 116 def remove_invite_friends_button
... ... @@ -121,4 +121,8 @@ class StoaPlugin &lt; Noosfero::Plugin
121 121 {:field => :usp_id, :name => _('USP Number'), :model => 'person'}
122 122 end
123 123  
  124 + def search_friend_fields
  125 + [{:field => :usp_id, :name => _('USP Number')}]
  126 + end
  127 +
124 128 end
... ...
plugins/stoa/test/functional/invite_controller_test.rb
... ... @@ -20,7 +20,7 @@ class InviteControllerTest &lt; ActionController::TestCase
20 20 person_without_usp_id = User.create!(:login => 'user-without', :email => 'user-without@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:invitation_code => 12345678}).person
21 21  
22 22 login_as(person_without_usp_id.identifier)
23   - get :select_address_book, :profile => person_without_usp_id.identifier
  23 + get :invite_friends, :profile => person_without_usp_id.identifier
24 24 assert_response 403
25 25 get :select_friends, :profile => person_without_usp_id.identifier
26 26 assert_response 403
... ... @@ -30,7 +30,7 @@ class InviteControllerTest &lt; ActionController::TestCase
30 30 person_with_usp_id = User.create!(:login => 'user-with', :email => 'user-with@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:usp_id => 12345678}).person
31 31  
32 32 login_as(person_with_usp_id.identifier)
33   - get :select_address_book, :profile => person_with_usp_id.identifier
  33 + get :invite_friends, :profile => person_with_usp_id.identifier
34 34 assert_response 200
35 35 get :select_friends, :profile => person_with_usp_id.identifier, :contact_list => ContactList.create.id
36 36 assert_response 200
... ... @@ -42,11 +42,23 @@ class InviteControllerTest &lt; ActionController::TestCase
42 42 organization.add_admin(person_with_usp_id)
43 43  
44 44 login_as(person_with_usp_id.identifier)
45   - get :select_address_book, :profile => organization.identifier
  45 + get :invite_friends, :profile => organization.identifier
46 46 assert_response 200
47 47 get :select_friends, :profile => organization.identifier, :contact_list => ContactList.create.id
48 48 assert_response 200
49 49 end
50 50  
  51 + should 'search friends profiles by usp_id' do
  52 + person1 = User.create!(:login => 'john', :email => 'john@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:usp_id => 12345678}).person
  53 + User.create!(:login => 'mary', :email => 'mary@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:usp_id => 11111111}).person
  54 + organization = fast_create(Organization)
  55 + organization.add_admin(person1)
  56 +
  57 + login_as(person1.identifier)
  58 + get :search_friend, :profile => organization.identifier, :q => '1234'
  59 +
  60 + assert_equal [{"name" => person1.name, "id" => person1.id}].to_json, @response.body
  61 + assert_response 200
  62 + end
51 63 end
52 64  
... ...
test/functional/invite_controller_test.rb
... ... @@ -99,7 +99,7 @@ class InviteControllerTest &lt; ActionController::TestCase
99 99 end
100 100  
101 101 should 'display invitation page' do
102   - get :select_address_book, :profile => profile.identifier
  102 + get :invite_friends, :profile => profile.identifier
103 103 assert_response :success
104 104 assert_tag :tag => 'h1', :content => 'Invite your friends'
105 105 end
... ... @@ -118,8 +118,8 @@ class InviteControllerTest &lt; ActionController::TestCase
118 118 assert_equal InviteFriend.mail_template, assigns(:mail_template)
119 119 end
120 120  
121   - should 'deny select_address_book f user has no rights to invite members' do
122   - get :select_address_book, :profile => community.identifier
  121 + should 'deny invite_friends if user has no rights to invite members' do
  122 + get :invite_friends, :profile => community.identifier
123 123 assert_response 403 # forbidden
124 124 end
125 125  
... ... @@ -128,13 +128,13 @@ class InviteControllerTest &lt; ActionController::TestCase
128 128 assert_response 403 # forbidden
129 129 end
130 130  
131   - should 'deny select_address_book access when trying to invite friends to another user' do
132   - get :select_address_book, :profile => friend.identifier
  131 + should 'deny invite_friends access when trying to invite friends to another user' do
  132 + get :invite_friends, :profile => friend.identifier
133 133 assert_response 403 # forbidden
134 134 end
135 135  
136 136 should 'deny select_friends access when trying to invite friends to another user' do
137   - get :select_address_book, :profile => friend.identifier
  137 + get :select_friends, :profile => friend.identifier
138 138 assert_response 403 # forbidden
139 139 end
140 140  
... ... @@ -155,7 +155,7 @@ class InviteControllerTest &lt; ActionController::TestCase
155 155 community.add_admin(profile)
156 156 contact_list = ContactList.create
157 157 assert_difference 'Delayed::Job.count', 1 do
158   - post :select_address_book, :profile => community.identifier, :contact_list => contact_list.id, :import_from => 'gmail'
  158 + post :invite_friends, :profile => community.identifier, :contact_list => contact_list.id, :import_from => 'gmail'
159 159 end
160 160 end
161 161  
... ... @@ -223,7 +223,7 @@ class InviteControllerTest &lt; ActionController::TestCase
223 223 assert_difference 'ContactList.count', -1 do
224 224 get :cancel_fetching_emails, :profile => profile.identifier, :contact_list => contact_list.id
225 225 end
226   - assert_redirected_to :action => 'select_address_book'
  226 + assert_redirected_to :action => 'invite_friends'
227 227 end
228 228  
229 229 should 'set locale in the background job' do
... ... @@ -235,6 +235,79 @@ class InviteControllerTest &lt; ActionController::TestCase
235 235 assert_equal 'pt', job.payload_object.locale
236 236 end
237 237  
  238 + should 'search friends profiles by name, email or identifier' do
  239 + friend1 = create_user('willy').person
  240 + friend2 = create_user('william').person
  241 + friend1.name = 'cris'
  242 + friend2.email = 'me@example.com'
  243 + friend1.save
  244 + friend2.save
  245 +
  246 + get :search_friend, :profile => profile.identifier, :q => 'me@'
  247 +
  248 + assert_equal 'text/html', @response.content_type
  249 + assert_equal [{"name" => friend2.name, "id" => friend2.id}].to_json, @response.body
  250 +
  251 + get :search_friend, :profile => profile.identifier, :q => 'cri'
  252 +
  253 + assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body
  254 +
  255 + get :search_friend, :profile => profile.identifier, :q => 'will'
  256 +
  257 + assert_equal [{"name" => friend1.name, "id" => friend1.id}, {"name" => friend2.name, "id" => friend2.id}].to_json, @response.body
  258 + end
  259 +
  260 + should 'not include members in search friends profiles' do
  261 + community.add_admin(profile)
  262 + friend1 = create_user('willy').person
  263 + friend2 = create_user('william').person
  264 + friend1.save
  265 + friend2.save
  266 +
  267 + community.add_member(friend2)
  268 +
  269 + get :search_friend, :profile => community.identifier, :q => 'will'
  270 +
  271 + assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body
  272 + end
  273 +
  274 + should 'search friends profiles by fields provided by plugins' do
  275 + class Plugin1 < Noosfero::Plugin
  276 + def search_friend_fields
  277 + [{:field => 'nickname'}, {:field => 'contact_phone'}]
  278 + end
  279 + end
  280 +
  281 + environment = Environment.default
  282 + environment.enable_plugin(Plugin1)
  283 +
  284 + friend1 = create_user('harry').person
  285 + friend2 = create_user('william').person
  286 + friend1.nickname = 'prince'
  287 + friend2.contact_phone = '2222'
  288 + friend1.save
  289 + friend2.save
  290 +
  291 + get :search_friend, :profile => profile.identifier, :q => 'prince'
  292 + assert_equal [{"name" => friend1.name, "id" => friend1.id}].to_json, @response.body
  293 +
  294 + get :search_friend, :profile => profile.identifier, :q => '222'
  295 + assert_equal [{"name" => friend2.name, "id" => friend2.id}].to_json, @response.body
  296 + end
  297 +
  298 + should 'invite registered users through profile id' do
  299 + friend1 = create_user('testuser1').person
  300 + friend2 = create_user('testuser2').person
  301 + assert_difference Delayed::Job, :count, 1 do
  302 + post :invite_registered_friend, :profile => profile.identifier, :q => "#{friend1.id},#{friend2.id}", :mail_template => "click: <url>"
  303 + assert_redirected_to :controller => 'profile', :action => 'friends'
  304 + end
  305 +
  306 + assert_difference InviteFriend, :count, 2 do
  307 + process_delayed_job_queue
  308 + end
  309 + end
  310 +
238 311 private
239 312  
240 313 def json_response
... ...
test/integration/routing_test.rb
... ... @@ -213,7 +213,7 @@ class RoutingTest &lt; ActionController::IntegrationTest
213 213 end
214 214  
215 215 def test_invite_routing
216   - assert_routing('/profile/colivre/invite/friends', :controller => 'invite', :action => 'select_address_book', :profile => 'colivre')
  216 + assert_routing('/profile/colivre/invite/friends', :controller => 'invite', :action => 'invite_friends', :profile => 'colivre')
217 217 end
218 218  
219 219 def test_chat_routing
... ...
test/unit/invitation_test.rb
... ... @@ -72,6 +72,29 @@ class InvitationTest &lt; ActiveSupport::TestCase
72 72 end
73 73 end
74 74  
  75 + should 'do nothing if the invited friend is already your friend' do
  76 + person = create_user('person').person
  77 + invited_friend = create_user('invited_friend').person
  78 +
  79 + invited_friend.add_friend(person)
  80 +
  81 + assert_no_difference InviteFriend, :count do
  82 + Invitation.invite( person, [invited_friend.user.email], "", person )
  83 + end
  84 + end
  85 +
  86 + should 'and yet be able to invite friends to community' do
  87 + person = create_user('person').person
  88 + invited_friend = create_user('invited_friend').person
  89 +
  90 + invited_friend.add_friend(person)
  91 + community = fast_create(Community)
  92 +
  93 + assert_difference InviteMember, :count do
  94 + Invitation.invite( person, [invited_friend.user.email], "", community )
  95 + end
  96 + end
  97 +
75 98 should 'add url on message if user removed it' do
76 99 person = create_user('testuser1').person
77 100 friend = create_user('testuser2').person
... ... @@ -97,4 +120,37 @@ class InvitationTest &lt; ActiveSupport::TestCase
97 120 should 'have a message with url' do
98 121 assert_equal "\n\nTo accept invitation, please follow this link: <url>", Invitation.default_message_to_accept_invitation
99 122 end
  123 +
  124 + should 'invite friends through profile id' do
  125 + person = create_user('testuser1').person
  126 + friend = create_user('testuser2').person
  127 + community = fast_create(Community)
  128 +
  129 + assert_difference InviteMember, :count do
  130 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  131 + end
  132 + assert_difference InviteFriend, :count do
  133 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  134 + end
  135 + end
  136 +
  137 + should 'not invite friends if there is a pending invitation' do
  138 + person = create_user('testuser1').person
  139 + friend = create_user('testuser2').person
  140 + community = fast_create(Community)
  141 +
  142 + assert_difference InviteMember, :count do
  143 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  144 + end
  145 + assert_difference InviteFriend, :count do
  146 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  147 + end
  148 +
  149 + assert_no_difference InviteMember, :count do
  150 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)
  151 + end
  152 + assert_no_difference InviteFriend, :count do
  153 + Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)
  154 + end
  155 + end
100 156 end
... ...
test/unit/invite_member_test.rb
... ... @@ -22,6 +22,20 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
22 22 ok('friend is member of community') { community.members.include?(friend) }
23 23 end
24 24  
  25 + should 'cancel other invitations for same community when confirmed' do
  26 + friend = create_user('friend').person
  27 + p1 = create_user('testuser1').person
  28 + p2 = create_user('testuser2').person
  29 + community = fast_create(Community)
  30 +
  31 + task = InviteMember.create!(:person => p1, :friend => friend, :community_id => community.id)
  32 + InviteMember.create!(:person => p2, :friend => friend, :community_id => community.id)
  33 +
  34 + assert_difference friend.tasks.pending, :count, -2 do
  35 + task.finish
  36 + end
  37 + end
  38 +
25 39 should 'require community (person inviting other to be a member)' do
26 40 task = InviteMember.new
27 41 task.valid?
... ... @@ -78,11 +92,11 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
78 92 task = InviteMember.create!(:person => p1, :friend_email => 'test@test.com', :message => '<url>', :community_id => fast_create(Community).id)
79 93 end
80 94  
81   - should 'not send e-mails to friend if target given (person being invited)' do
  95 + should 'send e-mails notification to friend if target given (person being invited)' do
82 96 p1 = create_user('testuser1').person
83 97 p2 = create_user('testuser2').person
84 98  
85   - TaskMailer.expects(:deliver_invitation_notification).never
  99 + TaskMailer.expects(:deliver_target_notification).once
86 100  
87 101 task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
88 102 end
... ... @@ -117,7 +131,8 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
117 131 assert_match(/#{task.requestor.name} invited you to join #{community.name}/, email.subject)
118 132 end
119 133  
120   - should 'destroy InviteMember task when the community is destroyed' do
  134 +<<<<<<< HEAD
  135 + should 'destroy InviteMember task when the community is destroyed' do
121 136 p1 = create_user('testuser1').person
122 137 p2 = create_user('testuser2').person
123 138 p3 = create_user('testuser3').person
... ... @@ -131,5 +146,28 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
131 146 assert_raise ActiveRecord::RecordNotFound do; t2.reload; end
132 147 end
133 148  
134   -end
  149 + should 'have target notification message only if target given (person being invited)' do
  150 + p1 = create_user('testuser1').person
  151 + p2 = create_user('testuser2').person
  152 +
  153 + task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
  154 + assert_nothing_raised NotImplementedError do
  155 + task.target_notification_message
  156 + end
  157 +
  158 + task = InviteMember.create!(:person => p1, :friend_email => 'test@test.com', :message => '<url>', :community_id => fast_create(Community).id)
  159 + assert_raise NotImplementedError do
  160 + task.target_notification_message
  161 + end
  162 + end
135 163  
  164 + should 'deliver target notification message if target given (person being invited)' do
  165 + p1 = create_user('testuser1').person
  166 + p2 = create_user('testuser2').person
  167 +
  168 + task = InviteMember.create!(:person => p1, :friend => p2, :community_id => fast_create(Community).id)
  169 +
  170 + email = TaskMailer.deliver_target_notification(task, task.target_notification_message)
  171 + assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject)
  172 + end
  173 +end
... ...