require "rails_helper" RSpec.describe "cart item request spec",:product_embedded => true,:shopping => true, :type => :request do before(:all) do ActionController::Base.allow_forgery_protection = false User.delete_all Auth::Client.delete_all Shopping::CartItem.delete_all Shopping::Product.delete_all ## THIS PRODUCT IS USED IN THE CART_ITEM FACTORY, TO PROVIDE AND ID. #@product = Shopping::Product.new(:name => "test product", :price => 400.00) #@product.save @u = User.new(attributes_for(:user_confirmed)) @u.save @c = Auth::Client.new(:resource_id => @u.id, :api_key => "test", :app_ids => ["testappid"]) @c.redirect_urls = ["http://www.google.com"] @c.versioned_create @u.client_authentication["testappid"] = "testestoken" @u.save @ap_key = @c.api_key @headers = { "CONTENT_TYPE" => "application/json" , "ACCEPT" => "application/json", "X-User-Token" => @u.authentication_token, "X-User-Es" => @u.client_authentication["testappid"], "X-User-Aid" => "testappid"} ### CREATE ONE ADMIN USER ### It will use the same client as the user. @admin = User.new(attributes_for(:admin_confirmed)) @admin.admin = true @admin.client_authentication["testappid"] = "testestoken2" @admin.save @admin_headers = { "CONTENT_TYPE" => "application/json" , "ACCEPT" => "application/json", "X-User-Token" => @admin.authentication_token, "X-User-Es" => @admin.client_authentication["testappid"], "X-User-Aid" => "testappid"} end context " -- bar code -- ",:product_bar_code => true do before(:example) do Auth.configuration.product_class.constantize.delete_all Auth::Shopping::BarCode.delete_all end context " -- json requests -- " do it " -- adds its attribute accessors to the primary link of the object -- " do end it " --can creates a product without a barcode -- " do post shopping_products_path,{product: {name: "first product", price: 200, bar_code_tag: "1alkadkj"},:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers expect(response.code).to eq("201") end it " -- creates product with a bar_code_tag -- ", :product_bar_code_first => true do post shopping_products_path,{product: {name: "first product", price: 200, bar_code_tag: "1alkadkj"},:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers product = Auth.configuration.product_class.constantize.all.first bar_codes = Auth::Shopping::BarCode.where(:assigned_to_object_id => product.id.to_s) expect(bar_codes.size).to eq(1) end it " -- clears barcode from a product -- " do product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy ## this will have to be provided, alongwith that a = {product: {remove_bar_code: true}} put shopping_product_path({:id => product.id.to_s,:api_key => @ap_key, :current_app_id => "testappid"}), a.to_json,@admin_headers expect(response.code).to eq("204") bar_codes = Auth::Shopping::BarCode.where(:assigned_to_object_id => "1alkadkj") expect(bar_codes.size).to eq(0) end it " -- creating a new record with an existing bar code tag , only the new record is created, but it will not have an associated entry in the barcodes collection. -- ", :reuse_bc_tag => true do product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy ## check that a barcode was made with an assigned_to_object_id as this one. bar_codes = Auth::Shopping::BarCode.where(:assigned_to_object_id => product.id.to_s) expect(bar_codes.size).to eq(1) post shopping_products_path,{product: {name: "second product", price: 200, bar_code_tag: "1alkadkj"},:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers ## now another product will be created, but not another barcode entry. product_two = Auth.configuration.product_class.constantize.where(:name => "second product") expect(product_two.size).to eq(1) ## the barcode should be the first one with the first assigned_object id. expect(Auth::Shopping::BarCode.all.size).to eq(1) expect(Auth::Shopping::BarCode.all.first.assigned_to_object_id).to eq(product.id.to_s) end ## simplest way is to add a new barcode ## if it has changed, then it should automatically call clear the barcode. ## now first let me make the cart items, carts and all that and see if its searchable, ## also the personalities. it " -- cannot direcly update barcode on product -- ", :direct_update_barcode => true do product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy ## put directly with a barcode tag. a = {product: {bar_code_tag: "hello"}} put shopping_product_path({:id => product.id.to_s,:api_key => @ap_key, :current_app_id => "testappid"}), a.to_json,@admin_headers expect(response.code).to eq("422") ## so in this case, there should be no barcodetag with hello created. bar_codes = Auth::Shopping::BarCode.where(:assigned_to_object_id => "hello") expect(bar_codes.size).to eq(0) end it " -- searches the barcode collection -> finds the barcode -> redirects to the show action on the record -- ", :show_bar_code => true do ## should contain the product primary link. product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy get bar_code_path({:id => "1alkadkj",api_key: @ap_key, :current_app_id => "testappid"}),nil,@headers response_json = JSON.parse(response.body) expect(response_json["redirect_to"]).to eq("/shopping/products/#{Auth.configuration.product_class.constantize.all.first.id.to_s}") end it " -- if assigned to product id is missing, then it will return an error -- ", :assigned_object_missing => true do ## should contain the product primary link. product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy ## delete the product Auth.configuration.product_class.constantize.first.delete get bar_code_path({:id => "1alkadkj",api_key: @ap_key, :current_app_id => "testappid"}),nil,@headers response_json = JSON.parse(response.body) expect(response_json["errors"]["_id"]).to eq(["could not find the assigned object"]) end it " -- force show will yield the barcode record as json -- ", :bar_code_force_show => true do ## should contain the product primary link. product = Auth.configuration.product_class.constantize.new(name: "first product", price: 200, bar_code_tag: "1alkadkj", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy get bar_code_path({:id => "1alkadkj", :bar_code => {:force_show => true},api_key: @ap_key, :current_app_id => "testappid"}),nil,@headers response_as_json = JSON.parse(response.body) bar_code = Auth::Shopping::BarCode.new(response_as_json["bar_code"]) expect(bar_code.bar_code_tag).to eq("1alkadkj") end it " -- updates a product with a barcode tag and creates a new barcode entry at that time -- " do end end ## so let me try this with creating a new product, and using the laptop webcam. end context " -- product code -- ", :product_code => true do before(:example) do Shopping::Product.delete_all Shopping::CartItem.delete_all end it " -- auto assigns a product code to the product on creation -- " do product = Shopping::Product.new(:name => "test", :price => 300, :description => "a test product") post shopping_products_path,{product: product,:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers expect(Auth.configuration.product_class.constantize.all.first.product_code).not_to be_nil end it " -- assigns product code of other product if create_from_product_id is provided -- " do product_one = Shopping::Product.new(:name => "p1", :price => 12, :description => "hello", signed_in_resource: @admin, resource_id: @admin.id.to_s, :resource_class => @admin.class.name.to_s) expect(product_one.save).to be_truthy product_one = Shopping::Product.all.first product = Shopping::Product.new(:name => "test", :price => 300, :description => "a test product", :create_from_product_id => product_one.id.to_s) post shopping_products_path,{product: product.attributes.merge({:create_from_product_id => product_one.id.to_s}),:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers products = Shopping::Product.where(:product_code => product_one.product_code) expect(products.size).to eq(2) end it " -- assigns only attributese mntioned in def #attrs_to_copy_from_prod_to_prod to the new product. -- " do product_one = Shopping::Product.new(:name => "p1", :price => 12, :description => "hello", signed_in_resource: @admin, resource_id: @admin.id.to_s, :resource_class => @admin.class.name.to_s, :bar_code_tag => "abracadabra") expect(product_one.save).to be_truthy product_one = Shopping::Product.all.first product = Shopping::Product.new(:name => "test", :price => 300, :description => "a test product", :create_from_product_id => product_one.id.to_s) post shopping_products_path,{product: product.attributes.merge({:create_from_product_id => product_one.id.to_s}),:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @admin_headers expect(response.code).to eq("201") product_two = Shopping::Product.find(JSON.parse(response.body)["_id"]) product_one = Shopping::Product.find(product_one.id.to_s) product_one.attrs_to_copy_from_prod_to_prod.each do |attr| expect(product_one.send("#{attr}")).to eq(product_two.send("#{attr}")) end expect(Auth::Shopping::BarCode.all.size).to eq(1) end it " -- passes on the product code to any cart item created from the product -- " do product_one = Shopping::Product.new(:name => "p1", :price => 12, :description => "hello", signed_in_resource: @admin, resource_id: @admin.id.to_s, :resource_class => @admin.class.name.to_s, :bar_code_tag => "abracadabra") expect(product_one.save).to be_truthy product_one = Shopping::Product.all.first cart_item = Shopping::CartItem.new(product_id: product_one.id.to_s, signed_in_resource: @u, resource_id: @u.id.to_s, resource_class: @u.class.name) ## will have to change in dummy. ## now what do i do ## post shopping_cart_items_path,{cart_item: cart_item,:api_key => @ap_key, :current_app_id => "testappid"}.to_json, @headers @cart_item_created = assigns(:auth_shopping_cart_item) end ## i can continue and knock off the materials ## or first finish for the cart_items. ## it " -- create_from_proudct_id is ignored for updates -- " do ## so this is not used for updates ## basically if it has one, it cannot be changed product = Shopping::Product.new(:name => "test", :price => 300, :description => "a test product", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product.save).to be_truthy product_two = Shopping::Product.new(:name => "test_product_two", :price => 300, :description => "a test product", signed_in_resource: @admin, resource_id: @admin.id.to_s, resource_class: @admin.class.name.to_s) expect(product_two.save).to be_truthy product_two = Shopping::Product.where(:name => "test_product_two").first product_one = Shopping::Product.where(:name => "test").first earlier_product_code = product_one.product_code pr = {:product => {:create_from_product_id => product_one.id.to_s}, :api_key => @ap_key, :current_app_id => "testappid"} put shopping_product_path(:id => product_two.id.to_s),pr.to_json, @admin_headers ## now we tried to create product one from product two ## so that should not have worked. product_one = Shopping::Product.where(:name => "test").first expect(product_one.product_code).to eq(earlier_product_code) end end context " -- json requests -- " do before(:example) do #Shopping::CartItem.delete_all #Shopping::Product.delete_all Auth.configuration.product_class.constantize.delete_all Auth.configuration.cart_item_class.constantize.delete_all end it " -- adds images/ videos to the bullets -- " do ## so we need a form helper ## as well as another partial ## no put it into the form itself ## it will look for an input_field with a class of barcode, and add the correctly scanned barcode to that. ## then that can be submitted as a barcode ## javascript has to be enabled. end it " -- deletes a bullet -- " do ## so we want to set a bullet to newil. product = Auth.configuration.product_class.constantize.new product.name = "first product" product.price = 200 product.resource_id = @admin.id.to_s product.resource_class = @admin.class.name.to_s product.signed_in_resource = @admin instructions = [] 1.times do |n| instruction = Auth::Work::Instruction.new instruction.title = "Before Test" 1.times do |b| bullet = Auth::Work::Bullet.new bullet.text = "this is the #{b} bullet" instruction.bullets << bullet end instructions << instruction end product.instructions = instructions product.valid? puts product.errors.full_messages expect(product.save).to be_truthy ## now we update the text of the first bullet embedded_document = nil embedded_document_path = "instructions.0.bullets.0" a = {:product => {:embedded_document => embedded_document, :embedded_document_path => embedded_document_path}} put shopping_product_path({:id => product.id.to_s,:api_key => @ap_key, :current_app_id => "testappid"}), a.to_json,@admin_headers #puts response.body.to_s #puts response.code.to_s updated_product = assigns(:auth_shopping_product) expect(response.code).to eq("204") end it " -- deletes an instruction -- " do end it " -- show product returns summary, and individual instructions with bullets -- " do end it "-- product parameter can be created and updated -- " do end it " -- parameters can be given options -- " do end it " -- parameter options can be selected -- " do end it " -- generates instructions based on variables and actors -- " do end end end