From ac9585229983b28ef0d92424d5166b1b9c22c8c6 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: Tue, 6 Nov 2012 13:31:04 +0300
Subject: [PATCH] writing documentation & testing

---
 Gemfile                                       |  1 +
 Gemfile.lock                                  | 10 ++++-
 app/controllers/redmine_oauth_controller.rb   |  8 +---
 .../hooks/_view_account_login_bottom.html.erb |  2 +-
 app/views/settings/_google_settings.html.erb  |  4 ++
 init.rb                                       |  4 +-
 lib/helpers/mail_helper.rb                    |  8 ++++
 .../redmine_oauth_controller_test.rb          | 40 ++++++++++++++-----
 8 files changed, 55 insertions(+), 22 deletions(-)
 create mode 100644 lib/helpers/mail_helper.rb

diff --git a/Gemfile b/Gemfile
index 74038f2..1fd0171 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,4 +5,5 @@ gem 'json'
 
 group :development, :test do
   gem 'mailcatcher'
+  gem 'pry'
 end
\ No newline at end of file
diff --git a/Gemfile.lock b/Gemfile.lock
index fc83bce..2fcbb31 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -5,6 +5,7 @@ GEM
       i18n (~> 0.6)
       multi_json (~> 1.0)
     addressable (2.3.2)
+    coderay (1.0.7)
     daemons (1.1.8)
     eventmachine (0.12.10)
     faraday (0.7.6)
@@ -27,6 +28,7 @@ GEM
       skinny (~> 0.2, >= 0.2.1)
       sqlite3 (~> 1.3)
       thin (~> 1.2)
+    method_source (0.8)
     mime-types (1.19)
     multi_json (1.3.6)
     multipart-post (1.1.5)
@@ -34,6 +36,10 @@ GEM
       faraday (~> 0.7)
       multi_json (~> 1.0)
     polyglot (0.3.3)
+    pry (0.9.10)
+      coderay (~> 1.0.5)
+      method_source (~> 0.8)
+      slop (~> 3.3.1)
     rack (1.4.1)
     rack-protection (1.2.0)
       rack
@@ -44,13 +50,13 @@ GEM
     skinny (0.2.1)
       eventmachine (~> 0.12)
       thin (~> 1.2)
+    slop (3.3.3)
     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)
@@ -62,4 +68,4 @@ DEPENDENCIES
   json
   mailcatcher
   oauth2
-  tmail
+  pry
diff --git a/app/controllers/redmine_oauth_controller.rb b/app/controllers/redmine_oauth_controller.rb
index 8cbf886..19ba295 100644
--- a/app/controllers/redmine_oauth_controller.rb
+++ b/app/controllers/redmine_oauth_controller.rb
@@ -2,8 +2,9 @@ require 'account_controller'
 require 'json'
 
 class RedmineOauthController < AccountController
+  include Helpers::MailHelper
   def oauth_google
-    if Setting.openid?
+    if Setting.plugin_redmine_omniauth_google[:oauth_authentification]
       redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes)
     else
       password_authentication
@@ -57,11 +58,6 @@ 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/app/views/hooks/_view_account_login_bottom.html.erb b/app/views/hooks/_view_account_login_bottom.html.erb
index 09d8ea9..524d507 100644
--- a/app/views/hooks/_view_account_login_bottom.html.erb
+++ b/app/views/hooks/_view_account_login_bottom.html.erb
@@ -1,3 +1,3 @@
-<% if Setting.openid? %>
+<% if Setting.plugin_redmine_omniauth_google[:oauth_authentification] %>
   <%= link_to image_tag('/plugin_assets/redmine_omniauth_google/images/google_login_icon.jpg'), oauth_google_path %>
 <% end %>
\ No newline at end of file
diff --git a/app/views/settings/_google_settings.html.erb b/app/views/settings/_google_settings.html.erb
index 1fe7a11..969f568 100644
--- a/app/views/settings/_google_settings.html.erb
+++ b/app/views/settings/_google_settings.html.erb
@@ -5,4 +5,8 @@
 <p>
   <label>Client Secret:</label>
   <%= text_field_tag 'settings[client_secret]', @settings[:client_secret] %>
+</p>
+<p>
+  <label>Oauth authentification:</label>
+  <%= check_box_tag "settings[oauth_authentification]", true, @settings[:oauth_authentification] %>
 </p>
