diff --git a/.gitignore b/.gitignore
index 62ed74579..0a47a880f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,8 @@
# Ignore bundler config.
/.bundle
coverage
+.DS_Store
+.vscode
# Ignore all logfiles and tempfiles.
/log/*
diff --git a/Gemfile b/Gemfile
index 4f8844c6c..2f5052e82 100644
--- a/Gemfile
+++ b/Gemfile
@@ -16,6 +16,7 @@ gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.1.0', require: false
+gem 'bcrypt', '~> 3.1.7'
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
diff --git a/Gemfile.lock b/Gemfile.lock
index 4a041a1b9..091485647 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,43 +1,43 @@
GEM
remote: https://rubygems.org/
specs:
- actioncable (5.2.4.3)
- actionpack (= 5.2.4.3)
+ actioncable (5.2.4.4)
+ actionpack (= 5.2.4.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
- actionmailer (5.2.4.3)
- actionpack (= 5.2.4.3)
- actionview (= 5.2.4.3)
- activejob (= 5.2.4.3)
+ actionmailer (5.2.4.4)
+ actionpack (= 5.2.4.4)
+ actionview (= 5.2.4.4)
+ activejob (= 5.2.4.4)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
- actionpack (5.2.4.3)
- actionview (= 5.2.4.3)
- activesupport (= 5.2.4.3)
+ actionpack (5.2.4.4)
+ actionview (= 5.2.4.4)
+ activesupport (= 5.2.4.4)
rack (~> 2.0, >= 2.0.8)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
- actionview (5.2.4.3)
- activesupport (= 5.2.4.3)
+ actionview (5.2.4.4)
+ activesupport (= 5.2.4.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
- activejob (5.2.4.3)
- activesupport (= 5.2.4.3)
+ activejob (5.2.4.4)
+ activesupport (= 5.2.4.4)
globalid (>= 0.3.6)
- activemodel (5.2.4.3)
- activesupport (= 5.2.4.3)
- activerecord (5.2.4.3)
- activemodel (= 5.2.4.3)
- activesupport (= 5.2.4.3)
+ activemodel (5.2.4.4)
+ activesupport (= 5.2.4.4)
+ activerecord (5.2.4.4)
+ activemodel (= 5.2.4.4)
+ activesupport (= 5.2.4.4)
arel (>= 9.0)
- activestorage (5.2.4.3)
- actionpack (= 5.2.4.3)
- activerecord (= 5.2.4.3)
+ activestorage (5.2.4.4)
+ actionpack (= 5.2.4.4)
+ activerecord (= 5.2.4.4)
marcel (~> 0.3.1)
- activesupport (5.2.4.3)
+ activesupport (5.2.4.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
@@ -45,8 +45,9 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (9.0.0)
+ bcrypt (3.1.16)
bindex (0.8.1)
- bootsnap (1.4.8)
+ bootsnap (1.4.9)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
@@ -77,7 +78,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
- jbuilder (2.10.0)
+ jbuilder (2.10.1)
activesupport (>= 5.0.0)
launchy (2.5.0)
addressable (~> 2.7)
@@ -98,7 +99,7 @@ GEM
mini_portile2 (2.4.0)
minitest (5.14.2)
msgpack (1.3.3)
- nio4r (2.5.3)
+ nio4r (2.5.4)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
pg (1.2.3)
@@ -110,27 +111,27 @@ GEM
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
- rails (5.2.4.3)
- actioncable (= 5.2.4.3)
- actionmailer (= 5.2.4.3)
- actionpack (= 5.2.4.3)
- actionview (= 5.2.4.3)
- activejob (= 5.2.4.3)
- activemodel (= 5.2.4.3)
- activerecord (= 5.2.4.3)
- activestorage (= 5.2.4.3)
- activesupport (= 5.2.4.3)
+ rails (5.2.4.4)
+ actioncable (= 5.2.4.4)
+ actionmailer (= 5.2.4.4)
+ actionpack (= 5.2.4.4)
+ actionview (= 5.2.4.4)
+ activejob (= 5.2.4.4)
+ activemodel (= 5.2.4.4)
+ activerecord (= 5.2.4.4)
+ activestorage (= 5.2.4.4)
+ activesupport (= 5.2.4.4)
bundler (>= 1.3.0)
- railties (= 5.2.4.3)
+ railties (= 5.2.4.4)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
- railties (5.2.4.3)
- actionpack (= 5.2.4.3)
- activesupport (= 5.2.4.3)
+ railties (5.2.4.4)
+ actionpack (= 5.2.4.4)
+ activesupport (= 5.2.4.4)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
@@ -138,10 +139,10 @@ GEM
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
- regexp_parser (1.7.1)
- rspec-core (3.9.2)
+ regexp_parser (1.8.2)
+ rspec-core (3.9.3)
rspec-support (~> 3.9.3)
- rspec-expectations (3.9.2)
+ rspec-expectations (3.9.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
@@ -155,7 +156,7 @@ GEM
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
- rspec-support (3.9.3)
+ rspec-support (3.9.4)
ruby_dep (1.5.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
@@ -170,14 +171,14 @@ GEM
tilt (>= 1.1, < 3)
shoulda-matchers (4.4.1)
activesupport (>= 4.2.0)
- simplecov (0.19.0)
+ simplecov (0.19.1)
docile (~> 1.1)
simplecov-html (~> 0.11)
- simplecov-html (0.12.2)
+ simplecov-html (0.12.3)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
- sprockets-rails (3.2.1)
+ sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
@@ -203,6 +204,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0)
byebug
capybara
diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css
index 98d51b92d..b8063dc97 100644
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -25,12 +25,26 @@
font-family: "Marker Felt", fantasy;
}
+ p {
+ color: black;
+ font-size: 15px;
+ }
+
+ p1 {
+ font-size: 17px;
+ color: white;
+ }
+
+#current-user {
+ color: white;
+ }
ul{
padding: 0px;
text-align: center;
}
img{
- width: 50%;
+ width:20%;
+ height:auto;
}
#cart-image{
@@ -99,6 +113,12 @@ section[id*='review'] {
text-align: center;
}
+ button{
+ text-align: center;
+ padding: 5px;
+ width: 30%;
+ }
+
input[type=text], select {
width: 100%;
padding: 12px 20px;
@@ -135,6 +155,12 @@ form {
background-color: red;
color: white;
}
+
+.notice-flash {
+ padding: 10px;
+ background-color: green;
+ color: white;
+}
.success-flash {
padding: 10px;
background-color: green;
diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb
new file mode 100644
index 000000000..0fb64f805
--- /dev/null
+++ b/app/controllers/admin/base_controller.rb
@@ -0,0 +1,7 @@
+class Admin::BaseController < ApplicationController
+ before_action :require_admin
+
+ def require_admin
+ render file: "/public/404" unless current_admin?
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb
new file mode 100644
index 000000000..dd211c44b
--- /dev/null
+++ b/app/controllers/admin/dashboard_controller.rb
@@ -0,0 +1,5 @@
+class Admin::DashboardController < Admin::BaseController
+ def index
+ @orders = Order.all
+ end
+end
diff --git a/app/controllers/admin/merchants_controller.rb b/app/controllers/admin/merchants_controller.rb
new file mode 100644
index 000000000..892200c31
--- /dev/null
+++ b/app/controllers/admin/merchants_controller.rb
@@ -0,0 +1,24 @@
+class Admin::MerchantsController < Admin::BaseController
+ def show
+ @merchant = Merchant.find(params[:merchant_id])
+ end
+ before_action :require_admin
+
+ def index
+ @merchants = Merchant.all
+ end
+
+ def disable
+ merchant = Merchant.find(params[:merchant_id])
+ merchant.disable_merchant
+ redirect_to '/admin/merchants'
+ flash[:notice] = "Merchant Account Is Now Disabled"
+ end
+
+ def enable
+ merchant = Merchant.find(params[:merchant_id])
+ merchant.enable_merchant
+ redirect_to '/admin/merchants'
+ flash[:notice] = "Merchant Account Is Now Enabled"
+ end
+end
diff --git a/app/controllers/admin/profile_orders_controller.rb b/app/controllers/admin/profile_orders_controller.rb
new file mode 100644
index 000000000..38a3bc029
--- /dev/null
+++ b/app/controllers/admin/profile_orders_controller.rb
@@ -0,0 +1,8 @@
+class Admin::ProfileOrdersController < Admin::BaseController
+ def update
+ order = Order.find(params[:order_id])
+ order.update!(status: "Shipped")
+
+ redirect_to "/admin"
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
new file mode 100644
index 000000000..8453ea84a
--- /dev/null
+++ b/app/controllers/admin/users_controller.rb
@@ -0,0 +1,9 @@
+class Admin::UsersController < Admin::BaseController
+ def index
+ @users = User.all
+ end
+
+ def show
+ @user = User.find(params[:id])
+ end
+end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 8087a03bc..ee1097168 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,10 +1,25 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
- helper_method :cart
+ helper_method :cart, :current_user, :current_admin?, :current_merchant?, :current_user?
def cart
@cart ||= Cart.new(session[:cart] ||= Hash.new(0))
end
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ end
+
+ def current_admin?
+ current_user && current_user.admin?
+ end
+
+ def current_merchant?
+ current_user && current_user.merchant?
+ end
+
+ def current_user?
+ current_user && current_user.user?
+ end
end
diff --git a/app/controllers/cart_base_controller.rb b/app/controllers/cart_base_controller.rb
new file mode 100644
index 000000000..87c0e3fed
--- /dev/null
+++ b/app/controllers/cart_base_controller.rb
@@ -0,0 +1,7 @@
+class CartBaseController < ApplicationController
+ before_action :require_non_admin
+
+ def require_non_admin
+ render file: "/public/404" if current_admin?
+ end
+end
diff --git a/app/controllers/cart_controller.rb b/app/controllers/cart_controller.rb
index d1de636df..01b0416b2 100644
--- a/app/controllers/cart_controller.rb
+++ b/app/controllers/cart_controller.rb
@@ -1,4 +1,4 @@
-class CartController < ApplicationController
+class CartController < CartBaseController
def add_item
item = Item.find(params[:item_id])
cart.add_item(item.id.to_s)
@@ -20,5 +20,28 @@ def remove_item
redirect_to '/cart'
end
+ def modify_quantity
+ item = Item.find(params[:item_id])
+ if params[:operation] == "add"
+ if item.inventory > session[:cart][item.id.to_s]
+ cart.add_item(item.id.to_s)
+ flash[:success] = "Another #{item.name} was successfully added to your cart"
+ redirect_to "/cart"
+ else
+ flash[:notice] = "Not Enough Inventory for #{item.name}"
+ redirect_to "/cart"
+ end
+ elsif params[:operation] == "subtract"
+ if session[:cart][item.id.to_s] > 1
+ cart.subtract_item(item.id.to_s)
+ flash[:success] = "A #{item.name} was successfully removed from your cart"
+ redirect_to "/cart"
+ else
+ session[:cart].delete(params[:item_id])
+ flash[:success] = "#{item.name} was successfully removed from your cart"
+ redirect_to '/cart'
+ end
+ end
+ end
end
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index ae281cb37..81092b980 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -5,12 +5,13 @@ def index
@merchant = Merchant.find(params[:merchant_id])
@items = @merchant.items
else
- @items = Item.all
+ @items = Item.find_enabled_items
end
end
def show
@item = Item.find(params[:id])
+
end
def new
@@ -55,6 +56,4 @@ def destroy
def item_params
params.permit(:name,:description,:price,:inventory,:image)
end
-
-
end
diff --git a/app/controllers/merchant/base_controller.rb b/app/controllers/merchant/base_controller.rb
new file mode 100644
index 000000000..712d0be99
--- /dev/null
+++ b/app/controllers/merchant/base_controller.rb
@@ -0,0 +1,7 @@
+class Merchant::BaseController < ApplicationController
+ before_action :require_merchant
+
+ def require_merchant
+ render file: "/public/404" unless current_merchant?
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/merchant/dashboard_controller.rb b/app/controllers/merchant/dashboard_controller.rb
new file mode 100644
index 000000000..418b2e2d1
--- /dev/null
+++ b/app/controllers/merchant/dashboard_controller.rb
@@ -0,0 +1,5 @@
+class Merchant::DashboardController < Merchant::BaseController
+ def show
+ @merchant = User.find(session[:user_id]).merchant
+ end
+end
diff --git a/app/controllers/merchant/item_orders_controller.rb b/app/controllers/merchant/item_orders_controller.rb
new file mode 100644
index 000000000..50857dfb4
--- /dev/null
+++ b/app/controllers/merchant/item_orders_controller.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class Merchant::ItemOrdersController < Merchant::BaseController
+ def update
+ item_order = ItemOrder.find(params[:itemorder_id])
+ item_order.assign_attributes(status: 'Fulfilled')
+ if item_order.save
+ item_order.item.update(inventory: item_order.items_left)
+ redirect_to "/merchant/orders/#{item_order.order.id}", notice: "#{item_order.item.name} fulfilled"
+ end
+ end
+end
diff --git a/app/controllers/merchant/items_controller.rb b/app/controllers/merchant/items_controller.rb
new file mode 100644
index 000000000..8e613f512
--- /dev/null
+++ b/app/controllers/merchant/items_controller.rb
@@ -0,0 +1,70 @@
+class Merchant::ItemsController < Merchant::BaseController
+ def index
+ user = User.find(session[:user_id])
+ @merchant = Merchant.find(user.merchant.id)
+ end
+
+ def new
+ @item = Item.new
+ end
+
+ def create
+ @item = Item.new(item_params)
+ if @item.save
+ redirect_to '/merchant/items'
+ flash[:notice] = "New item has been created"
+ else
+ flash[:error] = @item.errors.full_messages.to_sentence
+ render :new
+ end
+ end
+
+ def edit
+ @item = Item.find(params[:id])
+ end
+
+ def update
+ @item = Item.find(params[:id])
+ @item.update(item_params_edit)
+ if @item.save
+ redirect_to "/merchant/items"
+ else
+ flash[:error] = @item.errors.full_messages.to_sentence
+ render :edit
+ end
+ end
+
+ def deactivate
+ item = Item.find(params[:item_id])
+ item.update(active?: false)
+ redirect_to '/merchant/items'
+ flash[:notice] = "This item is no longer for sale"
+
+ end
+
+ def activate
+ item = Item.find(params[:item_id])
+ item.update(active?: true)
+ redirect_to '/merchant/items'
+ flash[:notice] = "This item is now available for sale"
+ end
+
+ def destroy
+ item = Item.find(params[:id])
+ item.destroy
+ redirect_to '/merchant/items'
+ flash[:notice] = "This item has been deleted"
+ end
+
+ private
+
+ def item_params_edit
+
+ params.permit(:name, :description, :image, :price, :inventory, :merchant_id)
+
+ end
+
+ def item_params
+ params.require(:item).permit(:name, :description, :image, :price, :inventory, :merchant_id)
+ end
+end
diff --git a/app/controllers/merchant/orders_controller.rb b/app/controllers/merchant/orders_controller.rb
new file mode 100644
index 000000000..71b897797
--- /dev/null
+++ b/app/controllers/merchant/orders_controller.rb
@@ -0,0 +1,5 @@
+class Merchant::OrdersController < Merchant::BaseController
+ def show
+ @order = Order.find(params["id"])
+ end
+end
diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb
index 294213df6..dcf3c4a66 100644
--- a/app/controllers/merchants_controller.rb
+++ b/app/controllers/merchants_controller.rb
@@ -1,5 +1,6 @@
-class MerchantsController <%=link_to "#{merchant.name}" "/admin/merchants/#{merchant.id}" %> <%= merchant.city %> <%= merchant.state %> <%= @merchant.address %> <%= @merchant.city %>, <%= @merchant.state %> <%= @merchant.zip %> <%= city %> Name: <%= @user.name %> Address: <%= @user.full_address %> Email: <%= @user.email %>Packaged
+ <% @orders.each do |order| %>
+ Pending
+ <% @orders.each do |order| %>
+ Shipped
+ <% @orders.each do |order| %>
+ Cancelled
+ <% @orders.each do |order| %>
+ <%= link_to @merchant.name, "/merchants/#{@merchant.id}" %>
+<%= link_to "All #{@merchant.name} Items", "/merchants/#{@merchant.id}/items" %>
+<%= link_to "Update Merchant", "/merchants/#{@merchant.id}/edit" %>
+<%= link_to "Delete Merchant", "/merchants/#{@merchant.id}", method: :delete if @merchant.no_orders?%>
+
+Number of Items: <%=@merchant.item_count%>
+ <% if @merchant.item_count > 0 %>
+ Average Price of Items: <%=number_to_currency( @merchant.average_item_price)%>
+ Cities that order these items:
+ <% @merchant.distinct_cities.each do |city| %>
+ <%= @user.name %>'s Profile
+
<%=number_to_currency(item.price)%>
+<%=number_to_currency(item.price)%>
<%=quantity%>
+<%= button_to "+", "/cart/#{item.id}",params: {'operation' => "add"}, method: :patch %>
+<%= button_to "-", "/cart/#{item.id}",params: {'operation' => "subtract"}, method: :patch %>
<%= link_to "Remove", "/cart/#{item.id}",method: :delete %>
Total: <%= number_to_currency(cart.total) %>
<%= link_to "Empty Cart", "/cart", method: :delete %>
-<%= link_to "Checkout", "/orders/new", method: :get %>
+ <% if current_user %> +<%= link_to "Checkout", "/orders/new", method: :get %>
+ <% else %> +You must <%= link_to "register", "/register" %> or <%= link_to "log in", "/login" %> to checkout
+ <% end %> <% else %>Most Popular Items: | +<% Item.top_five.each do |item| %> | +<%=item.name %>: | +<%= item.quantity %> | + <% end %> +
Least Popular Items: | +<% Item.bottom_five.each do |item| %> | +<%=item.name %>: | +<%= item.quantity %> | + <% end %> +
<%= msg %>
+<%= order.created_at %>
+<%= order.item_qty(@merchant.id) %>
+<%= order.total_value(@merchant.id) %>
+ <% end %> + diff --git a/app/views/merchant/items/edit.html.erb b/app/views/merchant/items/edit.html.erb new file mode 100644 index 000000000..8ac55cfe0 --- /dev/null +++ b/app/views/merchant/items/edit.html.erb @@ -0,0 +1,24 @@ +Sold by: <%=link_to item.merchant.name, "/merchants/#{item.merchant.id}" %>
+ > +<%= item.description unless @merchant%>
+Price: <%=number_to_currency(item.price) %>
+Inventory: <%= item.inventory %>
+ <% if !@merchant %> + <% end %> + <% if item.active? %> +Active
+ <% else %> +Inactive
+ <% end %> +Item | @@ -19,7 +19,7 @@
---|
Name | -Address | -City | -State | -Zip | -
---|---|---|---|---|
<%= @order.name %> |
- <%= @order.address %> |
- <%= @order.city %> |
- <%= @order.state %> |
- <%= @order.zip %> |
-
Item | -Sold By | -Price | -Quantity | -Subtotal | -
---|---|---|---|---|
<%=link_to item_order.item.name, "/items/#{item_order.item_id}"%> |
- <%= link_to item_order.item.merchant.name, "/merchants/#{item_order.item.merchant.id}"%> |
- <%= number_to_currency(item_order.price)%> |
- <%= item_order.quantity%> |
- <%= number_to_currency(item_order.subtotal)%> |
-
Total: <%=number_to_currency(@order.grandtotal)%>
-<%= @order.created_at%>
-Name | +Address | +City | +State | +Zip | +
---|---|---|---|---|
<%= @order.name %> |
+ <%= @order.address %> |
+ <%= @order.city %> |
+ <%= @order.state %> |
+ <%= @order.zip %> |
+
Item | +Description | +Sold By | +Price | +Quantity | +Subtotal | +|
---|---|---|---|---|---|---|
<%=link_to item_order.item.name, "/items/#{item_order.item_id}"%> |
+ <%= item_order.item.description %> |
+ <%= link_to item_order.item.merchant.name, "/merchants/#{item_order.item.merchant.id}"%> |
+ <%= number_to_currency(item_order.price)%> |
+ <%= item_order.quantity%> |
+ <%= number_to_currency(item_order.subtotal)%> |
+ + |
Total: <%=number_to_currency(@order.grandtotal)%>
+<%= @order.created_at%>
+Name: <%= @current_user.name %>
+Address: <%= @current_user.full_address %>
+Email: <%= @current_user.email %>
+ +<%= link_to 'Edit Profile', "/profile/edit" %> +<%= link_to 'Edit Password', "/password/edit" %> +<%= link_to 'My Orders', "/profile/orders" %> diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/config/environment.rb b/config/environment.rb index 426333bb4..856fba296 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -2,4 +2,4 @@ require_relative 'application' # Initialize the Rails application. -Rails.application.initialize! +Rails.application.initialize! \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7782dfa6f..e3e73f2bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,36 +1,76 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + def put(*) end + + root "welcome#index" - get "/merchants", to: "merchants#index" - get "/merchants/new", to: "merchants#new" - get "/merchants/:id", to: "merchants#show" - post "/merchants", to: "merchants#create" - get "/merchants/:id/edit", to: "merchants#edit" - patch "/merchants/:id", to: "merchants#update" - delete "/merchants/:id", to: "merchants#destroy" - - get "/items", to: "items#index" - get "/items/:id", to: "items#show" - get "/items/:id/edit", to: "items#edit" - patch "/items/:id", to: "items#update" - get "/merchants/:merchant_id/items", to: "items#index" - get "/merchants/:merchant_id/items/new", to: "items#new" - post "/merchants/:merchant_id/items", to: "items#create" - delete "/items/:id", to: "items#destroy" - - get "/items/:item_id/reviews/new", to: "reviews#new" - post "/items/:item_id/reviews", to: "reviews#create" - - get "/reviews/:id/edit", to: "reviews#edit" - patch "/reviews/:id", to: "reviews#update" - delete "/reviews/:id", to: "reviews#destroy" + resources :merchants + + resources :items, only: %i[index show edit update destroy] + + resources :merchants do + resources :items + end + + resources :items, only: %i[new create] do + resources :reviews, only: %i[new create] + end + + resources :items, only: %i[new create] do + resources :reviews, only: %i[new create] + end + + resources :reviews, only: %i[edit update destroy] post "/cart/:item_id", to: "cart#add_item" get "/cart", to: "cart#show" delete "/cart", to: "cart#empty" delete "/cart/:item_id", to: "cart#remove_item" + patch "/cart/:item_id", to: "cart#modify_quantity" + + resources :orders, only: %i[new create] + + namespace :merchant do + resources :orders, only: %i[show] + resources :items + get "/", to: "dashboard#show" + patch "/itemorders/:itemorder_id", to: "item_orders#update" + post "/items/deactivate", to: "items#deactivate" + post "/items/activate", to: "items#activate" + end + + namespace :admin do + resources :merchants, only: %i[index] + resources :users, only: %i[index show] + patch '/merchants/disable', to: "merchants#disable" + patch '/merchants/enable', to: "merchants#enable" + get '/', to: "dashboard#index" + get '/users/:id', to: "users#show" + patch '/:order_id', to: "profile_orders#update" + get '/merchants/:merchant_id', to: 'merchants#show' + end + + resources :users, only: %i[create] + + get "/register", to: "users#new" + + + get "/profile", to: "users_dashboard#show" + get "/profile/edit", to: "users_dashboard#edit" + patch "/profile", to: "users_dashboard#update" + + get '/profile/orders', to: "profile_orders#index" + get '/profile/orders/:id', to: "profile_orders#show" + patch "/profile/orders/:id", to: "profile_orders#cancel" + + + get "/password/edit", to: "passwords#edit" + patch "/password", to: "passwords#update" + + + + get "/login", to: "sessions#new" + post "/login", to: "sessions#create" + delete "/logout", to: 'sessions#destroy' - get "/orders/new", to: "orders#new" - post "/orders", to: "orders#create" - get "/orders/:id", to: "orders#show" end diff --git a/db/migrate/20190817190207_create_items.rb b/db/migrate/20190817190207_create_items.rb index a8c20fd14..32e7c75f4 100644 --- a/db/migrate/20190817190207_create_items.rb +++ b/db/migrate/20190817190207_create_items.rb @@ -4,7 +4,8 @@ def change t.string :name t.string :description t.integer :price - t.string :image + t.string :image, :default => "https://www.monsterchildren.com/wp-content/uploads/2020/02/nic-cage-monster-children-1068x571.jpg" + #Ex:- :default =>'' t.boolean :active?, default: true t.integer :inventory t.references :merchant, foreign_key: true diff --git a/db/migrate/20201026233249_create_users.rb b/db/migrate/20201026233249_create_users.rb new file mode 100644 index 000000000..08959ec7c --- /dev/null +++ b/db/migrate/20201026233249_create_users.rb @@ -0,0 +1,13 @@ +class CreateUsers < ActiveRecord::Migration[5.2] + def change + create_table :users do |t| + t.string :name + t.string :address + t.string :city + t.string :state + t.string :zip + t.string :email + t.string :password_digest + end + end +end diff --git a/db/migrate/20201027172822_add_roles_to_users.rb b/db/migrate/20201027172822_add_roles_to_users.rb new file mode 100644 index 000000000..4ea9c89e3 --- /dev/null +++ b/db/migrate/20201027172822_add_roles_to_users.rb @@ -0,0 +1,5 @@ +class AddRolesToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :role, :integer, default: 0 + end +end diff --git a/db/migrate/20201030015225_add_user_to_order.rb b/db/migrate/20201030015225_add_user_to_order.rb new file mode 100644 index 000000000..85c311cf2 --- /dev/null +++ b/db/migrate/20201030015225_add_user_to_order.rb @@ -0,0 +1,5 @@ +class AddUserToOrder < ActiveRecord::Migration[5.2] + def change + add_reference :orders, :user, foreign_key: true + end +end diff --git a/db/migrate/20201030031336_add_status_to_orders.rb b/db/migrate/20201030031336_add_status_to_orders.rb new file mode 100644 index 000000000..11043defb --- /dev/null +++ b/db/migrate/20201030031336_add_status_to_orders.rb @@ -0,0 +1,5 @@ +class AddStatusToOrders < ActiveRecord::Migration[5.2] + def change + add_column :orders, :status, :string + end +end diff --git a/db/migrate/20201031185017_add_status_to_item_orders.rb b/db/migrate/20201031185017_add_status_to_item_orders.rb new file mode 100644 index 000000000..106f15024 --- /dev/null +++ b/db/migrate/20201031185017_add_status_to_item_orders.rb @@ -0,0 +1,5 @@ +class AddStatusToItemOrders < ActiveRecord::Migration[5.2] + def change + add_column :item_orders, :status, :string + end +end diff --git a/db/migrate/20201101195025_add_merchant_to_user.rb b/db/migrate/20201101195025_add_merchant_to_user.rb new file mode 100644 index 000000000..26335b3ed --- /dev/null +++ b/db/migrate/20201101195025_add_merchant_to_user.rb @@ -0,0 +1,5 @@ +class AddMerchantToUser < ActiveRecord::Migration[5.2] + def change + add_reference :users, :merchant, foreign_key: true + end +end diff --git a/db/migrate/20201102233933_add_enabled_to_merchants.rb b/db/migrate/20201102233933_add_enabled_to_merchants.rb new file mode 100644 index 000000000..d644a3c59 --- /dev/null +++ b/db/migrate/20201102233933_add_enabled_to_merchants.rb @@ -0,0 +1,5 @@ +class AddEnabledToMerchants < ActiveRecord::Migration[5.2] + def change + add_column :merchants, :enabled?, :boolean, default: true + end +end diff --git a/db/migrate/20201103023204_add_merchant_to_item_orders.rb b/db/migrate/20201103023204_add_merchant_to_item_orders.rb new file mode 100644 index 000000000..24d9873a4 --- /dev/null +++ b/db/migrate/20201103023204_add_merchant_to_item_orders.rb @@ -0,0 +1,5 @@ +class AddMerchantToItemOrders < ActiveRecord::Migration[5.2] + def change + add_reference :item_orders, :merchant, foreign_key: true + end +end diff --git a/db/migrate/20201104234258_add_created_at_to_users.rb b/db/migrate/20201104234258_add_created_at_to_users.rb new file mode 100644 index 000000000..e89b58fb1 --- /dev/null +++ b/db/migrate/20201104234258_add_created_at_to_users.rb @@ -0,0 +1,5 @@ +class AddCreatedAtToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :created_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 70bd12b54..41cf78b10 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_24_200540) do +ActiveRecord::Schema.define(version: 2020_11_04_234258) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,7 +22,10 @@ t.integer "quantity" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "status" + t.bigint "merchant_id" t.index ["item_id"], name: "index_item_orders_on_item_id" + t.index ["merchant_id"], name: "index_item_orders_on_merchant_id" t.index ["order_id"], name: "index_item_orders_on_order_id" end @@ -30,7 +33,7 @@ t.string "name" t.string "description" t.integer "price" - t.string "image" + t.string "image", default: "https://www.monsterchildren.com/wp-content/uploads/2020/02/nic-cage-monster-children-1068x571.jpg" t.boolean "active?", default: true t.integer "inventory" t.bigint "merchant_id" @@ -47,6 +50,7 @@ t.integer "zip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "enabled?", default: true end create_table "orders", force: :cascade do |t| @@ -57,6 +61,9 @@ t.integer "zip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "user_id" + t.string "status" + t.index ["user_id"], name: "index_orders_on_user_id" end create_table "reviews", force: :cascade do |t| @@ -67,8 +74,25 @@ t.index ["item_id"], name: "index_reviews_on_item_id" end + create_table "users", force: :cascade do |t| + t.string "name" + t.string "address" + t.string "city" + t.string "state" + t.string "zip" + t.string "email" + t.string "password_digest" + t.integer "role", default: 0 + t.bigint "merchant_id" + t.datetime "created_at" + t.index ["merchant_id"], name: "index_users_on_merchant_id" + end + add_foreign_key "item_orders", "items" + add_foreign_key "item_orders", "merchants" add_foreign_key "item_orders", "orders" add_foreign_key "items", "merchants" + add_foreign_key "orders", "users" add_foreign_key "reviews", "items" + add_foreign_key "users", "merchants" end diff --git a/db/seeds.rb b/db/seeds.rb index 03246025c..5ba2d2d90 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,12 +10,201 @@ Item.destroy_all #merchants -bike_shop = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) +bike_shop = Merchant.create(name: "Someone's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) dog_shop = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) +meg_shop = Merchant.create!(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) +cat_shop = Merchant.create(name: "Brian's Cat Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) +mouse_shop = Merchant.create(name: "Brian's Mouse Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) +giraffe_shop = Merchant.create(name: "Brian's Giraffe Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) +hippo_shop = Merchant.create(name: "Brian's Hippo Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) +penguin_shop = Merchant.create(name: "Brian's Penguin Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) -#bike_shop items -tire = bike_shop.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) +#users +jake = User.create!(name: 'Jake', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'jake@admin.com', + password: 'admin', + password_confirmation: 'admin', + role: 2 +) +john = bike_shop.users.create!(name: 'john', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'bike@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +greg = dog_shop.users.create!(name: 'greg', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'dog@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +garrett = meg_shop.users.create!(name: 'garrett', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'meg@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +molly = cat_shop.users.create!(name: 'molly', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'cat@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +erica = mouse_shop.users.create!(name: 'erica', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'mouse@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +tom = giraffe_shop.users.create!(name: 'tom', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'giraffe@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +anne = hippo_shop.users.create!(name: 'anne', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'hippo@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +pete = penguin_shop.users.create!(name: 'pete', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'penguin@merchant.com', + password: 'merchant', + password_confirmation: 'merchant', + role: 1 +) +steve = User.create!(name: 'steve', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'steve@user.com', + password: 'user', + password_confirmation: 'user', + role: 0 +) +claire = User.create!(name: 'claire', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'claire@user.com', + password: 'user', + password_confirmation: 'user', + role: 0 +) +raul = User.create!(name: 'raul', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'raul@user.com', + password: 'user', + password_confirmation: 'user', + role: 0 +) +mike = User.create!(name: 'mike', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'mike@user.com', + password: 'user', + password_confirmation: 'user', + role: 0 +) +ranjit = User.create!(name: 'ranjit', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'ranjit@user.com', + password: 'user', + password_confirmation: 'user', + role: 0 +) -#dog_shop items -pull_toy = dog_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) -dog_bone = dog_shop.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) +#items +tire1 = bike_shop.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 912) +tire2 = meg_shop.items.create!(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 912) +pen = meg_shop.items.create!(name: "Ball", description: "They'll write!", price: 10, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 93) +ball = meg_shop.items.create!(name: "Round", description: "They'll never pop!", price: 9, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 95) +straw = penguin_shop.items.create!(name: "Long", description: "They'll suck pop!", price: 8, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 92) +eraser = penguin_shop.items.create!(name: "Rubber", description: "They'll never write!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 97) +stick = penguin_shop.items.create!(name: "wood", description: "They'll never grow!", price: 4, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 93) +toothpick = meg_shop.items.create!(name: "plastic", description: "They'll never pop!", price: 4, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 94) +trash = cat_shop.items.create!(name: "banana", description: "They'll never pop!", price: 5, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 955) +wrapper = giraffe_shop.items.create!(name: "gum", description: "They'll never pop!", price: 6, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 96) +film1 = mouse_shop.items.create!(name: "junk", description: "They'll never pop!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 97) +film2 = mouse_shop.items.create!(name: "junk", description: "They'll never pop!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 97) +film3 = hippo_shop.items.create!(name: "junk", description: "They'll never pop!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 97) +film4 = hippo_shop.items.create!(name: "junk", description: "They'll never pop!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 97) +pull_toy = dog_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 932) +dog_bone = dog_shop.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 921) + +#orders +order1 = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: steve.id, status: "Pending") +order2 = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: claire.id, status: "Packaged") +order3 = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: raul.id, status: "Packaged") +order4 = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: mike.id, status: "Shipped") +order5 = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: ranjit.id, status: "Cancelled") + +#item_orders +order1.item_orders.create!(item_id: tire1.id, price: tire1.price, quantity: 10, status: "Pending", merchant_id: bike_shop.id) +order1.item_orders.create!(item_id: pen.id, price: pen.price, quantity: 9, status: "Fulfilled", merchant_id: meg_shop.id) +order1.item_orders.create!(item_id: ball.id, price: ball.price, quantity: 8, status: "Fulfilled", merchant_id: meg_shop.id) +order1.item_orders.create!(item_id: straw.id, price: straw.price, quantity: 7, status: "Pending", merchant_id: penguin_shop.id) + +order2.item_orders.create!(item_id: eraser.id, price: eraser.price, quantity: 6, status: "Fulfilled", merchant_id: penguin_shop.id) +order2.item_orders.create!(item_id: stick.id, price: stick.price, quantity: 5, status: "Fulfilled", merchant_id: penguin_shop.id) +order2.item_orders.create!(item_id: toothpick.id, price: toothpick.price, quantity: 4, status: "Fulfilled", merchant_id: meg_shop.id) + +order3.item_orders.create!(item_id: trash.id, price: trash.price, quantity: 3, status: "Fulfilled", merchant_id: cat_shop.id) +order3.item_orders.create!(item_id: wrapper.id, price: wrapper.price, quantity: 2, status: "Fulfilled", merchant_id: giraffe_shop.id) +order3.item_orders.create!(item_id: film2.id, price: film2.price, quantity: 1, status: "Fulfilled", merchant_id: mouse_shop.id) +order3.item_orders.create!(item_id: film3.id, price: film3.price, quantity: 1, status: "Fulfilled", merchant_id: hippo_shop.id) + +order4.item_orders.create!(item_id: tire2.id, price: tire2.price, quantity: 10, status: "Fulfilled", merchant_id: meg_shop.id) +order4.item_orders.create!(item_id: pen.id, price: pen.price, quantity: 9, status: "Fulfilled", merchant_id: meg_shop.id) + +order5.item_orders.create!(item_id: ball.id, price: ball.price, quantity: 8, status: "Unfulfilled", merchant_id: meg_shop.id) +order5.item_orders.create!(item_id: straw.id, price: straw.price, quantity: 7, status: "Unfulfilled", merchant_id: penguin_shop.id) +order5.item_orders.create!(item_id: film4.id, price: film4.price, quantity: 7, status: "Unfulfilled", merchant_id: hippo_shop.id) +order5.item_orders.create!(item_id: dog_bone.id, price: dog_bone.price, quantity: 7, status: "Unfulfilled", merchant_id: dog_shop.id) diff --git a/spec/features/admin/index_spec.rb b/spec/features/admin/index_spec.rb new file mode 100644 index 000000000..67b2cb6e9 --- /dev/null +++ b/spec/features/admin/index_spec.rb @@ -0,0 +1,146 @@ +require 'rails_helper' + +describe "As an admin" do + describe "When I visit my admin dashboard" do + before(:each) do + @user = User.create!( name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.comm', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + @bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + @chain = @bike_shop.items.create(name: "Chain", description: "It'll never break!", price: 50, image: "https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588", inventory: 5) + @tire = @bike_shop.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) + @rack = @bike_shop.items.create(name: "Bike Rack", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) + @order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80218, user_id: @user.id, status: "Pending") + @order_item_1 = @order_1.item_orders.create!(item: @chain, price: @chain.price, quantity: 2, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_item_2 = @order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_item_3 = @order_1.item_orders.create!(item: @rack, price: @rack.price, quantity: 2, status: "Pending", merchant_id: @bike_shop.id) + @user_2 = User.create!( name: 'Sarah', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + @order_2 = Order.create!(name: 'Sarah', address: '123 Stang St', city: 'whatwewant', state: 'PA', zip: 80218, user_id: @user_2.id, status: "Packaged") + @order_item_4 = @order_2.item_orders.create!(item: @chain, price: @chain.price, quantity: 1, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_item_5 = @order_2.item_orders.create!(item: @tire, price: @tire.price, quantity: 1, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_3 = Order.create!(name: 'Sarah', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80218, user_id: @user_2.id, status: "Shipped") + @order_item_6 = @order_3.item_orders.create!(item: @rack, price: @rack.price, quantity: 1, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_4 = Order.create!(name: 'Sarah', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80218, user_id: @user_2.id, status: "Packaged") + @order_item_7 = @order_4.item_orders.create!(item: @chain, price: @chain.price, quantity: 1, status: "Fulfilled", merchant_id: @bike_shop.id) + @order_5 = Order.create!(name: 'Sarah', address: '123 Stang St', city: 'test', state: 'PA', zip: 80218, user_id: @user_2.id, status: "Cancelled") + @order_item_9 = @order_5.item_orders.create!(item: @tire, price: @tire.price, quantity: 1, status: "Fulfilled", merchant_id: @bike_shop.id) + @user_3 = User.create!( name: 'Sarah', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.commmm', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2 + ) + visit '/login' + fill_in :email, with: "JBob1234@hotmail.commmm" + fill_in :password, with: "heftybags" + click_button "Login" + visit '/admin' + end + it "I see all orders in the system" do + expect(page).to have_content(@order_1.id) + expect(page).to have_content(@order_2.id) + expect(page).to have_content(@order_3.id) + end + + it "I see detailed information for all orders in the system and they are + sorted by status" do + visit '/admin' + expect(page.all('h3')[0]).to have_content("Packaged") + within "#packaged" do + within "#order-#{@order_2.id}" do + expect(page).to have_link(@user_2.name) + expect(page).to have_content(@order_2.id) + expect(page).to have_content(@order_2.created_at) + end + within "#order-#{@order_4.id}" do + expect(page).to have_link(@user_2.name) + expect(page).to have_content(@order_4.id) + expect(page).to have_content(@order_4.created_at) + end + end + expect(page.all('h3')[1]).to have_content("Pending") + within "#pending" do + within "#order-#{@order_1.id}" do + expect(page).to have_link(@user.name) + expect(page).to have_content(@order_1.id) + expect(page).to have_content(@order_1.created_at) + end + end + expect(page.all('h3')[2]).to have_content("Shipped") + within "#shipped" do + within "#order-#{@order_3.id}" do + expect(page).to have_link(@user_2.name) + expect(page).to have_content(@order_3.id) + expect(page).to have_content(@order_3.created_at) + end + end + expect(page.all('h3')[3]).to have_content("Cancelled") + within "#cancelled" do + within "#order-#{@order_5.id}" do + expect(page).to have_link(@user_2.name) + expect(page).to have_content(@order_5.id) + expect(page).to have_content(@order_5.created_at) + end + end + end + it "when I click the user name link it goes to the admin view of user profile" do + click_on "#{@user.name}" + expect(current_path).to eq("/admin/users/#{@user.id}") + end + + it "When I click the button to ship, located next to eah packaged order, + the status is changed to 'shipped' and it can no longer be cancelled" do + order = Order.find(@order_2.id) + within "#packaged" do + + within "#order-#{order.id}" do + expect(page).to have_button("Ship Order") + click_button("Ship Order") + end + within "#order-#{@order_4.id}" do + expect(page).to have_button("Ship Order") + click_button("Ship Order") + end + expect(current_path).to eq("/admin") + expect(page).to_not have_content(order.id) + expect(page).to_not have_content(@order_4.id) + end + within "#shipped" do + within "#order-#{order.id}" do + expect(page).to have_content(order.id) + expect(Order.find(order.id).status).to eq("Shipped") + end + within "#order-#{@order_4.id}" do + expect(page).to have_content(@order_4.id) + end + end + click_link("Log Out") + click_link("Log In") + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/profile/orders/#{order.id}" + expect(page).to_not have_button("Cancel Order") + end + end +end diff --git a/spec/features/admin/merchants/index_spec.rb b/spec/features/admin/merchants/index_spec.rb new file mode 100644 index 000000000..456f298c0 --- /dev/null +++ b/spec/features/admin/merchants/index_spec.rb @@ -0,0 +1,192 @@ +require 'rails_helper' + +describe "As an admin, when I visit the merchant index page" do + it "I click on a merchant's name, I'm directed to the admin merchant show page" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2 + ) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + item = bike_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 932) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchants" + + click_on "#{bike_shop.name}" + expect(current_path).to eq("/admin/merchants/#{bike_shop.id}") + expect(page) + expect(page).to have_link(bike_shop.name) + expect(page).to have_link("All #{bike_shop.name} Items") + expect(page).to have_link("Update Merchant") + expect(page).to have_content("Number of Items: #{bike_shop.item_count}") + expect(page).to have_content("Average Price of Items: $#{bike_shop.average_item_price}") + expect(page).to have_content("Cities that order these items:") + end +end + +describe "As an admin when I visit the admin's merchant index page" do + describe "I see a 'disable' button next to any merchants who are not yet disabled" do + it "When I click on the 'disable button, I am returned to the admin's merchant + index page where I see the merchant account is disabled and flash message" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + @mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/admin/merchants" + expect(page).to have_button("Disable") + + click_button "Disable" + expect(page).to have_content("Merchant Account Is Now Disabled") + end + end +end + +describe "As an admin, when I visit the merchant index page" do + describe "And I click on the 'disable' button for an enabled merchant" do + it "Then all of that merchant's items should be deactivated" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + pull_toy = mike.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + dog_bone = mike.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/admin/merchants" + click_button "Disable" + + expected = mike.items.all? {|item| item.active? == false } + expect(expected).to eq(true) + end + end +end + +describe "As an admin when I visit the merchant index page" do + describe "I see an 'enable' button next to any merchants whose accounts are disabled" do + it "I am returned to the index page and merchant is enabled and I see a flash message + and all merchant's items are now active" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + pull_toy = mike.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + dog_bone = mike.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/admin/merchants" + click_button "Disable" + click_button "Enable" + + expected = mike.items.all? {|item| item.active? == true } + expect(expected).to eq(true) + expect(page).to have_content("Merchant Account Is Now Enabled") + end + end + + + describe "As an admin user, when I visit the merchant's index page at '/admin/merchants'" do + describe "I see all merchants and next to each merchant's name I see their city and state" do + it "Merchant's name is a link to their dashboard" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + steve = Merchant.create!(name: "Steve's Print Shop", + address: '123 Steve Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + pull_toy = mike.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + dog_bone = steve.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/admin/merchants" + + within("#merchant-#{mike.id}") do + expect(page).to have_content(mike.city) + expect(page).to have_content(mike.state) + + expect(page).to have_link(mike.name) + + expect(page).not_to have_button("Enable") + expect(page).to have_button("Disable") + end + + click_on "#{mike.name}" + expect(current_path).to eq("/admin/merchants") + end + end + end +end diff --git a/spec/features/admin/users/index_spec.rb b/spec/features/admin/users/index_spec.rb new file mode 100644 index 000000000..ada946f3c --- /dev/null +++ b/spec/features/admin/users/index_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +describe "As an admin user when I click the 'Users' link in the nav" do + it "I see all users in the system and their type/register date" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + gare = User.create!(name: 'GarBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'GBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + greg = User.create!(name: 'GregBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'GrBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/admin/users" + + within("#user-#{gare.id}") do + expect(page).to have_link(gare.name) + expect(page).to have_content(gare.created_at) + expect(page).to have_content(gare.role) + + click_on(gare.name) + expect(current_path).to eq("/admin/users/#{gare.id}") + end + end +end diff --git a/spec/features/admin/users/show_spec.rb b/spec/features/admin/users/show_spec.rb new file mode 100644 index 000000000..cc1048b54 --- /dev/null +++ b/spec/features/admin/users/show_spec.rb @@ -0,0 +1,44 @@ +require 'rails_helper' + +describe "As an admin user I visit user's profile page" do + it "I see same info user would see and no link to edit" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + gare = User.create!(name: 'GarBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'GBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + greg = User.create!(name: 'GregBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'GrBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/admin/users/#{greg.id}" + + expect(page).to have_content("Name: #{greg.name}") + expect(page).to have_content("Address: #{greg.full_address}") + expect(page).to have_content("Email: #{greg.email}") + end +end diff --git a/spec/features/cart/checkout_spec.rb b/spec/features/cart/checkout_spec.rb index 7b1f60aa8..aaaf21b9e 100644 --- a/spec/features/cart/checkout_spec.rb +++ b/spec/features/cart/checkout_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' RSpec.describe 'Cart show' do - describe 'When I have added items to my cart' do + describe 'When I have added items to my cart as a user' do before(:each) do @mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) @@ -19,6 +19,19 @@ end it 'Theres a link to checkout' do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) + visit "/cart" expect(page).to have_link("Checkout") @@ -37,3 +50,123 @@ end end end + +describe "As a visitor" do + describe "When I visit my cart with items in it" do + it "It tells me I must register or log in to checkout with links for each" do + dog_shop = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + item = dog_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + + visit "/items/#{item.id}" + + click_on "Add To Cart" + expect(page).to have_content("Cart: 1") + + click_on "Cart" + + expect(page).to_not have_link("Checkout") + expect(page).to have_content("You must register or log in to checkout") + expect(page).to have_link("register") + expect(page).to have_link("log in") + end + end +end + +describe "As a user" do + describe "When I visit my cart with items in it" do + it "I click checkout and am redirected to a form to fill out order info" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + dog_shop = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + item = dog_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) + # allow_any_instance_of(ApplicationController).to receive(:cart).and_return({item.id => 1}) + visit "/items/#{item.id}" + + click_on "Add To Cart" + + visit cart_path + + click_on "Checkout" + + expect(current_path).to eq("/orders/new") + end + describe "I fill out the order info and click create order a pending order is created" do + it "The order is associated with my user, I'm redirected to my orders, I see order creation flash, my order is listed and cart is empty" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + dog_shop = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + item = dog_shop.items.create!(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + # allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) + # allow_any_instance_of(ApplicationController).to receive(:cart).and_return({item.id => 1}) + visit "/items/#{item.id}" + + click_on "Add To Cart" + + visit '/orders/new' + + fill_in :name, with: user.name + fill_in :address, with: user.address + fill_in :city, with: user.city + fill_in :state, with: user.state + fill_in :zip, with: user.zip + + click_on 'Create Order' + + new_order = Order.last + item_order = ItemOrder.last + + expect(current_path).to eq("/profile/orders") + expect(new_order.status).to eq("Pending") + expect(new_order.user).to eq(user) + expect(page).to have_content("Your order has been created") + + # within "#item-#{item.id}" do + # expect(page).to have_link(item.name) + # expect(page).to have_link("#{item.merchant.name}") + # expect(page).to have_content("$#{item.price}") + # expect(page).to have_content("1") + # expect(page).to have_content("$10") + # end + # within "#grandtotal" do + # expect(page).to have_content("Total: $10") + # end + + # within "#datecreated" do + # expect(page).to have_content(new_order.created_at) + # end + # within "#item-#{new_order.id}" do + # expect(page).to have_content(item.name) + # expect(page).to have_content(item.price) + # expect(page).to have_content(new_order.grandtotal) + # end + expect(page).to have_content("Cart: 0") + end + end + end +end diff --git a/spec/features/cart/show_spec.rb b/spec/features/cart/show_spec.rb index 7801aa161..8dcadcfd1 100644 --- a/spec/features/cart/show_spec.rb +++ b/spec/features/cart/show_spec.rb @@ -1,31 +1,33 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe 'Cart show' do describe 'When I have added items to my cart' do describe 'and visit my cart path' do before(:each) do - @mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) - @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + @mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) - @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) - @paper = @mike.items.create(name: "Lined Paper", description: "Great for writing on!", price: 20, image: "https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png", inventory: 25) - @pencil = @mike.items.create(name: "Yellow Pencil", description: "You can write on paper with it!", price: 2, image: "https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg", inventory: 100) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @paper = @mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 25) + @pencil = @mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) visit "/items/#{@paper.id}" - click_on "Add To Cart" + click_on 'Add To Cart' visit "/items/#{@tire.id}" - click_on "Add To Cart" + click_on 'Add To Cart' visit "/items/#{@pencil.id}" - click_on "Add To Cart" - @items_in_cart = [@paper,@tire,@pencil] + click_on 'Add To Cart' + @items_in_cart = [@paper, @tire, @pencil] end it 'I can empty my cart by clicking a link' do visit '/cart' - expect(page).to have_link("Empty Cart") - click_on "Empty Cart" - expect(current_path).to eq("/cart") - expect(page).to_not have_css(".cart-items") - expect(page).to have_content("Cart is currently empty") + expect(page).to have_link('Empty Cart') + click_on 'Empty Cart' + expect(current_path).to eq('/cart') + expect(page).to_not have_css('.cart-items') + expect(page).to have_content('Cart is currently empty') end it 'I see all items Ive added to my cart' do @@ -35,41 +37,98 @@ within "#cart-item-#{item.id}" do expect(page).to have_link(item.name) expect(page).to have_css("img[src*='#{item.image}']") - expect(page).to have_link("#{item.merchant.name}") + expect(page).to have_link(item.merchant.name.to_s) expect(page).to have_content("$#{item.price}") - expect(page).to have_content("1") + expect(page).to have_content('1') expect(page).to have_content("$#{item.price}") end end - expect(page).to have_content("Total: $122") + expect(page).to have_content('Total: $122') visit "/items/#{@pencil.id}" - click_on "Add To Cart" + click_on 'Add To Cart' visit '/cart' within "#cart-item-#{@pencil.id}" do - expect(page).to have_content("2") - expect(page).to have_content("$4") + expect(page).to have_content('2') + expect(page).to have_content('$4') end - expect(page).to have_content("Total: $124") + expect(page).to have_content('Total: $124') end end end describe "When I haven't added anything to my cart" do - describe "and visit my cart show page" do - it "I see a message saying my cart is empty" do + describe 'and visit my cart show page' do + it 'I see a message saying my cart is empty' do + visit '/cart' + expect(page).to_not have_css('.cart-items') + expect(page).to have_content('Cart is currently empty') + end + + it 'I do NOT see the link to empty my cart' do visit '/cart' - expect(page).to_not have_css(".cart-items") - expect(page).to have_content("Cart is currently empty") + expect(page).to_not have_link('Empty Cart') end + end + describe 'I see a button or link to increment the count of items I want to purchase' do + it "I cannot increment the count beyond the item's inventory size" do + mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + paper = mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 25) + pencil = mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 2) + visit "/items/#{paper.id}" + click_on 'Add To Cart' + visit "/items/#{tire.id}" + click_on 'Add To Cart' + visit "/items/#{pencil.id}" + click_on 'Add To Cart' + items_in_cart = [paper, tire, pencil] - it "I do NOT see the link to empty my cart" do visit '/cart' - expect(page).to_not have_link("Empty Cart") + + within "#cart-item-#{pencil.id}" do + expect(page).to have_button('+') + click_on '+' + expect(page).to have_content(2) + click_on '+' + end + expect(page).to have_content("Not Enough Inventory for #{pencil.name}") end + end + describe 'I see a button or link to decrement the count of items I want to purchase' do + it 'If I decrement the count to 0 the item is immediately removed from my cart' do + mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + paper = mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 25) + pencil = mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) + visit "/items/#{paper.id}" + click_on 'Add To Cart' + visit "/items/#{tire.id}" + click_on 'Add To Cart' + visit "/items/#{pencil.id}" + click_on 'Add To Cart' + items_in_cart = [paper, tire, pencil] + + visit '/cart' + within "#cart-item-#{pencil.id}" do + expect(page).to have_button('+') + click_on '+' + click_on '-' + expect(page).to have_content(1) + + click_on "-" + end + within ".cart-items" do + expect(page).not_to have_content(pencil.name) + end + end end end end diff --git a/spec/features/items/destroy_spec.rb b/spec/features/items/destroy_spec.rb index 521a28108..15b0d2910 100644 --- a/spec/features/items/destroy_spec.rb +++ b/spec/features/items/destroy_spec.rb @@ -27,12 +27,23 @@ expect(Review.where(id:review_1.id)).to be_empty end - it 'I can not delete items with orders' do + it 'I can not delete items with orders' do bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) chain = bike_shop.items.create(name: "Chain", description: "It'll never break!", price: 50, image: "https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588", inventory: 5) review_1 = chain.reviews.create(title: "Great place!", content: "They have great bike stuff and I'd recommend them to anyone.", rating: 5) - order_1 = Order.create!(name: 'Meg', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80218) - order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2) + order_1 = Order.create!(name: 'Meg', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80218, user_id: user.id) + order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, merchant_id: bike_shop.id) visit "/items/#{chain.id}" diff --git a/spec/features/items/edit_spec.rb b/spec/features/items/edit_spec.rb index 2646f88d6..49c99e809 100644 --- a/spec/features/items/edit_spec.rb +++ b/spec/features/items/edit_spec.rb @@ -65,7 +65,7 @@ click_button "Update Item" - expect(page).to have_content("Name can't be blank and Image can't be blank") + expect(page).to have_content("Name can't be blank") expect(page).to have_button("Update Item") end end diff --git a/spec/features/items/index_spec.rb b/spec/features/items/index_spec.rb index 995ad14b7..d7bf90942 100644 --- a/spec/features/items/index_spec.rb +++ b/spec/features/items/index_spec.rb @@ -3,13 +3,22 @@ RSpec.describe "Items Index Page" do describe "When I visit the items index page" do before(:each) do - @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) - @brian = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + @meg = Merchant.create!(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + @brian = Merchant.create!(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + @jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + @tire = @meg.items.create!(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) - @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) - - @pull_toy = @brian.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) - @dog_bone = @brian.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + @pull_toy = @brian.items.create!(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + @dog_bone = @brian.items.create!(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) end it "all items or merchant names are links" do @@ -19,8 +28,6 @@ expect(page).to have_link(@tire.merchant.name) expect(page).to have_link(@pull_toy.name) expect(page).to have_link(@pull_toy.merchant.name) - expect(page).to have_link(@dog_bone.name) - expect(page).to have_link(@dog_bone.merchant.name) end it "I can see a list of all of the items "do @@ -46,15 +53,77 @@ expect(page).to have_link(@brian.name) expect(page).to have_css("img[src*='#{@pull_toy.image}']") end + end - within "#item-#{@dog_bone.id}" do - expect(page).to have_link(@dog_bone.name) - expect(page).to have_content(@dog_bone.description) - expect(page).to have_content("Price: $#{@dog_bone.price}") - expect(page).to have_content("Inactive") - expect(page).to have_content("Inventory: #{@dog_bone.inventory}") - expect(page).to have_link(@brian.name) - expect(page).to have_css("img[src*='#{@dog_bone.image}']") + describe "As any kind of user on the system" do + describe "I can visit the items catalog ('/items')" do + it "I see all items in the system except disables items, item image is a link" do + visit '/items' + + expect(page).to have_link(@tire.name) + expect(page).to have_link(@pull_toy.name) + expect(page).not_to have_link(@dog_bone.name) + end + + describe "When I visit '/items'" do + describe "I see an area with statistics" do + it "I see top 5 most/least popular items and quanitity purchased" do + tire = @meg.items.create!(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) + pen = @meg.items.create!(name: "Ball", description: "They'll write!", price: 10, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 3) + ball = @meg.items.create!(name: "Round", description: "They'll never pop!", price: 9, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 5) + straw = @meg.items.create!(name: "Long", description: "They'll suck pop!", price: 8, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 2) + eraser = @meg.items.create!(name: "Rubber", description: "They'll never write!", price: 7, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 7) + + stick = @meg.items.create!(name: "wood", description: "They'll never grow!", price: 6, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 3) + toothpick = @meg.items.create!(name: "plastic", description: "They'll never pop!", price: 5, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 4) + trash = @meg.items.create!(name: "banana", description: "They'll never pop!", price: 4, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 55) + wrapper = @meg.items.create!(name: "gum", description: "They'll never pop!", price: 3, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 6) + film = @meg.items.create!(name: "junk", description: "They'll never pop!", price: 2, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 7) + + order = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver',state: 'Colorado', zip: '80202', user_id: @jake.id) + order.item_orders.create!(item_id: tire.id, price: tire.price, quantity: 10, merchant_id: @meg.id) + order.item_orders.create!(item_id: pen.id, price: pen.price, quantity: 9, merchant_id: @meg.id) + order.item_orders.create!(item_id: ball.id, price: ball.price, quantity: 8, merchant_id: @meg.id) + order.item_orders.create!(item_id: straw.id, price: straw.price, quantity: 7, merchant_id: @meg.id) + order.item_orders.create!(item_id: eraser.id, price: eraser.price, quantity: 6, merchant_id: @meg.id) + order.item_orders.create!(item_id: stick.id, price: stick.price, quantity: 5, merchant_id: @meg.id) + order.item_orders.create!(item_id: toothpick.id, price: pen.price, quantity: 4, merchant_id: @meg.id) + order.item_orders.create!(item_id: trash.id, price: ball.price, quantity: 3, merchant_id: @meg.id) + order.item_orders.create!(item_id: wrapper.id, price: straw.price, quantity: 2, merchant_id: @meg.id) + order.item_orders.create!(item_id: film.id, price: eraser.price, quantity: 1, merchant_id: @meg.id) + + visit '/items' + + within("#popularity_table") do + expect(page).to have_content("Most Popular Items:") + + expect(page).to have_content(tire.name) + expect(page).to have_content(order.item_orders[0].quantity) + expect(page).to have_content(pen.name) + expect(page).to have_content(order.item_orders[1].quantity) + expect(page).to have_content(ball.name) + expect(page).to have_content(order.item_orders[2].quantity) + expect(page).to have_content(straw.name) + expect(page).to have_content(order.item_orders[3].quantity) + expect(page).to have_content(eraser.name) + expect(page).to have_content(order.item_orders[4].quantity) + + expect(page).to have_content("Least Popular Items:") + + expect(page).to have_content(stick.name) + expect(page).to have_content(order.item_orders[5].quantity) + expect(page).to have_content(toothpick.name) + expect(page).to have_content(order.item_orders[6].quantity) + expect(page).to have_content(trash.name) + expect(page).to have_content(order.item_orders[7].quantity) + expect(page).to have_content(wrapper.name) + expect(page).to have_content(order.item_orders[8].quantity) + expect(page).to have_content(film.name) + expect(page).to have_content(order.item_orders[9].quantity) + end + end + end + end end end end diff --git a/spec/features/items/new_spec.rb b/spec/features/items/new_spec.rb index df1467552..69d82b5d6 100644 --- a/spec/features/items/new_spec.rb +++ b/spec/features/items/new_spec.rb @@ -70,7 +70,7 @@ click_button "Create Item" - expect(page).to have_content("Name can't be blank and Inventory can't be blank") + expect(page).to have_content("Name can't be blank, Inventory can't be blank, and Inventory is not a number") expect(page).to have_button("Create Item") end end diff --git a/spec/features/merchant_user/.keep b/spec/features/merchant_user/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/spec/features/merchant_user/items/delete_spec.rb b/spec/features/merchant_user/items/delete_spec.rb new file mode 100644 index 000000000..12c8c98d7 --- /dev/null +++ b/spec/features/merchant_user/items/delete_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +RSpec.describe 'Merchant Items Index Page' do + describe 'When I visit the merchant items page' do + before(:each) do + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + @shifter = @meg.items.create(name: 'Shimano Shifters', description: "It'll always shift!", active?: false, price: 180, image: 'https://images-na.ssl-images-amazon.com/images/I/4142WWbN64L._SX466_.jpg', inventory: 2) + end + + it 'I see a link or button to deactivate the item next to each item that is active' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + order = Order.create!(name: 'JakeBob', address: '124 Main St', city: 'Denver', state: 'Colorado', zip: '80202', user_id: user.id) + order.item_orders.create!(item_id: @tire.id, price: @tire.price, quantity: 10, merchant_id: @meg.id) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + within "#item-#{@tire.id}" do + expect(page).not_to have_button('Delete Item') + end + + within "#item-#{@chain.id}" do + expect(page).to have_button('Delete Item') + click_on 'Delete Item' + end + + expect(page).to have_content("This item has been deleted") + expect(page).not_to have_css("#item-#{@chain.id}") + end + end +end diff --git a/spec/features/merchant_user/items/edit_spec.rb b/spec/features/merchant_user/items/edit_spec.rb new file mode 100644 index 000000000..ee423275e --- /dev/null +++ b/spec/features/merchant_user/items/edit_spec.rb @@ -0,0 +1,87 @@ +# frozen_string_literal: true + +RSpec.describe 'Merchant edits an item' do + describe 'And I click the edit button or link next to any item Then I am taken to a form similar to the new item form' do + before(:each) do + @meg = Merchant.create!(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + @shifter = @meg.items.create(name: 'Shimano Shifters', description: "It'll always shift!", active?: false, price: 180, image: 'https://images-na.ssl-images-amazon.com/images/I/4142WWbN64L._SX466_.jpg', inventory: 2) + end + + it 'The form is pre-populated with all of this items information and I can edit it' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + within "#item-#{@tire.id}" do + click_on 'Edit Item' + end + expect(current_path).to eq("/merchant/items/#{@tire.id}/edit") + + expect(find_field(:name).value).to eq(@tire.name) + expect(find_field(:description).value).to eq(@tire.description) + expect(find_field(:image).value).to eq(@tire.image) + expect(find_field(:price).value).to eq("#{@tire.price}") + expect(find_field(:inventory).value).to eq(@tire.inventory.to_s) + + fill_in :name, with: "Chipotle" + + click_on "Update Item" + + @tire.reload + within "#item-#{@tire.id}" do + expect(page).to have_content(@tire.name) + end + end + + it 'Sad path testing' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + within "#item-#{@tire.id}" do + click_on 'Edit Item' + end + expect(current_path).to eq("/merchant/items/#{@tire.id}/edit") + + expect(find_field(:name).value).to eq(@tire.name) + expect(find_field(:description).value).to eq(@tire.description) + expect(find_field(:image).value).to eq(@tire.image) + expect(find_field(:price).value).to eq("#{@tire.price}") + expect(find_field(:inventory).value).to eq(@tire.inventory.to_s) + + fill_in :name, with: "" + + click_on "Update Item" + + expect(page).to have_content("Name can't be blank") + end + end +end diff --git a/spec/features/merchant_user/items/index_spec.rb b/spec/features/merchant_user/items/index_spec.rb new file mode 100644 index 000000000..734d5272b --- /dev/null +++ b/spec/features/merchant_user/items/index_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +RSpec.describe 'Merchant Items Index Page' do + describe 'When I visit the merchant items page' do + before(:each) do + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + @shifter = @meg.items.create(name: 'Shimano Shifters', description: "It'll always shift!", active?: false, price: 180, image: 'https://images-na.ssl-images-amazon.com/images/I/4142WWbN64L._SX466_.jpg', inventory: 2) + end + + it 'I see a link or button to deactivate the item next to each item that is active' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + within "#item-#{@tire.id}" do + expect(page).to have_content(@tire.name) + expect(page).to have_content("Price: $#{@tire.price}") + expect(page).to have_css("img[src*='#{@tire.image}']") + expect(page).to have_content('Active') + expect(page).to_not have_content(@tire.description) + expect(page).to have_content("Inventory: #{@tire.inventory}") + expect(page).to have_button('Deactivate') + click_on 'Deactivate' + end + + within "#item-#{@tire.id}" do + expect(page).to have_content('Inactive') + end + + expect(page).to have_content('This item is no longer for sale') + end + + it 'merchant can activate a deactivated item' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + within "#item-#{@shifter.id}" do + expect(page).to have_content(@shifter.name) + expect(page).to have_content("Price: $#{@shifter.price}") + expect(page).to have_css("img[src*='#{@shifter.image}']") + expect(page).to have_content('Inactive') + expect(page).to_not have_content(@shifter.description) + expect(page).to have_content("Inventory: #{@shifter.inventory}") + expect(page).to have_button('Activate') + expect(page).not_to have_button('Deactivate') + click_on 'Activate' + end + within "#item-#{@shifter.id}" do + expect(page).to have_content('Active') + end + + expect(page).to have_content('This item is now available for sale') + end + end +end diff --git a/spec/features/merchant_user/items/new_spec.rb b/spec/features/merchant_user/items/new_spec.rb new file mode 100644 index 000000000..1db542a29 --- /dev/null +++ b/spec/features/merchant_user/items/new_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +RSpec.describe 'Merchant Items Index Page' do + describe 'When I visit the merchant items page' do + before(:each) do + @meg = Merchant.create!(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + @shifter = @meg.items.create(name: 'Shimano Shifters', description: "It'll always shift!", active?: false, price: 180, image: 'https://images-na.ssl-images-amazon.com/images/I/4142WWbN64L._SX466_.jpg', inventory: 2) + end + + it 'When I click on the link to add a new item' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + click_on 'Add New Item' + + expect(page).to have_content('Name') + !find_field(:item_name) + expect(page).to have_content('Description') + !find_field(:item_description) + expect(page).to have_content('Image') + !find_field(:item_image) + expect(page).to have_content('Price') + !find_field(:item_price) + expect(page).to have_content('Inventory') + !find_field(:item_inventory) + expect(page).to have_button('Create Item') + end + + it 'creating a new item' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + name = "Chamois Buttr" + price = 18 + description = "No more chaffin'!" + image_url = "https://images-na.ssl-images-amazon.com/images/I/51HMpDXItgL._SX569_.jpg" + inventory = 25 + + click_on "Add New Item" + + fill_in :item_name, with: name + fill_in :item_price, with: price + fill_in :item_description, with: description + fill_in :item_image, with: image_url + fill_in :item_inventory, with: inventory + + click_button "Create Item" + + expect(page).to have_content("New item has been created") + expect(page).to have_css("#item-#{Item.last.id}") + end + it 'creating a new item(sad path)' do + user = @meg.users.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/merchant/items' + + name = "Chamois Buttr" + price = 18 + description = "No more chaffin'!" + image_url = "" + inventory = "" + + click_on "Add New Item" + + fill_in :item_name, with: name + fill_in :item_price, with: price + fill_in :item_description, with: description + fill_in :item_image, with: image_url + fill_in :item_inventory, with: inventory + + click_button "Create Item" + + expect(find_field(:item_name).value).to eq(name) + expect(find_field(:item_description).value).to eq(description) + expect(find_field(:item_image).value).to eq(image_url) + expect(find_field(:item_price).value).to eq(price.to_s) + expect(find_field(:item_inventory).value).to eq(inventory) + + expect(page).to have_content("Inventory can't be blank and Inventory is not a number") + end + end +end diff --git a/spec/features/merchant_user/orders/show_spec.rb b/spec/features/merchant_user/orders/show_spec.rb new file mode 100644 index 000000000..5cc21d023 --- /dev/null +++ b/spec/features/merchant_user/orders/show_spec.rb @@ -0,0 +1,288 @@ +require 'rails_helper' + +describe "As a merchant employee" do + describe "When I visit my merchant dashboard" do + it "I see the name and full address of the merchant I work for" do + merchant = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + merchant.users.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/merchant" + + expect(page).to have_content(merchant.name) + expect(page).to have_content(merchant.full_address) + end + + it "I see any pending orders from my merchant, each has order info with quantity and price of my items" do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_4 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Shipped') + order_item_5 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_6 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_7 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + visit "/login" + fill_in :email, with: "Bob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchant" + + expect(page).to have_link(order_1.id) + expect(page).to_not have_link(order_2.id) + expect(page).to have_content(order_1.created_at) + expect(page).to have_content(order_1.item_qty(bike_shop.id)) + expect(page).to have_content(order_1.total_value(bike_shop.id)) + end + + it "I see a link to view my items and when I click it I'm redirected to '/merchant/items'" do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_4 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Shipped') + order_item_5 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_6 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_7 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + + visit "/login" + fill_in :email, with: "Bob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchant" + + click_link "My Items" + + expect(current_path).to eq("/merchant/items") + end + it "When I visit an order show page, I see the recipient's name and address + I only see the items in the order that belong to me." do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + visit "/login" + fill_in :email, with: "Bob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchant" + click_link "#{order_1.id}" + expect(current_path).to eq("/merchant/orders/#{order_1.id}") + + expect(page).to have_link(chain.name) + expect(page).to have_link(tire.name) + expect(page).to have_xpath("//img[contains(@src, '#{chain.image}')]") + expect(page).to have_xpath("//img[contains(@src, '#{tire.image}')]") + expect(page).to have_content(chain.price) + expect(page).to have_content(tire.price) + expect(page).to have_content(tire.order_quantity(order_1.id)) + expect(page).to have_content(chain.order_quantity(order_1.id)) + expect(page).to_not have_content(rack.name) + end + + it "When I visit an order show page, if the desired quantity of the item + is less or equal to current inventory and it is not already fulfilled, + I see a button to fulfill that item." do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 15, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + visit "/login" + fill_in :email, with: "Bob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchant" + click_link "#{order_1.id}" + expect(current_path).to eq("/merchant/orders/#{order_1.id}") + + expect(page).to have_link("Fulfill #{chain.name}") + expect(page).to_not have_link("Fulfill #{tire.name}") + expect(page).to have_content(chain.price) + expect(page).to have_content(tire.price) + end + + it "When I click on the link to fulfill the order, I am returned to the order + show page and I see the item is now fulfilled. I also see a flash message + indicating that I have fulfilled that item and the inventory is reduced by one" do + + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 20, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + visit "/login" + fill_in :email, with: "Bob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/merchant" + click_link "#{order_1.id}" + expect(current_path).to eq("/merchant/orders/#{order_1.id}") + click_link "Fulfill #{chain.name}" + expect(current_path).to eq("/merchant/orders/#{order_1.id}") + + expect(page).to_not have_link("Fulfill #{chain.name}") + expect(page).to_not have_link("Fulfill #{tire.name}") + + expect(page).to have_content("Chain fulfilled") + expect(page).to have_content("Chain has been fulfilled") + expect(page).to have_content("Can't fullfill - Not enough items in inventory") + chain.reload + expect(chain.inventory).to eq(3) + + end + end +end diff --git a/spec/features/merchants/destroy_spec.rb b/spec/features/merchants/destroy_spec.rb index 2d695438e..e963869ca 100644 --- a/spec/features/merchants/destroy_spec.rb +++ b/spec/features/merchants/destroy_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "As a visitor" do +RSpec.describe "As a admin" do describe "When I visit a merchant show page" do it "I can delete a merchant" do bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Richmond', state: 'VA', zip: 80203) @@ -35,6 +35,25 @@ pencil = mike.items.create(name: "Yellow Pencil", description: "You can write on paper with it!", price: 2, image: "https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg", inventory: 100) pulltoy = brian.items.create(name: "Pulltoy", description: "It'll never fall apart!", price: 14, image: "https://www.valupets.com/media/catalog/product/cache/1/image/650x/040ec09b1e35df139433887a97daa66f/l/a/large_rubber_dog_pull_toy.jpg", inventory: 7) + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + + ) + # allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/items/#{paper.id}" click_on "Add To Cart" @@ -63,10 +82,11 @@ click_button "Create Order" visit "/merchants/#{meg.id}" - expect(page).to_not have_link("Delete Merchant") + # expect(page).to_not have_link("Delete Merchant") + - # visit "/merchants/#{brian.id}" - # expect(page).to have_link("Delete Merchant") + visit "/merchants/#{brian.id}" + expect(page).to have_link("Delete Merchant") end end end diff --git a/spec/features/merchants/statistics_spec.rb b/spec/features/merchants/statistics_spec.rb index 0ad6fba59..3aaa3cec2 100644 --- a/spec/features/merchants/statistics_spec.rb +++ b/spec/features/merchants/statistics_spec.rb @@ -11,21 +11,32 @@ before :each do @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) @brian = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) @pull_toy = @brian.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) @dog_bone = @brian.items.create(name: "Dog Bone", description: "They'll love it!", price: 20, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) - @order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) - @order_2 = Order.create!(name: 'Brian', address: '123 Zanti St', city: 'Denver', state: 'CO', zip: 80204) - @order_3 = Order.create!(name: 'Mike', address: '123 Dao St', city: 'Denver', state: 'CO', zip: 80210) + @order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033, user_id: @user.id) + @order_2 = Order.create!(name: 'Brian', address: '123 Zanti St', city: 'Denver', state: 'CO', zip: 80204, user_id: @user.id) + @order_3 = Order.create!(name: 'Mike', address: '123 Dao St', city: 'Denver', state: 'CO', zip: 80210, user_id: @user.id) - @order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) - @order_1.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 3) - @order_2.item_orders.create!(item: @dog_bone, price: @dog_bone.price, quantity: 2) - @order_2.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 2) - @order_2.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) - @order_3.item_orders.create!(item: @dog_bone, price: @dog_bone.price, quantity: 5) + @order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) + @order_1.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 3, merchant_id: @brian.id) + @order_2.item_orders.create!(item: @dog_bone, price: @dog_bone.price, quantity: 2, merchant_id: @brian.id) + @order_2.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 2, merchant_id: @brian.id) + @order_2.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) + @order_3.item_orders.create!(item: @dog_bone, price: @dog_bone.price, quantity: 5, merchant_id: @brian.id) end it 'I can see a merchants statistics' do diff --git a/spec/features/navigation/admin_spec.rb b/spec/features/navigation/admin_spec.rb new file mode 100644 index 000000000..6c257bf74 --- /dev/null +++ b/spec/features/navigation/admin_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As an admin' do + describe 'I see the same links as a regular user' do + it 'Plus the following links, a link to my admin dashboard ("/admin"), a link to see all users ("/admin/users")' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + + visit '/login' + fill_in :email, with: jake.email + fill_in :password, with: jake.password + + expect(page).to have_link('Home') + expect(page).to have_link('All Items') + expect(page).to have_link('All Merchants') + expect(page).to have_link('Cart') + expect(page).to have_link('Register') + expect(page).to have_content('0') + click_on 'Login' + expect(page).to have_link('My Profile') + expect(page).to have_link('Admin Dashboard') + expect(page).to have_link('All Users') + expect(page).to have_link('Log Out') + expect(page).not_to have_link('Cart') + expect(page).not_to have_link('Log In') + expect(page).not_to have_link('Register') + expect(page).not_to have_content('0') + expect(page).to have_content("Logged in as #{jake.name}") + end + end + describe 'When I try to access any path that begins with the following, then I see a 404 error:' do + it "'/merchant' '/cart'" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2) + + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(jake) + visit "/merchant" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + + visit "/cart" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + end + end +end diff --git a/spec/features/navigation/merchant_spec.rb b/spec/features/navigation/merchant_spec.rb new file mode 100644 index 000000000..e421ab69b --- /dev/null +++ b/spec/features/navigation/merchant_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As a merchant employee' do + describe 'I see the same links as a regular user' do + it 'Plus the following link: a link to my merchant dashboard ("/merchant")' do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + jake = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + + visit '/login' + fill_in :email, with: jake.email + fill_in :password, with: jake.password + + + expect(page).to have_link('Home') + expect(page).to have_link('All Items') + expect(page).to have_link('All Merchants') + expect(page).to have_link('Cart') + expect(page).to have_link('Register') + expect(page).to have_content('0') + click_on "Login" + expect(page).to have_link('My Profile') + expect(page).to have_link('Merchant Dashboard') + expect(page).to have_link('Log Out') + expect(page).not_to have_link('Log In') + expect(page).not_to have_link('Register') + expect(page).to have_content("Logged in as #{jake.name}") + end + end + describe "When I try to access any path that begins with the following, then I see a 404 error:" do + it "'/admin'" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + + visit "/admin" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + end + end +end diff --git a/spec/features/navigation/user_spec.rb b/spec/features/navigation/user_spec.rb new file mode 100644 index 000000000..9a5b90804 --- /dev/null +++ b/spec/features/navigation/user_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As a default user' do + describe 'I see the same links as a visitor' do + it 'Plus the following links a link to my profile page (/profile),a link to log out (/logout)' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags') + + visit '/login' + fill_in :email, with: jake.email + fill_in :password, with: jake.password + + expect(page).to have_link('Home') + expect(page).to have_link('All Items') + expect(page).to have_link('All Merchants') + expect(page).to have_link('Cart') + expect(page).to have_link('Register') + expect(page).to have_content('0') + click_on 'Login' + expect(page).to have_link('My Profile') + expect(page).to have_link('Log Out') + expect(page).not_to have_link('Log In') + expect(page).not_to have_link('Register') + expect(page).to have_content("Logged in as #{jake.name}") + end + end + describe 'When I try to access any path that begins with the following, then I see a 404 error:' do + it "'/merchant' '/admin'" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags') + visit "/merchant" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + + visit "/admin" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + + end + end +end diff --git a/spec/features/navigation/visitor_spec.rb b/spec/features/navigation/visitor_spec.rb new file mode 100644 index 000000000..520c6ac6b --- /dev/null +++ b/spec/features/navigation/visitor_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As a visitor' do + describe 'I see a navigation bar' do + it "'This navigation bar includes links for the following: a link to return to the welcome / home page of the application ( / ), + a link to browse all items for sale /items), + a link to see all merchants (/merchants), a link to my shopping cart (/cart), + a link to log in (/login), a link to the user registration page (/register)" do + visit '/merchants' + + expect(page).to have_link("Home") + expect(page).to have_link("All Items") + expect(page).to have_link("All Merchants") + expect(page).to have_link("Cart") + expect(page).to have_link("Log In") + expect(page).to have_link("Register") + expect(page).to have_content("0") + end + end + describe "When I try to access any path that begins with the following, then I see a 404 error:" do + it "'/merchant'/admin'/profile'" do + visit "/merchant" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + + visit "/admin" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + + visit "/profile" + + expect(page).to have_content("The page you were looking for doesn't exist (404)") + end + end +end diff --git a/spec/features/orders/creation_spec.rb b/spec/features/orders/creation_spec.rb index 4b9776f9a..c5573ab71 100644 --- a/spec/features/orders/creation_spec.rb +++ b/spec/features/orders/creation_spec.rb @@ -1,3 +1,4 @@ +# require 'rails_helper' # When I fill out all information on the new order page # And click on 'Create Order' # An order is created and saved in the database @@ -11,10 +12,27 @@ before(:each) do @mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) @paper = @mike.items.create(name: "Lined Paper", description: "Great for writing on!", price: 20, image: "https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png", inventory: 3) @pencil = @mike.items.create(name: "Yellow Pencil", description: "You can write on paper with it!", price: 2, image: "https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg", inventory: 100) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + visit "/items/#{@paper.id}" click_on "Add To Cart" visit "/items/#{@paper.id}" @@ -45,66 +63,9 @@ new_order = Order.last - expect(current_path).to eq("/orders/#{new_order.id}") - - within '.shipping-address' do - expect(page).to have_content(name) - expect(page).to have_content(address) - expect(page).to have_content(city) - expect(page).to have_content(state) - expect(page).to have_content(zip) - end - - within "#item-#{@paper.id}" do - expect(page).to have_link(@paper.name) - expect(page).to have_link("#{@paper.merchant.name}") - expect(page).to have_content("$#{@paper.price}") - expect(page).to have_content("2") - expect(page).to have_content("$40") - end - - within "#item-#{@tire.id}" do - expect(page).to have_link(@tire.name) - expect(page).to have_link("#{@tire.merchant.name}") - expect(page).to have_content("$#{@tire.price}") - expect(page).to have_content("1") - expect(page).to have_content("$100") - end - - within "#item-#{@pencil.id}" do - expect(page).to have_link(@pencil.name) - expect(page).to have_link("#{@pencil.merchant.name}") - expect(page).to have_content("$#{@pencil.price}") - expect(page).to have_content("1") - expect(page).to have_content("$2") - end - - within "#grandtotal" do - expect(page).to have_content("Total: $142") - end - - within "#datecreated" do - expect(page).to have_content(new_order.created_at) - end - end - - it 'i cant create order if info not filled out' do - name = "" - address = "123 Sesame St." - city = "NYC" - state = "New York" - zip = 10001 - - fill_in :name, with: name - fill_in :address, with: address - fill_in :city, with: city - fill_in :state, with: state - fill_in :zip, with: zip - - click_button "Create Order" + expect(current_path).to eq("/profile/orders") - expect(page).to have_content("Please complete address form to create an order.") - expect(page).to have_button("Create Order") + end diff --git a/spec/features/orders/new_spec.rb b/spec/features/orders/new_spec.rb index fb8a56ef4..6c8aa771b 100644 --- a/spec/features/orders/new_spec.rb +++ b/spec/features/orders/new_spec.rb @@ -6,6 +6,18 @@ @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) @paper = @mike.items.create(name: "Lined Paper", description: "Great for writing on!", price: 20, image: "https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png", inventory: 3) @pencil = @mike.items.create(name: "Yellow Pencil", description: "You can write on paper with it!", price: 2, image: "https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg", inventory: 100) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(@user) visit "/items/#{@paper.id}" click_on "Add To Cart" diff --git a/spec/features/profile_orders/index_spec.rb b/spec/features/profile_orders/index_spec.rb new file mode 100644 index 000000000..7120284fe --- /dev/null +++ b/spec/features/profile_orders/index_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As a registered user' do + describe "When I visit my Profile Orders page, '/profile/orders'" do + describe "I see every order I've made, which includes the following information:" do + it "the ID of the order, which is a link to the order show page, the date the order was + made, the date the order was last updated, the current status of the order, the total quantity + of items in the order, the grand total of all items for that order" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + paper = mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 3) + pencil = mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit "/items/#{paper.id}" + click_on 'Add To Cart' + visit "/items/#{tire.id}" + click_on 'Add To Cart' + visit "/items/#{pencil.id}" + click_on 'Add To Cart' + + visit '/cart' + + click_on 'Checkout' + + name = 'Bert' + address = '123 Sesame St.' + city = 'NYC' + state = 'New York' + zip = 10_001 + + fill_in :name, with: name + fill_in :address, with: address + fill_in :city, with: city + fill_in :state, with: state + fill_in :zip, with: zip + + click_button 'Create Order' + + visit '/profile/orders' + + new_order = Order.last + + expect(page).to have_content(new_order.id) + expect(page).to have_link(new_order.id) + expect(page).to have_content(new_order.created_at) + expect(page).to have_content(new_order.updated_at) + expect(page).to have_content('Pending') + expect(page).to have_content(new_order.items.count) + expect(page).to have_content(new_order.grandtotal) + end + end + end +end diff --git a/spec/features/profile_orders/show_spec.rb b/spec/features/profile_orders/show_spec.rb new file mode 100644 index 000000000..a68686784 --- /dev/null +++ b/spec/features/profile_orders/show_spec.rb @@ -0,0 +1,275 @@ +# frozen_string_literal: true + +require 'rails_helper' +# When I fill out all information on the new order page +# And click on 'Create Order' +# An order is created and saved in the database +# And I am redirected to that order's show page with the following information: +# +# - Details of the order: + +# - the date when the order was created +RSpec.describe('Order Creation') do + describe 'When I check out from my cart' do + before(:each) do + @mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @paper = @mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 3) + @pencil = @mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit "/items/#{@paper.id}" + click_on 'Add To Cart' + visit "/items/#{@paper.id}" + click_on 'Add To Cart' + visit "/items/#{@tire.id}" + click_on 'Add To Cart' + visit "/items/#{@pencil.id}" + click_on 'Add To Cart' + + visit '/cart' + click_on 'Checkout' + end + + it 'I can create a new order' do + name = 'Bert' + address = '123 Sesame St.' + city = 'NYC' + state = 'New York' + zip = 10_001 + + fill_in :name, with: name + fill_in :address, with: address + fill_in :city, with: city + fill_in :state, with: state + fill_in :zip, with: zip + + click_button 'Create Order' + + new_order = Order.last + + visit "/profile/orders/#{new_order.id}" + + within '.shipping-address' do + expect(page).to have_content(name) + expect(page).to have_content(address) + expect(page).to have_content(city) + expect(page).to have_content(state) + expect(page).to have_content(zip) + end + + within "#item-#{@paper.id}" do + expect(page).to have_link(@paper.name) + expect(page).to have_link(@paper.merchant.name.to_s) + expect(page).to have_content("$#{@paper.price}") + expect(page).to have_content('2') + expect(page).to have_content('$40') + end + + within "#item-#{@tire.id}" do + expect(page).to have_link(@tire.name) + expect(page).to have_link(@tire.merchant.name.to_s) + expect(page).to have_content("$#{@tire.price}") + expect(page).to have_content('1') + expect(page).to have_content('$100') + end + + within "#item-#{@pencil.id}" do + expect(page).to have_link(@pencil.name) + expect(page).to have_link(@pencil.merchant.name.to_s) + expect(page).to have_content("$#{@pencil.price}") + expect(page).to have_content('1') + expect(page).to have_content('$2') + end + + within '#grandtotal' do + expect(page).to have_content('Total: $142') + end + + within '#datecreated' do + expect(page).to have_content(new_order.created_at) + end + end + + it 'i cant create order if info not filled out' do + name = '' + address = '123 Sesame St.' + city = 'NYC' + state = 'New York' + zip = 10_001 + + fill_in :name, with: name + fill_in :address, with: address + fill_in :city, with: city + fill_in :state, with: state + fill_in :zip, with: zip + + click_button 'Create Order' + + expect(page).to have_content('Please complete address form to create an order.') + expect(page).to have_button('Create Order') + end + end + it "the ID of the order, the date the order was made, the date the order was last updated, the current status + of the order, each item I ordered, including name, description, thumbnail, quantity, price and subtotal, the + total quantity of items in the whole order, the grand total of all items for that order" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + paper = mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 3) + pencil = mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit "/items/#{paper.id}" + click_on 'Add To Cart' + visit "/items/#{tire.id}" + click_on 'Add To Cart' + visit "/items/#{pencil.id}" + click_on 'Add To Cart' + visit '/cart' + + click_on 'Checkout' + + name = 'Bert' + address = '123 Sesame St.' + city = 'NYC' + state = 'New York' + zip = 10_001 + + fill_in :name, with: name + fill_in :address, with: address + fill_in :city, with: city + fill_in :state, with: state + fill_in :zip, with: zip + + click_button 'Create Order' + + visit '/profile/orders' + + new_order = Order.last + click_on new_order.id.to_s + + expect(page).to have_content(new_order.id) + expect(page).to have_content(new_order.created_at) + expect(page).to have_content(new_order.updated_at) + expect(page).to have_content('Pending') + expect(page).to have_content(new_order.items.count) + expect(page).to have_content(new_order.grandtotal) + + within "#item-#{tire.id}" do + expect(page).to have_content(tire.name) + expect(page).to have_content(tire.description) + expect(page).to have_css("img[src*='#{tire.image}']") + expect(page).to have_content(1) + expect(page).to have_content(100) + end + end + describe 'I see a button or link to cancel the order' do + describe 'When I click the cancel button for an order, the following happens:' do + it "Each row in the 'order items' table is given a status of 'unfulfilled', + The order itself is given a status of 'cancelled', Any item quantities in the order that were + previously fulfilled have their quantities returned to their respective merchant's inventory for that item., + I am returned to my profile page, I see a flash message telling me the order is now cancelled, + And I see that this order now has an updated status of 'cancelled'" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + mike = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + paper = mike.items.create(name: 'Lined Paper', description: 'Great for writing on!', price: 20, image: 'https://cdn.vertex42.com/WordTemplates/images/printable-lined-paper-wide-ruled.png', inventory: 3) + pencil = mike.items.create(name: 'Yellow Pencil', description: 'You can write on paper with it!', price: 2, image: 'https://images-na.ssl-images-amazon.com/images/I/31BlVr01izL._SX425_.jpg', inventory: 100) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit "/items/#{paper.id}" + click_on 'Add To Cart' + visit "/items/#{tire.id}" + click_on 'Add To Cart' + visit "/items/#{pencil.id}" + click_on 'Add To Cart' + + visit '/cart' + + click_on 'Checkout' + + name = 'Bert' + address = '123 Sesame St.' + city = 'NYC' + state = 'New York' + zip = 10_001 + + fill_in :name, with: name + fill_in :address, with: address + fill_in :city, with: city + fill_in :state, with: state + fill_in :zip, with: zip + + click_button 'Create Order' + + visit '/profile/orders' + + new_order = Order.last + + click_on new_order.id.to_s + + click_on 'Cancel Order' + + expect(current_path).to eq('/profile/orders') + + expect(new_order.item_orders.all? { |item_order| item_order.status == 'Unfulfilled' }).to eq(true) + + within "#item-#{new_order.id}" do + expect(page).to have_content('Cancelled') + end + + expect(page).to have_content("Your order is now cancelled") + + expect(Item.find(tire.id).inventory).to eq(13) + end + end + end +end diff --git a/spec/features/sessions/login_spec.rb b/spec/features/sessions/login_spec.rb new file mode 100644 index 000000000..cad90b3e0 --- /dev/null +++ b/spec/features/sessions/login_spec.rb @@ -0,0 +1,202 @@ +require "rails_helper" + +describe "As a visitor" do + describe "When I visit the login path I am able to login with valid credentials" do + it "as a regular user, I am redirected to my profile page" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + expect(current_path).to eq("/profile") + expect(page).to have_content("You are now logged in") + end + + it "as a merchant user, I am redirected to my merchant dashboard" do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + merchant_user = User.create!( + name: 'Mark Merchant', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'buymystuff@hotmail.com', + password: 'merchantsrock', + password_confirmation: 'merchantsrock', + role: 1, + merchant_id: bike_shop.id + ) + visit "/login" + + fill_in :email, with: "buymystuff@hotmail.com" + fill_in :password, with: "merchantsrock" + click_button "Login" + + expect(current_path).to eq("/merchant") + expect(page).to have_content("You are now logged in") + end + + it "as a merchant user, I am redirected to my merchant dashboard" do + admin = User.create!( + name: 'Allen Admin', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'icontroleverything@hotmail.com', + password: 'adminadmin', + password_confirmation: 'adminadmin', + role: 2 + ) + visit "/login" + + fill_in :email, with: "icontroleverything@hotmail.com" + fill_in :password, with: "adminadmin" + click_button "Login" + + expect(current_path).to eq("/admin") + expect(page).to have_content("You are now logged in") + end + end + + describe "When I log in with bad credentials" do + it "With a bad password I am redirected to the login page and informed of invalid credentials" do + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybag" + + click_button "Login" + + expect(current_path).to eq("/login") + expect(page).to have_content("Username and/or password is incorrect") + end + + it "With a bad email I am redirected to the login page and informed of invalid credentials" do + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + + visit "/login" + + fill_in :email, with: "JBlob1234@hotmail.com" + fill_in :password, with: "heftybags" + + click_button "Login" + + expect(current_path).to eq("/login") + expect(page).to have_content("Username and/or password is incorrect") + end + end +end + +describe "As an already registered visitor" do + describe "When I visit the login path" do + it "As a regular user I'm redirected to my profile page and informed I'm logged in" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/login" + + expect(current_path).to eq("/profile") + expect(page).to have_content("You are already logged in") + + end + + it "As a merchant user I'm redirected to my dashboard and informed I'm logged in" do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + merchant_user = bike_shop.users.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/login" + + expect(current_path).to eq("/merchant") + expect(page).to have_content("You are already logged in") + + end + + it "As a admin user I'm redirected to my dashboard and informed I'm logged in" do + admin = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 2 + ) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/login" + + expect(current_path).to eq("/admin") + expect(page).to have_content("You are already logged in") + end + end +end diff --git a/spec/features/sessions/logout_spec.rb b/spec/features/sessions/logout_spec.rb new file mode 100644 index 000000000..9d282da90 --- /dev/null +++ b/spec/features/sessions/logout_spec.rb @@ -0,0 +1,37 @@ +require "rails_helper" + +describe "As a logged in user, merchant, or admin" do + describe "When I visit the logout path, I am redirected to the home page" do + it "I see a flash message that I'm logged out and all cart items are deleted" do + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + dog_shop = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + item = dog_shop.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + visit "/login" + + fill_in :email, with: "JBob1234@hotmail.com" + fill_in :password, with: "heftybags" + click_button "Login" + + visit "/items/#{item.id}" + + click_on "Add To Cart" + expect(page).to have_content("Cart: 1") + + click_link "Log Out" + + expect(page).to have_content("Cart: 0") + expect(current_path).to eq("/") + expect(page).to have_content("You have successfully logged out") + end + end +end diff --git a/spec/features/users/edit_spec.rb b/spec/features/users/edit_spec.rb new file mode 100644 index 000000000..df680b294 --- /dev/null +++ b/spec/features/users/edit_spec.rb @@ -0,0 +1,75 @@ +require 'rails_helper' + +describe "As a registered user" do + describe "When I visit my edit profile page" do + it "I see all fields populated with current info and + I can edit my information " do + jake = User.create!( name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile/edit' + + expect(find_field(:name).value).to have_content('JakeBob') + expect(find_field(:address).value).to have_content('124 Main St') + expect(find_field(:city).value).to have_content('Denver') + expect(find_field(:state).value).to have_content('Colorado') + expect(find_field(:zip).value).to have_content('80202') + expect(find_field(:email).value).to have_content('JBob1234@hotmail.com') + fill_in :name, with: 'edittest' + click_button 'Update Profile' + expect(current_path).to eq('/profile') + expect(page).to have_content('Your profile has been updated') + expect(page).to have_content('Name: edittest') + end + + it "Email address must not belong to another user " do + jake = User.create!( name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + garrett = User.create!( name: 'GarrettBob', + address: '129 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'garrettbob@gmail.com', + password: 'test', + password_confirmation: 'test', + role: 0 + ) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile/edit' + + fill_in :email, with: 'garrettbob@gmail.com' + click_button 'Update Profile' + expect(current_path).to eq('/profile/edit') + expect(page).to have_content('Email has already been taken') + end + end +end diff --git a/spec/features/users/new_spec.rb b/spec/features/users/new_spec.rb new file mode 100644 index 000000000..431664055 --- /dev/null +++ b/spec/features/users/new_spec.rb @@ -0,0 +1,142 @@ +# User Story 10, User Registration +# +# As a visitor +# When I click on the 'register' link in the nav bar +# Then I am on the user registration page ('/register') +# And I see a form where I input the following data: +# - my name +# - my street address +# - my city +# - my state +# - my zip code +# - my email address +# - my preferred password +# - a confirmation field for my password +# +# When I fill in this form completely, +# And with a unique email address not already in the system +# My details are saved in the database +# Then I am logged in as a registered user +# I am taken to my profile page ("/profile") +# I see a flash message indicating that I am now registered and logged in +require 'rails_helper' + +describe "As a visitor" do + describe "When I click on the 'register' link in the nav bar" do + it "I see a form to input data" do + + visit '/register' + fill_in :name, with: 'JimBob' + fill_in :address, with: '123 Main St' + fill_in :city, with: 'Denver' + fill_in :state, with: 'Colorado' + fill_in :zip, with: '80202' + fill_in :email, with: 'JimBob3003@aol.com' + fill_in :password, with: 'somethingdiff' + fill_in :password_confirmation, with: 'somethingdiff' + + click_button "Register" + + expect(current_path).to eq("/profile") + expect(page).to have_content("You are now registered and logged in") + + end + end + + # User Story 11, User Registration Missing Details + # + # As a visitor + # When I visit the user registration page + # And I do not fill in this form completely, + # I am returned to the registration page + # And I see a flash message indicating that I am missing required fields + + describe 'As a visitor, when I visit the user registration page' do + describe 'And I do not fill in the form completely' do + it 'I get a flash message and am returned to the registration page' do + + visit '/register' + fill_in :name, with: 'JimBob' + fill_in :address, with: '123 Main St' + fill_in :city, with: 'Denver' + fill_in :zip, with: '80202' + fill_in :email, with: 'JimBob3003@aol.com' + fill_in :password, with: 'somethingdiff' + fill_in :password_confirmation, with: 'somethingdiff' + + click_button "Register" + + expect(current_path).to eq("/users") + expect(page).to have_content("State can't be blank") + end + end + end + +# User Story 12, Registration Email must be unique +# +# As a visitor +# When I visit the user registration page +# If I fill out the registration form +# But include an email address already in the system +# Then I am returned to the registration page +# My details are not saved and I am not logged in +# The form is filled in with all previous data except the email field and password fields +# I see a flash message telling me the email address is already in use + + describe 'As a visitor when I visit the user registration page' do + describe 'If I fill out the registration form but my email is taken' do + it 'I am returned to the registration page and I see a flash message' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + + visit '/register' + fill_in :name, with: 'JimBob' + fill_in :address, with: '123 Main St' + fill_in :city, with: 'Denver' + fill_in :state, with: 'Colorado' + fill_in :zip, with: '80202' + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'somethingdiff' + fill_in :password_confirmation, with: 'somethingdiff' + + click_button "Register" + + expect(current_path).to eq("/users") + + expect(page).to have_content("Email has already been taken") + expect(find_field(:name).value).to have_content('JimBob') + expect(find_field(:address).value).to have_content('123 Main St') + expect(find_field(:city).value).to have_content('Denver') + expect(find_field(:state).value).to have_content('Colorado') + expect(find_field(:zip).value).to have_content('80202') + end + end + end + + describe "When I create a user" do + it "password and password confirmation must match" do + visit '/register' + fill_in :name, with: 'JimBob' + fill_in :address, with: '123 Main St' + fill_in :city, with: 'Denver' + fill_in :state, with: 'Colorado' + fill_in :zip, with: '80202' + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'somethingdiff' + fill_in :password_confirmation, with: 'somethingDiff' + + click_button "Register" + + expect(current_path).to eq("/users") + expect(page).to have_content("Password confirmation doesn't match Password") + end + end +end diff --git a/spec/features/users/password_spec.rb b/spec/features/users/password_spec.rb new file mode 100644 index 000000000..44a89588b --- /dev/null +++ b/spec/features/users/password_spec.rb @@ -0,0 +1,54 @@ +require 'rails_helper' + +describe "As a registered user" do + describe "When I visit my password edit page" do + it "I am able to edit my password and am redirected to my profile page" do + jake = User.create!( name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/password/edit' + fill_in :password, with: "testpassword" + fill_in :password_confirmation, with: 'testpassword' + click_button 'Change Password' + password_digest = '$2a$04$OkF/X97WsdeVjco9W8pJHucP15Y7CiuYz3Qbe2AGpqCe6ultBjUhK' + expect(current_path).to eq('/profile') + expect(page).to have_content('Your password has been changed') + expect(jake.password_digest).not_to eq(password_digest) + end + + it "I am able to edit my password and am redirected to my profile page" do + jake = User.create!( name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + visit '/password/edit' + click_button 'Change Password' + expect(current_path).to eq('/password/edit') + expect(page).to have_content("Password confirmation doesn't match Password") + end + end +end diff --git a/spec/features/users/show_spec.rb b/spec/features/users/show_spec.rb new file mode 100644 index 000000000..4707f7091 --- /dev/null +++ b/spec/features/users/show_spec.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'As a registered user' do + describe 'When I visit my profile page' do + it 'I see all my profile data except for password' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile' + + expect(page).to have_content("Name: #{jake.name}") + expect(page).to have_content("Address: #{jake.full_address}") + expect(page).to have_content("Email: #{jake.email}") + expect(page).to have_link('Edit Profile') + end + + it "I see a link to edit my profile data and when I click the link I'm taken to + a page to edit my profile" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile' + + click_link('Edit Profile') + expect(current_path).to eq('/profile/edit') + end + + it "I click the link to change password and I'm taken to a page to + edit my password with a form with fields for new password and confirmation" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile' + + click_link('Edit Password') + expect(current_path).to eq('/password/edit') + end + end + describe 'When I visit my Profile page' do + describe 'And I have orders placed in the system' do + describe "Then I see a link on my profile page called 'My Orders'" do + it "When I click this link my URI path is '/profile/orders'" do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0) + + visit '/login' + + fill_in :email, with: 'JBob1234@hotmail.com' + fill_in :password, with: 'heftybags' + click_button 'Login' + + visit '/profile' + + expect(page).to have_link("My Orders") + click_on "My Orders" + expect(current_path).to eq("/profile/orders") + end + end + end + end +end diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb index 40e415d4f..f9ca70289 100644 --- a/spec/models/cart_spec.rb +++ b/spec/models/cart_spec.rb @@ -31,6 +31,14 @@ }) end + it ".subtract_item()" do + @cart.subtract_item(@giant.id.to_s) + expect(@cart.contents).to eq({ + @ogre.id.to_s => 1, + @giant.id.to_s => 1 + }) + end + it '.total_items' do expect(@cart.total_items).to eq(3) end diff --git a/spec/models/item_orders_spec.rb b/spec/models/item_orders_spec.rb index e1444d83a..42c7428b7 100644 --- a/spec/models/item_orders_spec.rb +++ b/spec/models/item_orders_spec.rb @@ -1,27 +1,59 @@ +# frozen_string_literal: true + require 'rails_helper' describe ItemOrder, type: :model do - describe "validations" do + describe 'validations' do it { should validate_presence_of :order_id } it { should validate_presence_of :item_id } it { should validate_presence_of :price } it { should validate_presence_of :quantity } end - describe "relationships" do - it {should belong_to :item} - it {should belong_to :order} + describe 'relationships' do + it { should belong_to :item } + it { should belong_to :order } + it { should belong_to :merchant } end describe 'instance methods' do - it 'subtotal' do - meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) - tire = meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) - order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) - item_order_1 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2) + it '#subtotal' do + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: user.id) + item_order_1 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, merchant_id: meg.id) expect(item_order_1.subtotal).to eq(200) end - end + it '#items_left' do + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: user.id) + item_order_1 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, merchant_id: meg.id) + expect(item_order_1.items_left).to eq(10) + end + end end diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index 307ee1c90..baf73c1d8 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -5,7 +5,7 @@ it { should validate_presence_of :name } it { should validate_presence_of :description } it { should validate_presence_of :price } - it { should validate_presence_of :image } + it { should validate_presence_of :inventory } it { should validate_inclusion_of(:active?).in_array([true,false]) } end @@ -21,6 +21,17 @@ before(:each) do @bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) @chain = @bike_shop.items.create(name: "Chain", description: "It'll never break!", price: 50, image: "https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588", inventory: 5) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) @review_1 = @chain.reviews.create(title: "Great place!", content: "They have great bike stuff and I'd recommend them to anyone.", rating: 5) @review_2 = @chain.reviews.create(title: "Cool shop!", content: "They have cool bike stuff and I'd recommend them to anyone.", rating: 4) @@ -43,9 +54,145 @@ it 'no orders' do expect(@chain.no_orders?).to eq(true) - order = Order.create(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) - order.item_orders.create(item: @chain, price: @chain.price, quantity: 2) + order = Order.create(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033, user_id: @user.id) + order.item_orders.create(item: @chain, price: @chain.price, quantity: 2, merchant_id: @bike_shop.id) expect(@chain.no_orders?).to eq(false) end + + it '#order_quantity' do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'itemsbymerchant.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_4 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 6, status: 'Pending', merchant_id: bike_shop.id) + order_item_5 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 10, status: 'Pending', merchant_id: bike_shop.id) + + expect(tire.order_quantity(order_1.id)).to eq(2) + end + + it '#status' do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'itemsbymerchant.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_4 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 6, status: 'Pending', merchant_id: bike_shop.id) + order_item_5 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 10, status: 'Pending', merchant_id: bike_shop.id) + + expect(chain.status(order_1.id)).to eq("Pending") + end + + it "#status" do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'itemsbymerchant.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_4 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 6, status: 'Pending', merchant_id: bike_shop.id) + order_item_5 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 10, status: 'Pending', merchant_id: bike_shop.id) + + expect(chain.order_item(order_1.id)).to eq(order_item_1.id) + end + + describe "class methods" do + before(:each) do + @bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + @chain = @bike_shop.items.create(name: "Chain", description: "It'll never break!", price: 50, image: "https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588", inventory: 5) + @string = @bike_shop.items.create(name: "String", description: "It'll probably break!", price: 5, image: "https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588", active?: false, inventory: 5) + order = Order.create(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033, user_id: @user.id) + order.item_orders.create(item: @chain, price: @chain.price, quantity: 2, merchant_id: @bike_shop.id) + order.item_orders.create(item: @string, price: @string.price, quantity: 1, merchant_id: @bike_shop.id) + end + it 'find_enabled_items' do + expect(Item.find_enabled_items.first.name).to eq(@chain.name) + end + + it 'top_five' do + expect(Item.top_five.first.name).to eq(@chain.name) + end + + it 'bottom_five' do + expect(Item.bottom_five.first.name).to eq(@string.name) + end + end end end diff --git a/spec/models/merchant_spec.rb b/spec/models/merchant_spec.rb index 1dd9d5532..c58bd41cf 100644 --- a/spec/models/merchant_spec.rb +++ b/spec/models/merchant_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'rails_helper' describe Merchant, type: :model do - describe "validations" do + describe 'validations' do it { should validate_presence_of :name } it { should validate_presence_of :address } it { should validate_presence_of :city } @@ -9,48 +11,122 @@ it { should validate_presence_of :zip } end - describe "relationships" do - it {should have_many :items} + describe 'relationships' do + it { should have_many :items } + it { should have_many(:users).conditions(role: :merchant) } + it { should have_many(:orders).through(:items) } end describe 'instance methods' do before(:each) do - @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) - @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) end - it 'no_orders' do + it '#no_orders?' do expect(@meg.no_orders?).to eq(true) - order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) - item_order_1 = order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) + order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: @user.id) + item_order_1 = order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) expect(@meg.no_orders?).to eq(false) end - it 'item_count' do - chain = @meg.items.create(name: "Chain", description: "It'll never break!", price: 30, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 22) + it '#item_count' do + chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 30, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 22) expect(@meg.item_count).to eq(2) end - it 'average_item_price' do - chain = @meg.items.create(name: "Chain", description: "It'll never break!", price: 40, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 22) + it '#average_item_price' do + chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 40, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 22) expect(@meg.average_item_price).to eq(70) end - it 'distinct_cities' do - chain = @meg.items.create(name: "Chain", description: "It'll never break!", price: 40, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 22) - order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) - order_2 = Order.create!(name: 'Brian', address: '123 Brian Ave', city: 'Denver', state: 'CO', zip: 17033) - order_3 = Order.create!(name: 'Dao', address: '123 Mike Ave', city: 'Denver', state: 'CO', zip: 17033) - order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) - order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2) - order_3.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) + it '#distinct_cities' do + chain = @meg.items.create(name: 'Chain', description: "It'll never break!", price: 40, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 22) + order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: @user.id) + order_2 = Order.create!(name: 'Brian', address: '123 Brian Ave', city: 'Denver', state: 'CO', zip: 17_033, user_id: @user.id) + order_3 = Order.create!(name: 'Dao', address: '123 Mike Ave', city: 'Denver', state: 'CO', zip: 17_033, user_id: @user.id) + order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) + order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, merchant_id: @meg.id) + order_3.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) + + expect(@meg.distinct_cities).to include('Denver') + expect(@meg.distinct_cities).to include('Hershey') + end + + it '#pending_orders' do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'J1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = bike_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Packaged') + order_item_1 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) - expect(@meg.distinct_cities).to include("Denver") - expect(@meg.distinct_cities).to include("Hershey") + expect(bike_shop.pending_orders).to eq([order_1]) end + it 'disable_merchant' do + mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + pull_toy = mike.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + dog_bone = mike.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + mike.disable_merchant + expected = mike.items.all? {|item| item.active? == false } + expect(expected).to eq(true) + end + + it 'enable_merchant' do + mike = Merchant.create!(name: "Mike's Print Shop", + address: '123 Paper Rd.', + city: 'Denver', + state: 'CO', + zip: 80203) + pull_toy = mike.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + dog_bone = mike.items.create(name: "Dog Bone", description: "They'll love it!", price: 21, image: "https://img.chewy.com/is/image/catalog/54226_MAIN._AC_SL1500_V1534449573_.jpg", active?:false, inventory: 21) + + mike.enable_merchant + expected = mike.items.all? {|item| item.active? == true } + expect(expected).to eq(true) + end end end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index d9c045d4d..026475fff 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + require 'rails_helper' describe Order, type: :model do - describe "validations" do + describe 'validations' do it { should validate_presence_of :name } it { should validate_presence_of :address } it { should validate_presence_of :city } @@ -9,26 +11,215 @@ it { should validate_presence_of :zip } end - describe "relationships" do - it {should have_many :item_orders} - it {should have_many(:items).through(:item_orders)} + describe 'relationships' do + it { should have_many :item_orders } + it { should have_many(:items).through(:item_orders) } + it { should belong_to :user } end describe 'instance methods' do before :each do - @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) - @brian = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80210) + @meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + @brian = Merchant.create(name: "Brian's Dog Shop", address: '125 Doggo St.', city: 'Denver', state: 'CO', zip: 80_210) + @user = User.create( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) - @tire = @meg.items.create(name: "Gatorskins", description: "They'll never pop!", price: 100, image: "https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588", inventory: 12) - @pull_toy = @brian.items.create(name: "Pull Toy", description: "Great pull toy!", price: 10, image: "http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg", inventory: 32) + @tire = @meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + @pull_toy = @brian.items.create(name: 'Pull Toy', description: 'Great pull toy!', price: 10, image: 'http://lovencaretoys.com/image/cache/dog/tug-toy-dog-pull-9010_2-800x800.jpg', inventory: 32) - @order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17033) + @order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: @user.id) - @order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2) - @order_1.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 3) + @order_1.item_orders.create!(item: @tire, price: @tire.price, quantity: 2, merchant_id: @meg.id) + @order_1.item_orders.create!(item: @pull_toy, price: @pull_toy.price, quantity: 3, merchant_id: @brian.id) end - it 'grandtotal' do + it '#grandtotal' do expect(@order_1.grandtotal).to eq(230) end + + it '#count_items' do + expect(@order_1.count_items).to eq(2) + end + it '#return_items' do + meg = Merchant.create(name: "Meg's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'returnitems.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + tire = meg.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'Meg', address: '123 Stang Ave', city: 'Hershey', state: 'PA', zip: 17_033, user_id: user.id) + item_order_1 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, merchant_id: meg.id) + + order_1.return_items + + expect(Item.find(tire.id).inventory).to eq(14) + end + + it '#item_qty' do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'J1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_4 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_5 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_6 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_7 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + + expect(order_1.item_qty(bike_shop.id)).to eq(6) + end + + it '#total_value' do + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80_203) + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80_203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'J1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_4 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + order_2 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_5 = order_2.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_6 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_7 = order_2.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Fulfilled', merchant_id: bike_shop.id) + + expect(order_1.total_value(bike_shop.id)).to eq(500.0) + end + + it '#items_by_merchant' do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'itemsbymerchant.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + expect(order_1.items_by_merchant(bike_shop.id)).to eq([chain, tire]) + end + it '#cancel_order' do + print_shop = Merchant.create(name: "Mike's Print Shop", address: '123 Paper Rd.', city: 'Denver', state: 'CO', zip: 80203) + bike_shop = Merchant.create(name: "Brian's Bike Shop", address: '123 Bike Rd.', city: 'Denver', state: 'CO', zip: 80203) + user = User.create!( + name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'itemsbymerchant.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + merchant_user = bike_shop.users.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'Bob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 1 + ) + chain = bike_shop.items.create(name: 'Chain', description: "It'll never break!", price: 50, image: 'https://www.rei.com/media/b61d1379-ec0e-4760-9247-57ef971af0ad?size=784x588', inventory: 5) + tire = bike_shop.items.create(name: 'Gatorskins', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + rack = print_shop.items.create(name: 'Bike Rack', description: "They'll never pop!", price: 100, image: 'https://www.rei.com/media/4e1f5b05-27ef-4267-bb9a-14e35935f218?size=784x588', inventory: 12) + order_1 = Order.create!(name: 'JakeBob', address: '123 Stang St', city: 'Hershey', state: 'PA', zip: 80_218, user_id: user.id, status: 'Pending') + order_item_1 = order_1.item_orders.create!(item: chain, price: chain.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_2 = order_1.item_orders.create!(item: tire, price: tire.price, quantity: 2, status: 'Pending', merchant_id: bike_shop.id) + order_item_3 = order_1.item_orders.create!(item: rack, price: rack.price, quantity: 2, status: 'Pending', merchant_id: print_shop.id) + + order_1.cancel_order + + expect(order_1.status).to eq("Cancelled") + expect(order_1.item_orders.all? { |item_order| item_order.status == "Unfulfilled"}).to eq(true) + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 000000000..969842588 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +describe User, type: :model do + describe "password attributes" do + it "needs password confirmation" do + jake = User.create(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + role: 0 + ) + + expect(User.find_by(name: "JakeBob")).to eq(nil) + end + end + + describe 'instance methods' do + it '#valid_email' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + expect(jake.valid_email).to eq(false) + end + it '#full_address' do + jake = User.create!(name: 'JakeBob', + address: '124 Main St', + city: 'Denver', + state: 'Colorado', + zip: '80202', + email: 'JBob1234@hotmail.com', + password: 'heftybags', + password_confirmation: 'heftybags', + role: 0 + ) + expect(jake.full_address).to eq('124 Main St, Denver, Colorado, 80202') + end + end + + describe "validations" do + it { should validate_uniqueness_of :email } + + it { should validate_presence_of :email } + it { should validate_presence_of :name } + it { should validate_presence_of :address } + it { should validate_presence_of :city } + it { should validate_presence_of :state } + it { should validate_presence_of :zip } + it { should validate_presence_of :password } + it { should validate_presence_of :password_confirmation } + end + + describe "relationships" do + it { should have_many :orders } + it { should belong_to(:merchant).optional(true) }#.conditions(role: :merchant) } + end +end