cirandas.net

ref: master

test/functional/contact_controller_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require_relative '../test_helper'

class ContactControllerTest < ActionController::TestCase

  all_fixtures

  def setup
    @controller = ContactController.new

    @profile = create_user('contact_test_user').person
    @enterprise = fast_create(Enterprise, :identifier => 'contact_test_enterprise', :name => 'Test contact enteprise')

    login_as('contact_test_user')
  end
  attr_reader :profile, :enterprise

  should 'respond to new' do
    get :new, :profile => enterprise.identifier
    assert_response :success
  end

  should 'display destinatary name in title if profile is a person' do
    get :new, :profile => profile.identifier
    assert_tag :tag => 'h1', :content => /Send.*#{profile.name}/
  end

  should 'display administrators in title if profile is an organization' do
    get :new, :profile => enterprise.identifier
    assert_tag :tag => 'h1', :content => /Send.*administrators/
  end

  should 'display profile name in tooltip if profile is an organization' do
    get :new, :profile => enterprise.identifier
    assert_tag :tag => 'div', :content => /administrators.*#{enterprise.name}/, :attributes => {:class => 'tooltip'}
  end

  should 'not display tooltip if profile is a person' do
    get :new, :profile => profile.identifier
    assert_no_tag :tag => 'div', :attributes => {:class => 'tooltip'}
  end

  should 'add form to create contact via post' do
    get :new, :profile => enterprise.identifier
    assert_tag :tag => 'form', :attributes => { :action => "/contact/#{enterprise.identifier}/new", :method => 'post' }
  end

  should 'display input for message' do
    get :new, :profile => enterprise.identifier
    assert_tag :tag => 'textarea', :attributes => { :name => 'contact[message]' }
  end

  should 'have logged user email' do
    get :new, :profile => enterprise.identifier
    assert_equal profile.email, assigns(:contact).email
  end

  should 'have logged user name' do
    get :new, :profile => enterprise.identifier
    assert_equal profile.name, assigns(:contact).name
  end

  should 'display checkbox for receive copy of email' do
    get :new, :profile => enterprise.identifier
    assert_tag :tag => 'input', :attributes => {:name => 'contact[receive_a_copy]'}
  end

  should 'not throws exception when city and state is blank' do
    State.expects(:exists?).with('').never
    City.expects(:exists?).with('').never
    assert_nothing_raised do
      post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''}
    end
  end

  should 'not display select/city select when disable it in environment' do
    state = State.create!(:name => "Bahia", :environment => Environment.default)
    get :new, :profile => profile.identifier
    assert_tag :tag => 'select', :attributes => {:name => 'state'}
    env = Environment.default
    env.enable('disable_select_city_for_contact')
    env.save!
    get :new, :profile => profile.identifier
    assert_no_tag :tag => 'select', :attributes => {:name => 'state'}
  end

  should 'show name, email and captcha if not logged' do
    logout
    get :new, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => {:name => 'contact[name]'}
    assert_tag :tag => 'input', :attributes => {:name => 'contact[email]'}
    assert_tag :attributes => {id: 'dynamic_recaptcha'}
  end

  should 'identify sender' do
    post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all', :state => '', :city => ''}
    assert_equal Person['contact_test_user'], assigns(:contact).sender
  end

  should 'deliver contact if subject and message are filled' do
    post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'}, :confirm => 'true'
    assert_response :redirect
    assert_redirected_to :action => 'new'
  end

  should 'redirect back to contact page after send contact' do
    post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'}, :confirm => 'true'
    assert_response :redirect
    assert_redirected_to :action => 'new'
  end

  should 'define city and state for contact object' do
    City.stubs(:exists?).returns(true)
    City.stubs(:find).returns(City.new(:name => 'Camaçari'))
    State.stubs(:exists?).returns(true)
    State.stubs(:find).returns(State.new(:name => 'Bahia'))
    post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :message => 'Hi, all'}, :state => '1', :city => '1', :confirm => 'true'
    assert_equal 'Camaçari', assigns(:contact).city
    assert_equal 'Bahia', assigns(:contact).state
  end

  should 'not show send e-mail page to non members of private community' do
    community = fast_create(Community, :identifier => 'private-community', :name => 'Private Community', :public_profile => false)

    post :new, :profile => community.identifier

    assert_response :forbidden
    assert_template "profile/_private_profile"
  end

  should 'not show send e-mail page to non members of invisible community' do
    community = fast_create(Community, :identifier => 'invisible-community', :name => 'Private Community', :visible => false)

    post :new, :profile => community.identifier

    assert_response :forbidden
    assert_template 'shared/access_denied'
  end

  should 'show send e-mail page to members of private community' do
    community = fast_create(Community, :identifier => 'private-community', :name => 'Private Community', :public_profile => false)
    community.add_member(@profile)

    post :new, :profile => community.identifier

    assert_response :success
  end

end