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

users_controller_spec.rb « controllers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffeb120ffc7ac0ecf51c2fb2580bd50becd04e01 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

describe UsersController, :type => :controller do
  include_context :gon
  include PostsHelper

  before do
    @user = alice
    sign_in @user, scope: :user
    allow(@controller).to receive(:current_user).and_return(@user)
  end

  describe '#export_profile' do
    it 'queues an export job' do
      expect(@user).to receive :queue_export
      post :export_profile
      expect(request.flash[:notice]).to eql(I18n.t('users.edit.export_in_progress'))
      expect(response).to redirect_to(edit_user_path)
    end
  end

  describe "#download_profile" do
    it "downloads a user's export file" do
      @user.perform_export!
      get :download_profile
      expect(response).to redirect_to(@user.export.url)
    end
  end

  describe '#export_photos' do
    it 'queues an export photos job' do
      expect(@user).to receive :queue_export_photos
      post :export_photos
      expect(request.flash[:notice]).to eql(I18n.t('users.edit.export_photos_in_progress'))
      expect(response).to redirect_to(edit_user_path)
    end
  end

  describe '#download_photos' do
    it "redirects to user's photos zip file"  do
      @user.perform_export_photos!
      get :download_photos
      expect(response).to redirect_to(@user.exported_photos_file.url)
    end
  end

  describe '#public' do
    context "entry xml contents" do
      before do
        @sm = FactoryGirl.create(
          :status_message,
          public: true,
          author: @user.person,
          text:   "Go to http://diasporafoundation.org/ now!"
        )
      end

      it "contains the text" do
        get :public, params: {username: @user.username}, format: :atom
        doc = Nokogiri::XML(response.body)
        expect(doc.css("entry content")[0].content).to eq(@sm.message.markdownified(disable_hovercards: true))
      end

      it "contains the title" do
        get :public, params: {username: @user.username}, format: :atom
        doc = Nokogiri::XML(response.body)
        expect(doc.css("entry title")[0].content).to eq(post_page_title(@sm))
      end

      it "contains the author" do
        get :public, params: {username: @user.username}, format: :atom
        doc = Nokogiri::XML(response.body)
        expect(doc.css("entry author name")[0].content).to eq(@sm.author_name)
      end

      it "contains the original author for reshares" do
        FactoryGirl.create(:reshare, root: @sm, author: bob.person)
        get :public, params: {username: bob.username}, format: :atom
        doc = Nokogiri::XML(response.body)
        expect(doc.css("entry author name")[0].content).to eq(@sm.author_name)
      end
    end

    it 'includes reshares in the atom feed' do
      reshare = FactoryGirl.create(:reshare, :author => @user.person)
      get :public, params: {username: @user.username}, format: :atom
      expect(response.body).to include reshare.root.text
    end

    it 'do not show reshares in atom feed if origin post is deleted' do
      post = FactoryGirl.create(:status_message, :public => true);
      reshare = FactoryGirl.create(:reshare, :root => post, :author => @user.person)
      post.delete
      get :public, params: {username: @user.username}, format: :atom
      expect(response.code).to eq('200')
    end

    it 'redirects to a profile page if html is requested' do
      get :public, params: {username: @user.username}
      expect(response).to be_redirect
    end

    it 'redirects to a profile page if mobile is requested' do
      get :public, params: {username: @user.username}, format: :mobile
      expect(response).to be_redirect
    end
  end

  describe '#update' do
    before do
      @params  = { :id => @user.id,
                  :user => { :diaspora_handle => "notreal@stuff.com" } }
    end

    it "doesn't overwrite random attributes" do
      expect {
        put :update, params: @params
      }.not_to change(@user, :diaspora_handle)
    end

    it 'renders the user edit page' do
      put :update, params: @params
      expect(response).to render_template('edit')
    end

    describe 'password updates' do
      let(:password_params) do
        {:current_password => 'bluepin7',
         :password => "foobaz",
         :password_confirmation => "foobaz"}
      end

      let(:params) do
        {id: @user.id, user: password_params, change_password: 'Change Password'}
      end

      it "uses devise's update with password" do
        expect(@user).to receive(:update_with_password).with(hash_including(password_params))
        allow(@controller).to receive(:current_user).and_return(@user)
        put :update, params: params
      end
    end

    describe 'language' do
      it "allows the user to change their language" do
        old_language = 'en'
        @user.language = old_language
        @user.save
        put :update, params: {id: @user.id, user: {language: "fr"}}
        @user.reload
        expect(@user.language).not_to eq(old_language)
      end
    end

    describe "color_theme" do
      it "allows the user to change their color theme" do
        old_color_theme = "original"
        @user.color_theme = old_color_theme
        @user.save
        put :update, params: {id: @user.id, user: {color_theme: "dark_green"}}
        @user.reload
        expect(@user.color_theme).not_to eq(old_color_theme)
      end
    end

    describe 'email' do
      it "disallows the user to change their new (unconfirmed) mail when it is the same as the old" do
        @user.email = "my@newemail.com"
        put :update, params: {id: @user.id, user: {email: "my@newemail.com"}}
        @user.reload
        expect(@user.unconfirmed_email).to eql(nil)
      end

      it "allows the user to change their (unconfirmed) email" do
        put :update, params: {id: @user.id, user: {email: "my@newemail.com"}}
        @user.reload
        expect(@user.unconfirmed_email).to eql("my@newemail.com")
      end

      it 'informs the user about success' do
        put :update, params: {id: @user.id, user: {email: "my@newemail.com"}}
        expect(request.flash[:notice]).to eql(I18n.t('users.update.unconfirmed_email_changed'))
        expect(request.flash[:error]).to be_blank
      end

      it 'informs the user about failure' do
        put :update, params: {id: @user.id, user: {email: "mynewemailcom"}}
        expect(request.flash[:error]).to eql(I18n.t('users.update.unconfirmed_email_not_changed'))
        expect(request.flash[:notice]).to be_blank
      end

      it "allow the user to change their (unconfirmed) email to blank (= abort confirmation)" do
        put :update, params: {id: @user.id, user: {email: ""}}
        @user.reload
        expect(@user.unconfirmed_email).to eql(nil)
      end

      it 'sends out activation email on success' do
        expect(Workers::Mail::ConfirmEmail).to receive(:perform_async).with(@user.id).once
        put :update, params: {id: @user.id, user: {email: "my@newemail.com"}}
      end

      it "saves unconfirmed_email when podmail is disabled" do
        AppConfig.mail.enable = false
        put :update, params: {id: @user.id, user: {email: "my@newemail.com"}}
        @user.reload
        expect(@user.email).to eql("my@newemail.com")
      end
    end

    describe "email settings" do
      UserPreference::VALID_EMAIL_TYPES.each do |email_type|
        context "for #{email_type}" do
          it "lets the user turn off mail" do
            par = {id: @user.id, user: {email_preferences: {email_type => "true"}}}
            expect {
              put :update, params: par
            }.to change(@user.user_preferences, :count).by(1)
          end

          it "lets the user get mail again" do
            @user.user_preferences.create(email_type: email_type)
            par = {id: @user.id, user: {email_preferences: {email_type => "false"}}}
            expect {
              put :update, params: par
            }.to change(@user.user_preferences, :count).by(-1)
          end
        end
      end
    end

    describe 'getting started' do
      it 'can be reenabled' do
        put :update, params: {user: {getting_started: true}}
        expect(@user.reload.getting_started?).to be true
      end
    end
  end

  describe '#privacy_settings' do
    it "returns a 200" do
      get 'privacy_settings'
      expect(response.status).to eq(200)
    end
  end

  describe '#edit' do
    it "returns a 200" do
      get :edit, params: {id: @user.id}
      expect(response.status).to eq(200)
    end

    it 'displays community spotlight checkbox' do
      AppConfig.settings.community_spotlight.enable = true
      get :edit, params: {id: @user.id}
      expect(response.body).to include('input name="user[show_community_spotlight_in_stream]"')
    end

    it 'hides community spotlight checkbox' do
      AppConfig.settings.community_spotlight = false
      get :edit, params: {id: @user.id}
      expect(response.body).not_to include('input name="user[show_community_spotlight_in_stream]"')
    end

    it 'set @email_pref to false when there is a user pref' do
      @user.user_preferences.create(:email_type => 'mentioned')
      get :edit, params: {id: @user.id}
      expect(assigns[:email_prefs]['mentioned']).to be false
    end

    it "does not allow token auth" do
      sign_out :user
      bob.reset_authentication_token!
      get :edit, params: {auth_token: bob.authentication_token}
      expect(response).to redirect_to new_user_session_path
    end
  end

  describe '#destroy' do
    it 'does nothing if the password does not match' do
      expect(Workers::DeleteAccount).not_to receive(:perform_async)
      delete :destroy, params: {user: {current_password: "stuff"}}
    end

    it 'closes the account' do
      expect(alice).to receive(:close_account!)
      delete :destroy, params: {user: {current_password: "bluepin7"}}
    end

    it 'enqueues a delete job' do
      expect(Workers::DeleteAccount).to receive(:perform_async).with(anything)
      delete :destroy, params: {user: {current_password: "bluepin7"}}
    end
  end

  describe '#confirm_email' do
    before do
      @user.update_attribute(:unconfirmed_email, 'my@newemail.com')
    end

    it 'redirects to to the user edit page' do
      get :confirm_email, params: {token: @user.confirm_email_token}
      expect(response).to redirect_to edit_user_path
    end

    it 'confirms email' do
      get :confirm_email, params: {token: @user.confirm_email_token}
      @user.reload
      expect(@user.email).to eql('my@newemail.com')
      expect(request.flash[:notice]).to eql(I18n.t('users.confirm_email.email_confirmed', :email => 'my@newemail.com'))
      expect(request.flash[:error]).to be_blank
    end

    it 'does NOT confirm email with wrong token' do
      get :confirm_email, params: {token: @user.confirm_email_token.reverse}
      @user.reload
      expect(@user.email).not_to eql('my@newemail.com')
      expect(request.flash[:error]).to eql(I18n.t('users.confirm_email.email_not_confirmed'))
      expect(request.flash[:notice]).to be_blank
    end
  end

  describe 'getting_started' do
    it 'does not fail miserably' do
      get :getting_started
      expect(response).to be_success
    end

    it 'does not fail miserably on mobile' do
      get :getting_started, format: :mobile
      expect(response).to be_success
    end

    context "with inviter" do
      it "preloads data using gon for the aspect memberships dropdown when sharing with the inviter" do
        alice.invited_by = bob
        get :getting_started
        expect_gon_preloads_for_aspect_membership_dropdown(:inviter, true)
      end

      it "preloads data using gon for the aspect memberships dropdown when not sharing with the inviter" do
        alice.invited_by = eve
        get :getting_started
        expect_gon_preloads_for_aspect_membership_dropdown(:inviter, false)
      end
    end
  end
end