From a7a020ada067cd7b6d829452de82b57c24fcd0cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9A=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B5=D0=BD=D0=BE=D0=BA?= <dmitry.kovalenok@twinslash.com> Date: Fri, 2 Nov 2012 13:46:47 +0300 Subject: [PATCH] testing --- Gemfile | 6 +- Gemfile.lock | 43 +++++++++++ app/controllers/redmine_oauth_controller.rb | 14 +++- .../redmine_oauth_controller_test.rb | 75 +++++++++++++++---- 4 files changed, 120 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index 9ee69f8..74038f2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,8 @@ source 'http://rubygems.org' gem 'oauth2' -gem 'json' \ No newline at end of file +gem 'json' + +group :development, :test do + gem 'mailcatcher' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index a495cd5..fc83bce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,22 +1,65 @@ GEM remote: http://rubygems.org/ specs: + activesupport (3.2.8) + i18n (~> 0.6) + multi_json (~> 1.0) addressable (2.3.2) + daemons (1.1.8) + eventmachine (0.12.10) faraday (0.7.6) addressable (~> 2.2) multipart-post (~> 1.1) rack (~> 1.1) + haml (3.1.7) + i18n (0.6.1) 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) multipart-post (1.1.5) oauth2 (0.5.2) faraday (~> 0.7) multi_json (~> 1.0) + polyglot (0.3.3) 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 ruby DEPENDENCIES json + mailcatcher oauth2 + tmail diff --git a/app/controllers/redmine_oauth_controller.rb b/app/controllers/redmine_oauth_controller.rb index 28ca925..8cbf886 100644 --- a/app/controllers/redmine_oauth_controller.rb +++ b/app/controllers/redmine_oauth_controller.rb @@ -3,7 +3,11 @@ require 'json' class RedmineOauthController < AccountController def oauth_google - redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes) + if Setting.openid? + redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes) + else + password_authentication + end end def oauth_google_callback @@ -20,8 +24,7 @@ class RedmineOauthController < AccountController user.firstname ||= info[:given_name] user.lastname ||= info[:family_name] user.mail = info["email"] - login = info["email"].match(/(.+)@/) unless info["email"].nil? - user.login = login[1] if login + user.login = email_prefix(info["email"]) user.login ||= [user.firstname, user.lastname]*"." user.random_password user.register @@ -54,6 +57,11 @@ class RedmineOauthController < AccountController end end + def email_prefix email + prefix = email.match(/(.+?)@/) if email + prefix[1] if prefix + end + def oauth_client @client ||= OAuth2::Client.new(settings[:client_id], settings[:client_secret], site: 'https://accounts.google.com', diff --git a/test/functional/redmine_oauth_controller_test.rb b/test/functional/redmine_oauth_controller_test.rb index 96b1bec..933a41f 100644 --- a/test/functional/redmine_oauth_controller_test.rb +++ b/test/functional/redmine_oauth_controller_test.rb @@ -1,37 +1,84 @@ require File.expand_path('../../test_helper', __FILE__) class RedmineOauthControllerTest < ActionController::TestCase + 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 Setting.openid = '1' OAuth2::AccessToken.any_instance.stubs(get: OAuth2::Response.new(nil)) OAuth2::Client.any_instance.stubs(get_token: OAuth2::AccessToken.new('code', 'redirect_uri')) end - def set_response_body_stub body - OAuth2::Response.any_instance.stubs(body: body.to_json) - end - def new_user options = nil - user_credentials = {:firstname => 'Cool', - :lastname => 'User', - :mail => 'user@somedomain.com'}.merge(options) - user = User.new(user_credentials) + #creates a new user with the credentials listed in the options and fills in the missing data by default data + def new_user options = {} + User.where(@default_user_credentials.merge(options)).delete_all + user = User.new @default_user_credentials.merge(options) user.login = options[:login] || 'cool_user' user 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' - user - 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, :email => new_user.mail + set_response_body_stub + get :oauth_google_callback 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 - def test_login_with_invalid_oauth_provider - Setting.self_registration = '0' + def test_oauth_google_callback_with_omniauth_for_new_user_with_valid_credentials_and_sefregistration_disabled + 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 + #def test_login_with + #assert existing_user.save! end \ No newline at end of file -- GitLab