diff --git a/.sun-ci.yml b/.sun-ci.yml index d1580739..e49e4410 100644 --- a/.sun-ci.yml +++ b/.sun-ci.yml @@ -39,4 +39,4 @@ jobs: MYSQL_ROOT_PASSWORD: password-test script: - sudo RAILS_ENV=test bundle exec rails db:drop db:create db:migrate - - bundle exec rspec + - sudo bundle exec rspec diff --git a/Gemfile b/Gemfile index a443ae91..5888aae6 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ ruby "3.2.0" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "bootstrap-sass", "3.4.1" gem "byebug" +gem "connection_pool" gem "image_processing", ">= 1.2" gem "jquery-rails" gem "owlcarousel-rails" diff --git a/Gemfile.lock b/Gemfile.lock index afd3080f..e2043997 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -321,6 +321,7 @@ DEPENDENCIES byebug capybara config + connection_pool cssbundling-rails (~> 1.4) debug dotenv diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 39acd905..8b361a8e 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -10,21 +10,6 @@ def index @product_outstandings = Product.product_outstanding end - def new - @product = Product.new - end - - def create - product = Product.new(product_params) - if product.save - flash[:success] = t("products.create.success") - redirect_to(products_path) - else - flash.now[:error] = t("products.create.error") - render(:new) - end - end - def show; end private @@ -53,8 +38,4 @@ def load_product flash[:error] = t("products.load.error") redirect_to(products_path) end - - def product_params - params.require(:product).permit(:name, :price, :image) - end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 8a33329b..990150e0 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -12,6 +12,7 @@ # While tests run files are not watched, reloading is not necessary. config.enable_reloading = false + config.active_job.queue_adapter = :inline # Eager loading loads your entire application. When running a single test locally, # this is usually not necessary, and can slow down your test suite. However, it's diff --git a/config/routes.rb b/config/routes.rb index b8416e1b..29c84662 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,14 +3,14 @@ Rails.application.routes.draw do scope "(:locale)", locale: /en|vi/ do root "products#index" - resources :products do + resources :products, only: %i(index show) do resources :comments end get "/login", to: "sessions#new" post "/login", to: "sessions#create" get "/logout", to: "sessions#destroy" - resource :cart, only: %i[show create destroy update] - resources :orders, only: %i[create show index] do + resource :cart, only: %i(show create destroy update) + resources :orders, only: %i(create show index) do member do patch "/cancel", to: "orders#cancel" end diff --git a/spec/requests/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb similarity index 50% rename from spec/requests/accounts_controller_spec.rb rename to spec/controllers/accounts_controller_spec.rb index 59ba2c22..0915ce2b 100644 --- a/spec/requests/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -17,10 +17,29 @@ describe "POST #create" do context "with valid attributes" do - before { post :create, params: { account: attributes_for(:account) } } + let(:valid_params) do + { + account: { + name: "Test User", + email: "test@example.com", + address: "123 Test Street", + phone_number: "1234567890", + password: "password", + password_confirmation: "password" + } + } + end + + before { post :create, params: valid_params } it "creates a new account" do - expect(assigns(:account).id).to(be_present) + expect(Account.last).to(have_attributes( + name: "Test User", + email: "test@example.com", + address: "123 Test Street", + phone_number: "1234567890" + ) + ) end it "redirects to root path" do @@ -33,11 +52,21 @@ end context "with invalid attributes" do - before do - invalid_attributes = attributes_for(:account).merge(name: nil, email: "invalid_email", password: "123", password_confirmation: "123456") - post :create, params: { account: invalid_attributes } + let(:invalid_attributes) do + { + account: { + name: "", + email: "", + address: "", + phone_number: "", + password: "", + password_confirmation: "" + } + } end + before { post :create, params: invalid_attributes } + it "does not save the new account" do expect(assigns(:account).id).not_to(be_present) end diff --git a/spec/controllers/products_controller_spec.rb b/spec/controllers/products_controller_spec.rb new file mode 100644 index 00000000..94b3cb22 --- /dev/null +++ b/spec/controllers/products_controller_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe(ProductsController, type: :controller) do + describe "GET #index" do + let!(:products) do + FactoryBot.create_list(:product, Settings.DIGIT_10) + end + + before { get :index } + + it "assigns @pagy and @products" do + expect(assigns(:pagy).count).to(eq(Settings.DIGIT_10)) + products_per_page = Settings.PAGE_9 + expect(assigns(:products).count).to(eq(products_per_page)) + end + + it "renders the index template" do + expect(response).to(render_template(:index)) + end + end + + describe "GET #show" do + context "when product exists" do + let!(:product) { FactoryBot.create(:product) } + + before do + get :show, params: { id: product.id } + end + + it "assigns the requested product to @product" do + expect(assigns(:product)).to(eq(product)) + end + + it "renders the show template" do + expect(response).to(render_template("show")) + end + end + + context "when product does not exist" do + before { get :show, params: { id: 0 } } + + it "flash error if product doesn't exist" do + expect(flash[:error]).to(eq(I18n.t("products.load.error"))) + end + + it "redirects to products_path if product is not found" do + expect(response).to(redirect_to(products_path)) + end + end + end +end diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb index 7926a2ff..73d9ad50 100644 --- a/spec/factories/categories.rb +++ b/spec/factories/categories.rb @@ -2,6 +2,6 @@ FactoryBot.define do factory :category do - name { Faker::Name.name } + name { %w(Apple Samsung Xiaomi Oppo Huawei Vivo).sample } end end diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 48ba6437..d2de9a7c 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -7,5 +7,6 @@ quantity { Faker::Number.between(from: 1, to: 100) } category { association :category } description { Faker::Lorem.paragraph } + image { Rack::Test::UploadedFile.new(File.open(Rails.root.join("spec/files/image_test.jpg").to_s)) } end end diff --git a/spec/files/image_test.jpg b/spec/files/image_test.jpg new file mode 100644 index 00000000..afda0ec0 Binary files /dev/null and b/spec/files/image_test.jpg differ