Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'user-info-from-university-web' of https://github.com/er…

…icgj/community into ericgj-user-info-from-university-web
  • Loading branch information...
commit 4548b09ad5c6b18cb6d834695acc9555f56a49a2 2 parents f81d20d + b17e291
@jordanbyron jordanbyron authored
View
20 app/controllers/sessions_controller.rb
@@ -10,8 +10,13 @@ def new
end
def create
+ logger.info "[SessionsController#create] github auth: #{auth_hash.inspect}"
+ logger.info "[SessionsController#create] uniweb_user: #{uniweb_user.inspect}"
+
unless user = User.find_by_uid(auth_hash['uid'])
- user = User.create_from_hash(user_hash)
+ user = User.create_from_hash(auth_hash, :name => uniweb_user.name,
+ :email => uniweb_user.email
+ )
end
if user.errors.any?
@@ -46,18 +51,15 @@ def auth_hash
hash
end
- def uniweb_user
- return @uniweb_user if @uniweb_user
- nick = auth_hash['info']['nickname']
- @uniweb_user ||= UniversityWeb::User.find_by_github(nick)
+ def nick
+ auth_hash['info']['nickname']
end
- def user_hash
- auth_hash.merge(:name => uniweb_user.name,
- :email => uniweb_user.email
- )
+ def uniweb_user
+ @uniweb_user ||= UniversityWeb::User.find_by_github(nick)
end
+
def check_permissions
user = uniweb_user
View
6 app/models/user.rb
@@ -28,7 +28,7 @@ class User < ActiveRecord::Base
attr_protected :admin
- def self.create_from_hash(hash)
+ def self.create_from_hash(hash, overrides={})
attributes = {
name: hash['info']['name'] || hash['info']['nickname'],
email: hash['info']['email'],
@@ -37,8 +37,8 @@ def self.create_from_hash(hash)
}
attributes[:website] = hash['info']['urls']['Blog'] if hash['info']['urls']
-
- create(attributes)
+
+ create(attributes.merge(overrides))
end
def to_param
View
7 test/factories/users.rb
@@ -12,3 +12,10 @@
f.admin true
f.uid '456'
end
+
+Factory.define :bart, :class => User do |f|
+ f.name 'Bart Simpson'
+ f.github 'bartsimpson'
+ f.email 'faker@mcfakerstein.com'
+ f.uid '789'
+end
View
74 test/integration/auth_test.rb
@@ -0,0 +1,74 @@
+require 'test_helper'
+
+class AuthTest < ActionDispatch::IntegrationTest
+
+ test "user not in university-web should flash not registered alert" do
+ sign_new_user_in_with_mocks(Factory.attributes_for(:user), {})
+
+ page.has_selector?("div#flash-alert")
+ page.has_content?(
+ 'Your github account is not registered on University-web'
+ )
+ end
+
+ [:alumnus, :staff, :visiting_teacher].each do |user_category|
+ test "user not #{user_category} should flash not authorized alert" do
+ mock_auth = Factory.attributes_for(:user)
+ uniweb_hash = { :github => mock_auth[:github],
+ :name => mock_auth[:name],
+ :email => mock_auth[:email],
+ :alumnus => !(user_category == :alumnus),
+ :staff => !(user_category == :staff),
+ :visiting_teacher => !(user_category == :visiting_teacher)
+ }
+
+ sign_new_user_in_with_mocks(mock_auth, uniweb_hash)
+
+ page.has_selector?("div#flash-alert")
+ page.has_content?(
+ 'Sorry, but currently only Alumni and Staff have access to this site'
+ )
+ end
+
+ test "#{user_category} user should not flash alert" do
+ mock_auth = Factory.attributes_for(:user)
+ uniweb_hash = { :github => mock_auth[:github],
+ :name => mock_auth[:name],
+ :email => mock_auth[:email],
+ :alumnus => (user_category == :alumnus),
+ :staff => (user_category == :staff),
+ :visiting_teacher => (user_category == :visiting_teacher)
+ }
+
+ sign_new_user_in_with_mocks(mock_auth, uniweb_hash)
+
+ page.has_no_selector?("div#flash-alert")
+
+ end
+ end
+
+ test "non-existent authorized user should be added with name and email taken from university-web" do
+
+ mock_auth = Factory.attributes_for(:bart)
+ uniweb_hash = { :github => mock_auth[:github],
+ :name => "Evil Twin of Bart Simpson",
+ :email => "dont.have.a.cow.man@gmail.com",
+ :alumnus => true,
+ :staff => false,
+ :visiting_teacher => false
+ }
+
+ sign_new_user_in_with_mocks(mock_auth, uniweb_hash)
+
+ page.has_no_selector?("div#flash-error")
+
+ new_user = User.find_by_uid(mock_auth[:uid])
+
+ assert_equal 'Evil Twin of Bart Simpson', new_user.name
+ assert_equal 'dont.have.a.cow.man@gmail.com', new_user.email
+
+ end
+
+
+
+end
View
36 test/support/auth.rb
@@ -2,6 +2,7 @@ module Support
module Auth
include Support::Services
+ # Note: for existing user, pass in user factory object
def mock_auth_for(user)
OmniAuth.config.mock_auth[:github] = {
'provider' => 'github',
@@ -14,11 +15,44 @@ def mock_auth_for(user)
}
end
+ # Note: for new user, pass in attributes
+ def mock_auth(hash)
+ OmniAuth.config.mock_auth[:github] = {
+ 'provider' => 'github',
+ 'uid' => hash[:uid],
+ 'info' => {
+ 'name' => hash[:name],
+ 'nickname' => hash[:github],
+ 'email' => hash[:email]
+ }
+ }
+ end
+
+ # Sign in new user
+ #
+ # Example:
+ # sign_new_user_in_with_mocks(Factory.attributes_for(:user), uniweb_hash)
+ def sign_new_user_in_with_mocks(auth_hash, uniweb_hash)
+ visit root_path
+
+ mock_auth(auth_hash)
+ mock_uniweb_user(uniweb_hash)
+
+ click_link 'Sign in with Github'
+
+ auth_hash
+ end
+
+ # Sign in existing user
+ #
+ # Example:
+ # sign_user_in_with_mocks(Factory(:user), uniweb_hash)
def sign_user_in_with_mocks(user, hash)
+ visit root_path
+
mock_auth_for(user)
mock_uniweb_user(hash)
- visit root_path
click_link 'Sign in with Github'
user
Please sign in to comment.
Something went wrong with that request. Please try again.