From df2ea95cebc1a2f3ad9d07ebdf4a9ac5476d2453 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Sat, 25 Feb 2017 11:00:27 -0500 Subject: [PATCH] user and recipient and tests --- .rspec | 2 + Gemfile | 1 + Gemfile.lock | 5 + app/assets/javascripts/recipients.coffee | 3 + app/assets/stylesheets/recipients.scss | 3 + app/assets/stylesheets/scaffolds.scss | 89 ++++++ app/controllers/recipients_controller.rb | 74 +++++ app/controllers/welcome_controller.rb | 6 + app/helpers/recipients_helper.rb | 2 + app/models/recipient.rb | 2 + app/models/user.rb | 6 + app/views/layouts/_header.html.haml | 38 +++ app/views/layouts/application.html.erb | 14 - app/views/layouts/application.html.haml | 12 + app/views/layouts/mailer.html.erb | 13 - app/views/layouts/mailer.html.haml | 8 + app/views/recipients/_form.html.erb | 67 +++++ app/views/recipients/edit.html.erb | 6 + app/views/recipients/index.html.erb | 45 +++ app/views/recipients/index.json.jbuilder | 4 + app/views/recipients/new.html.erb | 5 + app/views/recipients/show.html.erb | 54 ++++ app/views/recipients/show.json.jbuilder | 1 + app/views/welcome/index.html.haml | 1 + config/environments/development.rb | 2 + config/environments/production.rb | 2 + config/environments/test.rb | 2 + config/initializers/devise.rb | 274 ++++++++++++++++++ config/locales/devise.en.yml | 62 ++++ config/routes.rb | 4 + .../20170224181252_devise_create_users.rb | 42 +++ .../20170225150432_create_recipients.rb | 18 ++ db/schema.rb | 50 ++++ .../controllers/recipients_controller_spec.rb | 159 ++++++++++ spec/controllers/welcome_controller_spec.rb | 11 + spec/rails_helper.rb | 57 ++++ spec/routing/recipients_routing_spec.rb | 39 +++ spec/spec_helper.rb | 99 +++++++ spec/views/recipients/edit.html.erb_spec.rb | 42 +++ spec/views/recipients/index.html.erb_spec.rb | 43 +++ spec/views/recipients/new.html.erb_spec.rb | 42 +++ spec/views/recipients/show.html.erb_spec.rb | 30 ++ test/controllers/.keep | 0 test/fixtures/.keep | 0 test/fixtures/files/.keep | 0 test/helpers/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/test_helper.rb | 10 - 50 files changed, 1412 insertions(+), 37 deletions(-) create mode 100644 .rspec create mode 100644 app/assets/javascripts/recipients.coffee create mode 100644 app/assets/stylesheets/recipients.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/recipients_controller.rb create mode 100644 app/controllers/welcome_controller.rb create mode 100644 app/helpers/recipients_helper.rb create mode 100644 app/models/recipient.rb create mode 100644 app/models/user.rb create mode 100644 app/views/layouts/_header.html.haml delete mode 100644 app/views/layouts/application.html.erb create mode 100644 app/views/layouts/application.html.haml delete mode 100644 app/views/layouts/mailer.html.erb create mode 100644 app/views/layouts/mailer.html.haml create mode 100644 app/views/recipients/_form.html.erb create mode 100644 app/views/recipients/edit.html.erb create mode 100644 app/views/recipients/index.html.erb create mode 100644 app/views/recipients/index.json.jbuilder create mode 100644 app/views/recipients/new.html.erb create mode 100644 app/views/recipients/show.html.erb create mode 100644 app/views/recipients/show.json.jbuilder create mode 100644 app/views/welcome/index.html.haml create mode 100644 config/initializers/devise.rb create mode 100644 config/locales/devise.en.yml create mode 100644 db/migrate/20170224181252_devise_create_users.rb create mode 100644 db/migrate/20170225150432_create_recipients.rb create mode 100644 db/schema.rb create mode 100644 spec/controllers/recipients_controller_spec.rb create mode 100644 spec/controllers/welcome_controller_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/routing/recipients_routing_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/views/recipients/edit.html.erb_spec.rb create mode 100644 spec/views/recipients/index.html.erb_spec.rb create mode 100644 spec/views/recipients/new.html.erb_spec.rb create mode 100644 spec/views/recipients/show.html.erb_spec.rb delete mode 100644 test/controllers/.keep delete mode 100644 test/fixtures/.keep delete mode 100644 test/fixtures/files/.keep delete mode 100644 test/helpers/.keep delete mode 100644 test/integration/.keep delete mode 100644 test/mailers/.keep delete mode 100644 test/models/.keep delete mode 100644 test/test_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..83e16f80 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/Gemfile b/Gemfile index b7fdbb19..d8f91dc9 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,7 @@ end group 'test' do gem 'rspec-rails', '~> 3.5' + gem 'rails-controller-testing' gem 'capybara' gem 'phantomjs', require: 'phantomjs/poltergeist' gem 'poltergeist' diff --git a/Gemfile.lock b/Gemfile.lock index 4945192d..324659ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -147,6 +147,10 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 5.0.1) sprockets-rails (>= 2.0.0) + rails-controller-testing (0.1.1) + actionpack (~> 5.x) + actionview (~> 5.x) + activesupport (~> 5.x) rails-dom-testing (2.0.1) activesupport (>= 4.2.0, < 6.0) nokogiri (~> 1.6.0) @@ -258,6 +262,7 @@ DEPENDENCIES poltergeist puma (~> 3.0) rails (~> 5.0.1) + rails-controller-testing rspec-rails (~> 3.5) sass-rails (~> 5.0) seed_dump diff --git a/app/assets/javascripts/recipients.coffee b/app/assets/javascripts/recipients.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/recipients.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/recipients.scss b/app/assets/stylesheets/recipients.scss new file mode 100644 index 00000000..5266397c --- /dev/null +++ b/app/assets/stylesheets/recipients.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Recipients controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 00000000..4ce42667 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,89 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; + margin: 33px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; + margin: 33px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +th { + padding-bottom: 5px; +} + +td { + padding-bottom: 7px; + padding-left: 5px; + padding-right: 5px; +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} + +label { + display: block; +} diff --git a/app/controllers/recipients_controller.rb b/app/controllers/recipients_controller.rb new file mode 100644 index 00000000..be2db737 --- /dev/null +++ b/app/controllers/recipients_controller.rb @@ -0,0 +1,74 @@ +class RecipientsController < ApplicationController + before_action :set_recipient, only: [:show, :edit, :update, :destroy] + + # GET /recipients + # GET /recipients.json + def index + @recipients = Recipient.all + end + + # GET /recipients/1 + # GET /recipients/1.json + def show + end + + # GET /recipients/new + def new + @recipient = Recipient.new + end + + # GET /recipients/1/edit + def edit + end + + # POST /recipients + # POST /recipients.json + def create + @recipient = Recipient.new(recipient_params) + + respond_to do |format| + if @recipient.save + format.html { redirect_to @recipient, notice: 'Recipient was successfully created.' } + format.json { render :show, status: :created, location: @recipient } + else + format.html { render :new } + format.json { render json: @recipient.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /recipients/1 + # PATCH/PUT /recipients/1.json + def update + respond_to do |format| + if @recipient.update(recipient_params) + format.html { redirect_to @recipient, notice: 'Recipient was successfully updated.' } + format.json { render :show, status: :ok, location: @recipient } + else + format.html { render :edit } + format.json { render json: @recipient.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /recipients/1 + # DELETE /recipients/1.json + def destroy + @recipient.destroy + respond_to do |format| + format.html { redirect_to recipients_url, notice: 'Recipient was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_recipient + @recipient = Recipient.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def recipient_params + params.require(:recipient).permit(:name, :phone, :birth_date, :gender, :race, :ethnicity, :home_language_id, :income, :opted_out, :school_id) + end +end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb new file mode 100644 index 00000000..7d0e8050 --- /dev/null +++ b/app/controllers/welcome_controller.rb @@ -0,0 +1,6 @@ +class WelcomeController < ApplicationController + + def index + end + +end diff --git a/app/helpers/recipients_helper.rb b/app/helpers/recipients_helper.rb new file mode 100644 index 00000000..0595f089 --- /dev/null +++ b/app/helpers/recipients_helper.rb @@ -0,0 +1,2 @@ +module RecipientsHelper +end diff --git a/app/models/recipient.rb b/app/models/recipient.rb new file mode 100644 index 00000000..abf1da26 --- /dev/null +++ b/app/models/recipient.rb @@ -0,0 +1,2 @@ +class Recipient < ApplicationRecord +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..b2091f9a --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,6 @@ +class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable +end diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml new file mode 100644 index 00000000..b2209df7 --- /dev/null +++ b/app/views/layouts/_header.html.haml @@ -0,0 +1,38 @@ +%header.row + .title.col.s9.m11.l8 + .hide-on-small-only + %h5= link_to('Thoughtful
Recommendations.com'.html_safe, root_path) + .hide-on-med-and-up + %h6= link_to('Thoughtful
Recommendations.com'.html_safe, root_path) + + .navigation.col.hide-on-med-and-down.l4.right-align + - if current_user + - unless @limited_header + - if current_user.profile.blank? + = link_to 'Create Profile', new_profile_path, class: 'btn btn-small grey waves-effect waves-light ' + - else + = link_to 'My Profile', direct_profile_path(current_user.profile), class: 'btn btn-small grey waves-effect waves-light ' + = link_to 'My Bucketlist', bucketlist_path, class: 'btn btn-small grey waves-effect waves-light ' + + = link_to 'Sign Out', destroy_user_session_path, rel: "nofollow", method: :delete, class: 'btn btn-small grey waves-effect waves-light ' + - else + = link_to 'Login', new_user_session_path, class: 'btn btn-small grey waves-effect waves-light ' + = link_to 'Contribute / Sign Up', contribute_profile_recommendation_path, class: 'btn btn-small grey waves-effect waves-light ' + + .col.s3.m1.hide-on-large-only.right-align + = link_to ''.html_safe, '#', data: {activates: 'mobile-nav'}, class: 'button-collapse' + #mobile-nav.side-nav + %ul + - if current_user + - unless @limited_header + - if current_user.profile.present? + %li= link_to 'My Profile', direct_profile_path(current_user.profile) + %li= link_to 'My Bucketlist', bucketlist_path + %li= link_to 'Add Something', new_profile_recommendation_path + - else + %li= link_to 'Create Profile', new_profile_path + + %li= link_to 'Sign Out', destroy_user_session_path, rel: "nofollow", method: :delete + - else + %li= link_to 'Login', new_user_session_path + %li= link_to 'Contribute / Sign Up', contribute_profile_recommendation_path diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb deleted file mode 100644 index 9e76f283..00000000 --- a/app/views/layouts/application.html.erb +++ /dev/null @@ -1,14 +0,0 @@ - - - - Edcontext - <%= csrf_meta_tags %> - - <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> - - - - <%= yield %> - - diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml new file mode 100644 index 00000000..eee28d59 --- /dev/null +++ b/app/views/layouts/application.html.haml @@ -0,0 +1,12 @@ +!!! +%html + %head + %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/ + %title Edcontext + = csrf_meta_tags + = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' + = javascript_include_tag 'application', 'data-turbolinks-track': 'reload' + %body + %p.notice= notice + %p.alert= alert + = yield diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb deleted file mode 100644 index cbd34d2e..00000000 --- a/app/views/layouts/mailer.html.erb +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - <%= yield %> - - diff --git a/app/views/layouts/mailer.html.haml b/app/views/layouts/mailer.html.haml new file mode 100644 index 00000000..cbf6b8e2 --- /dev/null +++ b/app/views/layouts/mailer.html.haml @@ -0,0 +1,8 @@ +!!! +%html + %head + %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/ + :css + /* Email styles need to be inline */ + %body + = yield diff --git a/app/views/recipients/_form.html.erb b/app/views/recipients/_form.html.erb new file mode 100644 index 00000000..c9bcf414 --- /dev/null +++ b/app/views/recipients/_form.html.erb @@ -0,0 +1,67 @@ +<%= form_for(recipient) do |f| %> + <% if recipient.errors.any? %> +
+