\ No newline at end of file
diff --git a/init.rb b/init.rb
index 009f2c7..d12705b 100644
--- a/init.rb
+++ b/init.rb
@@ -10,5 +10,5 @@ Redmine::Plugin.register :redmine_omniauth_google do
   author_url 'https://tsdv.net/redmine/users/105'
   settings default: {
     client_id: '214698823792.apps.googleusercontent.com',
-    client_secret: 'M0HJPMypEgrDAKKHGiP6Y2R-' }, partial: 'settings/google_settings'
-end
+    client_secret: 'M0HJPMypEgrDAKKHGiP6Y2R-', oauth_autentification: false}, partial: 'settings/google_settings'
+end
\ No newline at end of file
diff --git a/lib/helpers/mail_helper.rb b/lib/helpers/mail_helper.rb
new file mode 100644
index 0000000..d1087f9
--- /dev/null
+++ b/lib/helpers/mail_helper.rb
@@ -0,0 +1,8 @@
+module Helpers
+  module MailHelper
+    def email_prefix email
+      prefix = email.match(/(.+?)@/) if email
+      prefix[1] if prefix
+    end
+  end
+end
\ No newline at end of file
diff --git a/test/functional/redmine_oauth_controller_test.rb b/test/functional/redmine_oauth_controller_test.rb
index 933a41f..15690e4 100644
--- a/test/functional/redmine_oauth_controller_test.rb
+++ b/test/functional/redmine_oauth_controller_test.rb
@@ -1,7 +1,8 @@
 require File.expand_path('../../test_helper', __FILE__)
+#require File.expand_path('../../../lib/helpers/mail_helper', __FILE__)
 
 class RedmineOauthControllerTest < ActionController::TestCase
-
+  include Helpers::MailHelper
   def setup
     @default_user_credentials = { firstname: 'Cool',
                                   lastname: 'User',
@@ -30,13 +31,13 @@ class RedmineOauthControllerTest < ActionController::TestCase
     OAuth2::Response.any_instance.stubs(body: @default_response_body.merge(options).to_json)
   end
 
-  def test_oauth_google_with_disabled_openid
-    Setting.openid = false
+  def test_oauth_google_with_enabled_oauth_authentification
+    Setting.plugin_redmine_omniauth_google[:oauth_authentification] = nil
     get :oauth_google
-    assert_redirected_to signin_path
+    assert_response 404
   end
 
-  def test_oauth_google_callback_with_oauth_for_existing_non_active_user
+  def test_oauth_google_callback_for_existing_non_active_user
     Setting.self_registration = '2'
     user = new_user status: User::STATUS_REGISTERED
     assert user.save
@@ -45,7 +46,7 @@ class RedmineOauthControllerTest < ActionController::TestCase
     assert_redirected_to signin_path
   end
 
-  def test_oauth_google_callback_with_oauth_for_existing_active_user
+  def test_oauth_google_callback_for_existing_active_user
     user = new_user
     user.activate
     assert user.save
@@ -54,7 +55,7 @@ class RedmineOauthControllerTest < ActionController::TestCase
     assert_redirected_to controller: 'my', action: 'page'
   end
 
-  def test_oauth_google_callback_with_omniauth_for_new_user_with_valid_credentials_and_sefregistration_enabled
+  def test_oauth_google_callback_for_new_user_with_valid_credentials_and_sefregistration_enabled
     Setting.self_registration = '3'
     set_response_body_stub
     get :oauth_google_callback
@@ -64,21 +65,38 @@ class RedmineOauthControllerTest < ActionController::TestCase
     assert_equal user.login, email_prefix(@default_response_body[:email])
   end
 
-  def test_oauth_google_callback_with_omniauth_for_new_user_with_valid_credentials_and_sefregistration_disabled
+  def test_oauth_google_callback_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
+  def test_oauth_google_callback_with_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
+  def test_oauth_google_callback_with_new_user_created_with_email_activation_should_have_a_token
+    Setting.self_registration = '1'
+    set_response_body_stub
+    get :oauth_google_callback
+    assert_redirected_to :signin
+    user = User.find_by_mail(@default_user_credentials[:mail])
+    assert user
+    token = Token.find_by_user_id_and_action(user.id, 'register')
+    assert token
+  end
 
-  #assert existing_user.save!
+  def test_oauth_google_callback_with_new_user_created_with_manual_activation
+      Setting.self_registration = '2'
+      set_response_body_stub
+      get :oauth_google_callback
+      assert_redirected_to :signin
+      user = User.find_by_mail(@default_user_credentials[:mail])
+      assert user
+      assert_equal User::STATUS_REGISTERED, user.status
+  end
 end
\ No newline at end of file
-- 
GitLab