cirandas.net

ref: master

test/unit/forgot_password_helper_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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require_relative "../test_helper"

class ForgotPasswordHelperTest < ActionView::TestCase
  include ForgotPasswordHelper

  def setup
    @environment = Environment.default
    @plugins = Noosfero::Plugin::Manager.new(@environment, self)
  end

  attr_accessor :environment

  should 'allow extra fields provided by plugins' do
    class Plugin1 < Noosfero::Plugin
      def change_password_fields
        {:field => 'f1', :name => 'F1', :model => 'person'}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def change_password_fields
        [{:field => 'f2', :name => 'F2', :model => 'person'},
         {:field => 'f3', :name => 'F3', :model => 'person'}]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['ForgotPasswordHelperTest::Plugin1', 'ForgotPasswordHelperTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_includes fields, 'f1'
    assert_includes fields, 'f2'
    assert_includes fields, 'f3'
  end

  should 'allow extra person fields provided by plugins' do
    class Plugin1 < Noosfero::Plugin
      def change_password_fields
        {:field => 'f1', :name => 'F1', :model => 'person'}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def change_password_fields
        [{:field => 'f2', :name => 'F2', :model => 'user'},
         {:field => 'f3', :name => 'F3', :model => 'person'}]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['ForgotPasswordHelperTest::Plugin1', 'ForgotPasswordHelperTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_includes person_fields, 'f1'
    assert_not_includes person_fields, 'f2'
    assert_includes person_fields, 'f3'
  end

  should 'allow extra user fields provided by plugins' do
    class Plugin1 < Noosfero::Plugin
      def change_password_fields
        {:field => 'f1', :name => 'F1', :model => 'user'}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def change_password_fields
        [{:field => 'f2', :name => 'F2', :model => 'person'},
         {:field => 'f3', :name => 'F3', :model => 'user'}]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['ForgotPasswordHelperTest::Plugin1', 'ForgotPasswordHelperTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_includes user_fields, 'f1'
    assert_not_includes user_fields, 'f2'
    assert_includes user_fields, 'f3'
  end

  should 'add plugins fields labels to final label' do
    class Plugin1 < Noosfero::Plugin
      def change_password_fields
        {:field => 'f1', :name => 'F1', :model => 'user'}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def change_password_fields
        [{:field => 'f2', :name => 'F2', :model => 'person'},
         {:field => 'f3', :name => 'F3', :model => 'user'}]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['ForgotPasswordHelperTest::Plugin1', 'ForgotPasswordHelperTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_match /F1/, fields_label
    assert_match /F2/, fields_label
    assert_match /F3/, fields_label
  end

  should 'fetch requestors based on fields available' do
    p1 = create_user('person1').person
    p2 = create_user('person2').person

    requestors = fetch_requestors('person1')
    assert_includes requestors, p1
    assert_not_includes requestors, p2

    p3 = create_user('person3').person
    p3.address = 'some address'
    p3.save!
    p4 = create_user('person4').person
    p4.address = 'some address'
    p4.save!
    p5 = create_user('person5').person
    p5.address = 'another address'
    p5.save!

    self.stubs(:person_fields).returns(%w[address])
    requestors = fetch_requestors('some address')
    assert_includes requestors, p3
    assert_includes requestors, p4
  end

  should 'not fetch people from other environments' do
    p1 = create_user('person', :environment => fast_create(Environment)).person
    p2 = create_user('person').person

    requestors = fetch_requestors('person')
    assert_not_includes requestors, p1
    assert_includes requestors, p2
  end
end