<%= pluralize(recipient.errors.count, "error") %> prohibited this recipient from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :name %> + <%= f.text_field :name %> +
+ +
+ <%= f.label :phone %> + <%= f.text_field :phone %> +
+ +
+ <%= f.label :birth_date %> + <%= f.date_select :birth_date %> +
+ +
+ <%= f.label :gender %> + <%= f.text_field :gender %> +
+ +
+ <%= f.label :race %> + <%= f.text_field :race %> +
+ +
+ <%= f.label :ethnicity %> + <%= f.text_field :ethnicity %> +
+ +
+ <%= f.label :home_language_id %> + <%= f.number_field :home_language_id %> +
+ +
+ <%= f.label :income %> + <%= f.text_field :income %> +
+ +
+ <%= f.label :opted_out %> + <%= f.check_box :opted_out %> +
+ +
+ <%= f.label :school_id %> + <%= f.number_field :school_id %> +
+ +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/recipients/edit.html.erb b/app/views/recipients/edit.html.erb new file mode 100644 index 00000000..7379e5c5 --- /dev/null +++ b/app/views/recipients/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Recipient

+ +<%= render 'form', recipient: @recipient %> + +<%= link_to 'Show', @recipient %> | +<%= link_to 'Back', recipients_path %> diff --git a/app/views/recipients/index.html.erb b/app/views/recipients/index.html.erb new file mode 100644 index 00000000..c51fdf72 --- /dev/null +++ b/app/views/recipients/index.html.erb @@ -0,0 +1,45 @@ +

