Skip to content

Commit

Permalink
Merge pull request #35 from thanhhung-131/rspec_sessions
Browse files Browse the repository at this point in the history
Rspec for sessions controller
  • Loading branch information
binhpt-1239 authored Apr 4, 2024
2 parents 970fca6 + 80f5284 commit c424782
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CartsController < ApplicationController
before_action :load_current_account, only: :create
before_action :load_product, only: %i(create destroy)
before_action :set_cart, only: :create
before_action :load_cart_product, only: :show
before_action :logged_in_user, :load_cart_product, only: :show

def show; end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class OrdersController < ApplicationController
before_action :parse_cart_data, only: :create
before_action :find_products, only: :create
before_action :load_order, only: %i(show cancel)
before_action :logged_in_user, only: :index

def index
@pagy, @orders = pagy(current_account.orders.order(created_at: :desc), items: Settings.PAGE_10)
Expand Down
1 change: 0 additions & 1 deletion app/views/layouts/_flash.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
<%= flash[:info] %>
</div>
<% end %>

</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= render "layouts/flash" %>
<div class="row">
<div class="col-md-4 offset-md-1">
<h2 class = "sign-up-title"><%= uppercase t("accounts.login_customer") %></h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<%= link_to t("header.home"), root_url, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to t("header.cart"), login_path, class: "nav-link" %>
<%= link_to t("header.cart"), cart_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to t("header.login"), login_path, class: "nav-link" %>
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/accounts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
end

context "with invalid attributes" do
let(:invalid_attributes) do
let(:invalid_params) do
{
account: {
name: "",
Expand All @@ -65,7 +65,7 @@
}
end

before { post :create, params: invalid_attributes }
before { post :create, params: invalid_params }

it "does not save the new account" do
expect(assigns(:account).id).not_to(be_present)
Expand Down
71 changes: 71 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

# spec/controllers/sessions_controller_spec.rb
require "rails_helper"
require "support/shared_examples/login_examples"

RSpec.describe(SessionsController, type: :controller) do
include SessionsHelper

describe "GET #new" do
it "renders the new template" do
get :new
expect(response).to(render_template(:new))
end
end

describe "POST #create" do
let(:account) { create(:account) }

context "load account failed" do
before do
post :create, params: { session: { email: "[email protected]", password: "password" } }
end

it "renders new template with error message" do
expect(flash.now[:error]).to(eq(I18n.t("sessions.login_email_err")))
end

it "returns unprocessable_entity status and render template new" do
expect(response).to(have_http_status(:unprocessable_entity))
expect(response).to(render_template(:new))
end
end

context "load account success but authenticate failed" do
before do
post :create, params: { session: { email: account.email, password: "incorrect_password" } }
end

it "renders new template with error message" do
expect(flash.now[:error]).to(eq(I18n.t("sessions.login_password_err")))
end

it "returns unprocessable_entity status and render template new" do
expect(response).to(have_http_status(:unprocessable_entity))
expect(response).to(render_template(:new))
end
end

context "login success" do
before do
post :create, params: { session: { email: account.email, password: account.password } }
end

it "logs in the account and redirects to root path" do
expect(session[:account_id]).to(eq(account.id))
expect(response).to(redirect_to(root_path))
end
end
end

describe "DELETE #destroy" do
include_examples "login examples"

it "logs out the current account and redirects to root path" do
delete :destroy
expect(session[:account_id]).to(be_nil)
expect(response).to(redirect_to(root_path))
end
end
end
10 changes: 10 additions & 0 deletions spec/support/shared_examples/login_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

RSpec.shared_examples("login examples") do
let(:account) { create(:account) }
before { log_in(account) }

it "logs in successfully" do
expect(current_account).to(be_present)
end
end

0 comments on commit c424782

Please sign in to comment.