require 'rails_helper' # This spec was generated by rspec-rails when you ran the scaffold generator. # It demonstrates how one might use RSpec to specify the controller code that # was generated by Rails when you ran the scaffold generator. # # It assumes that the implementation code is generated by the rails scaffold # generator. If you are using any extension libraries to generate different # controller code, this generated spec may or may not pass. # # It only uses APIs available in rails and/or rspec-rails. There are a number # of tools you can use to make these specs even more expressive, but we're # sticking to rails and rspec-rails APIs to keep things simple and stable. # # Compared to earlier versions of this generator, there is very limited use of # stubs and message expectations in this spec. Stubs are only used when there # is no simpler way to get a handle on the object needed for the example. # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. RSpec.describe WebapiKeysController, :type => :controller do # This should return the minimal set of attributes required to create a valid # WebapiKey. As you add validations to WebapiKey, be sure to # adjust the attributes here as well. let(:valid_attributes) { FactoryBot.attributes_for(:webapi_key) } let(:invalid_attributes) { FactoryBot.attributes_for(:webapi_key,app_name: "") } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in # WebapiKeysController. Be sure to keep this updated too. let(:valid_session) { {} } context "not auth users",auth_user: false do [:login_user,:login_number_handler,:login_number_manager].each do |login_type| describe login_type.to_s do send(login_type) describe "GET index" do it { get :index,params: {apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) } end describe "GET new" do it{ get :new,params: {apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) } end describe "POST create" do it { post :create,params: {:webapi_key => valid_attributes,apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) } it{ expect { post :create, params: {:webapi_key => valid_attributes,apartment: :sample} }.to change(WebapiKey, :count).by(0) } end describe "GET edit" do it{ webapi_key = WebapiKey.create! valid_attributes get :new,params: {:id => webapi_key.to_param,apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) } end describe "PUT update" do it { webapi_key = WebapiKey.create! valid_attributes put :update,params: {:id => webapi_key.to_param, :webapi_key => valid_attributes.merge({app_name: "UPDATE APP NAME"}), apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) webapi_key.reload expect(webapi_key.app_name).not_to eq("UPDATE APP NAME") } end describe "DELETE destroy" do it{ webapi_key = WebapiKey.create! valid_attributes delete :destroy, params: {:id => webapi_key.to_param,apartment: :sample} should render_template('errors/error_page') should set_flash.now[:error] should respond_with(403) } it{ webapi_key = WebapiKey.create! valid_attributes expect { delete :destroy, params: {:id => webapi_key.to_param,apartment: :sample} }.to change(WebapiKey, :count).by(0) } end end end end context "auth users",auth_user: true do [:login_admin,:login_super_admin].each do |login_type| describe login_type.to_s do send(login_type) describe "GET index" do it "assigns all webapi_keys as @webapi_keys" do webapi_key = WebapiKey.create! valid_attributes get :index,params: {apartment: :sample} expect(assigns(:webapi_keys)).to eq([webapi_key]) end it "作成日時の降順で並ぶこと" do webapi_keys = [] 3.times.each do webapi_keys.push(FactoryBot.create(:webapi_key)) end get :index,params: {apartment: :sample} expect(assigns(:webapi_keys)).to eq(webapi_keys.reverse) end end describe "GET new" do it "assigns a new webapi_key as @webapi_key" do get :new,params: {apartment: :sample} expect(assigns(:webapi_key)).to be_a_new(WebapiKey) end end describe "GET edit" do it "assigns the requested webapi_key as @webapi_key" do webapi_key = WebapiKey.create! valid_attributes get :edit, params: {:id => webapi_key.to_param,apartment: :sample} expect(assigns(:webapi_key)).to eq(webapi_key) end end describe "POST create" do describe "with valid params" do it "creates a new WebapiKey" do expect { post :create, params: {:webapi_key => valid_attributes,apartment: :sample} }.to change(WebapiKey, :count).by(1) end it "assigns a newly created webapi_key as @webapi_key" do post :create,params: {:webapi_key => valid_attributes,apartment: :sample} expect(assigns(:webapi_key)).to be_a(WebapiKey) expect(assigns(:webapi_key)).to be_persisted end it "redirects to the created webapi_key" do post :create,params: {:webapi_key => valid_attributes,apartment: :sample} expect(response).to redirect_to(webapi_keys_url) expect(flash[:notice]).not_to be_empty end end describe "with invalid params" do it "assigns a newly created but unsaved webapi_key as @webapi_key" do post :create,params: {:webapi_key => invalid_attributes,apartment: :sample} expect(assigns(:webapi_key)).to be_a_new(WebapiKey) end it "re-renders the 'new' template" do post :create,params: {:webapi_key => invalid_attributes,apartment: :sample} expect(response).to render_template("new") expect(flash.now[:error]).not_to be_empty end end end describe "PUT update" do describe "with valid params" do let(:new_attributes) { { app_name: "UPDATE_APP", app_id: "abcd", app_token: "a0s20a12dc3fbghs" } } it "updates the requested webapi_key" do webapi_key = WebapiKey.create! valid_attributes put :update,params: {:id => webapi_key.to_param, :webapi_key => new_attributes,apartment: :sample} webapi_key.reload expect(webapi_key.app_name).to eq(new_attributes[:app_name]) expect(webapi_key.app_id).to eq(new_attributes[:app_id]) expect(webapi_key.app_token).to eq(new_attributes[:app_token]) end it "assigns the requested webapi_key as @webapi_key" do webapi_key = WebapiKey.create! valid_attributes put :update, params: {:id => webapi_key.to_param, :webapi_key => valid_attributes,apartment: :sample} expect(assigns(:webapi_key)).to eq(webapi_key) end it "redirects to the webapi_key" do webapi_key = WebapiKey.create! valid_attributes put :update, params: {:id => webapi_key.to_param, :webapi_key => valid_attributes,apartment: :sample} expect(response).to redirect_to(webapi_keys_url) expect(flash[:notice]).not_to be_empty end end describe "with invalid params" do it "assigns the webapi_key as @webapi_key" do webapi_key = WebapiKey.create! valid_attributes put :update,params: {:id => webapi_key.to_param, :webapi_key => invalid_attributes,apartment: :sample} expect(assigns(:webapi_key)).to eq(webapi_key) end it "re-renders the 'edit' template" do webapi_key = WebapiKey.create! valid_attributes put :update,params: {:id => webapi_key.to_param, :webapi_key => invalid_attributes,apartment: :sample} expect(response).to render_template("edit") expect(flash.now[:error]).not_to be_empty end end end describe "DELETE destroy" do it "destroys the requested webapi_key" do webapi_key = WebapiKey.create! valid_attributes expect { delete :destroy,params: {:id => webapi_key.to_param,apartment: :sample} }.to change(WebapiKey, :count).by(-1) end it "redirects to the webapi_keys list" do webapi_key = WebapiKey.create! valid_attributes delete :destroy, params: {:id => webapi_key.to_param,apartment: :sample} expect(response).to redirect_to(webapi_keys_url) expect(flash[:notice]).not_to be_empty end end end end end end