diff --git a/bin/bundle b/bin/bundle
new file mode 100755
index 0000000000..66e9889e8b
--- /dev/null
+++ b/bin/bundle
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
+load Gem.bin_path('bundler', 'bundle')
diff --git a/bin/rails b/bin/rails
new file mode 100755
index 0000000000..5badb2fde0
--- /dev/null
+++ b/bin/rails
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+begin
+ load File.expand_path('../spring', __FILE__)
+rescue LoadError => e
+ raise unless e.message.include?('spring')
+end
+APP_PATH = File.expand_path('../config/application', __dir__)
+require_relative '../config/boot'
+require 'rails/commands'
diff --git a/bin/rake b/bin/rake
new file mode 100755
index 0000000000..d87d5f5781
--- /dev/null
+++ b/bin/rake
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+begin
+ load File.expand_path('../spring', __FILE__)
+rescue LoadError => e
+ raise unless e.message.include?('spring')
+end
+require_relative '../config/boot'
+require 'rake'
+Rake.application.run
diff --git a/bin/setup b/bin/setup
new file mode 100755
index 0000000000..78c4e861dc
--- /dev/null
+++ b/bin/setup
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+require 'pathname'
+require 'fileutils'
+include FileUtils
+
+# path to your application root.
+APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
+
+def system!(*args)
+ system(*args) || abort("\n== Command #{args} failed ==")
+end
+
+chdir APP_ROOT do
+ # This script is a starting point to setup your application.
+ # Add necessary setup steps to this file.
+
+ puts '== Installing dependencies =='
+ system! 'gem install bundler --conservative'
+ system('bundle check') || system!('bundle install')
+
+ # Install JavaScript dependencies if using Yarn
+ # system('bin/yarn')
+
+
+ # puts "\n== Copying sample files =="
+ # unless File.exist?('config/database.yml')
+ # cp 'config/database.yml.sample', 'config/database.yml'
+ # end
+
+ puts "\n== Preparing database =="
+ system! 'bin/rails db:setup'
+
+ puts "\n== Removing old logs and tempfiles =="
+ system! 'bin/rails log:clear tmp:clear'
+
+ puts "\n== Restarting application server =="
+ system! 'bin/rails restart'
+end
diff --git a/bin/spring b/bin/spring
new file mode 100755
index 0000000000..fb2ec2ebb4
--- /dev/null
+++ b/bin/spring
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+
+# This file loads spring without using Bundler, in order to be fast.
+# It gets overwritten when you run the `spring binstub` command.
+
+unless defined?(Spring)
+ require 'rubygems'
+ require 'bundler'
+
+ lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
+ spring = lockfile.specs.detect { |spec| spec.name == "spring" }
+ if spring
+ Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
+ gem 'spring', spring.version
+ require 'spring/binstub'
+ end
+end
diff --git a/bin/update b/bin/update
new file mode 100755
index 0000000000..a8e4462f20
--- /dev/null
+++ b/bin/update
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+require 'pathname'
+require 'fileutils'
+include FileUtils
+
+# path to your application root.
+APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
+
+def system!(*args)
+ system(*args) || abort("\n== Command #{args} failed ==")
+end
+
+chdir APP_ROOT do
+ # This script is a way to update your development environment automatically.
+ # Add necessary update steps to this file.
+
+ puts '== Installing dependencies =='
+ system! 'gem install bundler --conservative'
+ system('bundle check') || system!('bundle install')
+
+ puts "\n== Updating database =="
+ system! 'bin/rails db:migrate'
+
+ puts "\n== Removing old logs and tempfiles =="
+ system! 'bin/rails log:clear tmp:clear'
+
+ puts "\n== Restarting application server =="
+ system! 'bin/rails restart'
+end
diff --git a/bin/yarn b/bin/yarn
new file mode 100755
index 0000000000..c2bacef836
--- /dev/null
+++ b/bin/yarn
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby
+VENDOR_PATH = File.expand_path('..', __dir__)
+Dir.chdir(VENDOR_PATH) do
+ begin
+ exec "yarnpkg #{ARGV.join(" ")}"
+ rescue Errno::ENOENT
+ $stderr.puts "Yarn executable was not detected in the system."
+ $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
+ exit 1
+ end
+end
diff --git a/config.ru b/config.ru
new file mode 100644
index 0000000000..f7ba0b527b
--- /dev/null
+++ b/config.ru
@@ -0,0 +1,5 @@
+# This file is used by Rack-based servers to start the application.
+
+require_relative 'config/environment'
+
+run Rails.application
diff --git a/config/application.rb b/config/application.rb
new file mode 100644
index 0000000000..5f6ca0f9b2
--- /dev/null
+++ b/config/application.rb
@@ -0,0 +1,25 @@
+require_relative 'boot'
+
+require 'rails/all'
+
+# Require the gems listed in Gemfile, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(*Rails.groups)
+
+module Betsy
+ class Application < Rails::Application
+ config.generators do |g|
+ # Force new test files to be generated in the minitest-spec style
+ g.test_framework :minitest, spec: true
+
+ # Always use .js files, never .coffee
+ g.javascript_engine :js
+ end
+ # Initialize configuration defaults for originally generated Rails version.
+ config.load_defaults 5.1
+
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+ end
+end
diff --git a/config/boot.rb b/config/boot.rb
new file mode 100644
index 0000000000..30f5120df6
--- /dev/null
+++ b/config/boot.rb
@@ -0,0 +1,3 @@
+ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
+
+require 'bundler/setup' # Set up gems listed in the Gemfile.
diff --git a/config/cable.yml b/config/cable.yml
new file mode 100644
index 0000000000..3cba994bb2
--- /dev/null
+++ b/config/cable.yml
@@ -0,0 +1,10 @@
+development:
+ adapter: async
+
+test:
+ adapter: async
+
+production:
+ adapter: redis
+ url: redis://localhost:6379/1
+ channel_prefix: betsy_production
diff --git a/config/database.yml b/config/database.yml
new file mode 100644
index 0000000000..6903bb6083
--- /dev/null
+++ b/config/database.yml
@@ -0,0 +1,85 @@
+# PostgreSQL. Versions 9.1 and up are supported.
+#
+# Install the pg driver:
+# gem install pg
+# On OS X with Homebrew:
+# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
+# On OS X with MacPorts:
+# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
+# On Windows:
+# gem install pg
+# Choose the win32 build.
+# Install PostgreSQL and put its /bin directory on your path.
+#
+# Configure Using Gemfile
+# gem 'pg'
+#
+default: &default
+ adapter: postgresql
+ encoding: unicode
+ # For details on connection pooling, see Rails configuration guide
+ # http://guides.rubyonrails.org/configuring.html#database-pooling
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+
+development:
+ <<: *default
+ database: betsy_development
+
+ # The specified database role being used to connect to postgres.
+ # To create additional roles in postgres see `$ createuser --help`.
+ # When left blank, postgres will use the default role. This is
+ # the same name as the operating system user that initialized the database.
+ #username: betsy
+
+ # The password associated with the postgres role (username).
+ #password:
+
+ # Connect on a TCP socket. Omitted by default since the client uses a
+ # domain socket that doesn't need configuration. Windows does not have
+ # domain sockets, so uncomment these lines.
+ #host: localhost
+
+ # The TCP port the server listens on. Defaults to 5432.
+ # If your server runs on a different port number, change accordingly.
+ #port: 5432
+
+ # Schema search path. The server defaults to $user,public
+ #schema_search_path: myapp,sharedapp,public
+
+ # Minimum log levels, in increasing order:
+ # debug5, debug4, debug3, debug2, debug1,
+ # log, notice, warning, error, fatal, and panic
+ # Defaults to warning.
+ #min_messages: notice
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ <<: *default
+ database: betsy_test
+
+# As with config/secrets.yml, you never want to store sensitive information,
+# like your database password, in your source code. If your source code is
+# ever seen by anyone, they now have access to your database.
+#
+# Instead, provide the password as a unix environment variable when you boot
+# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
+# for a full rundown on how to provide these environment variables in a
+# production deployment.
+#
+# On Heroku and other platform providers, you may have a full connection URL
+# available as an environment variable. For example:
+#
+# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
+#
+# You can use this database configuration with:
+#
+# production:
+# url: <%= ENV['DATABASE_URL'] %>
+#
+production:
+ <<: *default
+ database: betsy_production
+ username: betsy
+ password: <%= ENV['BETSY_DATABASE_PASSWORD'] %>
diff --git a/config/environment.rb b/config/environment.rb
new file mode 100644
index 0000000000..426333bb46
--- /dev/null
+++ b/config/environment.rb
@@ -0,0 +1,5 @@
+# Load the Rails application.
+require_relative 'application'
+
+# Initialize the Rails application.
+Rails.application.initialize!
diff --git a/config/environments/development.rb b/config/environments/development.rb
new file mode 100644
index 0000000000..5187e22186
--- /dev/null
+++ b/config/environments/development.rb
@@ -0,0 +1,54 @@
+Rails.application.configure do
+ # Settings specified here will take precedence over those in config/application.rb.
+
+ # In the development environment your application's code is reloaded on
+ # every request. This slows down response time but is perfect for development
+ # since you don't have to restart the web server when you make code changes.
+ config.cache_classes = false
+
+ # Do not eager load code on boot.
+ config.eager_load = false
+
+ # Show full error reports.
+ config.consider_all_requests_local = true
+
+ # Enable/disable caching. By default caching is disabled.
+ if Rails.root.join('tmp/caching-dev.txt').exist?
+ config.action_controller.perform_caching = true
+
+ config.cache_store = :memory_store
+ config.public_file_server.headers = {
+ 'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}"
+ }
+ else
+ config.action_controller.perform_caching = false
+
+ config.cache_store = :null_store
+ end
+
+ # Don't care if the mailer can't send.
+ config.action_mailer.raise_delivery_errors = false
+
+ config.action_mailer.perform_caching = false
+
+ # Print deprecation notices to the Rails logger.
+ config.active_support.deprecation = :log
+
+ # Raise an error on page load if there are pending migrations.
+ config.active_record.migration_error = :page_load
+
+ # Debug mode disables concatenation and preprocessing of assets.
+ # This option may cause significant delays in view rendering with a large
+ # number of complex assets.
+ config.assets.debug = true
+
+ # Suppress logger output for asset requests.
+ config.assets.quiet = true
+
+ # Raises error for missing translations
+ # config.action_view.raise_on_missing_translations = true
+
+ # Use an evented file watcher to asynchronously detect changes in source code,
+ # routes, locales, etc. This feature depends on the listen gem.
+ config.file_watcher = ActiveSupport::EventedFileUpdateChecker
+end
diff --git a/config/environments/production.rb b/config/environments/production.rb
new file mode 100644
index 0000000000..9284f84839
--- /dev/null
+++ b/config/environments/production.rb
@@ -0,0 +1,91 @@
+Rails.application.configure do
+ # Settings specified here will take precedence over those in config/application.rb.
+
+ # Code is not reloaded between requests.
+ config.cache_classes = true
+
+ # Eager load code on boot. This eager loads most of Rails and
+ # your application in memory, allowing both threaded web servers
+ # and those relying on copy on write to perform better.
+ # Rake tasks automatically ignore this option for performance.
+ config.eager_load = true
+
+ # Full error reports are disabled and caching is turned on.
+ config.consider_all_requests_local = false
+ config.action_controller.perform_caching = true
+
+ # Attempt to read encrypted secrets from `config/secrets.yml.enc`.
+ # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or
+ # `config/secrets.yml.key`.
+ config.read_encrypted_secrets = true
+
+ # Disable serving static files from the `/public` folder by default since
+ # Apache or NGINX already handles this.
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
+
+ # Compress JavaScripts and CSS.
+ config.assets.js_compressor = :uglifier
+ # config.assets.css_compressor = :sass
+
+ # Do not fallback to assets pipeline if a precompiled asset is missed.
+ config.assets.compile = false
+
+ # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
+
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
+ # config.action_controller.asset_host = 'http://assets.example.com'
+
+ # Specifies the header that your server uses for sending files.
+ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
+
+ # Mount Action Cable outside main process or domain
+ # config.action_cable.mount_path = nil
+ # config.action_cable.url = 'wss://example.com/cable'
+ # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
+
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
+ # config.force_ssl = true
+
+ # Use the lowest log level to ensure availability of diagnostic information
+ # when problems arise.
+ config.log_level = :debug
+
+ # Prepend all log lines with the following tags.
+ config.log_tags = [ :request_id ]
+
+ # Use a different cache store in production.
+ # config.cache_store = :mem_cache_store
+
+ # Use a real queuing backend for Active Job (and separate queues per environment)
+ # config.active_job.queue_adapter = :resque
+ # config.active_job.queue_name_prefix = "betsy_#{Rails.env}"
+ config.action_mailer.perform_caching = false
+
+ # Ignore bad email addresses and do not raise email delivery errors.
+ # Set this to true and configure the email server for immediate delivery to raise delivery errors.
+ # config.action_mailer.raise_delivery_errors = false
+
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
+ # the I18n.default_locale when a translation cannot be found).
+ config.i18n.fallbacks = true
+
+ # Send deprecation notices to registered listeners.
+ config.active_support.deprecation = :notify
+
+ # Use default logging formatter so that PID and timestamp are not suppressed.
+ config.log_formatter = ::Logger::Formatter.new
+
+ # Use a different logger for distributed setups.
+ # require 'syslog/logger'
+ # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
+
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
+ logger = ActiveSupport::Logger.new(STDOUT)
+ logger.formatter = config.log_formatter
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
+ end
+
+ # Do not dump schema after migrations.
+ config.active_record.dump_schema_after_migration = false
+end
diff --git a/config/environments/test.rb b/config/environments/test.rb
new file mode 100644
index 0000000000..8e5cbde533
--- /dev/null
+++ b/config/environments/test.rb
@@ -0,0 +1,42 @@
+Rails.application.configure do
+ # Settings specified here will take precedence over those in config/application.rb.
+
+ # The test environment is used exclusively to run your application's
+ # test suite. You never need to work with it otherwise. Remember that
+ # your test database is "scratch space" for the test suite and is wiped
+ # and recreated between test runs. Don't rely on the data there!
+ config.cache_classes = true
+
+ # Do not eager load code on boot. This avoids loading your whole application
+ # just for the purpose of running a single test. If you are using a tool that
+ # preloads Rails for running tests, you may have to set it to true.
+ config.eager_load = false
+
+ # Configure public file server for tests with Cache-Control for performance.
+ config.public_file_server.enabled = true
+ config.public_file_server.headers = {
+ 'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}"
+ }
+
+ # Show full error reports and disable caching.
+ config.consider_all_requests_local = true
+ config.action_controller.perform_caching = false
+
+ # Raise exceptions instead of rendering exception templates.
+ config.action_dispatch.show_exceptions = false
+
+ # Disable request forgery protection in test environment.
+ config.action_controller.allow_forgery_protection = false
+ config.action_mailer.perform_caching = false
+
+ # Tell Action Mailer not to deliver emails to the real world.
+ # The :test delivery method accumulates sent emails in the
+ # ActionMailer::Base.deliveries array.
+ config.action_mailer.delivery_method = :test
+
+ # Print deprecation notices to the stderr.
+ config.active_support.deprecation = :stderr
+
+ # Raises error for missing translations
+ # config.action_view.raise_on_missing_translations = true
+end
diff --git a/config/initializers/application_controller_renderer.rb b/config/initializers/application_controller_renderer.rb
new file mode 100644
index 0000000000..89d2efab2b
--- /dev/null
+++ b/config/initializers/application_controller_renderer.rb
@@ -0,0 +1,8 @@
+# Be sure to restart your server when you modify this file.
+
+# ActiveSupport::Reloader.to_prepare do
+# ApplicationController.renderer.defaults.merge!(
+# http_host: 'example.org',
+# https: false
+# )
+# end
diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb
new file mode 100644
index 0000000000..4b828e80cb
--- /dev/null
+++ b/config/initializers/assets.rb
@@ -0,0 +1,14 @@
+# Be sure to restart your server when you modify this file.
+
+# Version of your assets, change this if you want to expire all your assets.
+Rails.application.config.assets.version = '1.0'
+
+# Add additional assets to the asset load path.
+# Rails.application.config.assets.paths << Emoji.images_path
+# Add Yarn node_modules folder to the asset load path.
+Rails.application.config.assets.paths << Rails.root.join('node_modules')
+
+# Precompile additional assets.
+# application.js, application.css, and all non-JS/CSS in the app/assets
+# folder are already added.
+# Rails.application.config.assets.precompile += %w( admin.js admin.css )
diff --git a/config/initializers/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb
new file mode 100644
index 0000000000..59385cdf37
--- /dev/null
+++ b/config/initializers/backtrace_silencers.rb
@@ -0,0 +1,7 @@
+# Be sure to restart your server when you modify this file.
+
+# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
+# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
+
+# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
+# Rails.backtrace_cleaner.remove_silencers!
diff --git a/config/initializers/cookies_serializer.rb b/config/initializers/cookies_serializer.rb
new file mode 100644
index 0000000000..5a6a32d371
--- /dev/null
+++ b/config/initializers/cookies_serializer.rb
@@ -0,0 +1,5 @@
+# Be sure to restart your server when you modify this file.
+
+# Specify a serializer for the signed and encrypted cookie jars.
+# Valid options are :json, :marshal, and :hybrid.
+Rails.application.config.action_dispatch.cookies_serializer = :json
diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb
new file mode 100644
index 0000000000..4a994e1e7b
--- /dev/null
+++ b/config/initializers/filter_parameter_logging.rb
@@ -0,0 +1,4 @@
+# Be sure to restart your server when you modify this file.
+
+# Configure sensitive parameters which will be filtered from the log file.
+Rails.application.config.filter_parameters += [:password]
diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb
new file mode 100644
index 0000000000..ac033bf9dc
--- /dev/null
+++ b/config/initializers/inflections.rb
@@ -0,0 +1,16 @@
+# Be sure to restart your server when you modify this file.
+
+# Add new inflection rules using the following format. Inflections
+# are locale specific, and you may define rules for as many different
+# locales as you wish. All of these examples are active by default:
+# ActiveSupport::Inflector.inflections(:en) do |inflect|
+# inflect.plural /^(ox)$/i, '\1en'
+# inflect.singular /^(ox)en/i, '\1'
+# inflect.irregular 'person', 'people'
+# inflect.uncountable %w( fish sheep )
+# end
+
+# These inflection rules are supported but not enabled by default:
+# ActiveSupport::Inflector.inflections(:en) do |inflect|
+# inflect.acronym 'RESTful'
+# end
diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb
new file mode 100644
index 0000000000..dc1899682b
--- /dev/null
+++ b/config/initializers/mime_types.rb
@@ -0,0 +1,4 @@
+# Be sure to restart your server when you modify this file.
+
+# Add new mime types for use in respond_to blocks:
+# Mime::Type.register "text/richtext", :rtf
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
new file mode 100644
index 0000000000..fd4416122a
--- /dev/null
+++ b/config/initializers/omniauth.rb
@@ -0,0 +1,3 @@
+Rails.application.config.middleware.use OmniAuth::Builder do
+ provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email"
+end
diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/wrap_parameters.rb
new file mode 100644
index 0000000000..bbfc3961bf
--- /dev/null
+++ b/config/initializers/wrap_parameters.rb
@@ -0,0 +1,14 @@
+# Be sure to restart your server when you modify this file.
+
+# This file contains settings for ActionController::ParamsWrapper which
+# is enabled by default.
+
+# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
+ActiveSupport.on_load(:action_controller) do
+ wrap_parameters format: [:json]
+end
+
+# To enable root element in JSON for ActiveRecord objects.
+# ActiveSupport.on_load(:active_record) do
+# self.include_root_in_json = true
+# end
diff --git a/config/locales/en.yml b/config/locales/en.yml
new file mode 100644
index 0000000000..decc5a8573
--- /dev/null
+++ b/config/locales/en.yml
@@ -0,0 +1,33 @@
+# Files in the config/locales directory are used for internationalization
+# and are automatically loaded by Rails. If you want to use locales other
+# than English, add the necessary files in this directory.
+#
+# To use the locales, use `I18n.t`:
+#
+# I18n.t 'hello'
+#
+# In views, this is aliased to just `t`:
+#
+# <%= t('hello') %>
+#
+# To use a different locale, set it with `I18n.locale`:
+#
+# I18n.locale = :es
+#
+# This would use the information in config/locales/es.yml.
+#
+# The following keys must be escaped otherwise they will not be retrieved by
+# the default I18n backend:
+#
+# true, false, on, off, yes, no
+#
+# Instead, surround them with single quotes.
+#
+# en:
+# 'true': 'foo'
+#
+# To learn more, please read the Rails Internationalization guide
+# available at http://guides.rubyonrails.org/i18n.html.
+
+en:
+ hello: "Hello world"
diff --git a/config/puma.rb b/config/puma.rb
new file mode 100644
index 0000000000..1e19380dcb
--- /dev/null
+++ b/config/puma.rb
@@ -0,0 +1,56 @@
+# Puma can serve each request in a thread from an internal thread pool.
+# The `threads` method setting takes two numbers: a minimum and maximum.
+# Any libraries that use thread pools should be configured to match
+# the maximum value specified for Puma. Default is set to 5 threads for minimum
+# and maximum; this matches the default thread size of Active Record.
+#
+threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
+threads threads_count, threads_count
+
+# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
+#
+port ENV.fetch("PORT") { 3000 }
+
+# Specifies the `environment` that Puma will run in.
+#
+environment ENV.fetch("RAILS_ENV") { "development" }
+
+# Specifies the number of `workers` to boot in clustered mode.
+# Workers are forked webserver processes. If using threads and workers together
+# the concurrency of the application would be max `threads` * `workers`.
+# Workers do not work on JRuby or Windows (both of which do not support
+# processes).
+#
+# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
+
+# Use the `preload_app!` method when specifying a `workers` number.
+# This directive tells Puma to first boot the application and load code
+# before forking the application. This takes advantage of Copy On Write
+# process behavior so workers use less memory. If you use this option
+# you need to make sure to reconnect any threads in the `on_worker_boot`
+# block.
+#
+# preload_app!
+
+# If you are preloading your application and using Active Record, it's
+# recommended that you close any connections to the database before workers
+# are forked to prevent connection leakage.
+#
+# before_fork do
+# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
+# end
+
+# The code in the `on_worker_boot` will be called if you are using
+# clustered mode by specifying a number of `workers`. After each worker
+# process is booted, this block will be run. If you are using the `preload_app!`
+# option, you will want to use this block to reconnect to any threads
+# or connections that may have been created at application boot, as Ruby
+# cannot share connections between processes.
+#
+# on_worker_boot do
+# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
+# end
+#
+
+# Allow puma to be restarted by `rails restart` command.
+plugin :tmp_restart
diff --git a/config/routes.rb b/config/routes.rb
new file mode 100644
index 0000000000..a834b49633
--- /dev/null
+++ b/config/routes.rb
@@ -0,0 +1,52 @@
+Rails.application.routes.draw do
+ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+
+ get 'merchants/:merchant_id/orders/status/:status_id', to: 'orders#index', as: 'merchant_orders'
+
+ resources :products
+ resources :merchants
+
+ resources :orders, except: [:show]
+ resources :reviews, except: [:new]
+ root 'main#index'
+
+ resources :order_items, only: [:create, :index, :destroy, :update]
+
+ resources :categories, only: [:create, :index]
+
+ # nested routes
+ resources :categories do
+ resources :products, only: [:index]
+ end
+
+ resources :merchants do
+ resources :products, only: [:index]
+ end
+
+ resources :merchants do
+ resources :categories, only: [:index, :new]
+ end
+
+ resources :merchants do
+ resources :categories, only: [:show] do
+ resources :products, only: [:index]
+ end
+ end
+
+ get '/products/:id/reviews/new', to: 'reviews#new', as: 'new_product_review'
+
+ resources :merchants do
+ resources :orders, only: [:show]
+ end
+
+ get '/products/merchant/:id', to: 'products#index_by_merchant', as: 'products_merchant'
+
+ get '/products/category/:id', to: 'products#index_by_category', as: 'products_category'
+
+ get '/auth/:provider/callback', to: 'merchants#login', as: 'auth_callback'
+
+ get 'logout', to: 'merchants#logout', as: 'logout'
+
+ get "*path", to:'application#render_404'
+
+end
diff --git a/config/secrets.yml b/config/secrets.yml
new file mode 100644
index 0000000000..28845a5772
--- /dev/null
+++ b/config/secrets.yml
@@ -0,0 +1,32 @@
+# Be sure to restart your server when you modify this file.
+
+# Your secret key is used for verifying the integrity of signed cookies.
+# If you change this key, all old signed cookies will become invalid!
+
+# Make sure the secret is at least 30 characters and all random,
+# no regular words or you'll be exposed to dictionary attacks.
+# You can use `rails secret` to generate a secure secret key.
+
+# Make sure the secrets in this file are kept private
+# if you're sharing your code publicly.
+
+# Shared secrets are available across all environments.
+
+# shared:
+# api_key: a1B2c3D4e5F6
+
+# Environmental secrets are only available for that specific environment.
+
+development:
+ secret_key_base: 6ad4cb09f1f6310238665bbedf21d3df757d2f4131527195e7c378acd15ece9e0f1ddb2b7a8856621109d116ef1854cf34d293c897dcdcc2929c8311389fd4d8
+
+test:
+ secret_key_base: 0f85486780575c561a663d649687d640df12955e544d74a258158c05c6b7ba8a808323a7e7f04e93d54253d37a1e0a572f33482e33443adf6b27bf08f1609190
+
+# Do not keep production secrets in the unencrypted secrets file.
+# Instead, either read values from the environment.
+# Or, use `bin/rails secrets:setup` to configure encrypted secrets
+# and move the `production:` environment over there.
+
+production:
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
diff --git a/config/spring.rb b/config/spring.rb
new file mode 100644
index 0000000000..c9119b40c0
--- /dev/null
+++ b/config/spring.rb
@@ -0,0 +1,6 @@
+%w(
+ .ruby-version
+ .rbenv-vars
+ tmp/restart.txt
+ tmp/caching-dev.txt
+).each { |path| Spring.watch(path) }
diff --git a/db/migrate/20171017230130_create_merchants.rb b/db/migrate/20171017230130_create_merchants.rb
new file mode 100644
index 0000000000..64a57c904c
--- /dev/null
+++ b/db/migrate/20171017230130_create_merchants.rb
@@ -0,0 +1,10 @@
+class CreateMerchants < ActiveRecord::Migration[5.1]
+ def change
+ create_table :merchants do |t|
+ t.string :username
+ t.string :email
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171017231310_create_products.rb b/db/migrate/20171017231310_create_products.rb
new file mode 100644
index 0000000000..e28175d8ae
--- /dev/null
+++ b/db/migrate/20171017231310_create_products.rb
@@ -0,0 +1,15 @@
+class CreateProducts < ActiveRecord::Migration[5.1]
+ def change
+ create_table :products do |t|
+ t.string :name
+ t.float :price
+ t.integer :stock
+ t.boolean :retired
+ t.text :description
+ t.string :image_url
+ t.integer :merchant_id
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171017232857_create_order_items.rb b/db/migrate/20171017232857_create_order_items.rb
new file mode 100644
index 0000000000..1b14bf865b
--- /dev/null
+++ b/db/migrate/20171017232857_create_order_items.rb
@@ -0,0 +1,11 @@
+class CreateOrderItems < ActiveRecord::Migration[5.1]
+ def change
+ create_table :order_items do |t|
+ t.integer :quantity
+ t.integer :product_id
+ t.integer :order_id
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171018001235_create_orders.rb b/db/migrate/20171018001235_create_orders.rb
new file mode 100644
index 0000000000..93ffcec7ea
--- /dev/null
+++ b/db/migrate/20171018001235_create_orders.rb
@@ -0,0 +1,15 @@
+class CreateOrders < ActiveRecord::Migration[5.1]
+ def change
+ create_table :orders do |t|
+ t.string :status
+ t.string :customer_name
+ t.string :customer_email
+ t.string :customer_address
+ t.string :cc_number
+ t.date :cc_exipration
+ t.string :cc_ccv
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171018045939_spelling_error.rb b/db/migrate/20171018045939_spelling_error.rb
new file mode 100644
index 0000000000..c91828a1d4
--- /dev/null
+++ b/db/migrate/20171018045939_spelling_error.rb
@@ -0,0 +1,5 @@
+class SpellingError < ActiveRecord::Migration[5.1]
+ def change
+ rename_column :orders, :cc_exipration, :cc_expiration
+ end
+end
diff --git a/db/migrate/20171018052441_add_zipcode.rb b/db/migrate/20171018052441_add_zipcode.rb
new file mode 100644
index 0000000000..d426c9a4ff
--- /dev/null
+++ b/db/migrate/20171018052441_add_zipcode.rb
@@ -0,0 +1,5 @@
+class AddZipcode < ActiveRecord::Migration[5.1]
+ def change
+ add_column :orders, :zip_code, :string
+ end
+end
diff --git a/db/migrate/20171018224742_create_reviews.rb b/db/migrate/20171018224742_create_reviews.rb
new file mode 100644
index 0000000000..14b85f5be4
--- /dev/null
+++ b/db/migrate/20171018224742_create_reviews.rb
@@ -0,0 +1,10 @@
+class CreateReviews < ActiveRecord::Migration[5.1]
+ def change
+ create_table :reviews do |t|
+ t.integer :product_id
+ t.string :text
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171019021137_create_categories.rb b/db/migrate/20171019021137_create_categories.rb
new file mode 100644
index 0000000000..5bef4913b8
--- /dev/null
+++ b/db/migrate/20171019021137_create_categories.rb
@@ -0,0 +1,9 @@
+class CreateCategories < ActiveRecord::Migration[5.1]
+ def change
+ create_table :categories do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171019021545_create_categories_products_join_table.rb b/db/migrate/20171019021545_create_categories_products_join_table.rb
new file mode 100644
index 0000000000..30c328c6eb
--- /dev/null
+++ b/db/migrate/20171019021545_create_categories_products_join_table.rb
@@ -0,0 +1,5 @@
+class CreateCategoriesProductsJoinTable < ActiveRecord::Migration[5.1]
+ def change
+ create_join_table :categories, :products
+ end
+end
diff --git a/db/migrate/20171019043601_add_uid_and_provider_to_merchant.rb b/db/migrate/20171019043601_add_uid_and_provider_to_merchant.rb
new file mode 100644
index 0000000000..80e3da689f
--- /dev/null
+++ b/db/migrate/20171019043601_add_uid_and_provider_to_merchant.rb
@@ -0,0 +1,6 @@
+class AddUidAndProviderToMerchant < ActiveRecord::Migration[5.1]
+ def change
+ add_column :merchants, :provider, :string, null: false
+ add_column :merchants, :uid, :integer, null: false
+ end
+end
diff --git a/db/migrate/20171021051351_create_products_categories_join.rb b/db/migrate/20171021051351_create_products_categories_join.rb
new file mode 100644
index 0000000000..6ef2b60ebd
--- /dev/null
+++ b/db/migrate/20171021051351_create_products_categories_join.rb
@@ -0,0 +1,10 @@
+class CreateProductsCategoriesJoin < ActiveRecord::Migration[5.1]
+ def change
+ create_table :products_categories_joins do |t|
+ t.belongs_to :product, index: true
+ t.belongs_to :category, index: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20171023181341_add_status_to_order_items.rb b/db/migrate/20171023181341_add_status_to_order_items.rb
new file mode 100644
index 0000000000..71ac6b4fa0
--- /dev/null
+++ b/db/migrate/20171023181341_add_status_to_order_items.rb
@@ -0,0 +1,5 @@
+class AddStatusToOrderItems < ActiveRecord::Migration[5.1]
+ def change
+ add_column :order_items, :status, :string
+ end
+end
diff --git a/db/migrate/20171023181913_add_rating_to_reviews.rb b/db/migrate/20171023181913_add_rating_to_reviews.rb
new file mode 100644
index 0000000000..8d3a2febcc
--- /dev/null
+++ b/db/migrate/20171023181913_add_rating_to_reviews.rb
@@ -0,0 +1,5 @@
+class AddRatingToReviews < ActiveRecord::Migration[5.1]
+ def change
+ add_column :reviews, :rating, :integer
+ end
+end
diff --git a/db/migrate/20171025005108_change_product_price_data_type_to_int.rb b/db/migrate/20171025005108_change_product_price_data_type_to_int.rb
new file mode 100644
index 0000000000..84ff6e3fa7
--- /dev/null
+++ b/db/migrate/20171025005108_change_product_price_data_type_to_int.rb
@@ -0,0 +1,5 @@
+class ChangeProductPriceDataTypeToInt < ActiveRecord::Migration[5.1]
+ def change
+ change_column :products, :price, :integer
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644
index 0000000000..063fea0ca8
--- /dev/null
+++ b/db/schema.rb
@@ -0,0 +1,89 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 20171025005108) do
+
+ # These are extensions that must be enabled in order to support this database
+ enable_extension "plpgsql"
+
+ create_table "categories", force: :cascade do |t|
+ t.string "name"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+ create_table "categories_products", id: false, force: :cascade do |t|
+ t.bigint "category_id", null: false
+ t.bigint "product_id", null: false
+ end
+
+ create_table "merchants", force: :cascade do |t|
+ t.string "username"
+ t.string "email"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "provider", null: false
+ t.integer "uid", null: false
+ end
+
+ create_table "order_items", force: :cascade do |t|
+ t.integer "quantity"
+ t.integer "product_id"
+ t.integer "order_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "status"
+ end
+
+ create_table "orders", force: :cascade do |t|
+ t.string "status"
+ t.string "customer_name"
+ t.string "customer_email"
+ t.string "customer_address"
+ t.string "cc_number"
+ t.date "cc_expiration"
+ t.string "cc_ccv"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.string "zip_code"
+ end
+
+ create_table "products", force: :cascade do |t|
+ t.string "name"
+ t.integer "price"
+ t.integer "stock"
+ t.boolean "retired"
+ t.text "description"
+ t.string "image_url"
+ t.integer "merchant_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+ create_table "products_categories_joins", force: :cascade do |t|
+ t.bigint "product_id"
+ t.bigint "category_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["category_id"], name: "index_products_categories_joins_on_category_id"
+ t.index ["product_id"], name: "index_products_categories_joins_on_product_id"
+ end
+
+ create_table "reviews", force: :cascade do |t|
+ t.integer "product_id"
+ t.string "text"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.integer "rating"
+ end
+
+end
diff --git a/db/seed_data/category_seeds.csv b/db/seed_data/category_seeds.csv
new file mode 100644
index 0000000000..f95e338a72
--- /dev/null
+++ b/db/seed_data/category_seeds.csv
@@ -0,0 +1,3 @@
+id,name
+1,small birds
+2,large birds
diff --git a/db/seed_data/merchant_seeds.csv b/db/seed_data/merchant_seeds.csv
new file mode 100644
index 0000000000..246a891ca3
--- /dev/null
+++ b/db/seed_data/merchant_seeds.csv
@@ -0,0 +1,4 @@
+id,username,email,uid,provider
+3000,Julia,julia@email.com,123,github
+3001,Rebecca,rebecca@email.com,234,github
+3002,Lindsey,lindsey@email.com,345,github
diff --git a/db/seed_data/product_seeds.csv b/db/seed_data/product_seeds.csv
new file mode 100644
index 0000000000..a97161abe4
--- /dev/null
+++ b/db/seed_data/product_seeds.csv
@@ -0,0 +1,12 @@
+id,name,price,stock,description,retired,image_url,merchant_id
+
+3001,Goldfinch,2500,2,"The American goldfinch is a small North American bird in the finch family. It is migratory, ranging from mid-Alberta to North Carolina during the breeding season, and from just south of the Canada–United States border to Mexico during the winter.",false,"Goldfinch.jpg",3000
+3002,Blue Jay,3000,5,"The blue jay is a passerine bird in the family Corvidae, native to North America. It is resident through most of eastern and central United States, although western populations may be migratory.",false,"BlueJay.jpg",3000
+3003,Canary,2000,1,"The domestic canary, often simply known as the canary, is a domesticated form of the wild canary, a small songbird in the finch family originating from the Macaronesian Islands. Canaries were first bred in captivity in the 17th century.",false,"Canary.jpg",3000
+3004,Cockatoo,3500,2,"A cockatoo is a parrot that is any of the 21 species belonging to the bird family Cacatuidae, the only family in the superfamily Cacatuoidea.",false,"Cockatoo.jpg",3001
+3005,Pelican,4000,6,"Pelicans are a genus of large water birds that makes up the family Pelecanidae. They are characterised by a long beak and a large throat pouch used for catching prey and draining water from the scooped up contents before swallowing.",false,"Pelican.jpg",3001
+3006,Flamingo,5500,1,"Flamingos or flamingoes are a type of wading bird in the family Phoenicopteridae, the only bird family in the order Phoenicopteriformes.",false,"Flamingo.jpg",1
+3007,Hawk,6500,2,"Hawks are a group of medium-sized diurnal birds of prey of the family Accipitridae. Hawks are widely distributed and vary greatly in size.",false,"Hawk.jpg",3001
+3008,Eagle,15000,4,"Eagle is a common name for many large birds of prey of the family Accipitridae; it belongs to several groups of genera that are not necessarily closely related to each other.",false,"Eagle.jpg",3002
+3009,Owl,13000,2,"Owls are birds from the order Strigiformes, which includes about 200 species of mostly solitary and nocturnal birds of prey typified by an upright stance, a large, broad head, binocular vision, binaural hearing, sharp talons, and feathers adapted for silent flight.",false,"Owl.jpg",3002
+3010,Hummingbird,4800,7,"Hummingbirds are birds from the Americas that constitute the family Trochilidae. They are among the smallest of birds, most species measuring 7.5–13 cm in length.",false,"Hummingbird.jpg",3002
diff --git a/db/seeds.rb b/db/seeds.rb
new file mode 100644
index 0000000000..fa06edfae3
--- /dev/null
+++ b/db/seeds.rb
@@ -0,0 +1,94 @@
+require 'csv'
+
+# This file should contain all the record creation needed to seed the database with its default values.
+# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
+#
+# Examples:
+#
+# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
+# Character.create(name: 'Luke', movie: movies.first)
+MERCHANT_FILE = Rails.root.join('db', 'seed_data', 'merchant_seeds.csv')
+puts "Loading raw merchant data from #{MERCHANT_FILE}"
+
+merchant_failures = []
+CSV.foreach(MERCHANT_FILE, :headers => true) do |row|
+ merchant = Merchant.new
+ merchant.id = row['id']
+ merchant.username = row['username']
+ merchant.email = row['email']
+ merchant.uid = row['uid']
+ merchant.provider = row['provider']
+ puts "Created merchant: #{merchant.inspect}"
+ successful = merchant.save
+ if !successful
+ merchant_failures << merchant
+ end
+end
+
+puts "Added #{Merchant.count} merchant records"
+puts "#{merchant_failures.length} merchants failed to save"
+
+
+
+PRODUCT_FILE = Rails.root.join('db', 'seed_data', 'product_seeds.csv')
+puts "Loading raw driver data from #{PRODUCT_FILE}"
+
+product_failures = []
+CSV.foreach(PRODUCT_FILE, :headers => true) do |row|
+ product = Product.new
+ product.id = row['id']
+ product.name = row['name']
+ product.price = row['price']
+ product.stock = row['stock']
+ product.description = row['description']
+ product.retired = row['retired']
+ product.image_url = row['image_url']
+ product.merchant_id = row['merchant_id']
+ puts "Created product: #{product.inspect}"
+ successful = product.save
+ if !successful
+ product_failures << product
+ end
+end
+
+puts "Added #{Product.count} product records"
+puts "#{product_failures.length} products failed to save"
+
+CATEGORY_FILE = Rails.root.join('db', 'seed_data', 'category_seeds.csv')
+puts "Loading raw categories products data from #{CATEGORY_FILE}"
+
+categories_failures = []
+CSV.foreach(CATEGORY_FILE, :headers => true) do |row|
+ category = Category.new
+ category.id = row['id']
+ category.name = row['name']
+ puts "Created category: #{category.inspect}"
+ successful = category.save
+ if !successful
+ categories_failures << category
+ end
+end
+
+puts "Added #{Category.count} categories records"
+puts "#{categories_failures.length} categories failed to save"
+
+#### Making categories products seeds data###
+category1 = Category.first
+category2 = Category.last
+category1.products << Product.first
+category1.products << Product.last
+category2.products << Product.all[6]
+category2.products << Product.all[7]
+
+category1.products.each do |product|
+ puts "#{category1.name} has #{product.name}"
+end
+category2.products.each do |product|
+ puts "#{category2.name} has #{product.name}"
+end
+
+ActiveRecord::Base.connection.tables.each do |t|
+ ActiveRecord::Base.connection.reset_pk_sequence!(t)
+end
+# puts "Added #{CategoriesProducts.count} categories products records"
+# puts "#{categories_failures.length} categories products failed to save"
diff --git a/lib/assets/.keep b/lib/assets/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/lib/tasks/.keep b/lib/tasks/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/log/.keep b/log/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/package.json b/package.json
new file mode 100644
index 0000000000..f874acf437
--- /dev/null
+++ b/package.json
@@ -0,0 +1,5 @@
+{
+ "name": "betsy",
+ "private": true,
+ "dependencies": {}
+}
diff --git a/public/404.html b/public/404.html
new file mode 100644
index 0000000000..2be3af26fc
--- /dev/null
+++ b/public/404.html
@@ -0,0 +1,67 @@
+
+
+
+ The page you were looking for doesn't exist (404)
+
+
+
+
+
+
+
+
+
The page you were looking for doesn't exist.
+
You may have mistyped the address or the page may have moved.
+
+
If you are the application owner check the logs for more information.
+
+
+
diff --git a/public/422.html b/public/422.html
new file mode 100644
index 0000000000..c08eac0d1d
--- /dev/null
+++ b/public/422.html
@@ -0,0 +1,67 @@
+
+
+
+ The change you wanted was rejected (422)
+
+
+
+
+
+
+
+
+
The change you wanted was rejected.
+
Maybe you tried to change something you didn't have access to.
+
+
If you are the application owner check the logs for more information.
+
+
+
diff --git a/public/500.html b/public/500.html
new file mode 100644
index 0000000000..78a030af22
--- /dev/null
+++ b/public/500.html
@@ -0,0 +1,66 @@
+
+
+
+ We're sorry, but something went wrong (500)
+
+
+
+
+
+
+
+
+
We're sorry, but something went wrong.
+
+
If you are the application owner check the logs for more information.
+
+
+
diff --git a/public/apple-touch-icon-precomposed.png b/public/apple-touch-icon-precomposed.png
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/public/apple-touch-icon.png b/public/apple-touch-icon.png
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/public/favicon.ico b/public/favicon.ico
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/public/robots.txt b/public/robots.txt
new file mode 100644
index 0000000000..37b576a4a0
--- /dev/null
+++ b/public/robots.txt
@@ -0,0 +1 @@
+# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb
new file mode 100644
index 0000000000..d19212abd5
--- /dev/null
+++ b/test/application_system_test_case.rb
@@ -0,0 +1,5 @@
+require "test_helper"
+
+class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
+ driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
+end
diff --git a/test/controllers/.keep b/test/controllers/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/controllers/categories_controller_test.rb b/test/controllers/categories_controller_test.rb
new file mode 100644
index 0000000000..216db31028
--- /dev/null
+++ b/test/controllers/categories_controller_test.rb
@@ -0,0 +1,145 @@
+require "test_helper"
+
+describe CategoriesController do
+ describe "correct logged in user" do
+ before do
+ @merchant = merchants(:eva)
+ login(@merchant)
+ end
+
+ describe "new" do
+ it "gets new and returns a success status for the new page" do
+ get new_merchant_category_path(@merchant.id)
+ must_respond_with :success
+ end
+ end
+
+ describe "index" do
+ it "succeeds when there are products" do
+ get merchant_categories_path(@merchant.id)
+ must_respond_with :success
+ end
+
+ it "succeeds when there are no products" do
+ Product.destroy_all
+ get merchant_categories_path(@merchant.id)
+ must_respond_with :success
+ end
+
+ it "succeeds when there are no categories" do
+ Category.destroy_all
+ get merchant_categories_path(@merchant.id)
+ must_respond_with :success
+ end
+ end
+
+ describe "create" do
+ it "redirects to merchant categories page when the category data is valid" do
+ category_data = {
+ category: {
+ name: "new_category",
+ }
+ }
+
+ Category.new(category_data[:category]).must_be :valid?
+
+ start_category_count = Category.count
+
+ post categories_path, params: category_data
+
+ must_respond_with :redirect
+ must_redirect_to merchant_categories_path(@merchant.id)
+ Category.count.must_equal start_category_count + 1
+ end
+
+ it "redirects to form page when given category name is not unique" do
+ category_data = {
+ category: {
+ name: Category.first.name,
+ }
+ }
+
+ Category.new(category_data[:category]).wont_be :valid?
+
+ start_category_count = Category.count
+
+ post categories_path, params: category_data
+
+ must_respond_with :redirect
+ must_redirect_to new_merchant_category_path(@merchant.id)
+ Category.count.must_equal start_category_count
+ end
+
+ it "redirects to form page when the category data is invalid" do
+ invalid_category_data = {
+ category: {
+ name: ""
+ }
+ }
+
+ Category.new(invalid_category_data[:category]).wont_be :valid?
+
+ start_category_count = Category.count
+ post categories_path, params: invalid_category_data
+
+ must_respond_with :redirect
+ must_redirect_to new_merchant_category_path(@merchant.id)
+
+ Category.count.must_equal start_category_count
+ end
+ end
+ end
+
+ describe "Wrong logged in users" do
+ before do
+ @different_merchant = merchants(:eva)
+ @wrong_merchant = merchants(:emma)
+ login(@wrong_merchant)
+ end
+ describe "new" do
+ it "cannot access another merchant's categories new page" do
+ #wrong_merchant is logged in and trying to create a new category as different_merchant (should trigger allowed_user method's flash messages)
+ get new_merchant_category_path(@different_merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ flash[:status].must_equal :failure
+ end
+ end
+
+ describe "index" do
+ it "cannot access another merchant's categories index page" do
+ # wrong user is logged in and trying to access a different merchant's index page
+ get merchant_categories_path(@different_merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ flash[:status].must_equal :failure
+ end
+ end
+ end
+
+
+ describe "Guest users" do
+ describe "new" do
+ it "cannot access a merchant's categories new page" do
+ #this merchant is not logged in (i.e. testing as a guest)
+ merchant = merchants(:eva)
+ get new_merchant_category_path(merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "You must be logged in to do that"
+ flash[:status].must_equal :failure
+ end
+ end
+
+ describe "index" do
+ it "cannot access index page" do
+ @merchant = merchants(:eva)
+ get merchant_categories_path(@merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "You must be logged in to do that"
+ flash[:status].must_equal :failure
+ end
+ end
+ end
+
+
+end
diff --git a/test/controllers/main_controller_test.rb b/test/controllers/main_controller_test.rb
new file mode 100644
index 0000000000..a5d6eb1803
--- /dev/null
+++ b/test/controllers/main_controller_test.rb
@@ -0,0 +1,23 @@
+require "test_helper"
+
+describe MainController do
+ describe "index" do
+ it "gets new and returns a success status for the new page" do
+ get root_path
+ must_respond_with :success
+ end
+
+ it "must work if there are no products in the database" do
+ Product.destroy_all
+ get root_path
+ must_respond_with :success
+ end
+
+ #TODO it "must work if there is between 0 and 6 Products"
+ #right now this will fail, but we decided we don't care.
+ #it will be able to deploy with an empty database,
+ #and then we will seed the database to have more thant
+ #6 products.
+
+ end
+end
diff --git a/test/controllers/merchants_controller_test.rb b/test/controllers/merchants_controller_test.rb
new file mode 100644
index 0000000000..defc66e933
--- /dev/null
+++ b/test/controllers/merchants_controller_test.rb
@@ -0,0 +1,145 @@
+require "test_helper"
+
+describe MerchantsController do
+
+ describe "Wrong logged in users" do
+ describe "show" do
+ it "redirects to root path when a merchant tries to access another merchant's show page" do
+ different_merchant = merchants(:eva)
+ wrong_merchant = merchants(:emma)
+ login(wrong_merchant)
+ get merchant_path(different_merchant)
+ must_redirect_to root_path
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ flash[:status].must_equal :failure
+ end
+ end
+ end
+
+ describe "Correct logged in user" do
+ describe "show" do
+ it "succeeds for a valid merchant ID from the correctly logged in user" do
+ merchant = merchants(:eva)
+ login(merchant)
+ get merchant_path(merchant)
+ must_respond_with :success
+ end
+
+ it "renders 404 not_found for a bogus product ID" do
+ merchant = merchants(:eva)
+ login(merchant)
+ bogus_merchant_id = Merchant.last.id + 1
+ get merchant_path(bogus_merchant_id)
+ must_respond_with :not_found
+ end
+ end
+ end
+
+describe "Guest" do
+ describe "show" do
+ it "cannot access a merchant's show page" do
+ #this merchant is not logged in (i.e. testing as a guest)
+ merchant = merchants(:eva)
+ get merchant_path(merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "You must be logged in to do that"
+ flash[:status].must_equal :failure
+ end
+
+
+ end
+end
+
+ describe "auth_callback" do
+ describe "login" do
+ it "logs in an existing merchant and redirects to the root path" do
+ start_count = Merchant.count
+
+ merchant = merchants(:eva)
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+
+ get auth_callback_path(:github)
+
+ flash[:status].must_equal :success
+ flash[:message].must_equal "Logged in successfully as existing merchant #{merchant.username}"
+
+ must_respond_with :redirect
+ must_redirect_to root_path
+ session[:merchant_id].must_equal merchant.id
+ Merchant.count.must_equal start_count
+ end
+
+ it "creates an account for a new user and redirects to the root path" do
+ start_count = Merchant.count
+
+ merchant = Merchant.new(provider: "github", uid: 456, username: "new_merchant", email: "new@mail.com")
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+ get auth_callback_path(:github)
+
+ must_respond_with :redirect
+ must_redirect_to root_path
+ flash[:status].must_equal :success
+ flash[:message].must_equal "Logged in successfully as new merchant #{merchant.username}"
+
+ session[:merchant_id].must_equal Merchant.last.id
+ Merchant.count.must_equal start_count + 1
+
+ end
+
+ it "tells the merchant that they're already logged in if that is true" do
+ start_count = Merchant.count
+
+ merchant = merchants(:eva)
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+ get auth_callback_path(:github)
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+ get auth_callback_path(:github)
+
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "You're already logged in"
+
+ must_respond_with :redirect
+ must_redirect_to root_path
+ Merchant.count.must_equal start_count
+
+ end
+
+ it "redirects to the root path if given invalid user data" do
+ start_count = Merchant.count
+
+ merchant = Merchant.new(username: "new_merchant", email: "new@mail.com")
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+ get auth_callback_path(:github)
+
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Could not log in"
+
+ must_respond_with :redirect
+ must_redirect_to root_path
+ session[:merchant_id].must_equal nil
+ Merchant.count.must_equal start_count
+ end
+ end
+ end
+
+ describe "logout" do
+ it "logs out merchant and redirects to the root path" do
+ merchant = merchants(:eva)
+
+ login(merchant)
+ get logout_path
+
+ flash[:status].must_equal :success
+ flash[:message].must_equal "Successfully logged out"
+
+ must_respond_with :redirect
+ must_redirect_to root_path
+ session[:merchant_id].must_equal nil
+ end
+ end
+end
diff --git a/test/controllers/order_items_controller_test.rb b/test/controllers/order_items_controller_test.rb
new file mode 100644
index 0000000000..da78f8b198
--- /dev/null
+++ b/test/controllers/order_items_controller_test.rb
@@ -0,0 +1,271 @@
+require "test_helper"
+
+describe OrderItemsController do
+
+
+ describe "create" do
+ it "creates a new order_item in the DB with valid data and puts it in a new order if there is not a pending order" do
+ OrderItem.destroy_all
+ #Arrange
+ #order_items start with just a product and a quantity
+ order_data = {
+ order_item: {
+ #order: orders(:one),
+ product_id: products(:one).id,
+ quantity: 1,
+ status: nil
+ }
+ }
+ start_order_item_count = OrderItem.count
+
+ #Act
+ post order_items_path, params: order_data
+ #When an order_item is created the cart is assigned
+
+ #Assert
+ #1. gave the order a cart
+ OrderItem.first.order_id.wont_equal nil
+ #2. set the session order id
+ session[:order_id].must_equal OrderItem.first.order.id
+ #3. sent the user to the right place
+ must_respond_with :redirect
+ must_redirect_to order_items_path
+ #added the order item to database
+ OrderItem.count.must_equal start_order_item_count + 1
+ end
+
+
+ it "creates a new order_item with valid data and puts it in a old order it there is already a pending order" do
+ OrderItem.destroy_all
+ Order.destroy_all
+ #Arrange
+ order_data1 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: 1,
+ status: nil
+ }
+ }
+ order_data2 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:two).id,
+ quantity: 2,
+ status: nil
+ }
+ }
+
+ #Act
+ post order_items_path, params: order_data1
+ post order_items_path, params: order_data2
+
+ #Assert
+ #1. made two order items
+ OrderItem.first.wont_equal OrderItem.last.quantity
+ OrderItem.count.must_equal 2
+ #2. put them in the same cart
+ OrderItem.first.order_id.must_equal OrderItem.last.order_id
+ Order.count.must_equal 1
+ #3. set the session order id to that cart id
+ session[:order_id].must_equal OrderItem.first.order.id
+ #4. sent the user to the right place
+ must_respond_with :redirect
+ must_redirect_to order_items_path
+ end
+
+ it "updates a order_item with additional quantity if given a repeated order_item, within the same order" do
+
+ OrderItem.destroy_all
+ Order.destroy_all
+ #Arrange
+ order_data1 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: 1,
+ status: nil
+ }
+ }
+ order_data2 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: 2,
+ status: nil
+ }
+ }
+
+ #Act
+ post order_items_path, params: order_data1
+ post order_items_path, params: order_data2
+
+ #Assert
+ #1. made one order_item
+ OrderItem.count.must_equal 1
+ #2. Updated the quantity to reflect the right amount
+ OrderItem.first.quantity.must_equal 3
+ #3. put it(them) in the same cart
+ Order.count.must_equal 1
+ #4. set the session order id to that cart id
+ session[:order_id].must_equal OrderItem.first.order.id
+ #5. sent the user to the right place
+ must_respond_with :redirect
+ must_redirect_to order_items_path
+ end
+
+ it "updates a order_item with max quantity in stock if given a repeated order_item that requests more quantity than is in stock, within the same pending order" do
+ OrderItem.destroy_all
+ Order.destroy_all
+ #Arrange
+ order_data1 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: 1,
+ status: nil
+ }
+ }
+ order_data2 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: 100,
+ status: nil
+ }
+ }
+
+ #Act
+ post order_items_path, params: order_data1
+ post order_items_path, params: order_data2
+
+ #Assert
+ #1. made one order_item
+ OrderItem.count.must_equal 1
+ #2. Updated the quantity to reflect the max amount
+ OrderItem.first.quantity.must_equal 3
+ #3. put it(them) in the same cart
+ Order.count.must_equal 1
+ #4. set the session order id to that cart id
+ session[:order_id].must_equal OrderItem.first.order.id
+ #5. sent the user to the right place
+ must_respond_with :redirect
+ must_redirect_to order_items_path
+ end
+
+ it "will not create an order item with bogus data" do
+ OrderItem.destroy_all
+ Order.destroy_all
+ #Arrange
+ order_data1 = {
+ order_item: {
+ #order_id: orders(:one).id,
+ product_id: products(:one).id,
+ quantity: -1,
+ status: nil
+ }
+ }
+ #Act
+ post order_items_path, params: order_data1
+
+ #Assert
+ flash[:status].must_equal :failure
+ must_redirect_to root_path
+ end
+
+ #TODO:it "does not you to create an order item for a a retired item"
+ #This is no longer nessesary(?) because there is no longer a "add to cart" button on the product show page if the product is retired
+ #end
+ end
+
+ describe "index" do
+ it "succeeds when there are order items" do
+ get order_items_path
+ must_respond_with :success
+ end
+
+ it "succeeds when there are no order items" do
+ OrderItem.destroy_all
+ get order_items_path
+ must_respond_with :success
+ end
+ end
+
+ describe "update" do
+ it "updates the quatntity of a existing order" do
+ #Arrange
+ OrderItem.destroy_all
+ order_data = {
+ order_item: {
+ order: orders(:one),
+ product_id: products(:one).id,
+ quantity: 1,
+ status: nil
+ }
+ }
+ new_order_item = OrderItem.create!(order_data[:order_item])
+ order_data_update = {
+ order_item: {
+ quantity: 4
+ }
+ }
+
+ #Act
+ patch order_item_path(new_order_item), params: order_data_update
+
+ #Assert
+ OrderItem.count.must_equal 1
+ OrderItem.first.quantity.must_equal 4
+ flash[:status].must_equal :success
+ must_respond_with :redirect
+ end
+
+ it "does not update if attempting to update with bogus parameters" do
+ #Arrange
+ OrderItem.destroy_all
+ order_data = {
+ order_item: {
+ order: orders(:one),
+ product_id: products(:one).id,
+ quantity: 2,
+ status: nil
+ }
+ }
+ new_order_item = OrderItem.create!(order_data[:order_item])
+ order_data_update = {
+ order_item: {
+ order: orders(:one),
+ quantity: -4
+ }
+ }
+
+ #Act
+ patch order_item_path(new_order_item), params: order_data_update
+
+ #Assert
+ OrderItem.count.must_equal 1
+ OrderItem.first.quantity.must_equal 2
+ flash[:status].must_equal :failure
+ must_respond_with :redirect
+ end
+ end
+
+ describe "destroy" do
+ it "succeeds for an extant order item ID" do
+ order_item_id = OrderItem.first
+ id = order_item_id.id
+ delete order_item_path(order_item_id)
+ flash[:status].must_equal :success
+ assert_nil OrderItem.find_by(id: id)
+ must_redirect_to order_items_path
+ end
+
+ it "fails for a non extant order item ID" do
+ bogus_order_item_id = OrderItem.last.id + 1
+
+ delete order_item_path(bogus_order_item_id)
+ flash[:status].must_equal :failure
+ must_redirect_to order_items_path
+ end
+ end
+end
diff --git a/test/controllers/orders_controller_test.rb b/test/controllers/orders_controller_test.rb
new file mode 100644
index 0000000000..e619d5f510
--- /dev/null
+++ b/test/controllers/orders_controller_test.rb
@@ -0,0 +1,159 @@
+require "test_helper"
+
+describe OrdersController do
+ describe "login tests" do
+ before do
+ @merchant = merchants(:eva)
+ @user = merchants(:emma)
+ login(@merchant)
+ end
+
+ describe "index" do
+ it "returns success for all orders" do
+ get merchant_orders_path(@merchant.id, "all")
+ must_respond_with :success
+ end
+
+ it "returns success for orders with status paid" do
+ get merchant_orders_path(@merchant.id, "paid")
+ must_respond_with :success
+ end
+
+ it "returns success for orders with status pending" do
+ get merchant_orders_path(@merchant.id, "pending")
+ must_respond_with :success
+ end
+
+ it "not allows other users to see orders page" do
+ get merchant_orders_path(@user.id, "all")
+ must_respond_with :redirect
+ must_redirect_to root_path
+ end
+ end
+ end
+
+ describe "primary pages that not requires login" do
+ before do
+ @merchant = merchants(:eva)
+ @order = orders(:one)
+ end
+
+ describe "index" do
+ it "requires login in order to be able to see orders page" do
+ get merchant_orders_path(@merchant.id, "all")
+ must_respond_with :redirect
+ must_redirect_to root_path
+ end
+ end
+
+ describe "merchant show page" do
+ it "gets show page for merchant order" do
+ get merchant_order_path(@merchant.id, @order.id)
+ must_respond_with :success
+ end
+
+ it "must not find invalid show page for merchant order" do
+ get merchant_order_path(merchants(:no_orders).id, orders(:two).id)
+ must_respond_with :bad_request
+ end
+ end
+
+ it "must get edit page" do
+ get edit_order_path(orders(:two).id)
+ assert_response :success
+ end
+
+ it "must not find an invalid edit page" do
+ get edit_order_path(Order.last.id + 1)
+ must_respond_with :not_found
+ end
+
+ end
+
+ describe "create" do
+ it "creates a new order with no data" do
+ order_data = {
+ order: {
+ status: "pending"
+ }
+ }
+ start_count = Order.count
+ post orders_path, params: order_data
+
+ must_respond_with :redirect
+ must_redirect_to orders_path
+ Order.count.must_equal start_count + 1
+ end
+ end
+
+ describe "update" do
+ it "places order when all the information filled in" do
+ order = Order.first
+ order_data = {
+ id: order.id,
+ order: {
+ status: "pending",
+ customer_name: "creator 1",
+ customer_email: "john@gmail.com",
+ customer_address: "123 5th ave",
+ cc_number: "12312312312",
+ cc_expiration: "2019-10-10",
+ cc_ccv: "907",
+ zip_code: "98101"
+ }
+ }
+ patch order_path(order), params: order_data
+ order = Order.first
+ order.customer_email.must_equal "john@gmail.com"
+ end
+
+ it "don't place order when not all the required fields are filled in" do
+ order = Order.last
+ invalid_order_data = {
+ id: order.id,
+ order: {
+ status: "pending",
+ customer_name: "creator 2",
+ customer_email: "",
+ customer_address: "",
+ cc_number: "",
+ cc_expiration: "",
+ cc_ccv: "",
+ zip_code: ""
+ }
+ }
+ patch order_path(order), params: invalid_order_data
+ order = Order.last
+ order.customer_name.wont_equal "creator 2"
+ end
+
+ it "reduces the stock of the product by placing an order" do
+ order = orders(:two)
+ order_data = {
+ id: order.id,
+ order: {
+ status: "pending",
+ customer_name: "creator 323",
+ customer_email: "john@gmail.com",
+ customer_address: "123 5th ave",
+ cc_number: "12312312312",
+ cc_expiration: "2019-10-10",
+ cc_ccv: "907",
+ zip_code: "98101"
+ }
+ }
+ order_item = order_items(:oi3)
+ start_stock = order_item.product.stock
+
+ patch order_path(order), params: order_data
+
+ order = Order.find(order.id)
+ order_item = OrderItem.find(order_item.id)
+
+ end_stock = order_item.product.stock
+ quantity = order_item.quantity
+
+ (start_stock - end_stock).must_equal quantity
+ end
+ end
+end
diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb
new file mode 100644
index 0000000000..df753d41e7
--- /dev/null
+++ b/test/controllers/products_controller_test.rb
@@ -0,0 +1,591 @@
+require "test_helper"
+
+describe ProductsController do
+
+ describe "require a user to be logged in" do
+ before do
+ @merchant = merchants(:eva)
+ login(@merchant)
+ end
+
+ describe "new" do
+ it "you can create a product if you are logged in" do
+ get new_product_path
+ must_respond_with :success
+ end
+ end
+
+ describe "create" do
+ it "any logged in user can create a product with valid data" do
+ product_data = {
+ product: {
+ name: "test product",
+ price: 20,
+ stock: 3,
+ merchant_id: merchants(:eva).id
+ }
+ }
+ start_count = Product.count
+ new_product = Product.new(product_data[:product])
+ new_product.must_be :valid?
+
+ post products_path, params: product_data
+ must_respond_with :redirect
+ flash[:status].must_equal :success
+ must_redirect_to merchant_products_path(session[:merchant_id])
+ Product.count.must_equal start_count + 1
+ end
+
+ it "any logged in user can not create a product with bogus data" do
+ product_data = {
+ product: {
+ name: "test product",
+ stock: 3,
+ merchant_id: merchants(:eva).id
+ }
+ }
+ start_count = Product.count
+ new_product = Product.new(product_data[:product])
+ new_product.wont_be :valid?
+
+ post products_path, params: product_data
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Could not create new product test product"
+ Product.count.must_equal start_count
+ end
+ end
+
+ describe "update" do
+ it "a logged in user cannot update other's products" do
+ Product.destroy_all
+ product_data = {
+ product: {
+ name: "test product",
+ price: 20,
+ stock: 3,
+ merchant_id: merchants(:emma).id,
+ retired: false
+ }
+ }
+ only_product = Product.create!(product_data[:product])
+ new_merchant = merchants(:eva)
+ login(new_merchant)
+
+ update_product = {
+ product: {
+ retired: true
+ }
+ }
+ patch product_path(only_product), params: update_product
+
+ Product.first.retired.must_equal false
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ must_redirect_to root_path
+ end
+ end
+
+ describe "index" do
+ it "a logged in user cannot see others products" do
+ params = {
+ merchant_id: merchants(:emma).id
+ }
+ get products_path(params)
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ must_redirect_to root_path
+ end
+
+
+ it "a logged in user cannot see other's products organized by category" do
+ params = {
+ merchant_id: merchants(:emma).id,
+ category_id: categories(:one)
+ }
+ get products_path(params)
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ must_redirect_to root_path
+ end
+ end
+
+ describe "edit" do
+ it "if you a logged in user, you cannot edit another person's products" do
+ merchant = merchants(:emma)
+ product = Product.find_by(merchant_id: merchant.id)
+ get edit_product_path(product.id)
+ flash[:message].must_equal "Failure: You cannot access account pages for other users"
+ flash[:status].must_equal :failure
+ end
+ end
+ end
+
+ describe "require the correct logged in user" do
+ before do
+ @merchant = merchants(:eva)
+ login(@merchant)
+ end
+
+ describe "edit" do
+ it "if you are the correct logged in user, you can edit your own products" do
+ @product = products(:one)
+ get edit_product_path(@product.id)
+ must_respond_with :success
+ end
+ end
+
+ describe "index" do
+ it "a logged in user can see their own products" do
+ params = {
+ merchant_id: merchants(:eva).id
+ }
+ get products_path(params)
+ must_respond_with :success
+ end
+
+ it "a logged in user can see their own products organized by category" do
+ params = {
+ merchant_id: merchants(:eva).id,
+ category_id: categories(:one)
+ }
+ get products_path(params)
+ must_respond_with :success
+ end
+ end
+
+ it "a logged in user can navigate to a page displaying all the products of one category from their merchant page" do #Note: this does same function as index_by_category but uses a different route. Ask Julia if this is nessesary.
+ params = {
+ category_id: categories(:one)
+ }
+ get products_path(params)
+ must_respond_with :success
+ end
+
+ describe "update" do
+ it "a logged in user can update their own products" do
+ Product.destroy_all
+ product_data = {
+ product: {
+ name: "test product",
+ price: 20,
+ stock: 3,
+ merchant_id: merchants(:eva).id,
+ retired: false
+ }
+ }
+ only_product = Product.create!(product_data[:product])
+
+ update_product = {
+ product: {
+ retired: true
+ }
+ }
+ patch product_path(only_product), params: update_product
+
+ Product.first.retired.must_equal true
+ must_redirect_to merchant_products_path(@merchant)
+ end
+
+ it "a logged in user cannot update their own products with bogus updates" do
+ Product.destroy_all
+ product_data = {
+ product: {
+ name: "test product",
+ price: 20,
+ stock: 3,
+ merchant_id: merchants(:eva).id,
+ retired: false
+ }
+ }
+ only_product = Product.create!(product_data[:product])
+
+ update_product = {
+ product: {
+ price: -20
+ }
+ }
+ patch product_path(only_product), params: update_product
+
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "Could not updated test product, ID number #{only_product.id}"
+ must_respond_with :bad_request
+ end
+ end
+ end
+
+
+ describe "all users can do these things" do
+ describe "edit" do
+ it "if you are not logged in, you can not edit products" do
+ merchant = merchants(:emma)
+ product = Product.find_by(merchant_id: merchant.id)
+ get edit_product_path(product.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "You must be logged in to do that"
+ flash[:status].must_equal :failure
+ end
+ end
+
+ describe "update" do
+ it "a guest user cannot update products" do
+ product = products(:one)
+ update_product = {
+ product: {
+ description: "this description has been updated"
+ }
+ }
+ patch product_path(products(:one)), params: update_product
+
+ product.description.wont_equal "this description has been updated"
+ flash[:status].must_equal :failure
+ flash[:message].must_equal "You must be logged in to do that"
+ must_redirect_to root_path
+ end
+ end
+
+ describe "new" do
+ it "if you are not logged in, you can not make a new products" do
+ merchant = merchants(:emma)
+ get new_product_path(merchant.id)
+ must_redirect_to root_path
+ flash[:message].must_equal "You must be logged in to do that"
+ flash[:status].must_equal :failure
+ end
+ end
+
+ describe "index" do
+ it "returns success for all products" do
+ get products_path
+ must_respond_with :success
+ end
+
+ it "returns success for no products" do
+ Product.destroy_all
+ get products_path
+ must_respond_with :success
+ end
+ end
+
+ describe "index_by_merchant" do
+ it "a user can see all the products for one merchant" do
+ merchant_id = Merchant.last.id
+ get products_merchant_path(merchant_id)
+ must_respond_with :success
+ end
+ end
+
+ describe "index_by_category" do
+ it "a user can see all the products for one merchant" do
+ category_id = Category.last.id
+ get products_category_path(category_id)
+ must_respond_with :success
+ end
+ end
+
+ describe "show" do
+ it "any user can see a products show page" do
+ product_id = Product.last.id
+ get product_path(product_id)
+ must_respond_with :success
+ end
+
+ it "any user cannot see a bogus products show page" do
+ product_id = Product.last.id + 1
+ get product_path(product_id)
+ must_respond_with :not_found
+ end
+ end
+ end
+end
+
+#
+#
+#
+# it "a user can see all the products in one category" do
+# product_data = {
+# product: {
+# name: "test product",
+# stock: 3,
+# #merchant_id: merchants(:eva).id,
+# category_id: Category.last.id
+# }
+# }
+# get products_path(product_data[:product])
+# must_respond_with :success
+# end
+#
+# it "a user cannot see products in bogus category" do
+# product_data = {
+# product: {
+# name: "test product",
+# stock: 3,
+# #merchant_id: merchants(:eva).id,
+# category_id: Category.last.id + 1
+# }
+# }
+# #category_id = Category.last.id + 1
+# get products_path(product_data[:product])
+# must_respond_with :Found
+# end
+#
+# it "a user can see all products for one merchant" do
+# product_data = {
+# product: {
+# name: "test product",
+# stock: 3,
+# merchant_id: merchants(:eva).id,
+# #category_id: Category.last.id
+# }
+# }
+# get products_path(product_data[:product])
+# must_respond_with :success
+# end
+#
+#
+# end
+#
+#
+#
+# #index
+# #index_by_merchant
+# #index_by_category
+# end
+#
+# #
+# # describe "edit" do
+# # it "succeeds for an extant product ID" do
+# # get edit_product_path(Product.first)
+# # must_respond_with :success
+# # end
+# #
+# # it "renders 404 not_found for a bogus product ID" do
+# # bogus_product_id = Product.last.id + 1
+# # get edit_product_path(bogus_product_id)
+# # must_respond_with :not_found
+# # end
+# # end
+# #
+# # describe "create" do
+# # it "creates a product with valid data" do
+# # product_data = {
+# # product: {
+# # name: "test product",
+# # price: 2.0,
+# # stock: 3,
+# # description: "testing",
+# # image_url: "http://via.placeholder.com/300x250",
+# }
+# }
+# new_product = Product.new(product_data[:product])
+# new_product[:retired] = false
+# new_product[:merchant_id] = session[:merchant_id]
+# new_product.must_be :valid?
+#
+# start_count = Product.count
+#
+# post products_path, params: product_data
+# must_respond_with :redirect
+# must_redirect_to merchant_products_path(session[:merchant_id])
+# Product.count.must_equal start_count + 1
+# end
+#
+# it "renders bad_request and does not update the database for bogus data" do
+# product_data = {
+# product: {
+# name: ""
+# }
+# }
+# Product.new(product_data[:product]).wont_be :valid?
+#
+# start_count = Product.count
+#
+# post products_path, params: product_data
+#
+# must_respond_with :bad_request
+# Product.count.must_equal start_count
+# end
+# end
+#
+# describe "update" do
+# it "succeeds for valid data and an valid product ID" do
+# product = Product.first
+#
+# product_data = {
+# product: {
+# name: "test product",
+# price: 2.0,
+# stock: 3,
+# description: "testing",
+# image_url: "http://via.placeholder.com/300x250",
+# }
+# }
+# product.update_attributes(product_data[:product])
+# product.must_be :valid?
+# patch product_path(product), params: product_data
+#
+# must_redirect_to merchant_products_path(session[:merchant_id])
+# Product.find(product.id).name.must_equal product_data[:product][:name]
+# end
+#
+# it "renders bad_request for bogus data" do
+# product = Product.first
+# product_data = {
+# product: {
+# name: ""
+# }
+# }
+# product.update_attributes(product_data[:product])
+# product.wont_be :valid?
+# patch product_path(product), params: product_data
+#
+# must_respond_with :bad_request
+# end
+#
+# it "renders 404 not_found for a bogus product ID" do
+#
+# bogus_product_id = Product.last.id + 1
+# get product_path(bogus_product_id)
+# must_respond_with :not_found
+# end
+# end
+# end
+#
+#
+# describe "show" do
+# it "succeeds for an valid product ID" do
+# get product_path(Product.first)
+# must_respond_with :success
+# end
+#
+# it "renders 404 not_found for a bogus product ID" do
+# bogus_product_id = Product.last.id + 1
+# get product_path(bogus_product_id)
+# must_respond_with :not_found
+# end
+# end
+#
+#
+# describe "index" do
+# it "returns success for all products" do
+# get products_path
+# must_respond_with :success
+# end
+#
+# it "returns success for no products" do
+# Product.destroy_all
+# get products_path
+# must_respond_with :success
+# end
+#
+# it "returns success and products sorted by category when passed category_id" do
+# category = Category.first
+# get category_products_path(category.id)
+# must_respond_with :success
+# end
+#
+# it "does not allow a user who does not own the products to see the products" do
+# bogus_id = Merchant.last.id + 1
+# session = {}
+# session[:merchant_id] = nil
+# params = {
+# merchant_id: bogus_id
+# }
+#
+# get products_path(params)
+# must_redirect_to root_path
+# flash[:status].must_equal :failure
+# flash[:message].must_equal "Failure: You cannot access account pages for other users"
+# end
+#
+# it "allows merchant who does own the products to see the products" do
+# session = {}
+# session[:merchant_id] = merchants(:eva).id
+# params = {
+# merchant_id: merchants(:eva).id,
+# product_id: products(:one).id,
+#
+# }
+#
+# get products_path(params)
+# must_respond_with :success
+#
+# end
+#
+# end
+#
+# ##I give up for right now
+# describe "create" do
+# it "creates a product with valid data" do
+# product_data = {
+# product: {
+# name: "mug",
+# price: 2.0,
+# stock: 3,
+# retired: false,
+# description: "testing",
+# image_url: "http://www.fillmurray.com/",
+# merchant: merchants(:eva)
+# }
+# }
+# new_product = Product.new(product_data[:product])
+# new_product.must_be :valid?
+#
+# start_count = Product.count
+#
+# post products_path, params: product_data
+# must_redirect_to product_path(Product.last)
+# Product.count.must_equal start_count + 1
+# end
+#
+#
+# it "renders bad_request and does not update the DB for bogus data" do
+# product_data = {
+# product: {
+# name: ""
+# }
+# }
+# start_count = Product.count
+#
+# post products_path, params: product_data
+#
+# must_respond_with :bad_request
+# Product.count.must_equal start_count
+# end
+# end
+#
+#
+# describe "update" do
+# it "succeeds for valid data and an valid product ID" do
+# product = Product.first
+#
+# product_data = {
+# product: {
+# name: "mug",
+# price: 2.0,
+# stock: 3,
+# retired: false,
+# description: "testing",
+# image_url: "http://www.fillmurray.com/",
+# merchant: Merchant.last
+# }
+# }
+# product.update_attributes(product_data[:product])
+# product.must_be :valid?
+# patch product_path(product), params: product_data
+#
+# must_redirect_to product_path(product)
+# Product.find(product.id).name.must_equal product_data[:product][:name]
+# end
+#
+# it "returns success and products sorted by merchant when passed merchant_id" do
+# merchant = Merchant.first
+# get merchant_products_path(merchant.id)
+# must_respond_with :success
+# end
+#
+# it "returns success and products sorted by merchant and category when passed merchant_id and category_id" do
+# merchant = Merchant.first
+# category = Category.first
+# get merchant_category_products_path(merchant.id, category.id)
+# must_respond_with :success
+# end
diff --git a/test/controllers/reviews_controller_test.rb b/test/controllers/reviews_controller_test.rb
new file mode 100644
index 0000000000..2eae448ca8
--- /dev/null
+++ b/test/controllers/reviews_controller_test.rb
@@ -0,0 +1,60 @@
+require "test_helper"
+
+describe ReviewsController do
+ describe "new" do
+ before do
+ @owner = merchants(:eva)
+ @user = merchants(:emma)
+ @product = @owner.products.first
+ @right_review = Review.new(product_id: @product.id, rating: 5)
+ @wrong_review = Review.new(product_id: @product.id, rating: 3)
+ end
+
+ it "works for not owners of the product" do
+ get new_product_review_path(@product.id)
+ must_respond_with :success
+ end
+
+ it "not works for the owner of the product" do
+ login(@owner)
+ get new_product_review_path(@product.id)
+ must_respond_with :redirect
+ must_redirect_to product_path(@product)
+ end
+ end
+
+ describe "create" do
+ it "creates a review with valid data" do
+ review_data = {
+ review: {
+ product_id: products(:one).id,
+ rating: 2,
+ text: "review text"
+ }
+ }
+ Review.new(review_data[:review]).must_be :valid?
+ start_count = Review.count
+
+ post reviews_path, params: review_data
+
+ must_respond_with :redirect
+ must_redirect_to product_path(products(:one))
+ Review.count.must_equal start_count + 1
+ end
+
+ it "renders bad_request and does not update the DB for bogus data" do
+ review_data = {
+ review: {
+ product_id: products(:one).id,
+ rating: nil
+ }
+ }
+ start_count = Review.count
+
+ post reviews_path, params: review_data
+
+ must_respond_with :bad_request
+ Review.count.must_equal start_count
+ end
+ end
+end
diff --git a/test/fixtures/.keep b/test/fixtures/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml
new file mode 100644
index 0000000000..56066c68af
--- /dev/null
+++ b/test/fixtures/categories.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/fixtures/merchants.yml b/test/fixtures/merchants.yml
new file mode 100644
index 0000000000..2d32ec953c
--- /dev/null
+++ b/test/fixtures/merchants.yml
@@ -0,0 +1,25 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the "{}" from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+
+# column: value
+eva:
+ username: eva
+ email: eva@email.com
+ provider: github
+ uid: 123
+
+emma:
+ username: emma
+ email: emma@email.com
+ provider: github
+ uid: 234
+
+no_orders:
+ username: test
+ email: test@email.com
+ provider: github
+ uid: 345
diff --git a/test/fixtures/order_items.yml b/test/fixtures/order_items.yml
new file mode 100644
index 0000000000..268d5c8a5c
--- /dev/null
+++ b/test/fixtures/order_items.yml
@@ -0,0 +1,46 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the "{}" from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+# zero:
+# quantity: 3
+# product_id: 1
+# order_id: 1
+#
+# one:
+# quantity: 3
+# product_id: 2
+# order_id: 1
+#
+# two:
+# quantity: 3
+# product_id: 1
+# order_id: 1
+#
+# three:
+# quantity: 3
+# product_id: 1
+# order_id: 1
+#
+#
+# four:
+# quantity: 3
+# product_id: 1
+# order_id: 1
+
+oi1:
+ quantity: 3
+ product: one
+ order: one
+
+oi2:
+ quantity: 1
+ product: one
+ order: one
+
+oi3:
+ quantity: 1
+ product: two
+ order: two
diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml
new file mode 100644
index 0000000000..a8f90d38af
--- /dev/null
+++ b/test/fixtures/orders.yml
@@ -0,0 +1,25 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the "{}" from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one:
+ customer_name: 'sue'
+ customer_email: 'sue@gmail.com'
+ customer_address: '420 blaze ave'
+ cc_number: '12312312'
+ cc_expiration: '2020-01-01'
+ cc_ccv: '908'
+ zip_code: '98101'
+ status: paid
+
+two:
+ customer_name: 'suzie'
+ customer_email: 'suzie@gmail.com'
+ customer_address: '123 internet ave'
+ cc_number: '333333'
+ cc_expiration: '2222-22-22'
+ cc_ccv: '222'
+ zip_code: '22222'
+ status: paid
diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml
new file mode 100644
index 0000000000..16a448fa1f
--- /dev/null
+++ b/test/fixtures/products.yml
@@ -0,0 +1,105 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the "{}" from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one:
+ name: product
+ price: 2.0
+ stock: 3
+ retired: false
+ description: testing
+ image_url: http://www.fillmurray.com/
+ merchant: eva
+
+two:
+ name: product2
+ price: 3.0
+ stock: 4
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: eva
+
+three:
+ name: product3
+ price: 4.0
+ stock: 5
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: eva
+
+four:
+ name: product4
+ price: 6.0
+ stock: 7
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+five:
+ name: product5
+ price: 8.0
+ stock: 9
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+six:
+ name: product6
+ price: 10.0
+ stock: 11
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+seven:
+ name: product7
+ price: 12.0
+ stock: 13
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+eight:
+ name: product8
+ price: 14.0
+ stock: 15
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+nine:
+ name: product9
+ price: 16.0
+ stock: 17
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+ten:
+ name: product9
+ price: 18.0
+ stock: 19
+ retired: false
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
+
+ninty:
+ id: 90
+ name: product90
+ price: 18.0
+ stock: 19
+ retired: true
+ description: testing2
+ image_url: http://www.fillmurray.com/
+ merchant: emma
diff --git a/test/fixtures/reviews.yml b/test/fixtures/reviews.yml
new file mode 100644
index 0000000000..7068fa80a1
--- /dev/null
+++ b/test/fixtures/reviews.yml
@@ -0,0 +1,16 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ product: one
+ rating: 3
+ text: 'review text 1'
+
+two:
+ product: one
+ rating: 4
+ text: 'review text 2'
+
+three:
+ product: one
+ rating: 3
+ text: 'review text 3'
diff --git a/test/helpers/.keep b/test/helpers/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/integration/.keep b/test/integration/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/mailers/.keep b/test/mailers/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/models/.keep b/test/models/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/models/category_test.rb b/test/models/category_test.rb
new file mode 100644
index 0000000000..b6651d47f0
--- /dev/null
+++ b/test/models/category_test.rb
@@ -0,0 +1,33 @@
+require "test_helper"
+
+describe Category do
+
+ describe "relations" do
+ it "has a list of products" do
+ category = Category.first
+ category.must_respond_to :products
+ category.products.each do |product|
+ product.must_be_kind_of Product
+ end
+ end
+ end
+
+ describe "validations" do
+ it "require a name" do
+ category = Category.new
+ category.valid?.must_equal false
+ category.errors.messages.must_include :name
+ end
+
+ it "requires a unique name" do
+ name = "test name"
+ category_1 = Category.new(name: name)
+ category_1.save!
+
+ category_2 = Category.new(name: name)
+ category_2.save.must_equal false
+ category_2.errors.messages.must_include :name
+ end
+
+ end
+end
diff --git a/test/models/merchant_test.rb b/test/models/merchant_test.rb
new file mode 100644
index 0000000000..04da93c6b4
--- /dev/null
+++ b/test/models/merchant_test.rb
@@ -0,0 +1,190 @@
+require "test_helper"
+
+describe Merchant do
+ describe "relations" do
+ before do
+ @merchant1 = merchants(:eva)
+ @merchant2 = merchants(:emma)
+ end
+
+ it "a merchant can have a many products" do
+ @merchant1.must_respond_to :products
+ end
+
+ it "a merchant can have zero products" do
+ Product.destroy_all
+ @merchant1.must_respond_to :products
+ end
+
+ it "a merchant can have many order_items" do
+ @merchant1.must_respond_to :order_items
+ end
+
+ it "a merchant can have zero order_items" do
+ OrderItem.destroy_all
+ @merchant1.must_respond_to :order_items
+ end
+
+ it "a merchant can have orders" do
+ @merchant1.must_respond_to :orders
+ end
+
+ it "a merchant can have zero orders" do
+ Order.destroy_all
+ @merchant1.must_respond_to :orders
+ end
+ end
+
+ describe 'validations' do
+ before do
+ @merchant1 = Merchant.new(username: 'new merchant', email: 'new@email.com', provider: 'newprovider', uid: 12345)
+ end
+
+ it 'allows a new merchant to be created' do
+ @merchant1.must_be :valid?
+ end
+
+ it 'does not allow merchant to be created without a provider' do
+ bogus_merchant = Merchant.new(username: 'new merchant', email: 'new@email.com', uid: 12345)
+ bogus_merchant.wont_be :valid?
+ bogus_merchant.errors.messages.must_include :provider
+ end
+
+ it 'does not allow merchant to be created without a uid' do
+ bogus_merchant = Merchant.new(email: 'new@email.com', provider: 'new provider')
+ bogus_merchant.wont_be :valid?
+ bogus_merchant.errors.messages.must_include :uid
+ end
+
+ it 'does not allow merchant to be created with an uid that is already registered' do
+ @merchant1.save!
+ repeat_merchant = Merchant.new(username: 'newer merchant', email: 'newer@email.com', provider: 'newerprovider', uid: 12345)
+ repeat_merchant.wont_be :valid?
+ repeat_merchant.errors.messages.must_include :uid
+ end
+
+ it 'does not allow merchant to be created without a username' do
+ bogus_merchant = Merchant.new(email: 'new@email.com', provider: 'newprovider', uid: 12345)
+ bogus_merchant.wont_be :valid?
+ bogus_merchant.errors.messages.must_include :username
+ end
+
+ it 'does not allow merchant to be created with an username that is already registered' do
+ @merchant1.save!
+ repeat_merchant = Merchant.new(username: 'new merchant', email: 'newer@email.com', provider: 'newerprovider', uid: 12346)
+ repeat_merchant.wont_be :valid?
+ repeat_merchant.errors.messages.must_include :username
+ end
+
+ it 'does not allow merchant to be created without a email' do
+ bogus_merchant = Merchant.new(username: 'new merchant', provider: 'newprovider', uid: 12345)
+ bogus_merchant.wont_be :valid?
+ bogus_merchant.errors.messages.must_include :email
+ end
+
+ it 'does not allow merchant to be created with an email that is already registered' do
+ @merchant1.save!
+ repeat_merchant = Merchant.new(username: 'newer merchant', email: 'new@email.com', provider: 'newerprovider', uid: 12346)
+ repeat_merchant.wont_be :valid?
+ repeat_merchant.errors.messages.must_include :email
+ end
+
+ end
+
+
+ describe "custom methods" do
+ describe 'from_auth_hash' do
+ it "returns a merchant when given valid data." do
+ provider = "github"
+ hash = {
+ "uid" =>"1234",
+ "info" =>{
+ "nickname" => "Dan",
+ "email" => "Dan@email.com"
+ }
+ }
+ user = Merchant.from_auth_hash(provider, hash)
+ user.provider.must_equal "github"
+ user.uid.must_equal 1234
+ user.username.must_equal "Dan"
+ user.email.must_equal "Dan@email.com"
+ user.must_be_kind_of Merchant
+ end
+ end
+
+ describe "order_items_quantity" do
+ # eva has two order items each with a quantity of 3 and 1
+ it "returns total quantity of a merchant's order items" do
+ merchants(:eva).order_items_quantity.must_equal 5
+ end
+
+ it "returns 0 for a merchant with no order items" do
+ OrderItem.destroy_all
+ merchants(:eva).order_items_quantity.must_equal 0
+ end
+
+
+ end
+
+ describe "order_items_total" do
+ # eva has two order items each with a quantity of 3 and 1 and a price of 2.
+ it "returns sum of a merchant's order items" do
+ puts "orderitems #{merchants(:eva).order_items.inspect}"
+ merchants(:eva).order_items_total.must_equal 11
+ end
+
+ it "returns 0 for a merchant with no order items" do
+ OrderItem.destroy_all
+ merchants(:eva).order_items_quantity.must_equal 0
+ end
+ end
+
+ describe "order_items_by_status" do
+ before do
+ @order_items = merchants(:eva).order_items_by_status("paid")
+ end
+
+ it "must return an array" do
+ @order_items.must_be_kind_of Array
+ end
+
+ it "returns all order_items with the same order status" do
+ statuses = @order_items.map { |order_item| order_item.order.status }
+ statuses.uniq.must_equal [statuses.first]
+ statuses.uniq.must_equal ["paid"]
+ statuses.uniq.length.must_equal 1
+ end
+
+ it "returns an empty array for no order_items wit the a given status" do
+ bogus_status = merchants(:eva).order_items_by_status("bogus")
+ statuses = bogus_status.map { |order_item| order_item.order.status }
+ statuses.uniq.must_equal []
+ statuses.uniq.length.must_equal 0
+ end
+ end
+
+ describe "revenue_by_status" do
+ before do
+ @order_items_paid = merchants(:eva).order_items_by_status("paid")
+ @order_items_pending = merchants(:eva).order_items_by_status("pending")
+ end
+
+ it "returns right numbers" do
+ (Merchant.revenue_by_status(@order_items_paid) + Merchant.revenue_by_status(@order_items_pending)).must_equal merchants(:eva).order_items_total
+
+ Merchant.revenue_by_status(@order_items_paid).must_equal 11
+
+
+ #if there is time, revisit this. We were trying to add another order_item and make sure that the revenue increased. But it was not working...
+ # order = OrderItem.new(quantity: 1, product: products(:one), order: orders(:one))
+ # order.save
+ #
+ # puts "#{order.inspect}"
+ # order_items_paid = merchants(:eva).order_items_by_status("paid")
+ # puts "#{order_items_paid.inspect}"
+ # Merchant.revenue_by_status(order_items_paid).must_equal 9
+
+ end
+ end
+ end
+end
diff --git a/test/models/order_item_test.rb b/test/models/order_item_test.rb
new file mode 100644
index 0000000000..81340adef3
--- /dev/null
+++ b/test/models/order_item_test.rb
@@ -0,0 +1,100 @@
+require "test_helper"
+
+describe OrderItem do
+
+ describe "relations" do
+ before do
+ @order_item = OrderItem.new(quantity: 3, order: orders(:one), product: products(:one))
+ end
+
+ it "belongs to a product and creates an error message if no product is given" do
+ o = OrderItem.new(quantity: 2, order: orders(:one))
+ o.must_respond_to :product
+ o.wont_be :valid?
+ o.errors.messages.must_include :product
+ end
+
+
+ it "belongs to an order and creates an error message if no order is given" do
+ o = OrderItem.new(quantity: 2, product: products(:one))
+ o.must_respond_to :order
+ o.wont_be :valid?
+ o.errors.messages.must_include :order
+ end
+
+ it "has one merchant through product" do
+ # merchant eva owns the product purchased in :oi1 (i.e., product :one in fixtures)
+ order_items(:oi1).merchant.must_equal merchants(:eva)
+ end
+
+ end
+
+ describe "validations" do
+ it "can be created with all required fields (including a positive integer for quantity)" do
+ o = OrderItem.new(quantity: 3, order: orders(:one), product: products(:one))
+ o.must_be :valid?
+ end
+
+ it "cannot be created without a quantity" do
+ o = OrderItem.new( order: orders(:one), product: products(:one))
+ o.wont_be :valid?
+ o.errors.messages.must_include :quantity
+ o.errors.messages.values.first.must_include "can't be blank"
+ end
+
+ it "cannot be created with a float" do
+ o = OrderItem.new(quantity: 3.3, order: orders(:one), product: products(:one))
+ o.wont_be :valid?
+ o.errors.messages.must_include :quantity
+ o.errors.messages.values.first.must_include "must be an integer"
+ end
+
+
+ it "cannot be created with a quantity of 0" do
+ o = OrderItem.new(quantity: 0, order: orders(:one), product: products(:one))
+ o.wont_be :valid?
+ o.errors.messages.must_include :quantity
+ o.errors.messages.values.first.must_include "must be greater than 0"
+ end
+
+ it "cannot be created with a negative quantity" do
+ o = OrderItem.new(quantity: -1, order: orders(:one), product: products(:one))
+ o.wont_be :valid?
+ o.errors.messages.must_include :quantity
+ o.errors.messages.values.first.must_include "must be greater than 0"
+ end
+ end
+
+ describe "custom methods" do
+ before do
+ @order_item = OrderItem.new(quantity: 3, order: orders(:one), product: products(:one))
+ @other_order_item = OrderItem.new(quantity: 2, order: orders(:one), product: products(:two))
+ end
+
+ describe "subtotal" do
+ it "returns the right subtotal" do
+ OrderItem.subtotal(@order_item).must_equal @order_item.quantity * @order_item.product.price
+ end
+ end
+
+ describe "total cost" do
+ it "returns the right total cost" do
+ total1 = OrderItem.total_cost([@order_item, @other_order_item])
+ order_item = OrderItem.new(order_id: orders(:one).id, product_id: products(:one).id, quantity: 50000000)
+ total2 = OrderItem.total_cost([order_item, @other_order_item])
+
+ # total is less than 5000 cents so have to pay 1000 cents in shipping
+ total1.must_equal @order_item.quantity * @order_item.product.price + @other_order_item.quantity * @other_order_item.product.price + 1000
+ # total is greater than 5000 cents so don't have to pay 1000 cents in shipping
+
+ total2.must_equal order_item.quantity * order_item.product.price + @other_order_item.quantity * @other_order_item.product.price
+ end
+ end
+
+ describe "total" do
+ it "returns the cost of an order_item" do
+ @order_item.total.must_equal @order_item.quantity * @order_item.product.price
+ end
+ end
+ end
+end
diff --git a/test/models/order_test.rb b/test/models/order_test.rb
new file mode 100644
index 0000000000..e225b22f3b
--- /dev/null
+++ b/test/models/order_test.rb
@@ -0,0 +1,53 @@
+require "test_helper"
+
+describe Order do
+ describe "relations" do
+ before do
+ @order = orders(:one)
+ # @order = Order.first
+ end
+ it "has many products" do
+ @order.must_respond_to :products
+ @order.products.each do |product|
+ product.must_be_kind_of Product
+ end
+ end
+
+ it "has many order_items" do
+ @order.must_respond_to :order_items
+ @order.order_items.each do |order_item|
+ order_item.must_be_kind_of OrderItem
+ end
+ end
+
+ # it "has many merchants through products" do
+ # @order.must_respond_to :merchants
+ # @order.merchants.each do |merchat|
+ # order_item.must_be_kind_of Merchant
+ # end
+ #
+ # end
+ end
+
+ describe "validations" do
+ it "must be invalid" do
+ order = Order.new
+ result = order.valid?
+ result.must_equal false
+ end
+
+ it 'must be valid' do
+ order = Order.new
+ order.status = 'pending'
+ order.customer_name = 'Sue'
+ order.customer_email = 'Sue@gmail.com'
+ order.customer_address = '123 internet street'
+ order.cc_number = '123123123'
+ order.cc_expiration = '2020-10-01'
+ order.cc_ccv = '980'
+ order.zip_code = '98101'
+ result = order.valid?
+ result.must_equal true
+ end
+ end
+end
diff --git a/test/models/product_test.rb b/test/models/product_test.rb
new file mode 100644
index 0000000000..831b452a3e
--- /dev/null
+++ b/test/models/product_test.rb
@@ -0,0 +1,234 @@
+require "test_helper"
+
+describe Product do
+ describe "relations" do
+ before do
+ @product = products(:one)
+ end
+
+ it "has a merchant" do
+ @product.must_respond_to :merchant
+ @product.merchant.must_be_kind_of Merchant
+ end
+
+ it "has a list of order_items" do
+ @product.must_respond_to :order_items
+ @product.order_items.each do |order_item|
+ order_item.must_be_kind_of OrderItem
+ end
+ end
+
+ it "has and belongs to many categories" do
+ @product.must_respond_to :categories
+ @product.categories.each do |category|
+ category.must_be_kind_of Category
+ end
+ end
+
+ it "has many reviews" do
+ @product.must_respond_to :reviews
+ @product.reviews.each do |review|
+ review.must_be_kind_of Review
+ end
+ end
+
+ it "has many orders" do
+ @product.must_respond_to :orders
+ @product.orders.each do |order|
+ order.must_be_kind_of Order
+ end
+ end
+ end
+
+ describe "validations" do
+
+ it "allows a product to be created with name and price" do
+ product = Product.new(name: "new_product", price: 10, merchant: merchants(:emma), stock: 5000)
+
+ product.must_be :valid?
+ end
+
+ it "requires a name to be created" do
+ product = Product.new(price: 10, merchant: merchants(:emma), stock: 5000)
+
+ product.wont_be :valid?
+ end
+
+ it "requires a unique name to be created" do
+ product_1 = Product.create(name: "new_product", price: 10, merchant: merchants(:emma), stock: 5000)
+ product_2 = Product.new(name: "new_product", price: 10, merchant: merchants(:emma), stock: 5000)
+
+ product_2.wont_be :valid?
+ end
+
+ it "requires a price to be created" do
+ product = Product.new(name: "new_product", merchant: merchants(:emma), stock: 5000)
+
+ product.wont_be :valid?
+ end
+
+ it "requires the price be greater than 0" do
+
+ product_0 = Product.new(name: "new_product", price: 0, merchant: merchants(:emma), stock: 5000)
+
+ product_less_than_0 = Product.new(name: "new_product", price: -10, merchant_id: (:emma), stock: 5000)
+
+ product_ten = Product.new(name: "new_product", price: "ten", merchant: merchants(:emma), stock: 5000)
+
+ product_0.wont_be :valid?
+ product_less_than_0.wont_be :valid?
+ product_ten.wont_be :valid?
+ end
+
+
+ it "cannot be created without stock" do
+ invalid_prod = Product.new(name: "test", price: 3000, merchant: merchants(:emma))
+ invalid_prod.wont_be :valid?
+ invalid_prod.errors.messages.must_include :stock
+ end
+
+ it "cannot be created with a float" do
+ invalid_prod = Product.new(name: "test", price: 3000, merchant: merchants(:emma), stock: 3.4)
+ invalid_prod.wont_be :valid?
+ invalid_prod.errors.messages.must_include :stock
+ end
+
+
+ it "cannot be created with a stock of 0" do
+ invalid_prod = Product.new(name: "test", price: 3000, merchant: merchants(:emma), stock: 0)
+ invalid_prod.wont_be :valid?
+ invalid_prod.errors.messages.must_include :stock
+ end
+
+ it "cannot be created with a negative quantity" do
+ invalid_prod = Product.new(name: "test", price: 3000, merchant: merchants(:emma), stock: -5)
+ invalid_prod.wont_be :valid?
+ invalid_prod.errors.messages.must_include :stock
+ end
+ end
+
+ describe "custom methods" do
+ describe "random_products" do
+ it "must return an array of asked length of products" do
+ rand_products = Product.random_products(6)
+ rand_products.must_be_kind_of Array
+ rand_products.length.must_equal 6
+ end
+
+ it "must return empty array if there are no products" do
+ Product.destroy_all
+ rand_products = Product.random_products(6)
+ rand_products.must_be_kind_of Array
+ rand_products.length.must_equal 0
+ end
+
+ it "must return all products if there are less products that it was asked in random method" do
+ Product.destroy_all
+ Product.create(name: "new_product", price: 10, merchant: merchants(:emma), retired: false, stock: 4)
+ rand_products = Product.random_products(6)
+ rand_products.must_be_kind_of Array
+ rand_products.length.must_equal 1
+ end
+
+ it "must return new products every time" do
+ rand_products_1 = Product.random_products(6)
+ rand_products_2 = Product.random_products(6)
+ rand_products_1.wont_equal rand_products_2
+ end
+ end
+
+ describe "new_products" do
+ it "must return a list of product with asked length if there are more products than it was asked" do
+ new_products = Product.new_products(5)
+ new_products.must_be_kind_of Array
+ new_products.length.must_equal 5
+ end
+
+ it "must return empty array if there are no products" do
+ Product.destroy_all
+ new_products = Product.new_products(5)
+ new_products.must_be_kind_of Array
+ new_products.length.must_equal 0
+ end
+
+ it "must return all products if there are less products that it was asked in new method" do
+ Product.destroy_all
+ Product.create(name: "new_product", price: 10, merchant: merchants(:emma), retired: false, stock: 1)
+ new_products = Product.new_products(5)
+ new_products.must_be_kind_of Array
+ new_products.length.must_equal 1
+ end
+
+ it "must return same products every time" do
+ new_products_1 = Product.new_products(5)
+ new_products_2 = Product.new_products(5)
+ new_products_1.must_equal new_products_2
+ end
+
+ it "must return products in the right order" do
+ Product.destroy_all
+ Product.create(name: "new_product", price: 10, merchant: merchants(:emma), retired: false, stock: 4)
+ Product.create(name: "another_new_product", price: 15, merchant: merchants(:emma), retired: false, stock: 7)
+ new_products = Product.new_products(2)
+ new_products.first.created_at.must_be :>, new_products.last.created_at
+ end
+ end
+
+ describe "average_rating" do
+ before do
+ @product = products(:one)
+ @ratings = []
+ @product.reviews.each do |review|
+ @ratings << review.rating
+ end
+ end
+
+ it "must return an integer" do
+ @product.average_rating.must_be_kind_of Integer
+ end
+
+ it "must return correct average rating for list of reviews" do
+ @product.average_rating.must_equal @ratings.inject{ |sum, el| sum + el } / @ratings.length
+ end
+ end
+
+ # describe "bestseller" do
+ # before do
+ # Order.destroy_all
+ # @product_1 = products(:one)
+ # @product_2 = products(:two)
+ # @product_3 = products(:three)
+ #
+ # #
+ # # customer_data = {
+ # # customer_name: "customer",
+ # # customer_email: "customer@email.com",
+ # # customer_address: "address",
+ # # cc_number: 1234,
+ # # cc_expiration: Date.today,
+ # # cc_ccv: 123,
+ # # zip_code: 12345
+ # # }
+ #
+ # @order1 = Order.create!(status: 'paid', customer_name: 'Sue', customer_email: 'Sue@gmail.com', customer_address:'123 internet street', cc_number: '123123123', cc_expiration: '2020-10-01', cc_ccv: '980', zip_code: '98101')
+ # @order2 = Order.create!(status: 'paid', customer_name: 'Suzie', customer_email: 'Sue@gmail.com', customer_address:'123 internet street', cc_number: '123123123', cc_expiration: '2020-10-01', cc_ccv: '980', zip_code: '98101')
+ # @order3 = Order.create!(status: 'pending', customer_name: 'Susie', customer_email: 'Sue@gmail.com', customer_address:'123 internet street', cc_number: '123123123', cc_expiration: '2020-10-01', cc_ccv: '980', zip_code: '98101')
+ #
+ # OrderItem.create!(order_id: @order1.id, product_id: @product_1.id, quantity: 2)
+ # OrderItem.create!(order_id: @order1.id, product_id: @product_1.id, quantity: 5)
+ # OrderItem.create!(order_id: @order1.id, product_id: @product_1.id, quantity: 8)
+ # OrderItem.create!(order_id: @order2.id, product_id: @product_1.id, quantity: 2)
+ #
+ # end
+ #
+ # it "must return an array" do
+ # @product_1.orders.length.must_equal 1
+ # @product_2.orders.length.must_equal 2
+ # @product_3.orders.length.must_equal 3
+ #
+ # Order.bestseller.must_be_kind_of Array
+ # Order.bestseller.first.must_be_kind_of Products
+ # end
+ # end
+ end
+end
diff --git a/test/models/review_test.rb b/test/models/review_test.rb
new file mode 100644
index 0000000000..45fe438c1d
--- /dev/null
+++ b/test/models/review_test.rb
@@ -0,0 +1,37 @@
+require "test_helper"
+
+describe Review do
+ describe "relations" do
+ before do
+ @review = reviews(:one)
+ end
+
+ it "belongs to product" do
+ @review.must_respond_to :product
+ @review.product.must_be_kind_of Product
+ end
+ end
+
+ describe "validations" do
+ it "requires rating to be created" do
+ review = Review.new
+ review.wont_be :valid?
+ end
+
+ it "can be created with valid rating" do
+ review = Review.new(rating: 4, product: products(:one))
+ review.must_be :valid?
+ end
+
+ it "requires rating to be 1-5" do
+ review = Review.new(rating: 10, product: products(:one))
+ review.wont_be :valid?
+ end
+
+ it "rating has to be an integer" do
+ review = Review.new(rating: 4.5, product: products(:one))
+ review.wont_be :valid?
+ end
+ end
+
+end
diff --git a/test/system/.keep b/test/system/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/test_helper.rb b/test/test_helper.rb
new file mode 100644
index 0000000000..c1a3e496b8
--- /dev/null
+++ b/test/test_helper.rb
@@ -0,0 +1,63 @@
+require 'simplecov'
+SimpleCov.start 'rails'
+
+ENV["RAILS_ENV"] = "test"
+require File.expand_path("../../config/environment", __FILE__)
+require "rails/test_help"
+require "minitest/rails"
+require "minitest/reporters" # for Colorized output
+
+# For colorful output!
+Minitest::Reporters.use!(
+ Minitest::Reporters::SpecReporter.new,
+ ENV,
+ Minitest.backtrace_filter
+)
+
+
+
+
+# To add Capybara feature tests add `gem "minitest-rails-capybara"`
+# to the test group in the Gemfile and uncomment the following:
+# require "minitest/rails/capybara"
+
+# Uncomment for awesome colorful output
+# require "minitest/pride"
+
+class ActiveSupport::TestCase
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
+ fixtures :all
+
+ OmniAuth.config.test_mode = true
+ omniauth_hash = { 'provider' => 'github',
+ 'uid' => '12345',
+ 'info' => {
+ 'name' => 'tester',
+ 'email' => 'tester@email.com',
+ 'nickname' => 'testerNickname'
+ }
+ }
+
+ OmniAuth.config.add_mock(:github, omniauth_hash)
+
+ # Add more helper methods to be used by all tests here...
+ def setup
+ OmniAuth.config.test_mode = true
+ end
+
+ def mock_auth_hash(merchant)
+ return {
+ provider: merchant.provider,
+ uid: merchant.uid,
+ info: {
+ nickname: merchant.username,
+ email: merchant.email
+ }
+ }
+ end
+
+ def login(merchant)
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(merchant))
+ get auth_callback_path(:github)
+ end
+end
diff --git a/tmp/.keep b/tmp/.keep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/vendor/.keep b/vendor/.keep
new file mode 100644
index 0000000000..e69de29bb2