Skip to content
Snippets Groups Projects
Commit a7a020ad authored by Дмитрий Коваленок's avatar Дмитрий Коваленок
Browse files

testing

parent 8e365234
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,7 @@ source 'http://rubygems.org' ...@@ -2,3 +2,7 @@ source 'http://rubygems.org'
gem 'oauth2' gem 'oauth2'
gem 'json' gem 'json'
group :development, :test do
gem 'mailcatcher'
end
\ No newline at end of file
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.3.2) addressable (2.3.2)
daemons (1.1.8)
eventmachine (0.12.10)
faraday (0.7.6) faraday (0.7.6)
addressable (~> 2.2) addressable (~> 2.2)
multipart-post (~> 1.1) multipart-post (~> 1.1)
rack (~> 1.1) rack (~> 1.1)
haml (3.1.7)
i18n (0.6.1)
json (1.7.5) json (1.7.5)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mailcatcher (0.5.8)
activesupport (~> 3.0)
eventmachine (~> 0.12)
haml (~> 3.1)
mail (~> 2.3)
sinatra (~> 1.2)
skinny (~> 0.2, >= 0.2.1)
sqlite3 (~> 1.3)
thin (~> 1.2)
mime-types (1.19)
multi_json (1.3.6) multi_json (1.3.6)
multipart-post (1.1.5) multipart-post (1.1.5)
oauth2 (0.5.2) oauth2 (0.5.2)
faraday (~> 0.7) faraday (~> 0.7)
multi_json (~> 1.0) multi_json (~> 1.0)
polyglot (0.3.3)
rack (1.4.1) rack (1.4.1)
rack-protection (1.2.0)
rack
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
skinny (0.2.1)
eventmachine (~> 0.12)
thin (~> 1.2)
sqlite3 (1.3.6)
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
tmail (1.2.7.1)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
json json
mailcatcher
oauth2 oauth2
tmail
...@@ -3,7 +3,11 @@ require 'json' ...@@ -3,7 +3,11 @@ require 'json'
class RedmineOauthController < AccountController class RedmineOauthController < AccountController
def oauth_google def oauth_google
if Setting.openid?
redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes) redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes)
else
password_authentication
end
end end
def oauth_google_callback def oauth_google_callback
...@@ -20,8 +24,7 @@ class RedmineOauthController < AccountController ...@@ -20,8 +24,7 @@ class RedmineOauthController < AccountController
user.firstname ||= info[:given_name] user.firstname ||= info[:given_name]
user.lastname ||= info[:family_name] user.lastname ||= info[:family_name]
user.mail = info["email"] user.mail = info["email"]
login = info["email"].match(/(.+)@/) unless info["email"].nil? user.login = email_prefix(info["email"])
user.login = login[1] if login
user.login ||= [user.firstname, user.lastname]*"." user.login ||= [user.firstname, user.lastname]*"."
user.random_password user.random_password
user.register user.register
...@@ -54,6 +57,11 @@ class RedmineOauthController < AccountController ...@@ -54,6 +57,11 @@ class RedmineOauthController < AccountController
end end
end end
def email_prefix email
prefix = email.match(/(.+?)@/) if email
prefix[1] if prefix
end
def oauth_client def oauth_client
@client ||= OAuth2::Client.new(settings[:client_id], settings[:client_secret], @client ||= OAuth2::Client.new(settings[:client_id], settings[:client_secret],
site: 'https://accounts.google.com', site: 'https://accounts.google.com',
......
require File.expand_path('../../test_helper', __FILE__) require File.expand_path('../../test_helper', __FILE__)
class RedmineOauthControllerTest < ActionController::TestCase class RedmineOauthControllerTest < ActionController::TestCase
def setup def setup
@default_user_credentials = { firstname: 'Cool',
lastname: 'User',
mail: 'user@somedomain.com'}
@default_response_body = {verified_email: true,
name: 'Cool User',
given_name: 'Cool',
family_name: 'User',
email: 'user@somedomain.com'}
User.current = nil User.current = nil
Setting.openid = '1' Setting.openid = '1'
OAuth2::AccessToken.any_instance.stubs(get: OAuth2::Response.new(nil)) OAuth2::AccessToken.any_instance.stubs(get: OAuth2::Response.new(nil))
OAuth2::Client.any_instance.stubs(get_token: OAuth2::AccessToken.new('code', 'redirect_uri')) OAuth2::Client.any_instance.stubs(get_token: OAuth2::AccessToken.new('code', 'redirect_uri'))
end end
def set_response_body_stub body
OAuth2::Response.any_instance.stubs(body: body.to_json)
end
def new_user options = nil #creates a new user with the credentials listed in the options and fills in the missing data by default data
user_credentials = {:firstname => 'Cool', def new_user options = {}
:lastname => 'User', User.where(@default_user_credentials.merge(options)).delete_all
:mail => 'user@somedomain.com'}.merge(options) user = User.new @default_user_credentials.merge(options)
user = User.new(user_credentials)
user.login = options[:login] || 'cool_user' user.login = options[:login] || 'cool_user'
user user
end end
def test_login_with_omniauth_for_new_user #creates a new user with the credentials listed in the options and fills in the missing data by default data
def set_response_body_stub options = {}
OAuth2::Response.any_instance.stubs(body: @default_response_body.merge(options).to_json)
end
def test_oauth_google_with_disabled_openid
Setting.openid = false
get :oauth_google
assert_redirected_to signin_path
end
def test_oauth_google_callback_with_oauth_for_existing_non_active_user
Setting.self_registration = '2'
user = new_user status: User::STATUS_REGISTERED
assert user.save
set_response_body_stub
get :oauth_google_callback
assert_redirected_to signin_path
end
def test_oauth_google_callback_with_oauth_for_existing_active_user
user = new_user
user.activate
assert user.save
set_response_body_stub
get :oauth_google_callback
assert_redirected_to controller: 'my', action: 'page'
end
def test_oauth_google_callback_with_omniauth_for_new_user_with_valid_credentials_and_sefregistration_enabled
Setting.self_registration = '3' Setting.self_registration = '3'
user set_response_body_stub
set_response_body_stub({verified_email: "true", name: [new_user.firstname, new_user.lastname]*" ", given_name: new_user.firstname, family_name: new_user.lastname, email: new_user.mail}) get :oauth_google_callback
get :oauth_google_callback, :email => new_user.mail
assert_redirected_to controller: 'my', action: 'account' assert_redirected_to controller: 'my', action: 'account'
user = User.find_by_mail(@default_response_body[:email])
assert_equal user.mail, @default_response_body[:email]
assert_equal user.login, email_prefix(@default_response_body[:email])
end end
def test_login_with_invalid_oauth_provider def test_oauth_google_callback_with_omniauth_for_new_user_with_valid_credentials_and_sefregistration_disabled
Setting.self_registration = '0' Setting.self_registration = '2'
set_response_body_stub
get :oauth_google_callback
assert_redirected_to signin_path
end
def test_oauth_google_callback_with_oauth_for_new_user_with_invalid_oauth_provider
Setting.self_registration = '3'
set_response_body_stub verified_email: false
get :oauth_google_callback
assert_redirected_to signin_path
end end
#def test_login_with
#assert existing_user.save! #assert existing_user.save!
end end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment