Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase coverage of project #934

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a5e39fa
Added user inactive_message method test
olexandervanzuriak Nov 1, 2024
f56c5da
Added test for calculator, message and product controllers
olexandervanzuriak Nov 5, 2024
54f70de
Added tests for calculators preferable service
SleekMutt Nov 5, 2024
32ec193
fixed formating
olexandervanzuriak Nov 5, 2024
061a35d
Merge branch '930-increase-coverage-of-project' of github.com:ita-soc…
olexandervanzuriak Nov 5, 2024
a5f508b
fixed formatting
olexandervanzuriak Nov 5, 2024
d146105
Remove Channels group and added Services group to simplecov
olexandervanzuriak Nov 6, 2024
9c816d6
Change the destroy products_controller method and added tests
olexandervanzuriak Nov 8, 2024
18a457a
added home test
olexandervanzuriak Nov 8, 2024
a30cb4f
added post #calculate test
olexandervanzuriak Nov 8, 2024
d3f8f11
Added tests for models and helper
KseniaGovorun Nov 9, 2024
5fdaec5
Fixed rubocop
KseniaGovorun Nov 9, 2024
cf45a54
Fixed in file application_helper
KseniaGovorun Nov 9, 2024
e634770
Added aplication controller tests
olexandervanzuriak Nov 12, 2024
69bf828
Merge branch '930-increase-coverage-of-project' of github.com:ita-soc…
olexandervanzuriak Nov 12, 2024
6cbcd50
Added tests for authorization
KseniaGovorun Nov 14, 2024
08039a3
Fixed rubocop
KseniaGovorun Nov 14, 2024
09668f9
Changed schema
KseniaGovorun Nov 14, 2024
299d661
Fixed rubocop
KseniaGovorun Nov 14, 2024
07ce648
Changed user model
KseniaGovorun Nov 14, 2024
aa10cfc
Added test blank_prices in product_spec
KseniaGovorun Nov 14, 2024
509a6e0
Added migrate for authorization model
KseniaGovorun Nov 18, 2024
0e7a248
Refactored tests
SleekMutt Dec 8, 2024
5d71e05
Merge branch 'develop' of github.com:ita-social-projects/ZeroWaste in…
olexandervanzuriak Dec 17, 2024
98c3767
Update gemlock
olexandervanzuriak Dec 17, 2024
1296e1c
rubocop
olexandervanzuriak Dec 17, 2024
92c0044
Fix request tests
olexandervanzuriak Dec 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ GEM
railties (>= 5.0.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
faraday (2.12.0)
faraday-net_http (>= 2.0, < 3.4)
faraday (2.12.2)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.3.0)
net-http
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
ffi (1.17.0-x86_64-linux-gnu)
flipper (1.3.1)
concurrent-ruby (< 2)
Expand Down Expand Up @@ -237,7 +237,7 @@ GEM
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.7.5)
json (2.9.0)
jsonapi-renderer (0.2.2)
jwt (2.9.3)
base64
Expand All @@ -263,7 +263,7 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
logger (1.6.1)
logger (1.6.3)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -283,7 +283,7 @@ GEM
msgpack (1.7.3)
multi_xml (0.6.0)
mutex_m (0.1.2)
net-http (0.4.1)
net-http (0.6.0)
uri
net-imap (0.5.0)
date
Expand Down Expand Up @@ -315,7 +315,7 @@ GEM
omniauth-facebook (10.0.0)
bigdecimal
omniauth-oauth2 (>= 1.2, < 3)
omniauth-google-oauth2 (0.8.2)
omniauth-google-oauth2 (0.8.0)
jwt (>= 2.0)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.6)
Expand Down Expand Up @@ -557,7 +557,7 @@ GEM
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.6.0)
uri (0.13.1)
uri (1.0.2)
useragent (0.16.10)
version_gem (1.1.4)
warden (1.2.9)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/account/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def update
def destroy
@product = resource

