Module: Clearance::Shoulda

Defined in:
lib/clearance/shoulda_macros.rb,
lib/clearance/shoulda_macros.rb

Defined Under Namespace

Modules: Helpers

Instance Method Summary collapse

Instance Method Details

#public_context(&blk) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/clearance/shoulda_macros.rb', line 84

def public_context(&blk)
  warn "[DEPRECATION] public_context: common case is no-op. call sign_out otherwise"
  context "The public" do
    setup { sign_out }
    merge_block(&blk)
  end
end

#should_be_signed_in_and_email_confirmed_as(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clearance/shoulda_macros.rb', line 18

def should_be_signed_in_and_email_confirmed_as(&block)
  warn "[DEPRECATION] should_be_signed_in_and_email_confirmed_as: questionable usefulness"
  should_be_signed_in_as &block

  should "have confirmed email" do
    user = block.bind(self).call

    assert_not_nil user
    assert_equal user, assigns(:user)
    assert assigns(:user).email_confirmed?
  end
end

#should_be_signed_in_as(&block) ⇒ Object

STATE OF AUTHENTICATION



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/clearance/shoulda_macros.rb', line 6

def should_be_signed_in_as(&block)
  warn "[DEPRECATION] should_be_signed_in_as cannot be used in functional tests anymore now that it depends on cookies, which are unavailable until the next request."
  should "be signed in as #{block.bind(self).call}" do
    user = block.bind(self).call
    assert_not_nil user,
      "please pass a User. try: should_be_signed_in_as { @user }"
    assert_equal user, @controller.send(:current_user),
      "#{user.inspect} is not the current_user, " <<
      "which is #{@controller.send(:current_user).inspect}"
  end
end

#should_create_user_successfullyObject

CREATING USERS



94
95
96
97
98
99
100
101
102
103
# File 'lib/clearance/shoulda_macros.rb', line 94

def should_create_user_successfully
  warn "[DEPRECATION] should_create_user_successfully: not meant to be public, no longer used internally"
  should assign_to(:user)
  should_change 'User.count', :by => 1

  should have_sent_email.with_subject(/account confirmation/i)

  should set_the_flash.to(/confirm/i)
  should_redirect_to_url_after_create
end

#should_deny_access(opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/clearance/shoulda_macros.rb', line 50

def should_deny_access(opts = {})
  if opts[:flash]
    should set_the_flash.to(opts[:flash])
  else
    should_not set_the_flash
  end

  should redirect_to('sign in page') {  }
end

#should_deny_access_on(http_method, action, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/clearance/shoulda_macros.rb', line 38

def should_deny_access_on(http_method, action, opts = {})
  warn "[DEPRECATION] should_deny_access_on: use a setup & should_deny_access(:flash => ?)"
  flash_message = opts.delete(:flash)
  context "on #{http_method} to #{action}" do
    setup do
      send(http_method, action, opts)
    end

    should_deny_access(:flash => flash_message)
  end
end

#should_display_a_password_update_formObject

FORMS



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/clearance/shoulda_macros.rb', line 165

def should_display_a_password_update_form
  warn "[DEPRECATION] should_display_a_password_update_form: not meant to be public, no longer used internally"
  should "have a form for the user's token, password, and password confirm" do
    update_path = ERB::Util.h(
      user_password_path(@user, :token => @user.confirmation_token)
    )

    assert_select 'form[action=?]', update_path do
      assert_select 'input[name=_method][value=?]', 'put'
      assert_select 'input[name=?]', 'user[password]'
      assert_select 'input[name=?]', 'user[password_confirmation]'
    end
  end
end

#should_display_a_sign_in_formObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/clearance/shoulda_macros.rb', line 197

def 
  warn "[DEPRECATION] should_display_a_sign_in_form: not meant to be public, no longer used internally"
  should 'display a "sign in" form' do
    assert_select "form[action=#{session_path}][method=post]",
      true, "There must be a form to sign in" do
        assert_select "input[type=text][name=?]",
          "session[email]", true, "There must be an email field"
        assert_select "input[type=password][name=?]",
          "session[password]", true, "There must be a password field"
        assert_select "input[type=submit]", true,
          "There must be a submit button"
    end
  end
end

#should_display_a_sign_up_formObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/clearance/shoulda_macros.rb', line 180

