2024-06-04 15:42:53 +08:00
# frozen_string_literal: true
RSpec . describe UserPasswordExpirer do
fab! ( :password ) { " somerandompassword " }
fab! ( :user ) { Fabricate ( :user , password : ) }
describe " .expire_user_password " do
it " should create a new UserPassword record with the user's current password information " do
freeze_time
2024-09-03 11:09:33 +08:00
expect { described_class . expire_user_password ( user ) } . to change ( UserPassword , :count ) . by 1
2024-06-04 15:42:53 +08:00
2024-09-03 11:09:33 +08:00
user_password = user . reload . user_password
2024-06-04 15:42:53 +08:00
expect ( user_password . password_hash ) . to eq ( user . password_hash )
expect ( user_password . password_salt ) . to eq ( user . salt )
expect ( user_password . password_algorithm ) . to eq ( user . password_algorithm )
expect ( user_password . password_expired_at ) . to eq_time ( Time . zone . now )
end
2024-06-05 15:22:40 +08:00
it " should update `UserPassword # password_expired_at` if the user already has an existing UserPassword record with the same password hash, salt and algorithm " do
freeze_time ( 1 . hour . ago ) do
described_class . expire_user_password ( user )
2024-09-03 11:09:33 +08:00
expect ( user . reload . user_password . password_expired_at ) . to eq_time ( Time . zone . now )
end
freeze_time do
expect { described_class . expire_user_password ( user ) } . not_to change ( UserPassword , :count )
user_password = user . user_password . reload
2024-06-05 15:22:40 +08:00
2024-09-03 11:09:33 +08:00
expect ( user_password . password_hash ) . to eq ( user . password_hash )
expect ( user_password . password_salt ) . to eq ( user . salt )
expect ( user_password . password_algorithm ) . to eq ( user . password_algorithm )
2024-06-05 15:22:40 +08:00
expect ( user_password . password_expired_at ) . to eq_time ( Time . zone . now )
end
2024-09-03 11:09:33 +08:00
end
2024-06-05 15:22:40 +08:00
2024-09-03 11:09:33 +08:00
it " updates UserPassword attributes if user already has an existing UserPassword record which has a different password_hash " do
new_password = password + " _new "
old_password_hash = user . password_hash
freeze_time ( 1 . hour . ago ) do
2024-06-05 15:22:40 +08:00
described_class . expire_user_password ( user )
2024-09-03 11:09:33 +08:00
expect ( user . user_password . password_hash ) . to eq ( old_password_hash )
expect ( user . user_password . password_expired_at ) . to eq_time ( Time . zone . now )
end
freeze_time do
user . update! ( password : new_password )
expect { described_class . expire_user_password ( user ) } . not_to change ( UserPassword , :count )
2024-06-05 15:22:40 +08:00
2024-09-03 11:09:33 +08:00
user_password = user . user_password . reload
2024-06-05 15:22:40 +08:00
2024-09-03 11:09:33 +08:00
expect ( user_password . password_hash ) . not_to eq ( old_password_hash )
2024-06-05 15:22:40 +08:00
expect ( user_password . password_hash ) . to eq ( user . password_hash )
expect ( user_password . password_salt ) . to eq ( user . salt )
expect ( user_password . password_algorithm ) . to eq ( user . password_algorithm )
expect ( user_password . password_expired_at ) . to eq_time ( Time . zone . now )
end
end
2024-06-04 15:42:53 +08:00
end
end