if @product.destroy
redirect_to account_products_path, notice: t(".deleted")
else
redirect_to account_products_path, status: :unprocessable_entity
end
notice = @product.destroy ? t(".deleted") : t(".not_deleted")

status = @product.destroy ? :moved_permanently : :unprocessable_entity

redirect_to account_products_path, notice: notice, status: status
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Authorization < ApplicationRecord
# include Encryptable
# attr_encrypted :token, :secret, :refresh_token

belongs_to :admin, optional: true
belongs_to :admin, class_name: "User", foreign_key: "uid", optional: true, inverse_of: :authorizations

validates :uid, uniqueness: { scope: :provider }
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class User < ApplicationRecord
scope :ordered_by_first_name, -> { order(:first_name) }
scope :ordered_by_last_name, -> { order(:last_name) }

has_many :authorizations, foreign_key: "uid", inverse_of: :admin, dependent: :destroy

has_paper_trail ignore: [
:current_sign_in_at, :last_sign_in_at, :confirmation_token,
:encrypted_password
Expand Down
1 change: 1 addition & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ en:
created: "A product was successfully created"
updated: "A product was successfully updated"
deleted: "A product was successfully deleted"
not_deleted: "Can't delete product."
users:
index:
meta-title: "Users"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexToAuthorizationsOnUidAndProvider < ActiveRecord::Migration[7.1]
def change
add_index :authorizations, [:uid, :provider], unique: true
end
end
21 changes: 16 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_10_31_011638) do
ActiveRecord::Schema[7.2].define(version: 2024_11_14_131028) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand All @@ -32,7 +32,7 @@
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.string "checksum"
t.datetime "created_at", precision: nil, null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
Expand All @@ -43,13 +43,23 @@
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "authorizations", force: :cascade do |t|
t.string "uid"
t.string "provider"
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["uid", "provider"], name: "index_authorizations_on_uid_and_provider", unique: true
t.index ["user_id"], name: "index_authorizations_on_user_id"
end

