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
|
# 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 InvitationsController, type: :controller do
describe "#create" do
let(:referer) { "http://test.host/cats/foo" }
let(:invite_params) { {email_inviter: {emails: "abc@example.com"}} }
before do
sign_in alice, scope: :user
request.env["HTTP_REFERER"] = referer
end
context "no emails" do
let(:invite_params) { {email_inviter: {emails: ""}} }
it "does not create an EmailInviter" do
expect(Workers::Mail::InviteEmail).not_to receive(:perform_async)
post :create, params: invite_params
end
it "returns to the previous page" do
post :create, params: invite_params
expect(response).to redirect_to referer
end
it "flashes an error" do
post :create, params: invite_params
expect(flash[:error]).to eq(I18n.t("invitations.create.empty"))
end
end
context "only valid emails" do
let(:emails) { "mbs@gmail.com" }
let(:invite_params) { {email_inviter: {emails: emails}} }
it "creates an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(
emails, alice.id, invite_params[:email_inviter]
)
post :create, params: invite_params
end
it "returns to the previous page on success" do
post :create, params: invite_params
expect(response).to redirect_to referer
end
it "flashes a notice" do
post :create, params: invite_params
expected = I18n.t("invitations.create.sent", emails: emails)
expect(flash[:notice]).to eq(expected)
end
end
context "only invalid emails" do
let(:emails) { "invalid_email" }
let(:invite_params) { {email_inviter: {emails: emails}} }
it "does not create an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).not_to receive(:perform_async)
post :create, params: invite_params
end
it "returns to the previous page" do
post :create, params: invite_params
expect(response).to redirect_to referer
end
it "flashes an error" do
post :create, params: invite_params
expected = I18n.t("invitations.create.rejected", emails: emails)
expect(flash[:error]).to eq(expected)
end
end
context "mixed valid and invalid emails" do
let(:valid_emails) { "foo@bar.com,mbs@gmail.com" }
let(:invalid_emails) { "invalid_email" }
let(:invite_params) { {email_inviter: {emails: valid_emails + "," + invalid_emails}} }
it "creates an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(
valid_emails, alice.id, invite_params[:email_inviter]
)
post :create, params: invite_params
end
it "returns to the previous page" do
post :create, params: invite_params
expect(response).to redirect_to referer
end
it "flashes a notice" do
post :create, params: invite_params
expected = I18n.t("invitations.create.sent", emails: valid_emails.split(",").join(", ")) + ". " +
I18n.t("invitations.create.rejected", emails: invalid_emails)
expect(flash[:error]).to eq(expected)
end
end
context "with registration disabled" do
before do
AppConfig.settings.enable_registrations = false
end
it "displays an error if invitations are closed" do
AppConfig.settings.invitations.open = false
post :create, params: invite_params
expect(flash[:error]).to eq(I18n.t("invitations.create.closed"))
end
it "displays an error when no invitations are left" do
alice.invitation_code.update_attributes(count: 0)
post :create, params: invite_params
expect(flash[:error]).to eq(I18n.t("invitations.create.no_more"))
end
end
it "does not display an error when registration is open" do
AppConfig.settings.invitations.open = false
alice.invitation_code.update_attributes(count: 0)
post :create, params: invite_params
expect(flash[:error]).to be_nil
end
end
describe '#new' do
it 'renders' do
sign_in alice, scope: :user
get :new
end
end
describe 'redirect logged out users to the sign in page' do
it 'redriects #new' do
get :new
expect(response).to be_redirect
expect(response).to redirect_to new_user_session_path
end
it 'redirects #create' do
post :create
expect(response).to be_redirect
expect(response).to redirect_to new_user_session_path
end
end
describe '.valid_email?' do
it 'returns false for empty email' do
expect(subject.send(:valid_email?, '')).to be false
end
it 'returns false for email without @-sign' do
expect(subject.send(:valid_email?, 'foo')).to be false
end
it 'returns true for valid email' do
expect(subject.send(:valid_email?, 'foo@bar.com')).to be true
end
end
describe '.html_safe_string_from_session_array' do
it 'returns "" for blank session[key]' do
expect(subject.send(:html_safe_string_from_session_array, :blank)).to eq ""
end
it 'returns "" if session[key] is not an array' do
session[:test_key] = "test"
expect(subject.send(:html_safe_string_from_session_array, :test_key)).to eq ""
end
it 'returns the correct value' do
session[:test_key] = ["test", "foo"]
expect(subject.send(:html_safe_string_from_session_array, :test_key)).to eq "test, foo"
end
it 'sets session[key] to nil' do
session[:test_key] = ["test"]
subject.send(:html_safe_string_from_session_array, :test_key)
expect(session[:test_key]).to be nil
end
end
end
|