From c15d5bf579f2f626de1513edc11cbfa45c11ac71 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 20:29:17 +0200 Subject: [PATCH 01/12] Rspec:recipe:is not valid without a user|is valid with valid attributes --- spec/models/recipe_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/models/recipe_spec.rb b/spec/models/recipe_spec.rb index 63f198b..2445a10 100644 --- a/spec/models/recipe_spec.rb +++ b/spec/models/recipe_spec.rb @@ -1 +1,34 @@ require 'rails_helper' + +RSpec.describe Recipe, type: :model do + describe Recipe, type: :model do + it 'is valid with valid attributes' do + user = User.create(name: 'johndoes', email: 'john@email.com', password: '123456') + recipe = Recipe.new( + name: 'Sample Recipe', + preparation_time: 30, + cooking_time: 60, + description: 'Recipe description', + public: true, + user: + ) + expect(recipe).to be_valid + end + + it 'is not valid without a name' do + recipe = Recipe.new(name: nil) + expect(recipe).not_to be_valid + end + + it 'is not valid without a user' do + recipe = Recipe.new( + name: 'MyRecipe', + preparation_time: 30, + cooking_time: 60, + description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', + public: true + ) + expect(recipe).not_to be_valid + end + end +end From 2c84580081084b53db9a5d6408a04a43ab883a0c Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 20:52:32 +0200 Subject: [PATCH 02/12] Rspec:recipe:is not valid with a negative values|Implemented authrization for remove recipe|Added error handling to recipes --- app/controllers/recipe_controller.rb | 1 + app/models/ability.rb | 34 ++++++++++++++++++++++++++++ app/models/recipe.rb | 10 ++++---- app/views/recipe/index.html.erb | 5 +++- app/views/recipe/new.html.erb | 6 ++++- spec/models/recipe_spec.rb | 25 ++++++++++++++++++++ 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 app/models/ability.rb diff --git a/app/controllers/recipe_controller.rb b/app/controllers/recipe_controller.rb index e28047d..5633fd8 100644 --- a/app/controllers/recipe_controller.rb +++ b/app/controllers/recipe_controller.rb @@ -1,4 +1,5 @@ class RecipeController < ApplicationController + load_and_authorize_resource def index @recipes = Recipe.all end diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..71b813c --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +class Ability + include CanCan::Ability + + def initialize(user) + user ||= User.new + can :manage, Recipe, user_id: user.id + # Define abilities for the user here. For example: + # + # return unless user.present? + # can :read, :all + # return unless user.admin? + # can :manage, :all + # + # The first argument to `can` is the action you are giving the user + # permission to do. + # If you pass :manage it will apply to every action. Other common actions + # here are :read, :create, :update and :destroy. + # + # The second argument is the resource the user can perform the action on. + # If you pass :all it will apply to every resource. Otherwise pass a Ruby + # class of the resource. + # + # The third argument is an optional hash of conditions to further filter the + # objects. + # For example, here the user can only update published articles. + # + # can :update, Article, published: true + # + # See the wiki for details: + # https://github.com/CanCanCommunity/cancancan/blob/develop/docs/define_check_abilities.md + end +end diff --git a/app/models/recipe.rb b/app/models/recipe.rb index ba33aef..3a539c3 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -3,9 +3,11 @@ class Recipe < ApplicationRecord has_many :recipe_foods has_many :foods, through: :recipe_foods - validates :name, presence: true - validates :preparation_time, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } - validates :cooking_time, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } - validates :description, presence: true + validates :name, presence: { message: 'Cannot be empty' } + validates :preparation_time, presence: { message: 'Cannot be empty' }, + numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :cooking_time, presence: { message: 'Cannot be empty' }, + numericality: { only_integer: true, greater_than_or_equal_to: 0 } + validates :description, presence: { message: 'Cannot be empty' } validates :public, inclusion: { in: [true, false] } end diff --git a/app/views/recipe/index.html.erb b/app/views/recipe/index.html.erb index 0e2a5b6..ec899ec 100644 --- a/app/views/recipe/index.html.erb +++ b/app/views/recipe/index.html.erb @@ -8,7 +8,10 @@
  • <%= link_to recipe.name, recipe_path(recipe) %>

    - <%= button_to "Remove", recipe, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger" %> + <% if can? :manage, recipe %> + <%= button_to "Remove", recipe, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger" %> +<% end %> +

    <%= recipe.description %>

    diff --git a/app/views/recipe/new.html.erb b/app/views/recipe/new.html.erb index 301c45a..a5080d4 100644 --- a/app/views/recipe/new.html.erb +++ b/app/views/recipe/new.html.erb @@ -1,28 +1,32 @@ -

    Create a New Recipe

    <%= form_for @recipe, url: new_recipe_path, method: :post do |f| %>
    <%= f.label :name %> <%= f.text_field :name %> + <%= f.object.errors.full_messages_for(:name).join(', ') if f.object.errors.include?(:name) %>
    <%= f.label :preparation_time %> <%= f.number_field :preparation_time %> + <%= f.object.errors.full_messages_for(:preparation_time).join(', ') if f.object.errors.include?(:preparation_time) %>
    <%= f.label :cooking_time %> <%= f.number_field :cooking_time %> + <%= f.object.errors.full_messages_for(:cooking_time).join(', ') if f.object.errors.include?(:cooking_time) %>
    <%= f.label :description %> <%= f.text_area :description %> + <%= f.object.errors.full_messages_for(:description).join(', ') if f.object.errors.include?(:description) %>
    <%= f.label :public %> <%= f.check_box :public %> + <%= f.object.errors.full_messages_for(:public).join(', ') if f.object.errors.include?(:public) %>
    diff --git a/spec/models/recipe_spec.rb b/spec/models/recipe_spec.rb index 2445a10..d249950 100644 --- a/spec/models/recipe_spec.rb +++ b/spec/models/recipe_spec.rb @@ -30,5 +30,30 @@ ) expect(recipe).not_to be_valid end + it 'is not valid with a negative preparation time' do + user = User.create(name: 'johndoes', email: 'john@email.com', password: '123456') + recipe = Recipe.new( + name: 'Sample Recipe', + preparation_time: -5, + cooking_time: 60, + description: 'Recipe description', + public: true, + user: + ) + expect(recipe).not_to be_valid + end + + it 'is not valid with a negative cooking time' do + user = User.create(name: 'johndoes', email: 'john@email.com', password: '123456') + recipe = Recipe.new( + name: 'Sample Recipe', + preparation_time: 30, + cooking_time: -10, + description: 'Recipe description', + public: true, + user: + ) + expect(recipe).not_to be_valid + end end end From d0fd2b71541ac5af8fb4e457341a782b81d9d9a0 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 21:00:02 +0200 Subject: [PATCH 03/12] Updated authrorization:recipe visibility --- app/controllers/recipe_controller.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/recipe_controller.rb b/app/controllers/recipe_controller.rb index 5633fd8..fb04a18 100644 --- a/app/controllers/recipe_controller.rb +++ b/app/controllers/recipe_controller.rb @@ -1,7 +1,11 @@ class RecipeController < ApplicationController load_and_authorize_resource - def index - @recipes = Recipe.all + def index + if user_signed_in? + @recipes = Recipe.where(public: true).or(Recipe.where(user_id: current_user.id)) + else + @recipes = Recipe.where(public: true) + end end def new From ab9dd67b9d82d1f504472d8c2a6507e0f54118b5 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 22:36:00 +0200 Subject: [PATCH 04/12] Recipe integration tests --- spec/features/recipe_index_spec.rb | 60 ++++++++++++++++++++++++++++++ spec/support/factory_bot.rb | 3 ++ 2 files changed, 63 insertions(+) create mode 100644 spec/features/recipe_index_spec.rb create mode 100644 spec/support/factory_bot.rb diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb new file mode 100644 index 0000000..c76a291 --- /dev/null +++ b/spec/features/recipe_index_spec.rb @@ -0,0 +1,60 @@ +require 'rails_helper' + +RSpec.describe 'Recipes Index Page', type: :feature do + before(:each) do + @user = User.create(name: 'Riley', email: 'riley@email.com', password: 'riley123', confirmed_at: Time.now) + + sign_in @user + + @recipes = [ + Recipe.create( + user: @user, + name: 'Apple Pie', + description: 'Made with love and apples', + public: true + ), + Recipe.create( + user: @user, + name: 'Apple Pie', + description: 'Warm and creamy', + public: false + ) + ] + + visit recipe_path + end + + describe 'GET /recipe' do + it 'can see all the recipes added' do + expect(page).to have_content('Apple Pie') + + end + + # it "can see each recipe's name." do + # @recipes.each do |recipe| + # expect(page).to have_content(recipe.name) + # end + # end + + # it "can see each recipe's description." do + # @recipes.each do |recipe| + # expect(page).to have_content(recipe.description) + # end + # end + + # it 'can see a button to remove each recipe' do + # @recipes.each do + # expect(page).to have_button('Remove') + # end + # end + # end + + # describe 'GET recipe/id' do + # it "redirects to the recipe's individual page when clicking on the recipe's name" do + # recipe = @recipes.first + # click_link(recipe.name) + # expect(page).to have_current_path(recipe_path(recipe)) + # end + # end +end +end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 0000000..dba338f --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods + end \ No newline at end of file From 3e228f781bb8be5bc1f3acf63f839ec126093160 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 22:39:02 +0200 Subject: [PATCH 05/12] Tests:intergrration updates --- spec/features/recipe_index_spec.rb | 59 +----------------------------- 1 file changed, 2 insertions(+), 57 deletions(-) diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index c76a291..120f913 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -1,60 +1,5 @@ require 'rails_helper' -RSpec.describe 'Recipes Index Page', type: :feature do - before(:each) do - @user = User.create(name: 'Riley', email: 'riley@email.com', password: 'riley123', confirmed_at: Time.now) - - sign_in @user - - @recipes = [ - Recipe.create( - user: @user, - name: 'Apple Pie', - description: 'Made with love and apples', - public: true - ), - Recipe.create( - user: @user, - name: 'Apple Pie', - description: 'Warm and creamy', - public: false - ) - ] - - visit recipe_path - end - - describe 'GET /recipe' do - it 'can see all the recipes added' do - expect(page).to have_content('Apple Pie') - - end - - # it "can see each recipe's name." do - # @recipes.each do |recipe| - # expect(page).to have_content(recipe.name) - # end - # end - - # it "can see each recipe's description." do - # @recipes.each do |recipe| - # expect(page).to have_content(recipe.description) - # end - # end - - # it 'can see a button to remove each recipe' do - # @recipes.each do - # expect(page).to have_button('Remove') - # end - # end - # end - - # describe 'GET recipe/id' do - # it "redirects to the recipe's individual page when clicking on the recipe's name" do - # recipe = @recipes.first - # click_link(recipe.name) - # expect(page).to have_current_path(recipe_path(recipe)) - # end - # end -end +RSpec.feature "RecipeIndices", type: :feature do + pending "add some scenarios (or delete) #{__FILE__}" end From 2adfb3a37ade6c77d622858ae6d07d3c2e8c8ebe Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 23:32:31 +0200 Subject: [PATCH 06/12] Intergration tests:setup --- Gemfile | 14 +++++----- Gemfile.lock | 32 ++++++---------------- app/controllers/recipe_controller.rb | 12 ++++----- app/models/ability.rb | 2 -- spec/features/recipe_index_spec.rb | 5 ---- spec/rails_helper.rb | 16 +++++++++++ spec/support/factory_bot.rb | 40 +++++++++++++++++++++++++--- 7 files changed, 73 insertions(+), 48 deletions(-) diff --git a/Gemfile b/Gemfile index b465a1b..1d66744 100644 --- a/Gemfile +++ b/Gemfile @@ -2,11 +2,6 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.2.2' -gem 'bootstrap', '~> 5.3' -gem 'cancancan' -gem 'devise' -gem 'rspec-rails' -gem 'rubocop', '>= 1.0', '< 2.0' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem 'rails', '~> 7.0.8' @@ -14,7 +9,7 @@ gem 'rails', '~> 7.0.8' gem 'sprockets-rails' # Use postgresql as the database for Active Record -gem 'pg', '~> 1.1' +gem 'pg', '~> 1.5', '>= 1.5.4' # Use the Puma web server [https://github.com/puma/puma] gem 'puma', '~> 5.0' @@ -46,6 +41,7 @@ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', require: false +gem 'rubocop', '>= 1.0', '< 2.0' # Use Sass to process CSS # gem "sassc-rails" @@ -60,7 +56,6 @@ end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] gem 'web-console' - # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] # gem "rack-mini-profiler" @@ -74,4 +69,7 @@ group :test do gem 'selenium-webdriver' end -gem 'cssbundling-rails', '~> 1.3' +gem 'cancancan' +gem 'devise', '~> 4.9' +gem 'factory_bot_rails' +gem 'rspec-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 7509784..2f19169 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -69,17 +69,11 @@ GEM addressable (2.8.5) public_suffix (>= 2.0.2, < 6.0) ast (2.4.2) - autoprefixer-rails (10.4.15.0) - execjs (~> 2) base64 (0.1.1) bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) - bootstrap (5.3.0) - autoprefixer-rails (>= 9.1.0) - popper_js (>= 2.11.7, < 3) - sassc-rails (>= 2.0.0) builder (3.2.4) cancancan (3.5.0) capybara (3.39.2) @@ -93,8 +87,6 @@ GEM xpath (~> 3.2) concurrent-ruby (1.2.2) crass (1.0.6) - cssbundling-rails (1.3.3) - railties (>= 6.0.0) date (3.3.3) debug (1.8.0) irb (>= 1.5.0) @@ -107,8 +99,11 @@ GEM warden (~> 1.2.3) diff-lcs (1.5.0) erubi (1.12.0) - execjs (2.9.1) - ffi (1.16.3) + factory_bot (6.2.1) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) globalid (1.2.1) activesupport (>= 6.1) i18n (1.14.1) @@ -157,7 +152,6 @@ GEM ast (~> 2.4.1) racc pg (1.5.4) - popper_js (2.11.8) psych (5.1.1) stringio public_suffix (5.0.3) @@ -239,14 +233,6 @@ GEM parser (>= 3.2.1.0) ruby-progressbar (1.13.0) rubyzip (2.3.2) - sassc (2.4.0) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt selenium-webdriver (4.14.0) rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) @@ -262,7 +248,6 @@ GEM railties (>= 6.0.0) stringio (3.0.8) thor (1.2.2) - tilt (2.3.0) timeout (0.4.0) turbo-rails (1.5.0) actionpack (>= 6.0.0) @@ -292,15 +277,14 @@ PLATFORMS DEPENDENCIES bootsnap - bootstrap (~> 5.3) cancancan capybara - cssbundling-rails (~> 1.3) debug - devise + devise (~> 4.9) + factory_bot_rails importmap-rails jbuilder - pg (~> 1.1) + pg (~> 1.5, >= 1.5.4) puma (~> 5.0) rails (~> 7.0.8) rspec-rails diff --git a/app/controllers/recipe_controller.rb b/app/controllers/recipe_controller.rb index fb04a18..e60f784 100644 --- a/app/controllers/recipe_controller.rb +++ b/app/controllers/recipe_controller.rb @@ -1,11 +1,11 @@ class RecipeController < ApplicationController load_and_authorize_resource - def index - if user_signed_in? - @recipes = Recipe.where(public: true).or(Recipe.where(user_id: current_user.id)) - else - @recipes = Recipe.where(public: true) - end + def index + @recipes = if user_signed_in? + Recipe.where(public: true).or(Recipe.where(user_id: current_user.id)) + else + Recipe.where(public: true) + end end def new diff --git a/app/models/ability.rb b/app/models/ability.rb index 71b813c..8e517c7 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - class Ability include CanCan::Ability diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index 120f913..e69de29 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -1,5 +0,0 @@ -require 'rails_helper' - -RSpec.feature "RecipeIndices", type: :feature do - pending "add some scenarios (or delete) #{__FILE__}" -end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6584c98..5c5847b 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -4,7 +4,13 @@ require_relative '../config/environment' # Prevent database truncation if the environment is production abort('The Rails environment is running in production mode!') if Rails.env.production? +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } require 'rspec/rails' +require 'capybara/rails' +require 'capybara/rspec' +require 'factory_bot_rails' +require 'devise' + # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in @@ -30,8 +36,16 @@ abort e.to_s.strip end RSpec.configure do |config| + config.before(:each, type: :feature) do + default_url_options[:host] = 'http://127.0.0.1:3000/' # Replace with your application's host + end + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{Rails.root}/spec/fixtures" + config.include FactoryBot::Syntax::Methods + config.include Devise::Test::IntegrationHelpers, type: :request + config.include Devise::Test::IntegrationHelpers, type: :feature + config.include Devise::Test::ControllerHelpers, type: :controller # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false @@ -61,3 +75,5 @@ # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") end + +Capybara.javascript_driver = :selenium_chrome_headless diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb index dba338f..49d2ec2 100644 --- a/spec/support/factory_bot.rb +++ b/spec/support/factory_bot.rb @@ -1,3 +1,37 @@ -RSpec.configure do |config| - config.include FactoryBot::Syntax::Methods - end \ No newline at end of file +FactoryBot.define do + factory :food do + name { 'Brocolli' } + measurement_unit { 'g' } + price { 0.7 } + quantity { 1000 } + user + end +end + +FactoryBot.define do + factory :user do + name { 'Riley' } + email { 'riley@example.com' } + password { 'password123' } + food + end +end + +FactoryBot.define do + factory :recipe do + name { 'Vegetable Soup' } + preparation_time { 10 } + cooking_time { 20 } + description { 'A delicious chicken curry' } + public { true } + user + end +end + +FactoryBot.define do + factory :recipe_food do + quantity { 500 } + recipe + food + end +end From 97c5fc8fd497ab4645188a7946f6cc1060514e98 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 23:45:41 +0200 Subject: [PATCH 07/12] IntergrationSpec:recipe:user can add recipe --- spec/features/recipe_index_spec.rb | 31 ++++++++++++++++++++++++++++++ spec/support/factory_bot.rb | 29 +++++++++++++--------------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index e69de29..45429d6 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.feature 'Recipe#index Page', type: :feature do + scenario 'user can add a new recipe' do + user = create(:user) + login_as(user, :scope => :user) + visit recipe_index_path + click_button('Add Recipe') + + expect(page).to have_current_path(new_recipe_path) + end +# scenario 'user can see the list of recipes' do +# user = create(:user) +# recipe = create(name: 'Vegetable Soup', description: 'A delicious chicken curry', user: user) +# visit recipe_index_path +# expect(page).to have_content('Recipes') +# expect(page).to have_content('Vegetable Soup') +# expect(page).to have_content('A delicious chicken curry') +# expect(page).to have_button('Remove') +# end + +# scenario 'user sees a message when there are no recipes available' do +# visit recipe_index_path + +# expect(page).to have_content('Recipes') +# expect(page).to have_content('No recipes available.') +# expect(page).not_to have_button('Remove') +# end + + +end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb index 49d2ec2..baab42a 100644 --- a/spec/support/factory_bot.rb +++ b/spec/support/factory_bot.rb @@ -1,10 +1,6 @@ FactoryBot.define do - factory :food do - name { 'Brocolli' } - measurement_unit { 'g' } - price { 0.7 } - quantity { 1000 } - user + factory :recipe_food do + quantity { 500 } end end @@ -13,7 +9,16 @@ name { 'Riley' } email { 'riley@example.com' } password { 'password123' } - food + end +end + +FactoryBot.define do + factory :food do + name { 'Broccoli' } + measurement_unit { 'g' } + price { 0.7 } + quantity { 1000 } + user end end @@ -22,16 +27,8 @@ name { 'Vegetable Soup' } preparation_time { 10 } cooking_time { 20 } - description { 'A delicious chicken curry' } + description { 'A delicious vegetable soup' } public { true } user end end - -FactoryBot.define do - factory :recipe_food do - quantity { 500 } - recipe - food - end -end From a1758b267e68f30e4f615e8196e6e7bd6e155aa0 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Wed, 18 Oct 2023 23:46:24 +0200 Subject: [PATCH 08/12] Linter fixes --- spec/features/recipe_index_spec.rb | 36 ++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index 45429d6..4973eee 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -1,31 +1,29 @@ require 'rails_helper' RSpec.feature 'Recipe#index Page', type: :feature do - scenario 'user can add a new recipe' do + scenario 'authorized user can add a new recipe' do user = create(:user) - login_as(user, :scope => :user) + login_as(user, scope: :user) visit recipe_index_path click_button('Add Recipe') expect(page).to have_current_path(new_recipe_path) end -# scenario 'user can see the list of recipes' do -# user = create(:user) -# recipe = create(name: 'Vegetable Soup', description: 'A delicious chicken curry', user: user) -# visit recipe_index_path -# expect(page).to have_content('Recipes') -# expect(page).to have_content('Vegetable Soup') -# expect(page).to have_content('A delicious chicken curry') -# expect(page).to have_button('Remove') -# end - -# scenario 'user sees a message when there are no recipes available' do -# visit recipe_index_path - -# expect(page).to have_content('Recipes') -# expect(page).to have_content('No recipes available.') -# expect(page).not_to have_button('Remove') -# end + # scenario 'user can see the list of recipes' do + # user = create(:user) + # recipe = create(name: 'Vegetable Soup', description: 'A delicious chicken curry', user: user) + # visit recipe_index_path + # expect(page).to have_content('Recipes') + # expect(page).to have_content('Vegetable Soup') + # expect(page).to have_content('A delicious chicken curry') + # expect(page).to have_button('Remove') + # end + # scenario 'user sees a message when there are no recipes available' do + # visit recipe_index_path + # expect(page).to have_content('Recipes') + # expect(page).to have_content('No recipes available.') + # expect(page).not_to have_button('Remove') + # end end From 08db375ecfc27a465a1738fa9c845278dd6e7be5 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Thu, 19 Oct 2023 07:57:40 +0200 Subject: [PATCH 09/12] Itergration Spec:recipe:user can remove their recipe|user can add a new recipe --- spec/features/recipe_index_spec.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index 4973eee..eb766c9 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -6,24 +6,20 @@ login_as(user, scope: :user) visit recipe_index_path click_button('Add Recipe') - expect(page).to have_current_path(new_recipe_path) end - # scenario 'user can see the list of recipes' do - # user = create(:user) - # recipe = create(name: 'Vegetable Soup', description: 'A delicious chicken curry', user: user) - # visit recipe_index_path - # expect(page).to have_content('Recipes') - # expect(page).to have_content('Vegetable Soup') - # expect(page).to have_content('A delicious chicken curry') - # expect(page).to have_button('Remove') - # end - # scenario 'user sees a message when there are no recipes available' do - # visit recipe_index_path + scenario 'user can remove their recipe' do + user = create(:user) + login_as(user, scope: :user) + visit recipe_index_path + expect(page).not_to have_button('Remove', exact: true) + end - # expect(page).to have_content('Recipes') - # expect(page).to have_content('No recipes available.') - # expect(page).not_to have_button('Remove') - # end + scenario 'user can add a new recipe' do + user = create(:user) + login_as(user, scope: :user) + visit recipe_index_path + expect(page).to have_button('Add Recipe', exact: true) + end end From c6e1c046bad64d3bd1efe458803e015d0e00a5d7 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Thu, 19 Oct 2023 08:18:53 +0200 Subject: [PATCH 10/12] Intergration:Spec:public recipes --- app/views/recipe_foods/index.html.erb | 1 - app/views/recipe_foods/show.html.erb | 0 spec/features/public_recipes_spec.rb | 11 +++++++++++ spec/features/recipe_index_spec.rb | 8 ++++++++ spec/support/factory_bot.rb | 11 +++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) delete mode 100644 app/views/recipe_foods/index.html.erb delete mode 100644 app/views/recipe_foods/show.html.erb create mode 100644 spec/features/public_recipes_spec.rb diff --git a/app/views/recipe_foods/index.html.erb b/app/views/recipe_foods/index.html.erb deleted file mode 100644 index 57cb460..0000000 --- a/app/views/recipe_foods/index.html.erb +++ /dev/null @@ -1 +0,0 @@ -

    Recipe Food

    \ No newline at end of file diff --git a/app/views/recipe_foods/show.html.erb b/app/views/recipe_foods/show.html.erb deleted file mode 100644 index e69de29..0000000 diff --git a/spec/features/public_recipes_spec.rb b/spec/features/public_recipes_spec.rb new file mode 100644 index 0000000..1259690 --- /dev/null +++ b/spec/features/public_recipes_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.feature 'Public Recipes Page', type: :feature do + scenario 'all users can see public recipes' do + user = create(:user) + login_as(user, scope: :user) + recipe = create(:recipe, user:) + visit public_recipes_path + expect(page).to have_content(recipe.name) + end +end diff --git a/spec/features/recipe_index_spec.rb b/spec/features/recipe_index_spec.rb index eb766c9..b3b1cc3 100644 --- a/spec/features/recipe_index_spec.rb +++ b/spec/features/recipe_index_spec.rb @@ -22,4 +22,12 @@ visit recipe_index_path expect(page).to have_button('Add Recipe', exact: true) end + + scenario 'user can view their own private and public recipes' do + user = create(:user) + login_as(user, scope: :user) + recipe = create(:recipe, user:) + visit recipe_index_path + expect(page).to have_content(recipe.name) + end end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb index baab42a..b2d5a13 100644 --- a/spec/support/factory_bot.rb +++ b/spec/support/factory_bot.rb @@ -32,3 +32,14 @@ user end end + +FactoryBot.define do + factory :public_recipe, class: 'Recipe' do + name { 'Public Recipe' } + preparation_time { 15 } + cooking_time { 25 } + description { 'A public recipe' } + public { true } + user + end +end From c6e52c63cf2e0151df3cbb0a6f3e2e1f0e3f3b4e Mon Sep 17 00:00:00 2001 From: RileyManda Date: Thu, 19 Oct 2023 08:31:07 +0200 Subject: [PATCH 11/12] Code cleanup --- spec/factories/recipe_foods.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 spec/factories/recipe_foods.rb diff --git a/spec/factories/recipe_foods.rb b/spec/factories/recipe_foods.rb new file mode 100644 index 0000000..2496118 --- /dev/null +++ b/spec/factories/recipe_foods.rb @@ -0,0 +1,4 @@ +FactoryBot.define do + factory :recipe_food do + end +end From c8834504f970f6d9e32cc2c7fcd77d310001d260 Mon Sep 17 00:00:00 2001 From: RileyManda Date: Thu, 19 Oct 2023 08:33:19 +0200 Subject: [PATCH 12/12] Linter fixes --- spec/factories/recipe_foods.rb | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 spec/factories/recipe_foods.rb diff --git a/spec/factories/recipe_foods.rb b/spec/factories/recipe_foods.rb deleted file mode 100644 index 2496118..0000000 --- a/spec/factories/recipe_foods.rb +++ /dev/null @@ -1,4 +0,0 @@ -FactoryBot.define do - factory :recipe_food do - end -end