<%= notice %>

+ +

Recipients

+ + + + + + + + + + + + + + + + + + + + <% @recipients.each do |recipient| %> + + + + + + + + + + + + + + + + <% end %> + +
NamePhoneBirth dateGenderRaceEthnicityHome languageIncomeOpted outSchool
<%= recipient.name %><%= recipient.phone %><%= recipient.birth_date %><%= recipient.gender %><%= recipient.race %><%= recipient.ethnicity %><%= recipient.home_language_id %><%= recipient.income %><%= recipient.opted_out %><%= recipient.school_id %><%= link_to 'Show', recipient %><%= link_to 'Edit', edit_recipient_path(recipient) %><%= link_to 'Destroy', recipient, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Recipient', new_recipient_path %> diff --git a/app/views/recipients/index.json.jbuilder b/app/views/recipients/index.json.jbuilder new file mode 100644 index 00000000..7f924b2c --- /dev/null +++ b/app/views/recipients/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@recipients) do |recipient| + json.extract! recipient, :id, :name, :phone, :birth_date, :gender, :race, :ethnicity, :home_language_id, :income, :opted_out, :school_id + json.url recipient_url(recipient, format: :json) +end diff --git a/app/views/recipients/new.html.erb b/app/views/recipients/new.html.erb new file mode 100644 index 00000000..7ed1313d --- /dev/null +++ b/app/views/recipients/new.html.erb @@ -0,0 +1,5 @@ +

New Recipient

+ +<%= render 'form', recipient: @recipient %> + +<%= link_to 'Back', recipients_path %> diff --git a/app/views/recipients/show.html.erb b/app/views/recipients/show.html.erb new file mode 100644 index 00000000..476126f0 --- /dev/null +++ b/app/views/recipients/show.html.erb @@ -0,0 +1,54 @@ +

<%= notice %>

+ +

+ Name: + <%= @recipient.name %> +

+ +

+ Phone: + <%= @recipient.phone %> +

+ +

+ Birth date: + <%= @recipient.birth_date %> +

+ +

+ Gender: + <%= @recipient.gender %> +

+ +

+ Race: + <%= @recipient.race %> +

+ +

+ Ethnicity: + <%= @recipient.ethnicity %> +

+ +

+ Home language: + <%= @recipient.home_language_id %> +

+ +

+ Income: + <%= @recipient.income %> +

+ +

