|
437 | 437 | expect(User.find_by(email: OmniAuth.config.mock_auth[:openid_connect][:info][:email]).role).to eq(role1) |
438 | 438 | end |
439 | 439 | end |
| 440 | + |
| 441 | + context 'avatar' do |
| 442 | + before do |
| 443 | + OmniAuth.config.mock_auth[:openid_connect] = OmniAuth::AuthHash.new( |
| 444 | + uid: Faker::Internet.uuid, |
| 445 | + info: { |
| 446 | + email: Faker::Internet.email, |
| 447 | + name: Faker::Name.name, |
| 448 | + image: Faker::Avatar.image |
| 449 | + } |
| 450 | + ) |
| 451 | + |
| 452 | + request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:openid_connect] |
| 453 | + stub_request(:get, OmniAuth.config.mock_auth[:openid_connect][:info][:image]) |
| 454 | + .to_return(body: file_fixture('default-avatar.png'), headers: { 'Content-Type' => 'image/jpeg' }, status: 200) |
| 455 | + end |
| 456 | + |
| 457 | + it 'attaches the avatar to the user' do |
| 458 | + get :create_user, params: { provider: 'openid_connect' } |
| 459 | + |
| 460 | + expect(User.find_by(email: OmniAuth.config.mock_auth[:openid_connect][:info][:email]).avatar).to be_attached |
| 461 | + end |
| 462 | + |
| 463 | + it 'does not re-attach the avatar if it hasnt changed' do |
| 464 | + reg_method = instance_double(SettingGetter) |
| 465 | + allow(SettingGetter).to receive(:new).with(setting_name: 'ResyncOnLogin', provider: 'greenlight').and_return(reg_method) |
| 466 | + allow(reg_method).to receive(:call).and_return(true) |
| 467 | + |
| 468 | + profile_file = URI.parse(OmniAuth.config.mock_auth[:openid_connect][:info][:image]) |
| 469 | + filename = File.basename(profile_file.path) |
| 470 | + |
| 471 | + user = create(:user, email: OmniAuth.config.mock_auth[:openid_connect][:info][:email]) |
| 472 | + user.avatar.attach(io: fixture_file_upload('default-avatar.png'), filename:, content_type: 'image/png') |
| 473 | + |
| 474 | + expect(User.find_by(email: OmniAuth.config.mock_auth[:openid_connect][:info][:email]).avatar).not_to receive(:attach) |
| 475 | + get :create_user, params: { provider: 'openid_connect' } |
| 476 | + end |
| 477 | + |
| 478 | + it 'does not prevent the user from being created if the avatar attaching fails' do |
| 479 | + allow(OmniAuth.config.mock_auth[:openid_connect][:info][:image]).to receive(:blank?).and_raise(StandardError, 'Some error') |
| 480 | + |
| 481 | + expect { get :create_user, params: { provider: 'openid_connect' } }.not_to raise_error |
| 482 | + expect(User.find_by(email: OmniAuth.config.mock_auth[:openid_connect][:info][:email])).to be_present |
| 483 | + end |
| 484 | + |
| 485 | + it 'does not try to attach the avatar if no image is passed' do |
| 486 | + OmniAuth.config.mock_auth[:openid_connect][:info][:image] = nil |
| 487 | + |
| 488 | + get :create_user, params: { provider: 'openid_connect' } |
| 489 | + |
| 490 | + expect(User.find_by(email: OmniAuth.config.mock_auth[:openid_connect][:info][:email]).avatar).not_to be_attached |
| 491 | + end |
| 492 | + |
| 493 | + it 'does not try to attach the avatar if the user is invalid' do |
| 494 | + allow_any_instance_of(User).to receive(:valid?).and_return(false) |
| 495 | + expect_any_instance_of(User).not_to receive(:avatar) |
| 496 | + get :create_user, params: { provider: 'openid_connect' } |
| 497 | + end |
| 498 | + end |
440 | 499 | end |
441 | 500 |
|
442 | 501 | describe '#recording_ready' do |
|
0 commit comments