Files
sure/test/models/oidc_identity_test.rb
Copilot 1ee20ab3a6 Eliminate code duplication in OIDC identity creation (#230)
* Eliminate duplication by using create_from_omniauth method

- Updated OidcIdentity.create_from_omniauth to set last_authenticated_at
- Refactored OidcAccountsController to use create_from_omniauth instead of direct create! calls
- Updated test to verify last_authenticated_at is set by create_from_omniauth

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Extract auth hash building into private helper method

- Added build_auth_hash helper method to eliminate OpenStruct creation duplication
- Both create_link and create_user actions now use the same helper

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Linter fix

* Fix button style on OIDC link step

* Fix dark mode styles

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2025-10-25 22:23:28 +02:00

83 lines
2.3 KiB
Ruby

require "test_helper"
class OidcIdentityTest < ActiveSupport::TestCase
setup do
@user = users(:family_admin)
@oidc_identity = oidc_identities(:bob_google)
end
test "belongs to user" do
assert_equal @user, @oidc_identity.user
end
test "validates presence of provider" do
@oidc_identity.provider = nil
assert_not @oidc_identity.valid?
assert_includes @oidc_identity.errors[:provider], "can't be blank"
end
test "validates presence of uid" do
@oidc_identity.uid = nil
assert_not @oidc_identity.valid?
assert_includes @oidc_identity.errors[:uid], "can't be blank"
end
test "validates presence of user_id" do
@oidc_identity.user_id = nil
assert_not @oidc_identity.valid?
assert_includes @oidc_identity.errors[:user_id], "can't be blank"
end
test "validates uniqueness of uid scoped to provider" do
duplicate = OidcIdentity.new(
user: users(:family_member),
provider: @oidc_identity.provider,
uid: @oidc_identity.uid
)
assert_not duplicate.valid?
assert_includes duplicate.errors[:uid], "has already been taken"
end
test "allows same uid for different providers" do
different_provider = OidcIdentity.new(
user: users(:family_member),
provider: "different_provider",
uid: @oidc_identity.uid
)
assert different_provider.valid?
end
test "records authentication timestamp" do
old_timestamp = @oidc_identity.last_authenticated_at
travel_to 1.hour.from_now do
@oidc_identity.record_authentication!
assert @oidc_identity.last_authenticated_at > old_timestamp
end
end
test "creates from omniauth hash" do
auth = OmniAuth::AuthHash.new({
provider: "google_oauth2",
uid: "google-123456",
info: {
email: "test@example.com",
name: "Test User",
first_name: "Test",
last_name: "User"
}
})
identity = OidcIdentity.create_from_omniauth(auth, @user)
assert identity.persisted?
assert_equal "google_oauth2", identity.provider
assert_equal "google-123456", identity.uid
assert_equal "test@example.com", identity.info["email"]
assert_equal "Test User", identity.info["name"]
assert_equal @user, identity.user
assert_not_nil identity.last_authenticated_at
end
end