Commit cec2bdf2 by tady

Merge branch 'wip/feat/1225'

parents f0482b07 847f145c
--color
source 'https://rubygems.org'
ruby '2.0.0'
ruby '2.1.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 4.0.2'
......@@ -65,8 +65,24 @@ group :development do
gem 'thin'
# gem 'capistrano', '~> 3.0.1'
gem 'pry-rails'
end
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'guard-spring'
gem 'factory_girl_rails'
gem 'spring'
end
group :test do
gem 'email_spec'
end
group :production do
gem 'rails_12factor'
gem 'pg'
......@@ -82,3 +98,9 @@ gem 'rack-mini-profiler'
# ref. https://github.com/popgiro/action-gmailer
gem 'mail'
gem 'action-gmailer', github: 'popgiro/action-gmailer'
# compose html mail
gem 'nokogiri'
gem 'premailer'
gem 'faraday'
......@@ -41,6 +41,7 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.5)
ancestry (2.0.0)
activerecord (>= 3.0.0)
arel (4.0.1)
......@@ -52,6 +53,8 @@ GEM
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.1.4)
celluloid (0.15.2)
timers (~> 1.1.0)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
......@@ -60,6 +63,8 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
css_parser (1.3.5)
addressable
daemons (1.1.9)
debug_inspector (0.0.2)
devise (3.2.2)
......@@ -68,15 +73,39 @@ GEM
railties (>= 3.2.6, < 5)
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.2.5)
email_spec (1.5.0)
launchy (~> 2.1)
mail (~> 2.2)
erubis (2.7.0)
eventmachine (1.0.3)
execjs (2.0.2)
factory_girl (4.3.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.3.0)
factory_girl (~> 4.3.0)
railties (>= 3.0.0)
faraday (0.8.8)
multipart-post (~> 1.2.0)
ffi (1.9.3)
formatador (0.2.4)
gmail_xoauth (0.4.1)
oauth (>= 0.3.6)
guard (2.2.4)
formatador (>= 0.2.4)
listen (~> 2.1)
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-rspec (4.0.4)
guard (>= 2.1.1)
rspec (~> 2.14)
guard-spring (0.0.4)
guard
spring
hashie (2.0.5)
hike (1.2.3)
htmlentities (4.3.1)
httpauth (0.2.0)
i18n (0.6.9)
jbuilder (1.5.3)
......@@ -85,13 +114,24 @@ GEM
json (1.8.1)
jwt (0.1.8)
multi_json (>= 1.5)
launchy (2.4.2)
addressable (~> 2.3)
listen (2.2.0)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.4)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25.1)
mini_portile (0.5.2)
minitest (4.7.5)
multi_json (1.8.2)
multipart-post (1.2.0)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
oauth (0.4.7)
oauth2 (0.8.1)
faraday (~> 0.8)
......@@ -111,6 +151,15 @@ GEM
orm_adapter (0.5.0)
pg (0.17.0)
polyglot (0.3.3)
premailer (1.7.9)
css_parser (>= 1.1.9)
htmlentities (>= 4.0.0)
pry (0.9.12.3)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
pry-rails (0.3.2)
pry (>= 0.9.10)
rack (1.5.2)
rack-mini-profiler (0.1.31)
rack (>= 1.1.3)
......@@ -135,8 +184,26 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.1.0)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
ffi (>= 0.5.0)
rdoc (3.12.2)
json (~> 1.4)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
rspec-rails (2.14.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
sass (3.2.12)
sass-rails (4.0.1)
railties (>= 4.0.0, < 5.0)
......@@ -145,6 +212,8 @@ GEM
sdoc (0.3.20)
json (>= 1.1.3)
rdoc (~> 3.10)
slop (3.4.7)
spring (1.0.0)
sprockets (2.10.1)
hike (~> 1.2)
multi_json (~> 1.0)
......@@ -163,6 +232,7 @@ GEM
thread_safe (0.1.3)
atomic
tilt (1.4.1)
timers (1.1.0)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
......@@ -184,16 +254,26 @@ DEPENDENCIES
coderay
coffee-rails (~> 4.0.0)
devise
email_spec
factory_girl_rails
faraday
guard-rspec
guard-spring
jbuilder (~> 1.2)
mail
nokogiri
omniauth-google-oauth2
pg
premailer
pry-rails
rack-mini-profiler
rails (~> 4.0.2)
rails_12factor
redcarpet!
rspec-rails
sass-rails (~> 4.0.0)
sdoc
spring
sqlite3
thin
uglifier (>= 1.3.0)
guard :rspec, spring: true do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^spec/factories/(.+)\.rb$}) { 'spec/factories_spec.rb' }
watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
watch('config/routes.rb') { 'spec/routing' }
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
end
if window.location.pathname.match /^\/$/
if window.location.pathname.match /^\/posts\/?$/
$ ->
......
.treeview, .treeview ul {
padding: 0;
margin: 0;
list-style: none;
}
.treeview ul {
background-color: white;
margin-top: 4px;
}
.treeview .hitarea {
background: url(images/treeview-default.gif) -64px -25px no-repeat;
height: 16px;
width: 16px;
margin-left: -16px;
float: left;
cursor: pointer;
}
/* fix for IE6 */
* html .hitarea {
display: inline;
float:none;
}
.treeview li {
margin: 0;
padding: 3px 0pt 3px 16px;
}
.treeview a.selected {
background-color: #eee;
}
#treecontrol { margin: 1em 0; display: none; }
.treeview .hover { color: red; cursor: pointer; }
.treeview li { background: url(images/treeview-default-line.gif) 0 0 no-repeat; }
.treeview li.collapsable, .treeview li.expandable { background-position: 0 -176px; }
.treeview .expandable-hitarea { background-position: -80px -3px; }
.treeview li.last { background-position: 0 -1766px }
.treeview li.lastCollapsable, .treeview li.lastExpandable { background-image: url(images/treeview-default.gif); }
.treeview li.lastCollapsable { background-position: 0 -111px }
.treeview li.lastExpandable { background-position: -32px -67px }
.treeview div.lastCollapsable-hitarea, .treeview div.lastExpandable-hitarea { background-position: 0; }
.treeview-red li { background-image: url(images/treeview-red-line.gif); }
.treeview-red .hitarea, .treeview-red li.lastCollapsable, .treeview-red li.lastExpandable { background-image: url(images/treeview-red.gif); }
.treeview-black li { background-image: url(images/treeview-black-line.gif); }
.treeview-black .hitarea, .treeview-black li.lastCollapsable, .treeview-black li.lastExpandable { background-image: url(images/treeview-black.gif); }
.treeview-gray li { background-image: url(images/treeview-gray-line.gif); }
.treeview-gray .hitarea, .treeview-gray li.lastCollapsable, .treeview-gray li.lastExpandable { background-image: url(images/treeview-gray.gif); }
.treeview-famfamfam li { background-image: url(images/treeview-famfamfam-line.gif); }
.treeview-famfamfam .hitarea, .treeview-famfamfam li.lastCollapsable, .treeview-famfamfam li.lastExpandable { background-image: url(images/treeview-famfamfam.gif); }
.treeview .placeholder {
background: url(images/ajax-loader.gif) 0 0 no-repeat;
height: 16px;
width: 16px;
display: block;
}
.filetree li { padding: 3px 0 2px 16px; }
.filetree span.folder, .filetree span.file { padding: 1px 0 1px 16px; display: block; }
.filetree span.folder { background: url(images/folder.gif) 0 0 no-repeat; }
.filetree li.expandable span.folder { background: url(images/folder-closed.gif) 0 0 no-repeat; }
.filetree span.file { background: url(images/file.gif) 0 0 no-repeat; }
......@@ -3,7 +3,6 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def require_login
unless user_signed_in?
flash[:alert] = 'You need Login!'
......
class HomeController < ApplicationController
def show
skip_before_action :require_login
def top
if user_signed_in?
redirect_to posts_path, status: 301
else
render template: 'home/login'
end
end
......
......@@ -4,25 +4,19 @@ require 'rv/mailer'
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :require_login, except: [:index]
before_action :require_login
include ApplicationHelper
include RV::Mailer
# GET /posts
# GET /posts.json
def index
if user_signed_in?
if params[:q].present?
@posts = Post.build_query(params).limit(10)
else
@posts = Post.order(updated_at: :desc).limit(10)
end
render
else
render file: 'home/login'
end
end
def preview
......@@ -46,19 +40,20 @@ class PostsController < ApplicationController
end
def fork
@post = set_post.clone
@post.title = @post.title.gsub(/%Name/, current_user.name)
@post.title = Time.now.strftime(@post.title) # TODO
@post = set_post.generate_fork(user: current_user)
render action: 'new'
end
def mail
@post = set_post
subject = @post.title
body = @post.body
RV::Mailer.compose_mail(subject, body, current_user).deliver
# refresh google oauth token if expired
current_user.google_oauth_token_refresh! if current_user.google_oauth_token_expired?
compose_mail(@post, current_user).deliver
redirect_to root_path(id: @post.id)
rescue ActionGmailer::DeliveryError
redirect_to root_path(id: @post.id), flash: { notice: 'Gmail authentication expired.' }
end
# GET /posts/1/edit
......
......@@ -37,4 +37,14 @@ class Post < ActiveRecord::Base
end
# generate forked post (not saved)
def generate_fork(user: )
forked_post = self.clone
forked_post.title = forked_post.title.gsub(/%Name/, user.name)
forked_post.title = Time.now.strftime(forked_post.title) # TODO
forked_post.author = user
forked_post
end
end
require 'faraday'
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
......@@ -10,7 +12,7 @@ class User < ActiveRecord::Base
# Device
def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info
user = User.where(:email => data["email"]).first
user = User.where(email: data["email"]).first
unless user
user = User.create(name: data["name"],
......@@ -19,7 +21,39 @@ class User < ActiveRecord::Base
password: Devise.friendly_token[0,20]
)
end
user.update_attribute(:google_auth_token, access_token.credentials['token'])
user.update_attributes(
google_auth_token: access_token.credentials['token'],
google_refresh_token: access_token.credentials['refresh_token'],
google_token_expires_at: Time.at(access_token.credentials['expires_at'])
)
user
end
# check if google oauth token is expired
def google_oauth_token_expired?
self.google_token_expires_at < Time.now
end
# refresh google oauth token
def google_oauth_token_refresh!
conn = Faraday.new(url: 'https://accounts.google.com') do |builder|
builder.request :url_encoded
builder.adapter :net_http
end
response = conn.post '/o/oauth2/token', {
client_id: ENV["GOOGLE_KEY"],
client_secret: ENV["GOOGLE_SECRET"],
refresh_token: self.google_refresh_token,
grant_type: "refresh_token"
}
res_json = JSON.parse(response.body)
self.update_attributes(
google_auth_token: res_json['access_token'],
google_token_expires_at: Time.now + res_json['expires_in'].seconds
)
end
end
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'spring' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('spring', 'spring')
......@@ -20,7 +20,7 @@ module Rendezvous
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.default_locale = :ja
# config.action_mailer.delivery_method = :action_gmailer
......
......@@ -3,7 +3,8 @@ Rails.application.config.middleware.use OmniAuth::Builder do
{
:name => "google_oauth2",
:scope => "https://mail.google.com/, userinfo.email, userinfo.profile",
:prompt => "select_account",
access_type: 'offline',
:prompt => "select_account consent",
:image_aspect_ratio => "square",
:image_size => 50
}
......
Rendezvous::Application.routes.draw do
root 'posts#index', as: 'root'
root 'home#top', as: 'root'
post 'posts/preview' => 'posts#preview'
get 'posts/show_fragment' => 'posts#show_fragment'
get 'posts/:id/fork' => 'posts#fork', as: 'fork_post'
get 'posts/:id/mail' => 'posts#mail', as: 'mail_post'
resources :posts
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
......
class AddRefreshTokenToUser < ActiveRecord::Migration
def change
add_column :users, :google_refresh_token, :string
add_column :users, :google_token_expires_at, :datetime
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20131226161253) do
ActiveRecord::Schema.define(version: 20131228110818) do
create_table "post_tags", force: true do |t|
t.integer "post_id", null: false
......@@ -56,6 +56,8 @@ ActiveRecord::Schema.define(version: 20131226161253) do
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "google_auth_token"
t.string "google_refresh_token"
t.datetime "google_token_expires_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
......
require 'spec_helper'
describe HomeController do
describe "GET 'top'" do
it "should be successful" do
get :top
expect(response).to be_success
expect(response.code).to eq("200")
end
end
describe 'Login' do
before (:each) do
@user = FactoryGirl.create(:login_user_1)
sign_in @user
end
describe "GET 'top'" do
it "should be successful" do
get :top
expect(subject).to redirect_to controller: 'posts',
action: 'index'
end
it "should find the right user" do
get :top
expect(assigns(:current_user)).to eq(@user)
end
end
end
end
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :post do
title 'sample title'
body 'sample body'
after(:create) do |post|
create(:post_tag, post: post, tag: create(:tag_ruby))
end
end
factory :post_tag do
end
end
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :tag_ruby, class: Tag do
name 'ruby'
end
end
FactoryGirl.define do
factory :alice, class: User do
name 'Alice'
email "alice@mail.com"
password Devise.friendly_token[0,20]
google_token_expires_at Time.now + 30.minutes
end
factory :bob, class: User do
name 'Bob'
email "bob@mail.com"
password Devise.friendly_token[0,20]
google_token_expires_at Time.now - 1.hour
end
factory :login_user_1, class: User do
name 'Test User'
email 'example@example.com'
password 'changeme'
password_confirmation 'changeme'
# required if the Devise Confirmable module is used
# confirmed_at Time.now
end
end
# Commonly used email steps
#
# To add your own steps make a custom_email_steps.rb
# The provided methods are:
#
# last_email_address
# reset_mailer
# open_last_email
# visit_in_email
# unread_emails_for
# mailbox_for
# current_email
# open_email
# read_emails_for
# find_email
#
# General form for email scenarios are:
# - clear the email queue (done automatically by email_spec)
# - execute steps that sends an email
# - check the user received an/no/[0-9] emails
# - open the email
# - inspect the email contents
# - interact with the email (e.g. click links)
#
# The Cucumber steps below are setup in this order.
# module EmailHelpers
# def current_email_address
# # Replace with your a way to find your current email. e.g @current_user.email
# # last_email_address will return the last email address used by email spec to find an email.
# # Note that last_email_address will be reset after each Scenario.
# last_email_address || "example@example.com"
# end
# end
# World(EmailHelpers)
# #
# # Reset the e-mail queue within a scenario.
# # This is done automatically before each scenario.
# #
# Given /^(?:a clear email queue|no emails have been sent)$/ do
# reset_mailer
# end
# #
# # Check how many emails have been sent/received
# #
# Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
# unread_emails_for(address).size.should == parse_email_count(amount)
# end
# Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
# mailbox_for(address).size.should == parse_email_count(amount)
# end
# Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
# unread_emails_for(address).select { |m| m.subject =~ Regexp.new(Regexp.escape(subject)) }.size.should == parse_email_count(amount)
# end
# Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject \/([^"]*?)\/$/ do |address, amount, subject|
# unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
# end
# Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
# open_email(address, :with_text => expected_body)
# end
# #
# # Accessing emails
# #
# # Opens the most recently received email
# When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
# open_email(address)
# end
# When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
# open_email(address, :with_subject => subject)
# end
# When /^(?:I|they|"([^"]*?)") opens? the email with subject \/([^"]*?)\/$/ do |address, subject|
# open_email(address, :with_subject => Regexp.new(subject))
# end
# When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
# open_email(address, :with_text => text)
# end
# When /^(?:I|they|"([^"]*?)") opens? the email with text \/([^"]*?)\/$/ do |address, text|
# open_email(address, :with_text => Regexp.new(text))
# end
# #
# # Inspect the Email Contents
# #
# Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
# current_email.should have_subject(text)
# end
# Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
# current_email.should have_subject(Regexp.new(text))
# end
# Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
# current_email.default_part_body.to_s.should include(text)
# end
# Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
# current_email.default_part_body.to_s.should =~ Regexp.new(text)
# end
# Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
# current_email.should be_delivered_from(text)
# end
# Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
# current_email.should have_header(name, text)
# end
# Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
# current_email.should have_header(name, Regexp.new(text))
# end
# Then /^I should see it is a multi\-part email$/ do
# current_email.should be_multipart
# end
# Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
# current_email.html_part.body.to_s.should include(text)
# end
# Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
# current_email.text_part.body.to_s.should include(text)
# end
# #
# # Inspect the Email Attachments
# #
# Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
# current_email_attachments.size.should == parse_email_count(amount)
# end
# Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
# current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
# end
# Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
# current_email_attachments[(index.to_i - 1)].filename.should == filename
# end
# Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
# current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
# end
# Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
# current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
# end
# Then /^all attachments should not be blank$/ do
# current_email_attachments.each do |attachment|
# attachment.read.size.should_not == 0
# end
# end
# Then /^show me a list of email attachments$/ do
# EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
# end
# #
# # Interact with Email Contents
# #
# When /^(?:I|they|"([^"]*?)") follows? "([^"]*?)" in the email$/ do |address, link|
# visit_in_email(link, address)
# end
# When /^(?:I|they) click the first link in the email$/ do
# click_first_link_in_email
# end
# #
# # Debugging
# # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
# # Patches accepted. ;)
# #
# Then /^save and open current email$/ do
# EmailSpec::EmailViewer::save_and_open_email(current_email)
# end
# Then /^save and open all text emails$/ do
# EmailSpec::EmailViewer::save_and_open_all_text_emails
# end
# Then /^save and open all html emails$/ do
# EmailSpec::EmailViewer::save_and_open_all_html_emails
# end
# Then /^save and open all raw emails$/ do
# EmailSpec::EmailViewer::save_and_open_all_raw_emails
# end
require 'spec_helper'
describe Post do
describe 'Instance method' do
before :each do
@post = create(:post)
@alice = create(:alice)
end
describe 'Fork' do
subject { @post.generate_fork(user: @alice) }
it 'valid title' do
expect(subject.title).to eq('sample title')
end
it 'valid body' do
expect(subject.body).to eq('sample body')
end
it 'valid user' do
expect(subject.author).to eq(@alice)
end
it 'valid user' do
expect(subject.tags).to include(@post.tags.first)
end
end
end
end
require 'spec_helper'
describe Tag do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'spec_helper'
describe User do
describe 'Instance method' do
before :each do
@alice = create(:alice)
@bob = create(:bob)
end
describe '#google_oauth_token_expired?' do
it 'not expired' do
expect(@alice.google_oauth_token_expired?).to be_false
end
it 'expired' do
expect(@bob.google_oauth_token_expired?).to be_true
end
end
end
describe 'validation' do
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "changeme",
:password_confirmation => "changeme"
}
end
it "should create a new instance given a valid attribute" do
User.create!(@attr)
end
it "should require an email address" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
addresses.each do |address|
valid_email_user = User.new(@attr.merge(:email => address))
valid_email_user.should be_valid
end
end
it "should reject invalid email addresses" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |address|
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end
it "should reject duplicate email addresses" do
User.create!(@attr)
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = @attr[:email].upcase
User.create!(@attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
describe "passwords" do
before(:each) do
@user = User.new(@attr)
end
it "should have a password attribute" do
@user.should respond_to(:password)
end
it "should have a password confirmation attribute" do
@user.should respond_to(:password_confirmation)
end
end
describe "password validations" do
it "should require a password" do
User.new(@attr.merge(:password => "", :password_confirmation => "")).
should_not be_valid
end
it "should require a matching password confirmation" do
User.new(@attr.merge(:password_confirmation => "invalid")).
should_not be_valid
end
it "should reject short passwords" do
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end
end
describe "password encryption" do
before(:each) do
@user = User.create!(@attr)
end
it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
it "should set the encrypted password attribute" do
@user.encrypted_password.should_not be_blank
end
end
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'email_spec'
require 'factory_girl'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# 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
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# 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"
config.include FactoryGirl::Syntax::Methods
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.before(:all) do
FactoryGirl.reload
end
end
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
require 'test_helper'
class HomeControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
end
end
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:posts)
end
test "should get new" do
get :new
assert_response :success
end
test "should create post" do
assert_difference('Post.count') do
post :create, post: { body: @post.body, title: @post.title }
end
assert_redirected_to post_path(assigns(:post))
end
test "should show post" do
get :show, id: @post
assert_response :success
end
test "should get edit" do
get :edit, id: @post
assert_response :success
end
test "should update post" do
patch :update, id: @post, post: { body: @post.body, title: @post.title }
assert_redirected_to post_path(assigns(:post))
end
test "should destroy post" do
assert_difference('Post.count', -1) do
delete :destroy, id: @post
end
assert_redirected_to posts_path
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
body: MyText
two:
title: MyString
body: MyText
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
two:
name: MyString
# 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: {}
# column: value
#
two: {}
# column: value
require 'test_helper'
class HomeHelperTest < ActionView::TestCase
end
require 'test_helper'
class PostsHelperTest < ActionView::TestCase
end
require 'test_helper'
class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class TagTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment