2019-04-29 20:27:42 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2018-05-08 10:42:58 -04:00
|
|
|
|
describe InvitesController do
|
2020-06-09 11:19:32 -04:00
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:user) { Fabricate(:user, trust_level: SiteSetting.min_trust_level_to_allow_invite) }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context '#show' do
|
2019-05-06 23:12:20 -04:00
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
2018-05-08 10:42:58 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'shows the accept invite page' do
|
2020-10-24 11:51:01 -04:00
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.status).to eq(200)
|
2022-06-20 10:33:05 -04:00
|
|
|
|
expect(response.body).to have_tag(:script, with: { src: "/assets/discourse.js" })
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.body).not_to include(invite.email)
|
|
|
|
|
expect(response.body).to_not include(I18n.t('invite.not_found_template', site_name: SiteSetting.title, base_url: Discourse.base_url))
|
2021-11-30 07:59:37 -05:00
|
|
|
|
|
|
|
|
|
expect(response.body).to have_tag('div#data-preloaded') do |element|
|
|
|
|
|
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
|
|
|
|
|
invite_info = JSON.parse(json['invite_info'])
|
|
|
|
|
expect(invite_info['username']).to eq('')
|
|
|
|
|
expect(invite_info['email']).to eq('i*****g@a***********e.ooo')
|
|
|
|
|
end
|
2020-10-24 11:51:01 -04:00
|
|
|
|
end
|
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
|
context 'when email data is present in authentication data' do
|
|
|
|
|
let(:store) { ActionDispatch::Session::CookieStore.new({}) }
|
|
|
|
|
let(:session_stub) { ActionDispatch::Request::Session.create(store, ActionDispatch::TestRequest.create, {}) }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
session_stub[:authentication] = { email: invite.email }
|
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:session).returns(session_stub)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'shows unobfuscated email' do
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.status).to eq(200)
|
2022-06-20 10:33:05 -04:00
|
|
|
|
expect(response.body).to have_tag(:script, with: { src: "/assets/discourse.js" })
|
2022-03-21 10:28:52 -04:00
|
|
|
|
expect(response.body).to include(invite.email)
|
|
|
|
|
expect(response.body).not_to include('i*****g@a***********e.ooo')
|
|
|
|
|
end
|
2020-09-02 06:24:49 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-29 07:03:19 -04:00
|
|
|
|
it 'shows default user fields' do
|
|
|
|
|
user_field = Fabricate(:user_field)
|
|
|
|
|
staged_user = Fabricate(:user, staged: true, email: invite.email)
|
|
|
|
|
staged_user.set_user_field(user_field.id, 'some value')
|
|
|
|
|
staged_user.save_custom_fields
|
|
|
|
|
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.body).to have_tag("div#data-preloaded") do |element|
|
|
|
|
|
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
|
|
|
|
|
invite_info = JSON.parse(json['invite_info'])
|
2021-07-11 17:57:38 -04:00
|
|
|
|
expect(invite_info['username']).to eq(staged_user.username)
|
2021-03-29 07:03:19 -04:00
|
|
|
|
expect(invite_info['user_fields'][user_field.id.to_s]).to eq('some value')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-26 04:47:44 -04:00
|
|
|
|
it 'includes token validity boolean' do
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.body).to have_tag("div#data-preloaded") do |element|
|
|
|
|
|
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
|
|
|
|
|
invite_info = JSON.parse(json['invite_info'])
|
|
|
|
|
expect(invite_info['email_verified_by_link']).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
get "/invites/#{invite.invite_key}?t=#{invite.email_token}"
|
|
|
|
|
expect(response.body).to have_tag("div#data-preloaded") do |element|
|
|
|
|
|
json = JSON.parse(element.current_scope.attribute('data-preloaded').value)
|
|
|
|
|
invite_info = JSON.parse(json['invite_info'])
|
|
|
|
|
expect(invite_info['email_verified_by_link']).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
describe 'logged in user viewing an invite' do
|
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
end
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
it "redeems the invite when user's email matches invite's email before redirecting to secured topic url" do
|
|
|
|
|
invite.update_columns(email: user.email)
|
|
|
|
|
group.add_owner(invite.invited_by)
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
secured_category = Fabricate(:category)
|
|
|
|
|
secured_category.permissions = { group.name => :full }
|
|
|
|
|
secured_category.save!
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
topic = Fabricate(:topic, category: secured_category)
|
|
|
|
|
TopicInvite.create!(invite: invite, topic: topic)
|
|
|
|
|
InvitedGroup.create!(invite: invite, group: group)
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect do
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
end.to change { InvitedUser.exists?(invite: invite, user: user) }.to(true)
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect(response).to redirect_to(topic.url)
|
|
|
|
|
expect(user.reload.groups).to include(group)
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect(Notification.exists?(user: user, notification_type: Notification.types[:invited_to_topic], topic: topic))
|
|
|
|
|
.to eq(true)
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect(Notification.exists?(user: invite.invited_by, notification_type: Notification.types[:invitee_accepted]))
|
|
|
|
|
.to eq(true)
|
|
|
|
|
end
|
2021-07-07 12:42:42 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
it "redeems the invite when user's email domain matches the domain an invite link is restricted to" do
|
|
|
|
|
invite.update!(email: nil, domain: 'discourse.org')
|
|
|
|
|
user.update!(email: "someguy@discourse.org")
|
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
TopicInvite.create!(invite: invite, topic: topic)
|
|
|
|
|
group.add_owner(invite.invited_by)
|
|
|
|
|
InvitedGroup.create!(invite: invite, group: group)
|
2022-02-09 10:22:30 -05:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect do
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
end.to change { InvitedUser.exists?(invite: invite, user: user) }.to(true)
|
2020-09-02 06:24:49 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect(response).to redirect_to(topic.url)
|
|
|
|
|
expect(user.reload.groups).to include(group)
|
|
|
|
|
end
|
2020-09-02 06:24:49 -04:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
it "redirects to root if a logged in user tries to view an invite link restricted to a certain domain but user's email domain does not match" do
|
|
|
|
|
user.update!(email: "someguy@discourse.com")
|
|
|
|
|
invite.update!(email: nil, domain: 'discourse.org')
|
2022-02-15 10:35:58 -05:00
|
|
|
|
|
2022-06-20 23:56:50 -04:00
|
|
|
|
expect { get "/invites/#{invite.invite_key}" }.to change { InvitedUser.count }.by(0)
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to("/")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "redirects to root if a tries to view an invite meant for a specific email that is not the user's" do
|
|
|
|
|
invite.update_columns(email: "notuseremail@discourse.org")
|
|
|
|
|
|
|
|
|
|
expect { get "/invites/#{invite.invite_key}" }.to change { InvitedUser.count }.by(0)
|
|
|
|
|
|
|
|
|
|
expect(response).to redirect_to("/")
|
|
|
|
|
end
|
2022-02-15 10:35:58 -05:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if invite does not exist' do
|
|
|
|
|
get '/invites/missing'
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.body).to_not have_tag(:script, with: { src: '/assets/application.js' })
|
|
|
|
|
expect(response.body).to include(I18n.t('invite.not_found', base_url: Discourse.base_url))
|
2018-05-08 10:42:58 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if invite expired' do
|
|
|
|
|
invite.update(expires_at: 1.day.ago)
|
2018-05-08 10:42:58 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
get "/invites/#{invite.invite_key}"
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.body).to_not have_tag(:script, with: { src: '/assets/application.js' })
|
|
|
|
|
expect(response.body).to include(I18n.t('invite.expired', base_url: Discourse.base_url))
|
2018-05-08 10:42:58 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'stores the invite key in the secure session if invite exists' do
|
2021-03-18 20:20:10 -04:00
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invite_key = read_secure_session['invite-key']
|
2021-03-18 20:20:10 -04:00
|
|
|
|
expect(invite_key).to eq(invite.invite_key)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'returns error if invite has already been redeemed' do
|
2021-04-02 04:11:07 -04:00
|
|
|
|
expect(invite.redeem).not_to eq(nil)
|
2018-05-08 10:42:58 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
get "/invites/#{invite.invite_key}"
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.body).to_not have_tag(:script, with: { src: '/assets/application.js' })
|
|
|
|
|
expect(response.body).to include(I18n.t('invite.not_found_template', site_name: SiteSetting.title, base_url: Discourse.base_url))
|
2021-04-02 04:11:07 -04:00
|
|
|
|
|
|
|
|
|
invite.update!(email: nil) # convert to email invite
|
|
|
|
|
|
|
|
|
|
get "/invites/#{invite.invite_key}"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.body).to_not have_tag(:script, with: { src: '/assets/application.js' })
|
|
|
|
|
expect(response.body).to include(I18n.t('invite.not_found_template_link', site_name: SiteSetting.title, base_url: Discourse.base_url))
|
2018-05-08 10:42:58 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context '#create' do
|
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
post '/invites.json', params: { email: 'test@example.com' }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'while logged in' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if you cannot invite to the forum' do
|
|
|
|
|
sign_in(Fabricate(:user))
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com' }
|
|
|
|
|
expect(response).to be_forbidden
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'invite to topic' do
|
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'works' do
|
|
|
|
|
sign_in(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', topic_id: topic.id }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.first['args'].first['invite_to_topic']).to be_falsey
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails when topic_id is invalid' do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', topic_id: -9999 }
|
|
|
|
|
expect(response.status).to eq(400)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
2022-02-16 11:35:02 -05:00
|
|
|
|
|
|
|
|
|
context 'topic is private' do
|
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
|
|
fab!(:secured_category) do |category|
|
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
category.permissions = { group.name => :full }
|
|
|
|
|
category.save!
|
|
|
|
|
category
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fab!(:topic) { Fabricate(:topic, category: secured_category) }
|
|
|
|
|
|
|
|
|
|
it 'does not work and returns a list of required groups' do
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', topic_id: topic.id }
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
expect(response.parsed_body["errors"]).to contain_exactly(I18n.t("invite.requires_groups", groups: group.name))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'does not work if user cannot edit groups' do
|
|
|
|
|
group.add(user)
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', topic_id: topic.id }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'invite to group' do
|
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
|
|
it 'works for admins' do
|
2020-06-09 11:19:32 -04:00
|
|
|
|
sign_in(admin)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
|
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', group_ids: [group.id] }
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Invite.find_by(email: 'test@example.com').invited_groups.count).to eq(1)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'works for group owners' do
|
|
|
|
|
sign_in(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', group_ids: [group.id] }
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Invite.find_by(email: 'test@example.com').invited_groups.count).to eq(1)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'works with multiple groups' do
|
2020-06-09 11:19:32 -04:00
|
|
|
|
sign_in(admin)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
group2 = Fabricate(:group)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', group_names: "#{group.name},#{group2.name}" }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Invite.find_by(email: 'test@example.com').invited_groups.count).to eq(2)
|
2020-06-09 11:19:32 -04:00
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails for group members' do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
group.add(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', group_ids: [group.id] }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails for other users' do
|
|
|
|
|
sign_in(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', group_ids: [group.id] }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
2020-06-09 21:29:28 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails to invite new user to a group-private topic' do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
private_category = Fabricate(:private_category, group: group)
|
|
|
|
|
group_private_topic = Fabricate(:topic, category: private_category)
|
2020-06-09 21:29:28 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { email: 'test@example.com', topic_id: group_private_topic.id }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'email invite' do
|
2022-05-10 11:45:43 -04:00
|
|
|
|
subject(:create_invite) { post '/invites.json', params: params }
|
|
|
|
|
|
|
|
|
|
let(:params) { { email: email } }
|
|
|
|
|
let(:email) { 'test@example.com' }
|
|
|
|
|
|
|
|
|
|
before do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
sign_in(user)
|
2022-05-10 11:45:43 -04:00
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
context 'when doing successive calls' do
|
|
|
|
|
let(:invite) { Invite.last }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
it 'creates invite once and updates it after' do
|
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :ok
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(1)
|
2021-05-12 06:06:39 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :ok
|
|
|
|
|
expect(response.parsed_body['id']).to eq(invite.id)
|
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
context 'when "skip_email" parameter is provided' do
|
|
|
|
|
before do
|
|
|
|
|
params[:skip_email] = true
|
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
it 'accepts the parameter' do
|
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :ok
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(0)
|
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
context 'when validations fail' do
|
|
|
|
|
let(:email) { 'test@mailinator.com' }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2022-05-10 11:45:43 -04:00
|
|
|
|
it 'fails' do
|
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :unprocessable_entity
|
|
|
|
|
expect(response.parsed_body['errors']).to be_present
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when providing an email belonging to an existing user' do
|
|
|
|
|
let(:email) { user.email }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
SiteSetting.hide_email_address_taken = hide_email_address_taken
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when "hide_email_address_taken" setting is disabled' do
|
|
|
|
|
let(:hide_email_address_taken) { false }
|
|
|
|
|
|
|
|
|
|
it 'returns an error' do
|
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :unprocessable_entity
|
|
|
|
|
expect(body).to match(/no need to invite/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when "hide_email_address_taken" setting is enabled' do
|
|
|
|
|
let(:hide_email_address_taken) { true }
|
|
|
|
|
|
|
|
|
|
it 'doesn’t inform the user' do
|
|
|
|
|
create_invite
|
|
|
|
|
expect(response).to have_http_status :ok
|
|
|
|
|
expect(response.parsed_body).to be_blank
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'link invite' do
|
|
|
|
|
it 'works' do
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
|
|
post '/invites.json'
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Invite.last.email).to eq(nil)
|
|
|
|
|
expect(Invite.last.invited_by).to eq(admin)
|
|
|
|
|
expect(Invite.last.max_redemptions_allowed).to eq(1)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if over invite_link_max_redemptions_limit' do
|
|
|
|
|
sign_in(admin)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { max_redemptions_allowed: SiteSetting.invite_link_max_redemptions_limit - 1 }
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { max_redemptions_allowed: SiteSetting.invite_link_max_redemptions_limit + 1 }
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if over invite_link_max_redemptions_limit_users' do
|
|
|
|
|
sign_in(user)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { max_redemptions_allowed: SiteSetting.invite_link_max_redemptions_limit_users - 1 }
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites.json', params: { max_redemptions_allowed: SiteSetting.invite_link_max_redemptions_limit_users + 1 }
|
|
|
|
|
expect(response.status).to eq(422)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-06 11:01:07 -04:00
|
|
|
|
context '#retrieve' do
|
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
get '/invites/retrieve.json', params: { email: 'test@example.com' }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
before do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, invited_by: user, email: 'test@example.com') }
|
|
|
|
|
|
|
|
|
|
it 'raises an error when the email is missing' do
|
|
|
|
|
get '/invites/retrieve.json'
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'raises an error when the email cannot be found' do
|
|
|
|
|
get '/invites/retrieve.json', params: { email: 'test2@example.com' }
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'can retrieve the invite' do
|
|
|
|
|
get '/invites/retrieve.json', params: { email: 'test@example.com' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-03 04:45:29 -05:00
|
|
|
|
context '#update' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, invited_by: admin, email: 'test@example.com') }
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
put "/invites/#{invite.id}", params: { email: 'test2@example.com' }
|
|
|
|
|
expect(response.status).to eq(400)
|
2021-03-06 06:29:35 -05:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'while logged in' do
|
|
|
|
|
before do
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'resends invite email if updating email address' do
|
|
|
|
|
put "/invites/#{invite.id}", params: { email: 'test2@example.com' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(1)
|
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not resend invite email if skip_email if updating email address' do
|
|
|
|
|
put "/invites/#{invite.id}", params: { email: 'test2@example.com', skip_email: true }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(0)
|
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not resend invite email when updating other fields' do
|
|
|
|
|
put "/invites/#{invite.id}", params: { custom_message: 'new message' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(invite.reload.custom_message).to eq('new message')
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(0)
|
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'can send invite email' do
|
|
|
|
|
sign_in(user)
|
|
|
|
|
RateLimiter.enable
|
|
|
|
|
RateLimiter.clear_all!
|
|
|
|
|
|
|
|
|
|
invite = Fabricate(:invite, invited_by: user, email: 'test@example.com')
|
|
|
|
|
|
|
|
|
|
expect { put "/invites/#{invite.id}", params: { send_email: true } }
|
|
|
|
|
.to change { RateLimiter.new(user, 'resend-invite-per-hour', 10, 1.hour).remaining }.by(-1)
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(1)
|
|
|
|
|
end
|
2021-05-12 06:06:39 -04:00
|
|
|
|
|
|
|
|
|
it 'cannot create duplicated invites' do
|
|
|
|
|
Fabricate(:invite, invited_by: admin, email: 'test2@example.com')
|
|
|
|
|
|
|
|
|
|
put "/invites/#{invite.id}.json", params: { email: 'test2@example.com' }
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
|
end
|
2022-05-10 11:45:43 -04:00
|
|
|
|
|
|
|
|
|
context "when providing an email belonging to an existing user" do
|
|
|
|
|
subject(:update_invite) { put "/invites/#{invite.id}.json", params: { email: admin.email } }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
SiteSetting.hide_email_address_taken = hide_email_address_taken
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when 'hide_email_address_taken' setting is disabled" do
|
|
|
|
|
let(:hide_email_address_taken) { false }
|
|
|
|
|
|
|
|
|
|
it "returns an error" do
|
|
|
|
|
update_invite
|
|
|
|
|
expect(response).to have_http_status :unprocessable_entity
|
|
|
|
|
expect(body).to match(/no need to invite/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when 'hide_email_address_taken' setting is enabled" do
|
|
|
|
|
let(:hide_email_address_taken) { true }
|
|
|
|
|
|
|
|
|
|
it "doesn't inform the user" do
|
|
|
|
|
update_invite
|
|
|
|
|
expect(response).to have_http_status :ok
|
|
|
|
|
expect(response.parsed_body).to be_blank
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2021-03-06 06:29:35 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context '#destroy' do
|
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
delete '/invites.json', params: { email: 'test@example.com' }
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
2021-03-06 06:29:35 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'while logged in' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, invited_by: user) }
|
2021-03-06 06:29:35 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
before { sign_in(user) }
|
2021-03-06 06:29:35 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'raises an error when id is missing' do
|
|
|
|
|
delete '/invites.json'
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'raises an error when invite does not exist' do
|
|
|
|
|
delete '/invites.json', params: { id: 848 }
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'raises an error when invite is not created by user' do
|
|
|
|
|
another_invite = Fabricate(:invite, email: 'test2@example.com')
|
|
|
|
|
|
|
|
|
|
delete '/invites.json', params: { id: another_invite.id }
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'destroys the invite' do
|
|
|
|
|
delete '/invites.json', params: { id: invite.id }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(invite.reload.trashed?).to be_truthy
|
|
|
|
|
end
|
2021-03-06 06:29:35 -05:00
|
|
|
|
end
|
2021-03-03 04:45:29 -05:00
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
context '#perform_accept_invitation' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'with an invalid invite' do
|
|
|
|
|
it 'redirects to the root' do
|
|
|
|
|
put '/invites/show/doesntexist.json'
|
2021-03-03 04:45:29 -05:00
|
|
|
|
expect(response.status).to eq(404)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.not_found_json'))
|
2019-02-06 08:49:00 -05:00
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
context 'with a deleted invite' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
2018-12-05 10:43:07 -05:00
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
before do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invite.trash!
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'redirects to the root' do
|
2018-06-01 01:06:08 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
2021-03-03 04:45:29 -05:00
|
|
|
|
expect(response.status).to eq(404)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.not_found_json'))
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-09 11:19:32 -04:00
|
|
|
|
context 'with an expired invite' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:invite) { Fabricate(:invite, expires_at: 1.day.ago) }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'response is not successful' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
2021-03-03 04:45:29 -05:00
|
|
|
|
expect(response.status).to eq(404)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.not_found_json'))
|
2020-06-09 11:19:32 -04:00
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'with an email invite' do
|
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
let(:invite) { Invite.generate(topic.user, email: 'iceking@adventuretime.ooo', topic: topic) }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
|
|
|
|
it 'redeems the invite' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(invite.reload.redeemed?).to be_truthy
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'logs in the user' do
|
|
|
|
|
events = DiscourseEvent.track_events do
|
2021-04-14 05:15:56 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(:user_logged_in, :user_first_logged_in)
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(session[:current_user_id]).to eq(invite.invited_users.first.user_id)
|
|
|
|
|
expect(invite.reload.redeemed?).to be_truthy
|
|
|
|
|
user = User.find(invite.invited_users.first.user_id)
|
|
|
|
|
expect(user.ip_address).to be_present
|
|
|
|
|
expect(user.registration_ip_address).to be_present
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'redirects to the first topic the user was invited to' do
|
2021-04-14 05:15:56 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.parsed_body['redirect_to']).to eq(topic.relative_url)
|
2021-08-26 03:43:56 -04:00
|
|
|
|
expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'sets the timezone of the user in user_options' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { timezone: 'Australia/Melbourne' }
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
invite.reload
|
2021-03-25 12:26:22 -04:00
|
|
|
|
user = User.find(invite.invited_users.first.user_id)
|
|
|
|
|
expect(user.user_option.timezone).to eq('Australia/Melbourne')
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not log in the user if there are validation errors' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: 'password' }
|
|
|
|
|
expect(response.status).to eq(412)
|
|
|
|
|
end
|
|
|
|
|
|
2022-01-12 15:24:54 -05:00
|
|
|
|
it 'does not log in the user if they were not approved' do
|
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: SecureRandom.hex, email_token: invite.email_token }
|
|
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to eq(nil)
|
|
|
|
|
expect(response.parsed_body["message"]).to eq(I18n.t('activation.approval_required'))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'does not log in the user if they were not activated' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: SecureRandom.hex }
|
|
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to eq(nil)
|
|
|
|
|
expect(response.parsed_body["message"]).to eq(I18n.t('invite.confirm_email'))
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails when local login is disabled and no external auth is configured' do
|
2021-03-02 02:13:04 -05:00
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'with OmniAuth provider' do
|
|
|
|
|
fab!(:authenticated_email) { 'test@example.com' }
|
2021-03-02 02:13:04 -05:00
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
OmniAuth.config.test_mode = true
|
|
|
|
|
|
|
|
|
|
OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new(
|
|
|
|
|
provider: 'google_oauth2',
|
|
|
|
|
uid: '12345',
|
|
|
|
|
info: OmniAuth::AuthHash::InfoHash.new(
|
|
|
|
|
email: authenticated_email,
|
|
|
|
|
name: 'First Last'
|
|
|
|
|
),
|
|
|
|
|
extra: {
|
|
|
|
|
raw_info: OmniAuth::AuthHash.new(
|
|
|
|
|
email_verified: true,
|
|
|
|
|
email: authenticated_email,
|
2021-03-25 12:26:22 -04:00
|
|
|
|
family_name: 'Last',
|
|
|
|
|
given_name: 'First',
|
|
|
|
|
gender: 'male',
|
|
|
|
|
name: 'First Last',
|
2021-03-02 02:13:04 -05:00
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:google_oauth2]
|
2021-03-02 02:13:04 -05:00
|
|
|
|
SiteSetting.enable_google_oauth2_logins = true
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
get '/auth/google_oauth2/callback.json'
|
2021-03-02 02:13:04 -05:00
|
|
|
|
expect(response.status).to eq(302)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
after do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:google_oauth2] = nil
|
2021-03-02 02:13:04 -05:00
|
|
|
|
OmniAuth.config.test_mode = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'should associate the invited user with authenticator records' do
|
|
|
|
|
SiteSetting.auth_overrides_name = true
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invite.update!(email: authenticated_email)
|
2021-03-02 02:13:04 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect { put "/invites/show/#{invite.invite_key}.json", params: { name: 'somename' } }
|
|
|
|
|
.to change { User.with_email(authenticated_email).exists? }.to(true)
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-02 02:13:04 -05:00
|
|
|
|
|
|
|
|
|
user = User.find_by_email(authenticated_email)
|
|
|
|
|
expect(user.name).to eq('First Last')
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(user.user_associated_accounts.first.provider_name).to eq('google_oauth2')
|
2021-03-02 02:13:04 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns the right response even if local logins has been disabled' do
|
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
|
invite.update!(email: authenticated_email)
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'returns the right response if authenticated email does not match invite email' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(412)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context '.post_process_invite' do
|
|
|
|
|
it 'sends a welcome message if set' do
|
|
|
|
|
SiteSetting.send_welcome_message = true
|
|
|
|
|
user.send_welcome_message = true
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Jobs::SendSystemMessage.jobs.size).to eq(1)
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'refreshes automatic groups if staff' do
|
|
|
|
|
topic.user.grant_admin!
|
|
|
|
|
invite.update!(moderator: true)
|
2019-11-24 19:49:27 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(invite.invited_users.first.user.groups.pluck(:name)).to contain_exactly('moderators', 'staff')
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'without password' do
|
|
|
|
|
it 'sends password reset email' do
|
2018-06-01 01:06:08 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-12-11 12:04:07 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(1)
|
|
|
|
|
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'with password' do
|
|
|
|
|
context 'user was invited via email' do
|
|
|
|
|
before { invite.update_column(:emailed_status, Invite.emailed_status_types[:pending]) }
|
2019-05-10 08:49:12 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not send an activation email and activates the user' do
|
|
|
|
|
expect do
|
2021-04-14 05:15:56 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: 'verystrongpassword', email_token: invite.email_token }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end.to change { UserAuthToken.count }.by(1)
|
2019-05-10 08:49:12 -04:00
|
|
|
|
|
2018-12-10 17:24:02 -05:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-12-11 12:04:07 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
|
2018-12-10 17:24:02 -05:00
|
|
|
|
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invited_user = User.find_by_email(invite.email)
|
|
|
|
|
expect(invited_user.active).to eq(true)
|
|
|
|
|
expect(invited_user.email_confirmed?).to eq(true)
|
2018-12-10 17:24:02 -05:00
|
|
|
|
end
|
2021-04-14 05:15:56 -04:00
|
|
|
|
|
|
|
|
|
it 'does not activate user if email token is missing' do
|
|
|
|
|
expect do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: 'verystrongpassword' }
|
|
|
|
|
end.to change { UserAuthToken.count }.by(0)
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
|
|
|
|
|
expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)
|
|
|
|
|
|
|
|
|
|
invited_user = User.find_by_email(invite.email)
|
|
|
|
|
expect(invited_user.active).to eq(false)
|
|
|
|
|
expect(invited_user.email_confirmed?).to eq(false)
|
|
|
|
|
end
|
2021-03-25 12:26:22 -04:00
|
|
|
|
end
|
2018-12-10 17:24:02 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'user was invited via link' do
|
|
|
|
|
before { invite.update_column(:emailed_status, Invite.emailed_status_types[:not_required]) }
|
2018-12-10 17:24:02 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'sends an activation email and does not activate the user' do
|
|
|
|
|
expect do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { password: 'verystrongpassword' }
|
|
|
|
|
end.not_to change { UserAuthToken.count }
|
2019-01-08 02:10:20 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.confirm_email'))
|
2018-12-10 17:24:02 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invited_user = User.find_by_email(invite.email)
|
|
|
|
|
expect(invited_user.active).to eq(false)
|
|
|
|
|
expect(invited_user.email_confirmed?).to eq(false)
|
2018-12-11 12:04:07 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
|
|
|
|
|
expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)
|
2018-12-11 12:04:07 -05:00
|
|
|
|
|
2021-12-13 00:29:47 -05:00
|
|
|
|
tokens = EmailToken.where(user_id: invited_user.id, confirmed: false, expired: false)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(tokens.size).to eq(1)
|
2018-12-11 12:04:07 -05:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
job_args = Jobs::CriticalUserEmail.jobs.first['args'].first
|
|
|
|
|
expect(job_args['type']).to eq('signup')
|
|
|
|
|
expect(job_args['user_id']).to eq(invited_user.id)
|
2021-12-13 00:29:47 -05:00
|
|
|
|
expect(EmailToken.hash_token(job_args['email_token'])).to eq(tokens.first.token_hash)
|
2018-12-10 17:24:02 -05:00
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-12-08 10:06:57 -05:00
|
|
|
|
context 'with a domain invite' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: nil, emailed_status: Invite.emailed_status_types[:not_required], domain: 'example.com') }
|
|
|
|
|
|
|
|
|
|
it 'creates an user if email matches domain' do
|
|
|
|
|
expect { put "/invites/show/#{invite.invite_key}.json", params: { email: 'test@example.com', password: 'verystrongpassword' } }
|
|
|
|
|
.to change { User.count }
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.confirm_email'))
|
|
|
|
|
expect(invite.reload.redemption_count).to eq(1)
|
|
|
|
|
|
|
|
|
|
invited_user = User.find_by_email('test@example.com')
|
|
|
|
|
expect(invited_user).to be_present
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'does not create an user if email does not match domain' do
|
|
|
|
|
expect { put "/invites/show/#{invite.invite_key}.json", params: { email: 'test@example2.com', password: 'verystrongpassword' } }
|
|
|
|
|
.not_to change { User.count }
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(412)
|
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.domain_not_allowed'))
|
|
|
|
|
expect(invite.reload.redemption_count).to eq(0)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context 'with an invite link' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: nil, emailed_status: Invite.emailed_status_types[:not_required]) }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'sends an activation email and does not activate the user' do
|
|
|
|
|
expect { put "/invites/show/#{invite.invite_key}.json", params: { email: 'test@example.com', password: 'verystrongpassword' } }
|
|
|
|
|
.not_to change { UserAuthToken.count }
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.parsed_body['message']).to eq(I18n.t('invite.confirm_email'))
|
|
|
|
|
expect(invite.reload.redemption_count).to eq(1)
|
2020-06-09 11:19:32 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
invited_user = User.find_by_email('test@example.com')
|
2020-06-09 11:19:32 -04:00
|
|
|
|
expect(invited_user.active).to eq(false)
|
|
|
|
|
expect(invited_user.email_confirmed?).to eq(false)
|
|
|
|
|
|
|
|
|
|
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
|
|
|
|
|
expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)
|
|
|
|
|
|
2021-12-13 00:29:47 -05:00
|
|
|
|
tokens = EmailToken.where(user_id: invited_user.id, confirmed: false, expired: false)
|
2020-06-09 11:19:32 -04:00
|
|
|
|
expect(tokens.size).to eq(1)
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
job_args = Jobs::CriticalUserEmail.jobs.first['args'].first
|
|
|
|
|
expect(job_args['type']).to eq('signup')
|
|
|
|
|
expect(job_args['user_id']).to eq(invited_user.id)
|
2021-12-13 00:29:47 -05:00
|
|
|
|
expect(EmailToken.hash_token(job_args['email_token'])).to eq(tokens.first.token_hash)
|
2020-06-09 11:19:32 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
context 'new registrations are disabled' do
|
2019-05-06 23:12:20 -04:00
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:invite) { Invite.generate(topic.user, email: 'test@example.com', topic: topic) }
|
2018-12-05 10:43:07 -05:00
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
before { SiteSetting.allow_new_registrations = false }
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not redeem the invite' do
|
2018-06-01 01:06:08 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(invite.reload.invited_users).to be_blank
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(invite.redeemed?).to be_falsey
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.body).to include(I18n.t('login.new_registrations_disabled'))
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'user is already logged in' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: 'test@example.com') }
|
|
|
|
|
fab!(:user) { sign_in(Fabricate(:user)) }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'does not redeem the invite' do
|
2018-06-01 01:06:08 -04:00
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { id: invite.invite_key }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
invite.reload
|
2020-06-09 11:19:32 -04:00
|
|
|
|
expect(invite.invited_users).to be_blank
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(invite.redeemed?).to be_falsey
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.body).to include(I18n.t('login.already_logged_in', current_user: user.username))
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-06-30 05:00:47 -04:00
|
|
|
|
|
|
|
|
|
context 'topic invites' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: 'test@example.com') }
|
|
|
|
|
|
|
|
|
|
fab!(:secured_category) do
|
|
|
|
|
secured_category = Fabricate(:category)
|
|
|
|
|
secured_category.permissions = { staff: :full }
|
|
|
|
|
secured_category.save!
|
|
|
|
|
secured_category
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'redirects user to topic if activated' do
|
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
TopicInvite.create!(invite: invite, topic: topic)
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
|
|
|
|
|
expect(response.parsed_body['redirect_to']).to eq(topic.relative_url)
|
2021-08-26 03:43:56 -04:00
|
|
|
|
expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
|
2021-06-30 05:00:47 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'sets destination_url cookie if user is not activated' do
|
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
TopicInvite.create!(invite: invite, topic: topic)
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json"
|
|
|
|
|
expect(cookies['destination_url']).to eq(topic.relative_url)
|
2021-08-26 03:43:56 -04:00
|
|
|
|
expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(1)
|
2021-06-30 05:00:47 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'does not redirect user if they cannot see topic' do
|
2021-08-26 03:43:56 -04:00
|
|
|
|
topic = Fabricate(:topic, category: secured_category)
|
|
|
|
|
TopicInvite.create!(invite: invite, topic: topic)
|
2021-06-30 05:00:47 -04:00
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: { email_token: invite.email_token }
|
|
|
|
|
expect(response.parsed_body['redirect_to']).to eq("/")
|
2021-08-26 03:43:56 -04:00
|
|
|
|
expect(Notification.where(notification_type: Notification.types[:invited_to_topic], topic: topic).count).to eq(0)
|
2021-06-30 05:00:47 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-07-11 17:57:38 -04:00
|
|
|
|
|
|
|
|
|
context 'staged user' do
|
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
|
|
|
|
fab!(:staged_user) { Fabricate(:user, staged: true, email: invite.email) }
|
|
|
|
|
|
|
|
|
|
it 'can keep the old username' do
|
|
|
|
|
old_username = staged_user.username
|
|
|
|
|
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: {
|
|
|
|
|
username: staged_user.username,
|
|
|
|
|
password: "Password123456",
|
|
|
|
|
email_token: invite.email_token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(invite.reload.redeemed?).to be_truthy
|
|
|
|
|
user = invite.invited_users.first.user
|
|
|
|
|
expect(user.username).to eq(old_username)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'can change the username' do
|
|
|
|
|
put "/invites/show/#{invite.invite_key}.json", params: {
|
|
|
|
|
username: "new_username",
|
|
|
|
|
password: "Password123456",
|
|
|
|
|
email_token: invite.email_token,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(invite.reload.redeemed?).to be_truthy
|
|
|
|
|
user = invite.invited_users.first.user
|
|
|
|
|
expect(user.username).to eq("new_username")
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
context '#destroy_all_expired' do
|
2021-03-03 04:45:29 -05:00
|
|
|
|
it 'removes all expired invites sent by a user' do
|
|
|
|
|
SiteSetting.invite_expiry_days = 1
|
|
|
|
|
|
|
|
|
|
user = Fabricate(:admin)
|
|
|
|
|
invite_1 = Fabricate(:invite, invited_by: user)
|
|
|
|
|
invite_2 = Fabricate(:invite, invited_by: user)
|
|
|
|
|
expired_invite = Fabricate(:invite, invited_by: user)
|
|
|
|
|
expired_invite.update!(expires_at: 2.days.ago)
|
|
|
|
|
|
|
|
|
|
sign_in(user)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/destroy-all-expired'
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(invite_1.reload.deleted_at).to eq(nil)
|
|
|
|
|
expect(invite_2.reload.deleted_at).to eq(nil)
|
|
|
|
|
expect(expired_invite.reload.deleted_at).to be_present
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
context '#resend_invite' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
post '/invites/reinvite.json', params: { email: 'first_name@example.com' }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'while logged in' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
fab!(:user) { sign_in(Fabricate(:user)) }
|
|
|
|
|
fab!(:invite) { Fabricate(:invite, invited_by: user) }
|
2019-05-06 23:12:20 -04:00
|
|
|
|
fab!(:another_invite) { Fabricate(:invite, email: 'last_name@example.com') }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
|
|
|
|
it 'raises an error when the email is missing' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/reinvite.json'
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'raises an error when the email cannot be found' do
|
|
|
|
|
post '/invites/reinvite.json', params: { email: 'first_name@example.com' }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'raises an error when the invite is not yours' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/reinvite.json', params: { email: another_invite.email }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'resends the invite' do
|
|
|
|
|
post '/invites/reinvite.json', params: { email: invite.email }
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(1)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-03 04:45:29 -05:00
|
|
|
|
context '#resend_all_invites' do
|
|
|
|
|
it 'resends all non-redeemed invites by a user' do
|
|
|
|
|
SiteSetting.invite_expiry_days = 30
|
|
|
|
|
|
2021-06-17 03:45:53 -04:00
|
|
|
|
freeze_time
|
|
|
|
|
|
2022-02-03 14:07:40 -05:00
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
new_invite = Fabricate(:invite, invited_by: admin)
|
|
|
|
|
expired_invite = Fabricate(:invite, invited_by: admin)
|
2021-03-03 04:45:29 -05:00
|
|
|
|
expired_invite.update!(expires_at: 2.days.ago)
|
2022-02-03 14:07:40 -05:00
|
|
|
|
redeemed_invite = Fabricate(:invite, invited_by: admin)
|
2021-03-03 04:45:29 -05:00
|
|
|
|
Fabricate(:invited_user, invite: redeemed_invite, user: Fabricate(:user))
|
|
|
|
|
redeemed_invite.update!(expires_at: 5.days.ago)
|
|
|
|
|
|
2022-02-03 14:07:40 -05:00
|
|
|
|
sign_in(admin)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/reinvite-all'
|
2021-03-03 04:45:29 -05:00
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2021-06-17 03:45:53 -04:00
|
|
|
|
expect(new_invite.reload.expires_at).to eq_time(30.days.from_now)
|
|
|
|
|
expect(expired_invite.reload.expires_at).to eq_time(2.days.ago)
|
|
|
|
|
expect(redeemed_invite.reload.expires_at).to eq_time(5.days.ago)
|
2021-03-03 04:45:29 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 01:06:08 -04:00
|
|
|
|
context '#upload_csv' do
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'requires to be logged in' do
|
|
|
|
|
post '/invites/upload_csv.json'
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
let(:csv_file) { File.new("#{Rails.root}/spec/fixtures/csv/discourse.csv") }
|
2021-03-25 12:26:22 -04:00
|
|
|
|
let(:file) { Rack::Test::UploadedFile.new(File.open(csv_file)) }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
|
2021-03-29 07:03:19 -04:00
|
|
|
|
let(:csv_file_with_headers) { File.new("#{Rails.root}/spec/fixtures/csv/discourse_headers.csv") }
|
|
|
|
|
let(:file_with_headers) { Rack::Test::UploadedFile.new(File.open(csv_file_with_headers)) }
|
2021-12-05 20:08:21 -05:00
|
|
|
|
let(:csv_file_with_locales) { File.new("#{Rails.root}/spec/fixtures/csv/invites_with_locales.csv") }
|
|
|
|
|
let(:file_with_locales) { Rack::Test::UploadedFile.new(File.open(csv_file_with_locales)) }
|
2021-03-29 07:03:19 -04:00
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'fails if you cannot bulk invite to the forum' do
|
2018-06-01 01:06:08 -04:00
|
|
|
|
sign_in(Fabricate(:user))
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'allows admin to bulk invite' do
|
2020-06-09 11:19:32 -04:00
|
|
|
|
sign_in(admin)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
2018-06-07 04:11:09 -04:00
|
|
|
|
expect(response.status).to eq(200)
|
2018-06-01 01:06:08 -04:00
|
|
|
|
expect(Jobs::BulkInvite.jobs.size).to eq(1)
|
|
|
|
|
end
|
2022-02-03 14:07:40 -05:00
|
|
|
|
|
|
|
|
|
it 'limits admins when bulk inviting' do
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
expect(Jobs::BulkInvite.jobs.size).to eq(1)
|
|
|
|
|
end
|
2019-06-12 05:05:21 -04:00
|
|
|
|
|
2021-11-09 12:43:23 -05:00
|
|
|
|
it 'allows admin to bulk invite when DiscourseConnect enabled' do
|
|
|
|
|
SiteSetting.discourse_connect_url = "https://example.com"
|
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
|
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
expect(Jobs::BulkInvite.jobs.size).to eq(1)
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-25 12:26:22 -04:00
|
|
|
|
it 'sends limited invites at a time' do
|
2019-06-12 05:05:21 -04:00
|
|
|
|
SiteSetting.max_bulk_invites = 3
|
2020-06-09 11:19:32 -04:00
|
|
|
|
sign_in(admin)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
post '/invites/upload_csv.json', params: { file: file, name: 'discourse.csv' }
|
2019-06-12 05:05:21 -04:00
|
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
expect(Jobs::BulkInvite.jobs.size).to eq(1)
|
2021-03-25 12:26:22 -04:00
|
|
|
|
expect(response.parsed_body['errors'][0]).to eq(I18n.t('bulk_invite.max_rows', max_bulk_invites: SiteSetting.max_bulk_invites))
|
2019-06-12 05:05:21 -04:00
|
|
|
|
end
|
2021-03-29 07:03:19 -04:00
|
|
|
|
|
|
|
|
|
it 'can import user fields' do
|
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
user_field = Fabricate(:user_field, name: "location")
|
|
|
|
|
Fabricate(:group, name: 'discourse')
|
|
|
|
|
Fabricate(:group, name: 'ubuntu')
|
|
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
|
|
post '/invites/upload_csv.json', params: { file: file_with_headers, name: 'discourse_headers.csv' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
user = User.where(staged: true).find_by_email('test@example.com')
|
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq('usa')
|
|
|
|
|
|
|
|
|
|
user2 = User.where(staged: true).find_by_email('test2@example.com')
|
|
|
|
|
expect(user2.user_fields[user_field.id.to_s]).to eq('europe')
|
|
|
|
|
end
|
2021-12-05 20:08:21 -05:00
|
|
|
|
|
|
|
|
|
it 'can pre-set user locales' do
|
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
|
|
post '/invites/upload_csv.json', params: { file: file_with_locales, name: 'discourse_headers.csv' }
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
|
|
user = User.where(staged: true).find_by_email('test@example.com')
|
|
|
|
|
expect(user.locale).to eq('de')
|
|
|
|
|
|
|
|
|
|
user2 = User.where(staged: true).find_by_email('test2@example.com')
|
|
|
|
|
expect(user2.locale).to eq('pl')
|
|
|
|
|
end
|
2018-06-01 01:06:08 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-05-08 10:42:58 -04:00
|
|
|
|
end
|