create_table "calculators", force: :cascade do |t|
t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.boolean "preferable", default: false, null: false
t.boolean "preferable", default: false
t.index ["name"], name: "index_calculators_on_name", unique: true
t.index ["slug"], name: "index_calculators_on_slug", unique: true
t.index ["uuid"], name: "index_calculators_on_uuid", unique: true
Expand Down Expand Up @@ -201,9 +211,9 @@
t.string "last_sign_in_ip"
t.string "provider"
t.string "uid"
t.boolean "blocked", default: false, null: false
t.boolean "blocked", default: false
t.integer "role", default: 0
t.boolean "receive_recomendations", default: false, null: false
t.boolean "receive_recomendations", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Expand All @@ -222,6 +232,7 @@

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "authorizations", "users"
add_foreign_key "category_categoryables", "categories"
add_foreign_key "diapers_periods", "categories"
end
7 changes: 7 additions & 0 deletions spec/factories/authorization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :authorization do
uid { "unique_uid" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SecureHex.uid якось так

provider { "provider_name" }
admin factory: [:user]
end
end
4 changes: 2 additions & 2 deletions spec/features/sign_out_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
end
end

xit "signs the user out" do
it "signs the user out" do
expect(page).to have_current_path("/calculators/#{calculator.slug}")
expect(page).to have_content("LOG IN")
expect(page).to have_content("Signed out successfully")
end
end
21 changes: 21 additions & 0 deletions spec/helpers/account/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

RSpec.describe ApplicationHelper, type: :helper do
describe "#current_locale?" do
context "when the current locale matches the given locale" do
it "returns true" do
I18n.with_locale(:en) do
expect(helper.current_locale?(:en)).to be true
end
end
end

context "when the current locale does not match the given locale" do
it "returns false" do
I18n.with_locale(:en) do
expect(helper.current_locale?(:uk)).to be false
end
end
end
end
end
7 changes: 7 additions & 0 deletions spec/models/authorization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "rails_helper"

RSpec.describe Authorization, type: :model do
describe "associations" do
it { is_expected.to belong_to(:admin).class_name("User").with_foreign_key("uid").optional.inverse_of(:authorizations) }
end
end
59 changes: 59 additions & 0 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@
end
end

describe "#price_by_category" do
let(:product) { create(:product, title: "Valid Title") }
let(:category) { create(:category, :medium) }
let(:valid_sum) { 10.0 }

context "when the product has a price for the category" do
let!(:existing_price) { create(:price, priceable: product, category: category, sum: valid_sum) }

it "returns the price for the specified category" do
expect(product.price_by_category(category)).to eq(existing_price)
end
end

context "when the product does not have a price for the category" do
it "returns nil" do
expect(product.price_by_category(category)).to be_nil
end
end
end

describe "#find_or_build_price_for_category" do
let(:product) { create(:product, title: "Valid Title") }
let(:category) { create(:category, :medium) }
Expand All @@ -91,4 +111,43 @@
end
end
end

describe ".diaper" do
let!(:diaper_product) { create(:product, title: "diaper") }
let!(:other_product) { create(:product, title: "shampoo") }

it "returns the product with title 'diaper'" do
expect(Product.diaper).to eq(diaper_product)
end

it "does not return a product with a different title" do
expect(Product.diaper).not_to eq(other_product)
end
end

describe "#blank_prices" do
let(:product) { create(:product, title: "title") }

context "when price sum is blank" do
it "rejects nested attributes for price" do
product.attributes = {
prices_attributes: [
{ sum: nil }
]
}
expect(product.prices).to be_empty
end
end

context "when price sum is present" do
it "accepts nested attributes for price" do
product.attributes = {
prices_attributes: [
{ sum: 100 }
]
}
Comment on lines +144 to +148
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEVER use local vars in specs

expect(product.prices.size).to eq(1)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@
end
end

describe "#inactive_message" do
context "when the user is blocked" do
let(:blocked_user) { create(:user, blocked: true) }

it "returns :locked" do
expect(blocked_user.inactive_message).to eq(:locked)
end
end

context "when the user is active" do
let(:active_user) { create(:user, blocked: false) }

it "returns the default devise message" do
expect(active_user.inactive_message).to eq(:inactive)
end
end
end

describe ".from_omniauth" do
context "when the user exists" do
let(:user) { create(:user) }
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/aplication_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe ApplicationController, type: :controller do
describe "GET #set_i18n_locale_from_params" do
context "when an invalid locale is provided" do
it "sets a flash notice about the unavailable translation" do
get :redirection, params: { locale: "invalid_locale" }

expect(flash.now[:notice]).to eq("invalid_locale translation not available")
end
end

context "when a valid locale is provided" do
it "does not set a flash notice" do
get :redirection, params: { locale: "en" }

expect(flash.now[:notice]).to be_nil
end
end
end
end
25 changes: 25 additions & 0 deletions spec/requests/calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
label: "three",
kind: "parameter", calculator: calculator)
end
let!(:user) { create(:user) }
let(:json_response) { response.parsed_body }

describe "POST api/v2/calculators/PERMALINK/compute" do
Expand Down Expand Up @@ -159,4 +160,28 @@
end
end
end

describe "POST #receive_recomendations" do
context "when user sign_in" do
it "does change recieve_recomendation" do
sign_in user

expect do
post receive_recomendations_path
end.to change(user, :receive_recomendations)
end
end
end

describe "POST #calculate" do
context "when user sign_in" do
it "returns a successful response" do
sign_in user

post calculate_calculator_path(calculator.slug)

expect(response).to be_successful
end
end
end
end
8 changes: 8 additions & 0 deletions spec/requests/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
end
end

context "redirect to root_url" do
it "is successful" do
get root_redirection_path

expect(response).to redirect_to(root_path)
end
end

context "with locale switching" do
it "is successful" do
get root_path(locale: :uk)
Expand Down
Loading
Loading