GRN2-252: Change to how sign ins are processed (#869)

* Social to local

* Social/Local to Social

* Rubocop fixes

* Added test cases

* Added the ability to clear social uids

* Update admins_controller.rb

* Update admins_controller.rb
This commit is contained in:
Ahmad Farhat
2020-01-22 16:32:56 -05:00
committed by farhatahmad
parent 42e6e4f235
commit 005ec84c73
10 changed files with 181 additions and 3 deletions

View File

@ -344,6 +344,36 @@ describe AdminsController, type: :controller do
expect(response).to redirect_to(admin_site_settings_path)
end
end
it "clears all users social uids if clear auth button is clicked" do
allow_any_instance_of(ApplicationController).to receive(:set_user_domain).and_return("provider1")
controller.instance_variable_set(:@user_domain, "provider1")
@request.session[:user_id] = @admin.id
@admin.add_role :super_admin
@admin.update_attribute(:provider, "greenlight")
@user2 = create(:user, provider: "provider1")
@user3 = create(:user, provider: "provider1")
@user.update_attribute(:social_uid, Faker::Internet.password)
@user2.update_attribute(:social_uid, Faker::Internet.password)
@user3.update_attribute(:social_uid, Faker::Internet.password)
expect(@user.social_uid).not_to be(nil)
expect(@user2.social_uid).not_to be(nil)
expect(@user3.social_uid).not_to be(nil)
post :clear_auth
@user.reload
@user2.reload
@user3.reload
expect(@user.social_uid).to be(nil)
expect(@user2.social_uid).to be(nil)
expect(@user3.social_uid).to be(nil)
end
end
describe "Roles" do

View File

@ -115,7 +115,7 @@ describe PasswordResetsController, type: :controller do
end
it "updates attributes if the password update is a success" do
user = create(:user)
user = create(:user, provider: "greenlight")
token = "reset_token"
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost

View File

@ -88,7 +88,11 @@ describe SessionsController, type: :controller do
end
describe "POST #create" do
before { allow(Rails.configuration).to receive(:enable_email_verification).and_return(true) }
before do
allow(Rails.configuration).to receive(:enable_email_verification).and_return(true)
allow_any_instance_of(SessionsController).to receive(:auth_changed_to_local?).and_return(false)
end
before(:each) do
@user1 = create(:user, provider: 'greenlight', password: 'example', password_confirmation: 'example')
@user2 = create(:user, password: 'example', password_confirmation: "example")
@ -251,6 +255,22 @@ describe SessionsController, type: :controller do
expect(@user1.rooms.find { |r| r.name == "Old Home Room" }).to_not be_nil
expect(@user1.rooms.find { |r| r.name == "Test" }).to_not be_nil
end
it "sends the user a reset password email if the authentication method is changing to local" do
allow_any_instance_of(SessionsController).to receive(:auth_changed_to_local?).and_return(true)
email = Faker::Internet.email
create(:user, email: email, provider: "greenlight", social_uid: "google-user")
expect {
post :create, params: {
session: {
email: email,
password: 'example',
},
}
}.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
describe "GET/POST #omniauth" do
@ -428,6 +448,66 @@ describe SessionsController, type: :controller do
expect(response).to redirect_to(root_path)
end
it "switches a social account to a different social account if the authentication method changed" do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:bn_launcher]
get :omniauth, params: { provider: 'bn_launcher' }
u = User.find_by(social_uid: "bn-launcher-user")
u.social_uid = nil
users_old_uid = u.uid
u.save!
new_user = OmniAuth::AuthHash.new(
provider: "bn_launcher",
uid: "bn-launcher-user-new",
info: {
email: "user@google.com",
name: "Office User",
nickname: "googleuser",
image: "touch.png",
customer: 'customer1',
}
)
allow_any_instance_of(SessionsController).to receive(:auth_changed_to_social?).and_return(true)
allow_any_instance_of(ApplicationController).to receive(:set_user_domain).and_return("customer1")
controller.instance_variable_set(:@user_domain, "customer1")
request.env["omniauth.auth"] = new_user
get :omniauth, params: { provider: 'bn_launcher' }
new_u = User.find_by(social_uid: "bn-launcher-user-new")
expect(users_old_uid).to eq(new_u.uid)
end
it "switches a local account to a different social account if the authentication method changed" do
email = Faker::Internet.email
user = create(:user, email: email, provider: "customer1")
users_old_uid = user.uid
new_user = OmniAuth::AuthHash.new(
provider: "bn_launcher",
uid: "bn-launcher-user-new",
info: {
email: email,
name: "Office User",
nickname: "googleuser",
image: "touch.png",
customer: 'customer1',
}
)
allow_any_instance_of(SessionsController).to receive(:auth_changed_to_social?).and_return(true)
allow_any_instance_of(ApplicationController).to receive(:set_user_domain).and_return("customer1")
controller.instance_variable_set(:@user_domain, "customer1")
request.env["omniauth.auth"] = new_user
get :omniauth, params: { provider: 'bn_launcher' }
new_u = User.find_by(social_uid: "bn-launcher-user-new")
expect(users_old_uid).to eq(new_u.uid)
end
end
describe "POST #ldap" do