2017-11-16 23 views
2

나는 멀티 테넌트 SaaS 애플리케이션을 보유하고 있습니다. 모든 입주자 모델에 대해 입주자 보안 테스트를 실시합니다.Rspec : 허용 목록에없는 경우 모든 모델에서 특정 단위 테스트 실행

describe 'tenant security' do 
    it "has only the current company's data" do 
    set_tenant_company 
    other_companys_data = create :model 

    set_tenant_company 
    this_companys_data = create :model 

    expect(Model.all).to include this_companys_data 
    expect(Model.all).not_to include other_companys_data 
    end 
end 

메타 모델링을 사용하여 모든 모델에서이 방법을 사용할 수 있습니까? [단위 테스트에 아무 것도 명시 적으로 코딩하지 않고] 그렇다면 비거주 모델을 허용 목록에 추가하는 방법이 있습니까?

임차인 보안은 중요하며 간과해서 간과하지 않기를 바랍니다. RSpec에 코어 팀에 게시

+0

허용 목록에 포함되지 않은 한 자동으로 수행하는 방법을 모르겠지만 모든 구현 모델에서 구현과 줄만 필요하기 때문에'behaves_like'를 사용할 수 있습니다. 이것 좀 봐 : https://relishapp.com/rspec/rspec-core/v/3-7/docs/example-groups/shared-examples –

+0

고마워! Shared_examples는 내가하는 일보다 나을 것 같습니다. shared_example을 각 모델 사양 파일에 대해 한 번 자동 실행하는 방법이 있습니까? 특정 파일 (허용 된 사이트)을 건너 뛰도록 지시하는 방법이 있습니까? –

답변

0

페드로의 의견에 대한 자료, 그리고 그의 질문 :

는 암시 적으로 각 모델에 포함 할 검사와 공유 예를 만듭니다. :이 조건을 포함하지 않아야하는 화이트리스트 모델의 경우 조건이 아닌 경우.

RSpec.shared_examples 'a tenant model' do 
    it "has only the current company's data",unless: metadata[:not_a_tenant_model] == true do 
    set_tenant_company 
    other_companys_data = create subject.class.name.underscore.to_sym 

    set_tenant_company 
    this_companys_data = create subject.class.name.underscore.to_sym 

    expect(subject.class.all).to include this_companys_data 
    expect(subject.class.all).not_to include other_companys_data 
    end 
end 

놓습니다 RSpec.configuration

RSpec.configure do |config| 
    config.include_context 'a tenant model', type: :model 
end 

이의 다음 행은 모든 모델에서 다음을 배치하는 효과가 있습니다.

Rspec.describe TenantModel do 
    it_behaves_like 'a tenant model' 
end 

는 적절한 태그를 추가, 비 테넌트 모델을 화이트리스트하려면

RSpec.decribe NonTenantModel, :not_a_tenant_model do 
    # shared example will not be included 
end 

큰 당신을 감사합니다! Pedro와 RSpec 핵심 팀에게.

+0

일부 연구 및 테스트 후에 런타임에 예제를 생성 할 수 없기 때문에 가능하지 않다고 생각합니다. RSpec은 이것을 허용하지 않습니다. 어쨌든, 핵심 팀에게이 작업을 수행 할 수있는 방법이 있는지 물었습니다. https://github.com/rspec/rspec-core/issues/2480 방법이 있다면 배열을 만들고 확인하는 것만 큼 쉬울 것입니다. 거기에 묘사 된 클래스의 존재. –

+0

고마워요! 또한 [Metadata] (https://relishapp.com/rspec/rspec-core/docs/metadata), [필터링] (https://relishapp.com/rspec/rspec-core/docs/filtering) , [after (: context) 후크] (https://relishapp.com/rspec/rspec-core/docs/hooks/before-and-after-hooks)를 참조하십시오. shared_examples를 조건부로 실행하기위한 해결책을 찾지 못했습니다. –

0

현재 수용된 솔루션은 모든 비 테넌트 모델에 특정 태그를 지정해야합니다. 이 솔루션을 사용하면 구성에서이 작업을 수행 할 수 있습니다.

는 는

음,이 문제 열기 덕분에 : https://github.com/rspec/rspec-core/issues/2480은,이 사건에 대한 가장 좋은 해결책이 될 것 같다 :이 모델은 모든 비 세입자, 그리고 화이트리스트 경우 말을 당신에게 명확한 메타 데이터를 제공

non_tenant_models = %w(NonTenantModelOne NonTenantModelTwo).map(&:constantize) 

RSpec.configure do |c| 
    c.define_derived_metadata(type: :model) do |meta| 
    meta[:tenant_model] = true unless meta[:described_class].presence_in non_tenant_models 
    end 

    c.include_context "shared", tenant_model: true 
end 

모델에 무엇이든 코딩 할 필요없이 공유 예제를 실행하도록 지정된 테넌트 모델.

+0

'화이트리스트'태그를 구성에 매우 잘 추상화했습니다. 필자는 화이트 리스팅 태그를 허용 된 사양에 명시 적으로 배치하는 것을 선호합니다. 이 테스트가 모든 모델에 포함되도록하고 싶지만 테스트를 가능한 한 명확하게 유지하고 싶습니다. 모든 의견을 보내 주셔서 감사합니다.나는 당신의 도움없이 해결책을 구현하지 못했습니다. –

+0

Youre welcome :) –