DEV: Improve specs and handle invalid email token

Follow-up to 7977b09025
This commit is contained in:
Gerhard Schlager 2018-12-11 18:04:07 +01:00
parent 8c227715ae
commit 688755baf2
3 changed files with 24 additions and 5 deletions

View File

@ -221,6 +221,7 @@ class InvitesController < ApplicationController
def send_activation_email(user) def send_activation_email(user)
email_token = user.email_tokens.create(email: user.email) email_token = user.email_tokens.create(email: user.email)
raise ActiveRecord::RecordInvalid.new(email_token) if !email_token.persisted?
Jobs.enqueue(:critical_user_email, Jobs.enqueue(:critical_user_email,
type: :signup, type: :signup,

View File

@ -59,7 +59,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_f
user.save! user.save!
if invite.via_email if invite.via_email
user.email_tokens.create(email: user.email) user.email_tokens.create!(email: user.email)
user.activate user.activate
end end

View File

@ -293,6 +293,8 @@ describe InvitesController do
user.send_welcome_message = true user.send_welcome_message = true
put "/invites/show/#{invite.invite_key}.json" put "/invites/show/#{invite.invite_key}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::SendSystemMessage.jobs.size).to eq(1) expect(Jobs::SendSystemMessage.jobs.size).to eq(1)
end end
@ -300,6 +302,8 @@ describe InvitesController do
it "sends password reset email" do it "sends password reset email" do
put "/invites/show/#{invite.invite_key}.json" put "/invites/show/#{invite.invite_key}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(1) expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(1)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0) expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
end end
@ -309,6 +313,8 @@ describe InvitesController do
SiteSetting.enable_sso = true SiteSetting.enable_sso = true
put "/invites/show/#{invite.invite_key}.json" put "/invites/show/#{invite.invite_key}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0) expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0) expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
end end
@ -317,6 +323,8 @@ describe InvitesController do
SiteSetting.enable_local_logins = false SiteSetting.enable_local_logins = false
put "/invites/show/#{invite.invite_key}.json" put "/invites/show/#{invite.invite_key}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0) expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0) expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
end end
@ -329,6 +337,7 @@ describe InvitesController do
it "doesn't send an activation email and activates the user" do it "doesn't send an activation email and activates the user" do
put "/invites/show/#{invite.invite_key}.json", params: { password: "verystrongpassword" } put "/invites/show/#{invite.invite_key}.json", params: { password: "verystrongpassword" }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0) expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(0) expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)
@ -345,15 +354,24 @@ describe InvitesController do
it "sends an activation email and doesn't activate the user" do it "sends an activation email and doesn't activate the user" do
put "/invites/show/#{invite.invite_key}.json", params: { password: "verystrongpassword" } put "/invites/show/#{invite.invite_key}.json", params: { password: "verystrongpassword" }
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(JSON.parse(response.body)["success"]).to eq(true)
expect(Jobs::InvitePasswordInstructionsEmail.jobs.size).to eq(0)
expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)
expect(Jobs::CriticalUserEmail.jobs.first["args"].first["type"]).to eq("signup")
invited_user = User.find_by_email(invite.email) invited_user = User.find_by_email(invite.email)
expect(invited_user.active).to eq(false) expect(invited_user.active).to eq(false)
expect(invited_user.email_confirmed?).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)
tokens = EmailToken.where(user_id: invited_user.id, confirmed: false, expired: false).pluck(:token)
expect(tokens.size).to eq(1)
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)
expect(job_args["email_token"]).to eq(tokens.first)
end end
end end
end end