Welcome to mirror list, hosted at ThFree Co, Russian Federation.

invitations_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58855a55092fe3fc2e56a0efb0759843b3fcd341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe InvitationsController do
  include Devise::TestHelpers

  before do
    @user   = alice
    @aspect = @user.aspects.first

    request.env["devise.mapping"] = Devise.mappings[:user]
    Webfinger.stub_chain(:new, :fetch).and_return(Factory(:person))
  end

  describe "#create" do
    before do
      @user.invites = 5

      sign_in :user, @user
      @invite = {:invite_message=>"test", :aspect_id=> @aspect.id.to_s, :email=>"abc@example.com"}
      @controller.stub!(:current_user).and_return(@user)
      request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
    end

    it 'calls the resque job Job::InviteUser'  do
      Resque.should_receive(:enqueue)
      post :create,  :user => @invite
    end

    it 'handles a comma seperated list of emails' do
      Resque.should_receive(:enqueue).twice()
      post :create, :user => @invite.merge(
        :email => "foofoofoofoo@example.com, mbs@gmail.com")
    end

    it 'handles a comma seperated list of emails with whitespace' do
      Resque.should_receive(:enqueue).twice()
      post :create, :user => @invite.merge(
        :email => "foofoofoofoo@example.com   ,        mbs@gmail.com")
    end

    it 'displays a message that tells the user how many invites were sent, and which REJECTED' do
      post :create, :user => @invite.merge(
        :email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
      flash[:error].should_not be_empty
      flash[:error].should =~ /foo\.com/
      flash[:error].should =~ /lala@foo/
    end

    it "doesn't invite anyone if you have 0 invites" do
      @user.invites = 0
      @user.save!

      Resque.should_not_receive(:enqueue)
      post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
    end

    it "allows invitations without limit if invitations are open" do
      open_bit = AppConfig[:open_invitations]
      AppConfig[:open_invitations] = true
      @user.invites = 0
      @user.save!

      Resque.should_receive(:enqueue).once
      post :create, :user => @invite

      AppConfig[:open_invitations] = open_bit
    end

    it 'returns to the previous page on success' do
      post :create, :user => @invite
      response.should redirect_to("http://test.host/cats/foo")
    end

    it 'strips out your own email' do
      lambda {
        post :create, :user => @invite.merge(:email => @user.email)
      }.should_not change(User, :count)

      Resque.should_receive(:enqueue).once
      post :create, :user => @invite.merge(:email => "hello@example.org, #{@user.email}")
    end
  end

  describe "#update" do
    before do
      @user.invites = 5
      @invited_user = @user.invite_user(@aspect.id, 'email', "a@a.com")
      @accept_params = {:user=>
        {:password_confirmation =>"password",
         :email => "a@a.com", 
         :username=>"josh",
         :password=>"password",
         :invitation_token => @invited_user.invitation_token}}

    end

    context 'success' do
      let(:invited) {User.find_by_username(@accept_params[:user][:username])}

      it 'creates a user' do
        put :update, @accept_params
        invited.should_not be_nil
      end

      it 'seeds the aspects' do
        put :update, @accept_params
        invited.aspects.count.should == 4
      end

      it 'adds a contact' do
        lambda { 
          put :update, @accept_params
        }.should change(@user.contacts, :count).by(1)
      end
    end

    context 'failure' do
      before do
        @fail_params = @accept_params
        @fail_params[:user][:username] = @user.username
      end

      it 'stays on the invitation accept form' do
        put :update, @fail_params
        response.location.include?(accept_user_invitation_path).should be_true
      end

      it 'keeps the invitation token' do
        put :update, @fail_params
        response.location.include?("invitation_token=#{@invited_user.invitation_token}").should be_true
      end
    end
  end

  describe '#new' do
    it 'renders' do
      sign_in :user, @user
      get :new
    end
  end

  describe '#resend' do
    before do
      @user.invites = 5

      sign_in :user, @user
      @controller.stub!(:current_user).and_return(@user)
      request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'

      @invited_user = @user.invite_user(@aspect.id, 'email', "a@a.com", "")
    end

    it 'calls resend invitation if one exists' do
      @user.reload.invitations_from_me.count.should == 1
      invitation = @user.invitations_from_me.first
      Resque.should_receive(:enqueue)
      put :resend, :id => invitation.id
    end

    it 'does not send an invitation for a different user' do
      @user2 = bob
      @aspect2 = @user2.aspects.create(:name => "cats")
      @user2.invite_user(@aspect2.id, 'email', "b@b.com", "")
      invitation2 = @user2.reload.invitations_from_me.first
      Resque.should_not_receive(:enqueue)
      put :resend, :id => invitation2.id 
    end
  end
end