def 
  warn "[DEPRECATION] should_display_a_sign_up_form: not meant to be public, no longer used internally"
  should "display a form to sign up" do
    assert_select "form[action=#{users_path}][method=post]",
    true, "There must be a form to sign up" do
      assert_select "input[type=text][name=?]",
        "user[email]", true, "There must be an email field"
      assert_select "input[type=password][name=?]",
        "user[password]", true, "There must be a password field"
      assert_select "input[type=password][name=?]",
        "user[password_confirmation]", true, "There must be a password confirmation field"
      assert_select "input[type=submit]", true,
        "There must be a submit button"
    end
  end
end

#should_forbid(description, &block) ⇒ Object

HTTP FLUENCY



62
63
64
65
66
67
68
# File 'lib/clearance/shoulda_macros.rb', line 62

def should_forbid(description, &block)
  should "forbid #{description}" do
    assert_raises ActionController::Forbidden do
      instance_eval(&block)
    end
  end
end

#should_not_be_signed_inObject



31
32
33
34
35
36
# File 'lib/clearance/shoulda_macros.rb', line 31

def should_not_be_signed_in
  warn "[DEPRECATION] should_not_be_signed_in is no longer a valid test since we now store a remember_token in cookies, not user_id in session"
  should "not be signed in" do
    assert_nil session[:user_id]
  end
end

#should_redirect_to_url_after_createObject

REDIRECTS



115
116
117
# File 'lib/clearance/shoulda_macros.rb', line 115

def should_redirect_to_url_after_create
  should redirect_to("the post-create url") { @controller.send(:url_after_create) }
end

#should_redirect_to_url_after_destroyObject



123
124
125
# File 'lib/clearance/shoulda_macros.rb', line 123

def should_redirect_to_url_after_destroy
  should redirect_to("the post-destroy url") { @controller.send(:url_after_destroy) }
end

#should_redirect_to_url_after_updateObject



119
120
121
# File 'lib/clearance/shoulda_macros.rb', line 119

def should_redirect_to_url_after_update
  should redirect_to("the post-update url") { @controller.send(:url_after_update) }
end

#should_redirect_to_url_already_confirmedObject



127
128
129
# File 'lib/clearance/shoulda_macros.rb', line 127

def should_redirect_to_url_already_confirmed
  should redirect_to("the already confirmed url") { @controller.send(:url_already_confirmed) }
end

#should_render_nothingObject

RENDERING



107
108
109
110
111
# File 'lib/clearance/shoulda_macros.rb', line 107

def should_render_nothing
  should "render nothing" do
    assert @response.body.blank?
  end
end

#should_validate_confirmation_is_not_bad(factory, attribute, opts = {}) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/clearance/shoulda_macros.rb', line 153

def should_validate_confirmation_is_not_bad(factory, attribute, opts = {})
  warn "[DEPRECATION] should_validate_confirmation_is_not_bad: not meant to be public, no longer used internally"
  should "validate #{attribute}_confirmation is different than #{attribute}" do
    model = Factory.build(factory, bad_confirmation_options(attribute))
    model.save
    assert_confirmation_error(model, attribute, 
      "#{attribute}_confirmation cannot be different than #{attribute}")
  end
end

#should_validate_confirmation_is_not_blank(factory, attribute, opts = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/clearance/shoulda_macros.rb', line 143

def should_validate_confirmation_is_not_blank(factory, attribute, opts = {})
  warn "[DEPRECATION] should_validate_confirmation_is_not_blank: not meant to be public, no longer used internally"
  should "validate #{attribute}_confirmation is not blank" do
    model = Factory.build(factory, blank_confirmation_options(attribute))
    model.save
    assert_confirmation_error(model, attribute,
      "#{attribute}_confirmation cannot be blank")
  end
end

#should_validate_confirmation_of(attribute, opts = {}) ⇒ Object

VALIDATIONS

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
141
# File 'lib/clearance/shoulda_macros.rb', line 133

def should_validate_confirmation_of(attribute, opts = {})
  warn "[DEPRECATION] should_validate_confirmation_of: not meant to be public, no longer used internally"
  raise ArgumentError if opts[:factory].nil?

  context "on save" do
    should_validate_confirmation_is_not_blank opts[:factory], attribute
    should_validate_confirmation_is_not_bad   opts[:factory], attribute
  end
end

#signed_in_user_context(&blk) ⇒ Object

CONTEXTS



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/clearance/shoulda_macros.rb', line 72

def signed_in_user_context(&blk)
  warn "[DEPRECATION] signed_in_user_context: creates a Mystery Guest, causes Obscure Test"
  context "A signed in user" do
    setup do
      @user = Factory(:user)
      @user.confirm_email!
       @user
    end
    merge_block(&blk)
  end
end