Commit 7c98d6857ebd25727e038ec6d374e5fc61204378
1 parent
1f0c22ce
Exists in
staging
and in
4 other branches
oauth_client: added tests
Showing
5 changed files
with
126 additions
and
6 deletions
Show diff stats
plugins/oauth_client/lib/ext/user.rb
| ... | ... | @@ -28,11 +28,7 @@ class User |
| 28 | 28 | end |
| 29 | 29 | |
| 30 | 30 | def make_activation_code_with_oauth |
| 31 | - if oauth_providers.blank? | |
| 32 | - nil | |
| 33 | - else | |
| 34 | - make_activation_code_without_oauth | |
| 35 | - end | |
| 31 | + oauth_providers.blank? ? make_activation_code_without_oauth : nil | |
| 36 | 32 | end |
| 37 | 33 | |
| 38 | 34 | alias_method_chain :make_activation_code, :oauth | ... | ... |
plugins/oauth_client/lib/oauth_client_plugin.rb
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +require File.dirname(__FILE__) + '/../../../test/test_helper' | ... | ... |
plugins/oauth_client/test/unit/oauth_client_plugin_test.rb
0 → 100644
| ... | ... | @@ -0,0 +1,74 @@ |
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | + | |
| 3 | +class OauthClientPluginTest < ActiveSupport::TestCase | |
| 4 | + | |
| 5 | + def setup | |
| 6 | + @plugin = OauthClientPlugin.new | |
| 7 | + @params = {} | |
| 8 | + @plugin.stubs(:context).returns(self) | |
| 9 | + @environment = Environment.default | |
| 10 | + end | |
| 11 | + | |
| 12 | + attr_reader :params, :plugin, :environment | |
| 13 | + | |
| 14 | + should 'has extra contents for login' do | |
| 15 | + assert plugin.login_extra_contents | |
| 16 | + end | |
| 17 | + | |
| 18 | + should 'has no signup extra contents if no provider was enabled' do | |
| 19 | + assert_equal '', instance_eval(&plugin.signup_extra_contents) | |
| 20 | + end | |
| 21 | + | |
| 22 | + should 'has signup extra contents if there is enabled providers' do | |
| 23 | + params[:user] = {:oauth_providers => [:provider]} | |
| 24 | + expects(:render).with(:partial => 'account/oauth_signup').once | |
| 25 | + instance_eval(&plugin.signup_extra_contents) | |
| 26 | + end | |
| 27 | + | |
| 28 | + should 'list enabled providers' do | |
| 29 | + settings = Noosfero::Plugin::Settings.new(environment, OauthClientPlugin) | |
| 30 | + providers = {:test => {:enabled => true}, :test2 => {:enabled => false}} | |
| 31 | + settings.set_setting(:providers, providers) | |
| 32 | + assert_equal({:test => {:enabled => true}}, plugin.enabled_providers) | |
| 33 | + end | |
| 34 | + | |
| 35 | + should 'define before filter for account controller' do | |
| 36 | + assert plugin.account_controller_filters | |
| 37 | + end | |
| 38 | + | |
| 39 | + should 'raise error if oauth email was changed' do | |
| 40 | + request = mock | |
| 41 | + stubs(:request).returns(request) | |
| 42 | + request.expects(:post?).returns(true) | |
| 43 | + stubs(:session).returns({:oauth_email => 'test@example.com'}) | |
| 44 | + params[:user] = {:email => 'test2@example.com'} | |
| 45 | + assert_raises RuntimeError do | |
| 46 | + instance_eval(&plugin.account_controller_filters[:block]) | |
| 47 | + end | |
| 48 | + end | |
| 49 | + | |
| 50 | + should 'do not raise error if oauth email was not changed' do | |
| 51 | + request = mock | |
| 52 | + stubs(:request).returns(request) | |
| 53 | + request.expects(:post?).returns(true) | |
| 54 | + stubs(:session).returns({:oauth_email => 'test@example.com'}) | |
| 55 | + params[:user] = {:email => 'test@example.com'} | |
| 56 | + instance_eval(&plugin.account_controller_filters[:block]) | |
| 57 | + end | |
| 58 | + | |
| 59 | + should 'do not raise error if oauth email is not set' do | |
| 60 | + request = mock | |
| 61 | + stubs(:request).returns(request) | |
| 62 | + request.expects(:post?).returns(true) | |
| 63 | + stubs(:session).returns({}) | |
| 64 | + instance_eval(&plugin.account_controller_filters[:block]) | |
| 65 | + end | |
| 66 | + | |
| 67 | + should 'do not raise error if it is not a post' do | |
| 68 | + request = mock | |
| 69 | + stubs(:request).returns(request) | |
| 70 | + request.expects(:post?).returns(false) | |
| 71 | + instance_eval(&plugin.account_controller_filters[:block]) | |
| 72 | + end | |
| 73 | + | |
| 74 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,49 @@ |
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | + | |
| 3 | +class UserTest < ActiveSupport::TestCase | |
| 4 | + | |
| 5 | + should 'find with omniauth params' do | |
| 6 | + user = fast_create(User) | |
| 7 | + user.settings[:oauth_providers] = [:test => {}] | |
| 8 | + user.save! | |
| 9 | + auth = {:info => OpenStruct.new({:email => user.email})} | |
| 10 | + assert_equal user, User.find_with_omniauth(OpenStruct.new(auth)) | |
| 11 | + end | |
| 12 | + | |
| 13 | + should 'do not return user if there is no provider' do | |
| 14 | + user = fast_create(User) | |
| 15 | + auth = {:info => OpenStruct.new({:email => user.email})} | |
| 16 | + assert_equal nil, User.find_with_omniauth(OpenStruct.new(auth)) | |
| 17 | + end | |
| 18 | + | |
| 19 | + should 'password is not required if there is a oauth provider' do | |
| 20 | + User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | |
| 21 | + end | |
| 22 | + | |
| 23 | + should 'password is required if there is a oauth provider' do | |
| 24 | + user = User.new(:email => 'testoauth@example.com', :login => 'testoauth') | |
| 25 | + user.save | |
| 26 | + assert user.errors[:password].present? | |
| 27 | + end | |
| 28 | + | |
| 29 | + should 'activate user when created with oauth' do | |
| 30 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | |
| 31 | + assert user.activated? | |
| 32 | + end | |
| 33 | + | |
| 34 | + should 'not activate user when created without oauth' do | |
| 35 | + user = fast_create(User) | |
| 36 | + assert !user.activated? | |
| 37 | + end | |
| 38 | + | |
| 39 | + should 'not make activation code when created with oauth' do | |
| 40 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :oauth_providers => [:test]) | |
| 41 | + assert !user.activation_code | |
| 42 | + end | |
| 43 | + | |
| 44 | + should 'make activation code when created without oauth' do | |
| 45 | + user = User.create!(:email => 'testoauth@example.com', :login => 'testoauth', :password => 'test', :password_confirmation => 'test') | |
| 46 | + assert user.activation_code | |
| 47 | + end | |
| 48 | + | |
| 49 | +end | ... | ... |