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

services_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7674e67530fda6ea66dd449187885aecb3d6186 (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
#   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 ServicesController do
  let(:mock_access_token) { Object.new }

  let(:omniauth_auth) {
    { 'provider' => 'twitter',
      'uid'      => '2',
      'user_info'   => { 'nickname' => 'grimmin' },
      'credentials' => { 'token' => 'tokin', 'secret' =>"not_so_much" }
      }
  }

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

    sign_in :user, @user
    @controller.stub!(:current_user).and_return(@user)
    mock_access_token.stub!(:token => "12345", :secret => "56789")
  end

  describe '#index' do
    it 'displays all connected serivices for a user' do
      4.times do
        Factory(:service, :user => @user)
      end

      get :index
      assigns[:services].should == @user.services
    end
  end

  describe '#create' do
    it 'creates a new OmniauthService' do
      request.env['omniauth.auth'] = omniauth_auth
      lambda{
        post :create, :provider => 'twitter'
      }.should change(@user.services, :count).by(1)
    end

    it 'redirects to getting started if the user is getting started' do
      @user.getting_started = true
      request.env['omniauth.auth'] = omniauth_auth
      post :create, :provider => 'twitter'
      response.should redirect_to getting_started_path(:step => 3)
    end

    it 'redirects to services url' do
      @user.getting_started = false
      request.env['omniauth.auth'] = omniauth_auth
      post :create, :provider => 'twitter'
      response.should redirect_to services_url
    end

    it 'creates a twitter service' do
      Service.delete_all
      @user.getting_started = false
      request.env['omniauth.auth'] = omniauth_auth
      post :create, :provider => 'twitter'
      @user.reload.services.first.class.name.should == "Services::Twitter"
    end
  end

  describe '#destroy' do
    before do
      @service1 = Factory.create(:service, :user => @user)
    end

    it 'destroys a service selected by id' do
      lambda{
        delete :destroy, :id => @service1.id
      }.should change(@user.services, :count).by(-1)
    end
  end

  describe '#finder' do
    before do
      @service1 = Services::Facebook.new
      @user.services << @service1
      @person = Factory(:person)
      @user.services.stub!(:where).and_return([@service1])
      @service_users = [ ServiceUser.create(:contact => @user.contact_for(bob.person), :name => "Robert Bobson", :photo_url => "cdn1.fb.com/pic1.jpg",
                                  :service => @service1, :uid => "321" ).tap{|su| su.stub!(:person).and_return(bob.person)},
                ServiceUser.create(:name => "Eve Doe", :photo_url => "cdn1.fb.com/pic1.jpg", :person => eve.person, :service => @service1,
                                   :uid => 'sdfae').tap{|su| su.stub!(:person).and_return(eve.person)},
                ServiceUser.create(:name => "Robert Bobson", :photo_url => "cdn1.fb.com/pic1.jpg", :service => @service1, :uid => "dsfasdfas")]
      @service1.should_receive(:finder).and_return(@service_users)
    end

    it 'calls the finder method for the service for that user' do
      get :finder, :provider => @service1.provider
      response.should be_success
    end

    it 'has no translations missing' do
      get :finder, :provider => @service1.provider
      response.body.match(/translation/).should be_nil
    end
  end

  describe '#inviter' do
    before do
      @uid = "abc"
      fb = Factory(:service, :type => "Services::Facebook", :user => @user)
      fb = Services::Facebook.find(fb.id)
      @su = Factory(:service_user, :service => fb, :uid => @uid)
      @invite_params = {:provider => 'facebook', :uid => @uid, :aspect_id => @user.aspects.first.id}
    end

    it 'enqueues an invite job if the fb user has a username' do
      pending
      @invite_params[:provider] = "email"
      @invite_params[:uid] = "username@facebook.com"
      put :inviter, @invite_params
    end

    it 'sets the subject' do
      put :inviter, @invite_params
      assigns[:subject].should_not be_nil
    end

    it 'sets a message containing the invitation link' do
      put :inviter, @invite_params
      assigns[:message].should include(User.last.invitation_token)
    end

    it 'redirects to a prefilled facebook message url' do
      put :inviter, @invite_params
      response.location.should match(/https:\/\/www\.facebook\.com\/\?compose=1&id=.*&subject=.*&message=.*&sk=messages/)
    end

    it 'creates an invitation' do
      lambda {
        put :inviter, @invite_params
      }.should change(Invitation, :count).by(1)
    end

    it 'sets the invitation_id on the service_user' do
      post :inviter, @invite_params
      @su.reload.invitation.should_not be_nil
    end

    it 'does not create a duplicate invitation' do
      inv = Invitation.create!(:sender_id => @user.id, :recipient_id => eve.id, :aspect_id => @user.aspects.first.id)
      @invite_params[:invitation_id] = inv.id

      lambda {
        put :inviter, @invite_params
      }.should_not change(Invitation, :count)
    end

    it 'disregares the amount of invites if open_invitations are enabled' do
      open_bit = AppConfig[:open_invitations]
      AppConfig[:open_invitations] = true

      lambda {
        put :inviter, @invite_params
      }.should change(Invitation, :count).by(1)
      AppConfig[:open_invitations] = open_bit
    end
  end
end