+ Opted out: + <%= @recipient.opted_out %> +

+ +

+ School: + <%= @recipient.school_id %> +

+ +<%= link_to 'Edit', edit_recipient_path(@recipient) %> | +<%= link_to 'Back', recipients_path %> diff --git a/app/views/recipients/show.json.jbuilder b/app/views/recipients/show.json.jbuilder new file mode 100644 index 00000000..473c6508 --- /dev/null +++ b/app/views/recipients/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @recipient, :id, :name, :phone, :birth_date, :gender, :race, :ethnicity, :home_language_id, :income, :opted_out, :school_id, :created_at, :updated_at diff --git a/app/views/welcome/index.html.haml b/app/views/welcome/index.html.haml new file mode 100644 index 00000000..20bf9702 --- /dev/null +++ b/app/views/welcome/index.html.haml @@ -0,0 +1 @@ +WELCOME diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f719704..75360736 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -51,4 +51,6 @@ Rails.application.configure do # 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 + + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } end diff --git a/config/environments/production.rb b/config/environments/production.rb index 08c5c1c3..cf0f8a29 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -83,4 +83,6 @@ Rails.application.configure do # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + config.action_mailer.default_url_options = { host: 'edcontext.org' } end diff --git a/config/environments/test.rb b/config/environments/test.rb index 30587ef6..27721380 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -39,4 +39,6 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb new file mode 100644 index 00000000..5e572a8b --- /dev/null +++ b/config/initializers/devise.rb @@ -0,0 +1,274 @@ +# Use this hook to configure devise mailer, warden hooks and so forth. +# Many of these configuration options can be set straight in your model. +Devise.setup do |config| + # The secret key used by Devise. Devise uses this key to generate + # random tokens. Changing this key will render invalid all existing + # confirmation, reset password and unlock tokens in the database. + # Devise will use the `secret_key_base` as its `secret_key` + # by default. You can change it below and use your own secret key. + # config.secret_key = 'cbe71dbfb7621e0dae495b43107ec6250d4ace32da7f80398990c6dd818d8a1c010077843595008d987f88d1c6042ac19ce981356bb09604fd381caf1ce20976' + + # ==> Mailer Configuration + # Configure the e-mail address which will be shown in Devise::Mailer, + # note that it will be overwritten if you use your own mailer class + # with default "from" parameter. + config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' + + # Configure the class responsible to send e-mails. + # config.mailer = 'Devise::Mailer' + + # Configure the parent class responsible to send e-mails. + # config.parent_mailer = 'ActionMailer::Base' + + # ==> ORM configuration + # Load and configure the ORM. Supports :active_record (default) and + # :mongoid (bson_ext recommended) by default. Other ORMs may be + # available as additional gems. + require 'devise/orm/active_record' + + # ==> Configuration for any authentication mechanism + # Configure which keys are used when authenticating a user. The default is + # just :email. You can configure it to use [:username, :subdomain], so for + # authenticating a user, both parameters are required. Remember that those + # parameters are used only when authenticating and not when retrieving from + # session. If you need permissions, you should implement that in a before filter. + # You can also supply a hash where the value is a boolean determining whether + # or not authentication should be aborted when the value is not present. + # config.authentication_keys = [:email] + + # Configure parameters from the request object used for authentication. Each entry + # given should be a request method and it will automatically be passed to the + # find_for_authentication method and considered in your model lookup. For instance, + # if you set :request_keys to [:subdomain], :subdomain will be used on authentication. + # The same considerations mentioned for authentication_keys also apply to request_keys. + # config.request_keys = [] + + # Configure which authentication keys should be case-insensitive. + # These keys will be downcased upon creating or modifying a user and when used + # to authenticate or find a user. Default is :email. + config.case_insensitive_keys = [:email] + + # Configure which authentication keys should have whitespace stripped. + # These keys will have whitespace before and after removed upon creating or + # modifying a user and when used to authenticate or find a user. Default is :email. + config.strip_whitespace_keys = [:email] + + # Tell if authentication through request.params is enabled. True by default. + # It can be set to an array that will enable params authentication only for the + # given strategies, for example, `config.params_authenticatable = [:database]` will + # enable it only for database (email + password) authentication. + # config.params_authenticatable = true + + # Tell if authentication through HTTP Auth is enabled. False by default. + # It can be set to an array that will enable http authentication only for the + # given strategies, for example, `config.http_authenticatable = [:database]` will + # enable it only for database authentication. The supported strategies are: + # :database = Support basic authentication with authentication key + password + # config.http_authenticatable = false + + # If 401 status code should be returned for AJAX requests. True by default. + # config.http_authenticatable_on_xhr = true + + # The realm used in Http Basic Authentication. 'Application' by default. + # config.http_authentication_realm = 'Application' + + # It will change confirmation, password recovery and other workflows + # to behave the same regardless if the e-mail provided was right or wrong. + # Does not affect registerable. + # config.paranoid = true + + # By default Devise will store the user in session. You can skip storage for + # particular strategies by setting this option. + # Notice that if you are skipping storage for all authentication paths, you + # may want to disable generating routes to Devise's sessions controller by + # passing skip: :sessions to `devise_for` in your config/routes.rb + config.skip_session_storage = [:http_auth] + + # By default, Devise cleans up the CSRF token on authentication to + # avoid CSRF token fixation attacks. This means that, when using AJAX + # requests for sign in and sign up, you need to get a new CSRF token + # from the server. You can disable this option at your own risk. + # config.clean_up_csrf_token_on_authentication = true + + # When false, Devise will not attempt to reload routes on eager load. + # This can reduce the time taken to boot the app but if your application + # requires the Devise mappings to be loaded during boot time the application + # won't boot properly. + # config.reload_routes = true + + # ==> Configuration for :database_authenticatable + # For bcrypt, this is the cost for hashing the password and defaults to 11. If + # using other algorithms, it sets how many times you want the password to be hashed. + # + # Limiting the stretches to just one in testing will increase the performance of + # your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use + # a value less than 10 in other environments. Note that, for bcrypt (the default + # algorithm), the cost increases exponentially with the number of stretches (e.g. + # a value of 20 is already extremely slow: approx. 60 seconds for 1 calculation). + config.stretches = Rails.env.test? ? 1 : 11 + + # Set up a pepper to generate the hashed password. + # config.pepper = '7066cd86cbe24109d4de02e8cb3a2c436d4a9a00def3d476822d005fa3fff6db10cae2fc9cfd4c3d5ec547e96bad30634b896dc7e7880896386962c4235907f1' + + # Send a notification email when the user's password is changed + # config.send_password_change_notification = false + + # ==> Configuration for :confirmable + # A period that the user is allowed to access the website even without + # confirming their account. For instance, if set to 2.days, the user will be + # able to access the website for two days without confirming their account, + # access will be blocked just in the third day. Default is 0.days, meaning + # the user cannot access the website without confirming their account. + # config.allow_unconfirmed_access_for = 2.days + + # A period that the user is allowed to confirm their account before their + # token becomes invalid. For example, if set to 3.days, the user can confirm + # their account within 3 days after the mail was sent, but on the fourth day + # their account can't be confirmed with the token any more. + # Default is nil, meaning there is no restriction on how long a user can take + # before confirming their account. + # config.confirm_within = 3.days + + # If true, requires any email changes to be confirmed (exactly the same way as + # initial account confirmation) to be applied. Requires additional unconfirmed_email + # db field (see migrations). Until confirmed, new email is stored in + # unconfirmed_email column, and copied to email column on successful confirmation. + config.reconfirmable = true + + # Defines which key will be used when confirming an account + # config.confirmation_keys = [:email] + + # ==> Configuration for :rememberable + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # Invalidates all the remember me tokens when the user signs out. + config.expire_all_remember_me_on_sign_out = true + + # If true, extends the user's remember period when remembered via cookie. + # config.extend_remember_period = false + + # Options to be passed to the created cookie. For instance, you can set + # secure: true in order to force SSL only cookies. + # config.rememberable_options = {} + + # ==> Configuration for :validatable + # Range for password length. + config.password_length = 6..128 + + # Email regex used to validate email formats. It simply asserts that + # one (and only one) @ exists in the given string. This is mainly + # to give user feedback and not to assert the e-mail validity. + config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ + + # ==> Configuration for :timeoutable + # The time you want to timeout the user session without activity. After this + # time the user will be asked for credentials again. Default is 30 minutes. + # config.timeout_in = 30.minutes + + # ==> Configuration for :lockable + # Defines which strategy will be used to lock an account. + # :failed_attempts = Locks an account after a number of failed attempts to sign in. + # :none = No lock strategy. You should handle locking by yourself. + # config.lock_strategy = :failed_attempts + + # Defines which key will be used when locking and unlocking an account + # config.unlock_keys = [:email] + + # Defines which strategy will be used to unlock an account. + # :email = Sends an unlock link to the user email + # :time = Re-enables login after a certain amount of time (see :unlock_in below) + # :both = Enables both strategies + # :none = No unlock strategy. You should handle unlocking by yourself. + # config.unlock_strategy = :both + + # Number of authentication tries before locking an account if lock_strategy + # is failed attempts. + # config.maximum_attempts = 20 + + # Time interval to unlock the account if :time is enabled as unlock_strategy. + # config.unlock_in = 1.hour + + # Warn on the last attempt before the account is locked. + # config.last_attempt_warning = true + + # ==> Configuration for :recoverable + # + # Defines which key will be used when recovering the password for an account + # config.reset_password_keys = [:email] + + # Time interval you can reset your password with a reset password key. + # Don't put a too small interval or your users won't have the time to + # change their passwords. + config.reset_password_within = 6.hours + + # When set to false, does not sign a user in automatically after their password is + # reset. Defaults to true, so a user is signed in automatically after a reset. + # config.sign_in_after_reset_password = true + + # ==> Configuration for :encryptable + # Allow you to use another hashing or encryption algorithm besides bcrypt (default). + # You can use :sha1, :sha512 or algorithms from others authentication tools as + # :clearance_sha1, :authlogic_sha512 (then you should set stretches above to 20 + # for default behavior) and :restful_authentication_sha1 (then you should set + # stretches to 10, and copy REST_AUTH_SITE_KEY to pepper). + # + # Require the `devise-encryptable` gem when using anything other than bcrypt + # config.encryptor = :sha512 + + # ==> Scopes configuration + # Turn scoped views on. Before rendering "sessions/new", it will first check for + # "users/sessions/new". It's turned off by default because it's slower if you + # are using only default views. + # config.scoped_views = false + + # Configure the default scope given to Warden. By default it's the first + # devise role declared in your routes (usually :user). + # config.default_scope = :user + + # Set this configuration to false if you want /users/sign_out to sign out + # only the current scope. By default, Devise signs out all scopes. + # config.sign_out_all_scopes = true + + # ==> Navigation configuration + # Lists the formats that should be treated as navigational. Formats like + # :html, should redirect to the sign in page when the user does not have + # access, but formats like :xml or :json, should return 401. + # + # If you have any extra navigational formats, like :iphone or :mobile, you + # should add them to the navigational formats lists. + # + # The "*/*" below is required to match Internet Explorer requests. + # config.navigational_formats = ['*/*', :html] + + # The default HTTP method used to sign out a resource. Default is :delete. + config.sign_out_via = :delete + + # ==> OmniAuth + # Add a new OmniAuth provider. Check the wiki for more information on setting + # up on your models and hooks. + # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' + + # ==> Warden configuration + # If you want to use other strategies, that are not supported by Devise, or + # change the failure app, you can configure them inside the config.warden block. + # + # config.warden do |manager| + # manager.intercept_401 = false + # manager.default_strategies(scope: :user).unshift :some_external_strategy + # end + + # ==> Mountable engine configurations + # When using Devise inside an engine, let's call it `MyEngine`, and this engine + # is mountable, there are some extra configurations to be taken into account. + # The following options are available, assuming the engine is mounted as: + # + # mount MyEngine, at: '/my_engine' + # + # The router that invoked `devise_for`, in the example above, would be: + # config.router_name = :my_engine + # + # When using OmniAuth, Devise cannot automatically set OmniAuth path, + # so you need to do it manually. For the users scope, it would be: + # config.omniauth_path_prefix = '/my_engine/users/auth' +end diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml new file mode 100644 index 00000000..bd4c3ebc --- /dev/null +++ b/config/locales/devise.en.yml @@ -0,0 +1,62 @@ +# Additional translations at https://github.com/plataformatec/devise/wiki/I18n + +en: + devise: + confirmations: + confirmed: "Your email address has been successfully confirmed." + send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes." + failure: + already_authenticated: "You are already signed in." + inactive: "Your account is not activated yet." + invalid: "Invalid %{authentication_keys} or password." + locked: "Your account is locked." + last_attempt: "You have one more attempt before your account is locked." + not_found_in_database: "Invalid %{authentication_keys} or password." + timeout: "Your session expired. Please sign in again to continue." + unauthenticated: "You need to sign in or sign up before continuing." + unconfirmed: "You have to confirm your email address before continuing." + mailer: + confirmation_instructions: + subject: "Confirmation instructions" + reset_password_instructions: + subject: "Reset password instructions" + unlock_instructions: + subject: "Unlock instructions" + password_change: + subject: "Password Changed" + omniauth_callbacks: + failure: "Could not authenticate you from %{kind} because \"%{reason}\"." + success: "Successfully authenticated from %{kind} account." + passwords: + no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided." + send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes." + send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes." + updated: "Your password has been changed successfully. You are now signed in." + updated_not_active: "Your password has been changed successfully." + registrations: + destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon." + signed_up: "Welcome! You have signed up successfully." + signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated." + signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked." + signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account." + update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirm link to confirm your new email address." + updated: "Your account has been updated successfully." + sessions: + signed_in: "Signed in successfully." + signed_out: "Signed out successfully." + already_signed_out: "Signed out successfully." + unlocks: + send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes." + send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes." + unlocked: "Your account has been unlocked successfully. Please sign in to continue." + errors: + messages: + already_confirmed: "was already confirmed, please try signing in" + confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one" + expired: "has expired, please request a new one" + not_found: "not found" + not_locked: "was not locked" + not_saved: + one: "1 error prohibited this %{resource} from being saved:" + other: "%{count} errors prohibited this %{resource} from being saved:" diff --git a/config/routes.rb b/config/routes.rb index 787824f8..0461e809 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,7 @@ Rails.application.routes.draw do + resources :recipients + devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + + root to: "welcome#index" end diff --git a/db/migrate/20170224181252_devise_create_users.rb b/db/migrate/20170224181252_devise_create_users.rb new file mode 100644 index 00000000..f577233a --- /dev/null +++ b/db/migrate/20170224181252_devise_create_users.rb @@ -0,0 +1,42 @@ +class DeviseCreateUsers < ActiveRecord::Migration[5.0] + def change + create_table :users do |t| + ## Database authenticatable + t.string :email, null: false, default: "" + t.string :encrypted_password, null: false, default: "" + + ## Recoverable + t.string :reset_password_token + t.datetime :reset_password_sent_at + + ## Rememberable + t.datetime :remember_created_at + + ## Trackable + t.integer :sign_in_count, default: 0, null: false + t.datetime :current_sign_in_at + t.datetime :last_sign_in_at + t.inet :current_sign_in_ip + t.inet :last_sign_in_ip + + ## Confirmable + # t.string :confirmation_token + # t.datetime :confirmed_at + # t.datetime :confirmation_sent_at + # t.string :unconfirmed_email # Only if using reconfirmable + + ## Lockable + # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts + # t.string :unlock_token # Only if unlock strategy is :email or :both + # t.datetime :locked_at + + + t.timestamps null: false + end + + add_index :users, :email, unique: true + add_index :users, :reset_password_token, unique: true + # add_index :users, :confirmation_token, unique: true + # add_index :users, :unlock_token, unique: true + end +end diff --git a/db/migrate/20170225150432_create_recipients.rb b/db/migrate/20170225150432_create_recipients.rb new file mode 100644 index 00000000..b749e24c --- /dev/null +++ b/db/migrate/20170225150432_create_recipients.rb @@ -0,0 +1,18 @@ +class CreateRecipients < ActiveRecord::Migration[5.0] + def change + create_table :recipients do |t| + t.string :name + t.string :phone + t.date :birth_date + t.string :gender + t.string :race + t.string :ethnicity + t.integer :home_language_id + t.string :income + t.boolean :opted_out + t.integer :school_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..ce57e096 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,50 @@ +# 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: 20170225150432) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "recipients", force: :cascade do |t| + t.string "name" + t.string "phone" + t.date "birth_date" + t.string "gender" + t.string "race" + t.string "ethnicity" + t.integer "home_language_id" + t.string "income" + t.boolean "opted_out" + t.integer "school_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "users", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0, null: false + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.inet "current_sign_in_ip" + t.inet "last_sign_in_ip" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["email"], name: "index_users_on_email", unique: true, using: :btree + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree + end + +end diff --git a/spec/controllers/recipients_controller_spec.rb b/spec/controllers/recipients_controller_spec.rb new file mode 100644 index 00000000..be2daa76 --- /dev/null +++ b/spec/controllers/recipients_controller_spec.rb @@ -0,0 +1,159 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +RSpec.describe RecipientsController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # Recipient. As you add validations to Recipient, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # RecipientsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "assigns all recipients as @recipients" do + recipient = Recipient.create! valid_attributes + get :index, params: {}, session: valid_session + expect(assigns(:recipients)).to eq([recipient]) + end + end + + describe "GET #show" do + it "assigns the requested recipient as @recipient" do + recipient = Recipient.create! valid_attributes + get :show, params: {id: recipient.to_param}, session: valid_session + expect(assigns(:recipient)).to eq(recipient) + end + end + + describe "GET #new" do + it "assigns a new recipient as @recipient" do + get :new, params: {}, session: valid_session + expect(assigns(:recipient)).to be_a_new(Recipient) + end + end + + describe "GET #edit" do + it "assigns the requested recipient as @recipient" do + recipient = Recipient.create! valid_attributes + get :edit, params: {id: recipient.to_param}, session: valid_session + expect(assigns(:recipient)).to eq(recipient) + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new Recipient" do + expect { + post :create, params: {recipient: valid_attributes}, session: valid_session + }.to change(Recipient, :count).by(1) + end + + it "assigns a newly created recipient as @recipient" do + post :create, params: {recipient: valid_attributes}, session: valid_session + expect(assigns(:recipient)).to be_a(Recipient) + expect(assigns(:recipient)).to be_persisted + end + + it "redirects to the created recipient" do + post :create, params: {recipient: valid_attributes}, session: valid_session + expect(response).to redirect_to(Recipient.last) + end + end + + context "with invalid params" do + it "assigns a newly created but unsaved recipient as @recipient" do + post :create, params: {recipient: invalid_attributes}, session: valid_session + expect(assigns(:recipient)).to be_a_new(Recipient) + end + + it "re-renders the 'new' template" do + post :create, params: {recipient: invalid_attributes}, session: valid_session + expect(response).to render_template("new") + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested recipient" do + recipient = Recipient.create! valid_attributes + put :update, params: {id: recipient.to_param, recipient: new_attributes}, session: valid_session + recipient.reload + skip("Add assertions for updated state") + end + + it "assigns the requested recipient as @recipient" do + recipient = Recipient.create! valid_attributes + put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session + expect(assigns(:recipient)).to eq(recipient) + end + + it "redirects to the recipient" do + recipient = Recipient.create! valid_attributes + put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session + expect(response).to redirect_to(recipient) + end + end + + context "with invalid params" do + it "assigns the recipient as @recipient" do + recipient = Recipient.create! valid_attributes + put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session + expect(assigns(:recipient)).to eq(recipient) + end + + it "re-renders the 'edit' template" do + recipient = Recipient.create! valid_attributes + put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session + expect(response).to render_template("edit") + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested recipient" do + recipient = Recipient.create! valid_attributes + expect { + delete :destroy, params: {id: recipient.to_param}, session: valid_session + }.to change(Recipient, :count).by(-1) + end + + it "redirects to the recipients list" do + recipient = Recipient.create! valid_attributes + delete :destroy, params: {id: recipient.to_param}, session: valid_session + expect(response).to redirect_to(recipients_url) + end + end + +end diff --git a/spec/controllers/welcome_controller_spec.rb b/spec/controllers/welcome_controller_spec.rb new file mode 100644 index 00000000..24f2b876 --- /dev/null +++ b/spec/controllers/welcome_controller_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe WelcomeController, type: :controller do + + describe "GET #index" do + it "works" do + get :index + end + end + +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..6f1ab146 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,57 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migration and applies them before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/routing/recipients_routing_spec.rb b/spec/routing/recipients_routing_spec.rb new file mode 100644 index 00000000..1dc4d291 --- /dev/null +++ b/spec/routing/recipients_routing_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe RecipientsController, type: :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/recipients").to route_to("recipients#index") + end + + it "routes to #new" do + expect(:get => "/recipients/new").to route_to("recipients#new") + end + + it "routes to #show" do + expect(:get => "/recipients/1").to route_to("recipients#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/recipients/1/edit").to route_to("recipients#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/recipients").to route_to("recipients#create") + end + + it "routes to #update via PUT" do + expect(:put => "/recipients/1").to route_to("recipients#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/recipients/1").to route_to("recipients#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/recipients/1").to route_to("recipients#destroy", :id => "1") + end + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..8f698be4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,99 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/spec/views/recipients/edit.html.erb_spec.rb b/spec/views/recipients/edit.html.erb_spec.rb new file mode 100644 index 00000000..4c6310bd --- /dev/null +++ b/spec/views/recipients/edit.html.erb_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +RSpec.describe "recipients/edit", type: :view do + before(:each) do + @recipient = assign(:recipient, Recipient.create!( + :name => "MyString", + :phone => "MyString", + :gender => "MyString", + :race => "MyString", + :ethnicity => "MyString", + :home_language_id => 1, + :income => "MyString", + :opted_out => false, + :school_id => 1 + )) + end + + it "renders the edit recipient form" do + render + + assert_select "form[action=?][method=?]", recipient_path(@recipient), "post" do + + assert_select "input#recipient_name[name=?]", "recipient[name]" + + assert_select "input#recipient_phone[name=?]", "recipient[phone]" + + assert_select "input#recipient_gender[name=?]", "recipient[gender]" + + assert_select "input#recipient_race[name=?]", "recipient[race]" + + assert_select "input#recipient_ethnicity[name=?]", "recipient[ethnicity]" + + assert_select "input#recipient_home_language_id[name=?]", "recipient[home_language_id]" + + assert_select "input#recipient_income[name=?]", "recipient[income]" + + assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]" + + assert_select "input#recipient_school_id[name=?]", "recipient[school_id]" + end + end +end diff --git a/spec/views/recipients/index.html.erb_spec.rb b/spec/views/recipients/index.html.erb_spec.rb new file mode 100644 index 00000000..b284f4ed --- /dev/null +++ b/spec/views/recipients/index.html.erb_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe "recipients/index", type: :view do + before(:each) do + assign(:recipients, [ + Recipient.create!( + :name => "Name", + :phone => "Phone", + :gender => "Gender", + :race => "Race", + :ethnicity => "Ethnicity", + :home_language_id => 2, + :income => "Income", + :opted_out => false, + :school_id => 3 + ), + Recipient.create!( + :name => "Name", + :phone => "Phone", + :gender => "Gender", + :race => "Race", + :ethnicity => "Ethnicity", + :home_language_id => 2, + :income => "Income", + :opted_out => false, + :school_id => 3 + ) + ]) + end + + it "renders a list of recipients" do + render + assert_select "tr>td", :text => "Name".to_s, :count => 2 + assert_select "tr>td", :text => "Phone".to_s, :count => 2 + assert_select "tr>td", :text => "Gender".to_s, :count => 2 + assert_select "tr>td", :text => "Race".to_s, :count => 2 + assert_select "tr>td", :text => "Ethnicity".to_s, :count => 2 + assert_select "tr>td", :text => 2.to_s, :count => 2 + assert_select "tr>td", :text => "Income".to_s, :count => 2 + assert_select "tr>td", :text => false.to_s, :count => 2 + assert_select "tr>td", :text => 3.to_s, :count => 2 + end +end diff --git a/spec/views/recipients/new.html.erb_spec.rb b/spec/views/recipients/new.html.erb_spec.rb new file mode 100644 index 00000000..255ec613 --- /dev/null +++ b/spec/views/recipients/new.html.erb_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +RSpec.describe "recipients/new", type: :view do + before(:each) do + assign(:recipient, Recipient.new( + :name => "MyString", + :phone => "MyString", + :gender => "MyString", + :race => "MyString", + :ethnicity => "MyString", + :home_language_id => 1, + :income => "MyString", + :opted_out => false, + :school_id => 1 + )) + end + + it "renders new recipient form" do + render + + assert_select "form[action=?][method=?]", recipients_path, "post" do + + assert_select "input#recipient_name[name=?]", "recipient[name]" + + assert_select "input#recipient_phone[name=?]", "recipient[phone]" + + assert_select "input#recipient_gender[name=?]", "recipient[gender]" + + assert_select "input#recipient_race[name=?]", "recipient[race]" + + assert_select "input#recipient_ethnicity[name=?]", "recipient[ethnicity]" + + assert_select "input#recipient_home_language_id[name=?]", "recipient[home_language_id]" + + assert_select "input#recipient_income[name=?]", "recipient[income]" + + assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]" + + assert_select "input#recipient_school_id[name=?]", "recipient[school_id]" + end + end +end diff --git a/spec/views/recipients/show.html.erb_spec.rb b/spec/views/recipients/show.html.erb_spec.rb new file mode 100644 index 00000000..9921821b --- /dev/null +++ b/spec/views/recipients/show.html.erb_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe "recipients/show", type: :view do + before(:each) do + @recipient = assign(:recipient, Recipient.create!( + :name => "Name", + :phone => "Phone", + :gender => "Gender", + :race => "Race", + :ethnicity => "Ethnicity", + :home_language_id => 2, + :income => "Income", + :opted_out => false, + :school_id => 3 + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/Name/) + expect(rendered).to match(/Phone/) + expect(rendered).to match(/Gender/) + expect(rendered).to match(/Race/) + expect(rendered).to match(/Ethnicity/) + expect(rendered).to match(/2/) + expect(rendered).to match(/Income/) + expect(rendered).to match(/false/) + expect(rendered).to match(/3/) + end +end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 92e39b2d..00000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end