2017-08-29 2 views
0

現在、レール(5.0.5)で奇妙な問題があります。私はレンダリングと戻りが実行されているので、binding.pryを使用することができます。ステータスコードが返されているはずですが、テストを実行すると、代わりにステータスコード204のコンテンツは返されません。私のテストでRailsは明示的に返すように指示されたときに204を返す401

# we already know this user has permission for org_claim_codes#create but we need to check they are part of this org 
unless @current_user.orgs.map(&:id).include?(new_org_claim_code_params[:org_id].to_i) || @current_user.has_permission?('*', '*') 
    render json: { errors: ['user not authorized']}, status: :forbidden && return 
end 

私は状況が

expect(response).to have_http_status(:forbidden) 

を禁止されることを期待するが、私は

Failures: 

1) OrgClaimCodesController#create permissions does not allow a user without permission weather or not they are in the org 
Failure/Error: expect(response).to have_http_status(:forbidden) 
    expected the response to have status code :forbidden (403) but it was :no_content (204) 
# ./spec/controllers/org_claim_codes_controller_spec.rb:49:in `block (4 levels) in <top (required)>' 

Finished in 0.3044 seconds (files took 2.26 seconds to load) 

1例、1失敗

全コントローラコードを示すエラーを取得し

# create new claim code based on org_id 
def create 
# we already know this user has permission for org_claim_codes#create but we need to check they are part of this org 
unless @current_user.orgs.map(&:id).include?(new_org_claim_code_params[:org_id].to_i) || @current_user.has_permission?('*', '*') 
    render json: { errors: ['user not authorized']}, status: :forbidden && return 
end 

claim = OrgClaimCode.new(new_org_claim_code_params) 
if claim.save 
    render json: claim 
else 
    render json: { errors: claim.errors.full_messages }, status: :unprocessable_entity 
end 

コントローラ書き換えフル試験

it 'does not allow a user without permission weather or not they are in the org' do 
    current_user 
    expect do 
     post :create, params: { org_claim_codes: { org_id: create(:org).id } } # a different org than we set perms for 
    end.to change(OrgClaimCode, :count).by(0) 

    expect(response).to have_http_status(:forbidden) 
    end 
+0

FYIの禁止されているエラーコードは403ではない401: – SickLickWill

答えて

0

render json: { errors: ['user not authorized']}, status: :forbidden && return 

説明

render json: { errors: ['user not authorized']}, status: :forbidden and return 

TO

&&andよりprecedenceが高いです。さらに、コードrender json: { errors: ['user not authorized']}, status: :forbiddentrueですので、これを実行してreturnには行かないでください。

Check it out、それはすべてを説明します。

乾杯!

+0

説明できますか? –

+0

確かに、私の答えを更新しました。 – rony36

0

&& returnまたはand returnを完全に削除すると正常に動作します。 また、403は禁止されたエラーコードです。

関連する問題