From 11505a386c9b58403a9c2ac3726f9d15035bd0af Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 16 Mar 2012 14:00:57 -0700 Subject: [PATCH] adding a test for decremented invites, as, and check the invite is valid in the controller --- app/controllers/invitations_controller.rb | 2 ++ app/controllers/registrations_controller.rb | 10 ++++++++-- app/models/invitation_code.rb | 4 ++++ app/models/user.rb | 2 +- app/views/services/_remote_friend.html.haml | 5 +---- config/locales/diaspora/en.yml | 1 + features/accepts_invitation.feature | 6 +++++- features/step_definitions/user_steps.rb | 8 ++++---- 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 560b9aeb7..2d66b9f6a 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -18,6 +18,8 @@ class InvitationsController < ApplicationController # this is for legacy invites. We try to look the person who sent them the # invite, and use their new invite code + # owe will be removing this eventually + # @depreciated def edit user = User.find_by_invitation_token(params[:invitation_token]) invitation_code = user.ugly_accept_invitation_code diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index d55cbb2ec..544531a6a 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -3,7 +3,7 @@ # the COPYRIGHT file. class RegistrationsController < Devise::RegistrationsController - before_filter :check_registrations_open_or_vaild_invite! + before_filter :check_registrations_open_or_vaild_invite!, :check_valid_invite! def create @user = User.build(params[:user]) @@ -16,7 +16,7 @@ class RegistrationsController < Devise::RegistrationsController Rails.logger.info("event=registration status=successful user=#{@user.diaspora_handle}") else @user.errors.delete(:person) - + flash[:error] = @user.errors.full_messages.join(";") Rails.logger.info("event=registration status=failure errors='#{@user.errors.full_messages.join(', ')}'") render :new @@ -28,6 +28,12 @@ class RegistrationsController < Devise::RegistrationsController end private + def check_valid_invite! + return true if invite.can_be_used? + flash[:error] = t('registrations.invalid_invite') + redirect_to new_user_session_path + end + def check_registrations_open_or_vaild_invite! return true if invite.present? if AppConfig[:registrations_closed] diff --git a/app/models/invitation_code.rb b/app/models/invitation_code.rb index 33e259642..e653648ce 100644 --- a/app/models/invitation_code.rb +++ b/app/models/invitation_code.rb @@ -9,6 +9,10 @@ class InvitationCode < ActiveRecord::Base token end + def can_be_used? + self.count > 0 + end + def add_invites! self.update_attributes(:count => self.count+100) end diff --git a/app/models/user.rb b/app/models/user.rb index 1f7c9e5a1..6d7abfb7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -107,7 +107,7 @@ class User < ActiveRecord::Base ConversationVisibility.sum(:unread, :conditions => "person_id = #{self.person.id}") end - #should be deprecated + #@deprecated def ugly_accept_invitation_code begin self.invitations_to_me.first.sender.invitation_code diff --git a/app/views/services/_remote_friend.html.haml b/app/views/services/_remote_friend.html.haml index 36e74627e..dd6dfcdcd 100644 --- a/app/views/services/_remote_friend.html.haml +++ b/app/views/services/_remote_friend.html.haml @@ -1,9 +1,6 @@ .stream_element{:id => friend.id} .float-right - - - if friend.already_invited? - = link_to t('.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id), :class => 'button resend' - - elsif friend.on_diaspora? + - if friend.on_diaspora? = aspect_membership_dropdown(contact_proxy(friend), friend.person, 'left') - else = render 'shared/aspect_dropdown', :selected_aspects => contact_proxy(friend).aspects, :person => friend.person, :hang => 'left', :dropdown_class => 'inviter', :service_uid => friend.uid diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index a0eb0adde..f2db7e70e 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -682,6 +682,7 @@ en: update: "Update" cancel_my_account: "Cancel my account" closed: "Signups are closed on this Diaspora pod." + invalid_invite: "The invite link you provided is no longer valid!" requests: manage_aspect_contacts: diff --git a/features/accepts_invitation.feature b/features/accepts_invitation.feature index 70d9dccad..440574e6c 100644 --- a/features/accepts_invitation.feature +++ b/features/accepts_invitation.feature @@ -19,7 +19,7 @@ Feature: invitation acceptance Then I should be on the stream page Scenario: accept invitation from user - Given I have been invited by a user + Given I have been invited by bob And I am on my acceptance form page And I fill in the following: | user_username | ohai | @@ -35,6 +35,10 @@ Feature: invitation acceptance And I preemptively confirm the alert And I follow "awesome_button" Then I should be on the stream page + And I log out + And I sign in as "bob@bob.bob" + And I follow "By email" + Then I should see "9 invites left" Scenario: sends an invitation Given a user with email "bob@bob.bob" diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index 2dbf8c281..1a042f010 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -27,13 +27,13 @@ end Given /^I have been invited by an admin$/ do admin = Factory(:user) - bob.invitation_code - i = EmailInviter.new("new_invitee@example.com", bob) + admin.invitation_code + i = EmailInviter.new("new_invitee@example.com", admin) i.send! end -Given /^I have been invited by a user$/ do - @inviter = Factory(:user) +Given /^I have been invited by bob$/ do + @inviter = Factory(:user, :email => 'bob@bob.bob') i = EmailInviter.new("new_invitee@example.com", @inviter) i.send! end