From 3378e5d8ff7fc367e2596154b54ae69bfcea39d9 Mon Sep 17 00:00:00 2001 From: djkazunoko Date: Sat, 6 Apr 2024 18:22:54 +0900 Subject: [PATCH] =?UTF-8?q?test:=20User.find=5For=5Fcreate=5Ffrom=5Fauth?= =?UTF-8?q?=5Fhash!=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AE=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E4=BD=9C=E6=88=90=20(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/user_spec.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d0bfacd..8caf064 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -57,4 +57,53 @@ user2 = FactoryBot.build(:user, name: 'tester2', image_url: 'example.com/2', provider: 'twitter') expect(user2).to be_valid end + + describe '.find_or_create_from_auth_hash!' do + let(:auth_hash) do + { + provider: 'github', + uid: '0001', + info: { + nickname: 'testuser', + image: 'https://example.com/testuser.png' + } + } + end + + context 'when user does not exist' do + it 'creates a new user' do + expect do + User.find_or_create_from_auth_hash!(auth_hash) + end.to change(User, :count).by(1) + end + + it 'returns the created user' do + user = User.find_or_create_from_auth_hash!(auth_hash) + expect(user).to be_a(User) + expect(user.provider).to eq('github') + expect(user.uid).to eq('0001') + expect(user.name).to eq('testuser') + expect(user.image_url).to eq('https://example.com/testuser.png') + end + end + + context 'when user already exists' do + let!(:existing_user) { FactoryBot.create(:user) } + + it 'does not create a new user' do + expect do + User.find_or_create_from_auth_hash!(auth_hash) + end.not_to change(User, :count) + end + + it 'returns the existing user' do + user = User.find_or_create_from_auth_hash!(auth_hash) + expect(user).to be_a(User) + expect(user.provider).to eq('github') + expect(user.uid).to eq('0001') + expect(user.name).to eq('tester1') + expect(user.image_url).to eq('example.com/1') + end + end + end end