From d8bd37b4ca856c8b0114411c5b0a6663384523e2 Mon Sep 17 00:00:00 2001 From: Russ Smith Date: Tue, 8 Oct 2013 14:16:29 -0700 Subject: [PATCH] Fixes for issue #23 --- .../users/omniauth_callbacks_controller.rb | 4 +- app/models/user.rb | 4 +- spec/models/user_spec.rb | 45 +++++++++---------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index f6fee39..3c3f10b 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -3,10 +3,10 @@ def meetup @user = User.find_for_meetup_oauth(request.env['omniauth.auth'], current_user) if @user.persisted? + @user.set_organizer_flag(request.env['omniauth.auth']) sign_in_and_redirect(@user, event: :authentication) else - session['devise.meetup_data'] = request.env['omniauth.auth'] - redirect_to(root_path) + redirect_to(root_path, notice: 'You are not a member of this meetup group. Please join first.') end end end diff --git a/app/models/user.rb b/app/models/user.rb index 38e950e..bb90938 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,11 +23,9 @@ def self.by_name end def self.find_for_meetup_oauth(auth, signed_in_resource = nil) - user = User.where(provider: auth.provider, uid: auth.uid.to_s).first_or_create.tap do |u| + User.where(provider: auth.provider, uid: auth.uid.to_s).first_or_create do |u| u.name = auth.info.name end - user.set_organizer_flag(auth) - user end def voted_on?(topic) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2b32849..db90bdc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,12 +1,10 @@ require 'spec_helper' describe User do - - describe '.find_for_meetup_oauth' do + let(:auth) { OpenStruct.new(provider: 'test', uid: '123456', info: OpenStruct.new(name: 'Joe Blow')) } + describe '.find_for_meetup_oauth' do context 'when the user does not exist' do - let(:auth) { OpenStruct.new(provider: 'test', uid: '123456', info: OpenStruct.new(name: 'Joe Blow')) } - before { Meetup::Profile.stub(:get).and_return({'role' => ''}) } it 'creates the user' do @@ -30,24 +28,6 @@ User.find_by_name('Joe Blow').organizer.should be_false end end - - context 'when the user is an organizer' do - before { Meetup::Profile.stub(:get).and_return({'role' => 'Organizer'}) } - - it 'sets the user as an organizer' do - User.find_for_meetup_oauth(auth) - User.find_by_name('Joe Blow').organizer.should be_true - end - end - - context 'when the user is a co-organizer' do - before { Meetup::Profile.stub(:get).and_return({'role' => 'Co-Organizer'}) } - - it 'sets the user as an organizer' do - User.find_for_meetup_oauth(auth) - User.find_by_name('Joe Blow').organizer.should be_true - end - end end context 'when the user does exist' do @@ -61,9 +41,28 @@ it 'does not create the user' do expect {User.find_for_meetup_oauth(auth)}.to_not change(User,:count) end + end + end + + describe '.set_organizer_flag' do + let(:user) { User.create(name: 'Joe Blow') } + + context 'when the user is an organizer' do + before { Meetup::Profile.stub(:get).and_return({'role' => 'Organizer'}) } + it 'sets the user as an organizer' do + user.set_organizer_flag(auth) + expect(user.organizer).to be_true + end end - end + context 'when the user is a co-organizer' do + before { Meetup::Profile.stub(:get).and_return({'role' => 'Co-Organizer'}) } + it 'sets the user as an organizer' do + user.set_organizer_flag(auth) + expect(user.organizer).to be_true + end + end + end end