ruby - Testing authorization for associated ressources with rspec and factory Girl (has_many through relations) -
I would like to check that a non-signed user can not access the reserved page for a specific customer (one of them Deal: Only they can see the draft page deal). It should be redirected to the customer sign-in page.
A customer has many deals
model / customer.rb
class customer
model / deal.rb
class deal & lt; ActiveRecord :: Base is_to: admin_user,: foreign_key = & gt; 'Admin_user_id' belong_to: partner,: foreign_key = & gt; 'Partner_id',: counter_cache = & gt; True has_many: customer_deals have_many: Customer, Through Customer: Customer_asses accepts __ nested_attributes_for: Customer #: reject_if # Required for active admin
model / customer_deal.rb
related_to: customer,: foreign_key => Deals with 'customer_id': deals, foreign_key = & gt; My test below is failing with this error: 1) Non-sign-in form of customer interface page The visitor does not have access to the deal page failure / error: two (: deal) (factorial.cent (: deal, customer = & gt; customer)} node method: # & lt; Customer = ': 0x007f4f34b3a788> # ./spec/requests/client_interface_pages_spec.rb:15:in' at the top (block) (2 level)> Head (required)> # ./spec/requests/ Client_interface_pages_sp
Here is the rspec test page: client_interface_pages_spec.rb
Pre-> requires 'spec_helper' description of 'cancan / matchers' on "customer page" subject {page} (user: {FACTGARG.RECTS (user:}} (go to: customer) {factoryGreat.Create (Customer:}} Let's (: prospect) {FactorialGreat.Create (: prospect)} Let's (: Incursory) {Factgear .create (customer, email: "wrong@example.com")} (: non-admin) {Factorial.com Ete (customer :)} Give (Deal) {Factorigiyrkkret (Deal, client = & gt; Customer)} refers to the reference "non-sign-in visitor" (customer) (FactoryGrul.Cact (customer)} does not have "access to make bargain page", this "do not have a page to prepare a deal" Can get it: Draft_deal_page ,: deal_id => Deal.id ,: customer_id => Customer.id response.should redirect_to (new_customer_session_path) Flash [: Warning]. Should eql ("Before continuing You must sign up or sign up. ") End And End And
Routes
customer_deals.rb
FactoryGear Define Factory: Client_Del Client Deals End Factory: Customer Tax Sequence (: Email) {| n | "Person_ # {n }@example.com "} password" abcde "password_confirmation" abcde "# is confirmed if confirmed if the Devise Confirmable module has been confirmed. Confirm time_No partner_ id3 # Provide customers the role of customers: customer_with_customer_status (create) {| Customer | Customers.Ad_Ol (customer)} End and End
deals. RB
FactoryGear.Define factory: deal title "Laurem Epsom" is shown correctly Admin_user_id 1 partner_id 3 customer_id 3 End End
A deal has_many: customers
. Trying to designate your deal
factory "customer_id" attribute. There is no such feature on the deal because the deal is many customers The customer_deal factory can be more suitable for use here:
Walk (: Customer) {FactoryGirl. Create (: customer_deal). Customer}
This will be required to get rid of your deal
factory customer_id
in general, I think it In your factories, it is a bad form for choosing a voluntary integer to stand for a foreign key. If a factory requires a relationship, then that relationship should be made by the factory itself. If the object object does not need a relationship for validity then I leave it completely out of the factory, perhaps it is adding a feature behind it, if there is something that you often need.
I suggest reading the guide - - the whole thing, but especially about the parts in which there are many organizations, signs, and organizations.
Comments
Post a Comment