レールは、ケ認証&RSpecどの試験の新しいモデルを必要とする認証
-
09-06-2019 - |
質問
私の知育アプリケーションを使用 Bort, 拠点としてアプリを含む安ら認証RSpec.そうだったんで稼動に加え、新しいオブジェクトを必要とするユーザーがログインする前に何でもできる(before_filter :login_required
のコントローラー).[編集:付言すると、ユーザー has_many
新しいクラスだけをユーザが見ることができます。]
私の新しいモデルコントローラを使用Rspecの発電機として作り上げた多数のデフォルト試験までを実施。彼らはすべてのパスがない場合 before_filter
しかし、失敗していることはあると思いますが、 before_filter
ています。
どうやっていけばいいですか?に発生する試験としての実行がある場合は/はログインユーザー?を必要としないシステム構築も全体のバッチのマッチングログインしていない-リダイレクトを検査できないのかこの何らかの嘲笑うや器具の技術面だけでなく、これまでRSpec少漂.良いRSpecチュートリアルリンクもできます。
解決
私も同様の設定は、以下のコードを使っている。それぞれの describe
sくつろぎいただけます:
it_should_behave_like "login-required object"
def attempt_access; do_post; end
場合に会員になるために必要なログインするか、
it_should_behave_like "ownership-required object"
def login_as_object_owner; login_as @product.user; end
def attempt_access; do_put; end
def successful_ownership_access
response.should redirect_to(product_url(@product))
end
が必要な場合は保有しました。当然のことながら、介助の方法の変更非常に少ない)、これはほとんどの作品です。これは私のspec_helper.rb
shared_examples_for "login-required object" do
it "should not be able to access this without logging in" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { redirect_to(login_url) }
format.xml { response.status_code.should == 401 }
end
end
end
shared_examples_for "ownership-required object" do
it_should_behave_like "login-required object"
it "should not be able to access this without owning it" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { response.should be_redirect }
format.xml { response.status_code.should == 401 }
end
end
it "should be able to access this if you own it" do
login_as_object_owner
attempt_access
if respond_to?(:successful_ownership_access)
successful_ownership_access
else
response.should be_success
end
end
end
他のヒント
ない場合は試験の認証が試験のタイミング制御やセンシング制御のニーズ、ユーザが認証されていた通常はスタブにフィルター方式:
before(:each) do
controller.stub!(:authenticate).and_return(true)
end
上記の例の作品が私のbefore_filter設定の認証方法:
before_filter :authenticate
の認証を私のアプリ利用のHTTP基本認証がいきがちですが、もっとできるその他の認証機構。
private
def authenticate
authenticate_or_request_with_http_basic do |user,password|
user == USER_NAME && password == PASSWORD
end
end
と思っているかをわかりやすく。
そして今日の答えを自分の質問です。うちは基本的に必要なのかを模擬ユーザーから restful_authentication
そのautogenerated rspecコントローラの試験にもパスしてきた私でも追加 before_filter: login_required
.
ここにいくつかの例がありますので見つかり資源
rspec,restful_authentication、login_required
模擬ユーザーがログインし、ハッキングのコントローラの設定 @current_user
手動
module AuthHelper
protected
def login_as(model, id_or_attributes = {})
attributes = id_or_attributes.is_a?(Fixnum) ? {:id => id} : id_or_attributes
@current_user = stub_model(model, attributes)
target = controller rescue template
target.instance_variable_set '@current_user', @current_user
if block_given?
yield
target.instance_variable_set '@current_user', nil
end
return @current_user
end
def login_as_user(id_or_attributes = {}, &block)
login_as(User, id_or_attributes, &block)
end
end