cirandas.net

ref: master

plugins/oauth_client/test/unit/oauth_client_plugin_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'test_helper'

class OauthClientPluginTest < ActiveSupport::TestCase

  def setup
    @plugin = OauthClientPlugin.new(self)
    @params = {}
    @plugin.stubs(:context).returns(self)
    @environment = Environment.default
    @session = {}
    @request = mock
    @provider = OauthClientPlugin::Provider.create!(:name => 'name', :strategy => 'strategy')
  end

  attr_reader :params, :plugin, :environment, :session, :request, :provider

  should 'has extra contents for login' do
    assert plugin.login_extra_contents
  end

  should 'has no signup extra contents if no provider was enabled' do
    assert_equal '', instance_eval(&plugin.signup_extra_contents)
  end

  should 'has signup extra contents if oauth_data exists in session' do
    session[:oauth_data] = {:oauth => 'test'}
    expects(:render).with(:partial => 'account/oauth_signup').once
    instance_eval(&plugin.signup_extra_contents)
  end

  should 'define before filter for account controller' do
    assert plugin.account_controller_filters
  end

  should 'raise error if oauth email was changed' do
    request.expects(:post?).returns(true)

    oauth_data = mock
    info = mock
    oauth_data.stubs(:info).returns(info)
    oauth_data.stubs(:uid).returns('uid')
    oauth_data.stubs(:provider).returns('provider')
    info.stubs(:email).returns('test@example.com')
    session[:oauth_data] = oauth_data
    session[:provider_id] = provider.id

    params[:user] = {:email => 'test2@example.com'}
    assert_raises RuntimeError do
      instance_eval(&plugin.account_controller_filters[:block])
    end
  end

  should 'do not raise error if oauth email was not changed' do
    request.expects(:post?).returns(true)

    oauth_data = mock
    info = mock
    oauth_data.stubs(:info).returns(info)
    oauth_data.stubs(:uid).returns('uid')
    oauth_data.stubs(:provider).returns('provider')
    info.stubs(:email).returns('test@example.com')
    session[:oauth_data] = oauth_data
    session[:provider_id] = provider.id

    params[:user] = {:email => 'test@example.com'}
    instance_eval(&plugin.account_controller_filters[:block])
  end

  should 'do not raise error if oauth session is not set' do
    instance_eval(&plugin.account_controller_filters[:block])
  end

  should 'do not raise error if it is not a post' do
    request.expects(:post?).returns(false)
    params[:user] = {:email => 'test2@example.com'}

    oauth_data = mock
    oauth_data.stubs(:uid).returns('uid')
    oauth_data.stubs(:provider).returns('provider')
    session[:provider_id] = provider.id

    session[:oauth_data] = oauth_data
    instance_eval(&plugin.account_controller_filters[:block])
  end

end