Skip to content

Commit e2fce3a

Browse files
committed
Testes com FactoryGirl
1 parent 610c7af commit e2fce3a

33 files changed

+760
-872
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ group :development do
99
gem 'sqlite3'
1010
gem 'better_errors'
1111
gem 'binding_of_caller'
12+
gem 'mailcatcher'
1213
end
1314

15+
gem 'devise'
16+
1417
group :production do
1518
gem 'pg'
1619
end
1720

1821
group :test do
1922
gem 'fuubar'
2023
gem 'simplecov'
24+
gem 'factory_girl_rails', "~> 4.0"
2125
# Acceptance test framework for web applications
2226
# https://github.com/jnicklas/capybara
2327
gem 'capybara'

Gemfile.lock

+46-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ GEM
2929
i18n (= 0.6.1)
3030
multi_json (~> 1.0)
3131
arel (3.0.2)
32+
bcrypt-ruby (3.0.1)
3233
better_errors (0.9.0)
3334
coderay (>= 1.0.0)
3435
erubis (>= 2.6.6)
@@ -49,15 +50,29 @@ GEM
4950
coffee-script-source
5051
execjs
5152
coffee-script-source (1.6.2)
53+
daemons (1.1.9)
5254
debug_inspector (0.0.2)
55+
devise (2.2.4)
56+
bcrypt-ruby (~> 3.0)
57+
orm_adapter (~> 0.1)
58+
railties (~> 3.1)
59+
warden (~> 1.2.1)
5360
diff-lcs (1.2.4)
5461
erubis (2.7.0)
62+
eventmachine (1.0.3)
5563
execjs (1.4.0)
5664
multi_json (~> 1.0)
65+
factory_girl (4.2.0)
66+
activesupport (>= 3.0.0)
67+
factory_girl_rails (4.2.1)
68+
factory_girl (~> 4.2.0)
69+
railties (>= 3.0.0)
5770
fuubar (1.1.1)
5871
rspec (~> 2.0)
5972
rspec-instafail (~> 0.2.0)
6073
ruby-progressbar (~> 1.0)
74+
haml (4.0.3)
75+
tilt
6176
hike (1.2.3)
6277
i18n (0.6.1)
6378
journey (1.0.4)
@@ -68,16 +83,28 @@ GEM
6883
mail (2.5.4)
6984
mime-types (~> 1.16)
7085
treetop (~> 1.4.8)
86+
mailcatcher (0.5.12)
87+
activesupport (~> 3.0)
88+
eventmachine (~> 1.0.0)
89+
haml (>= 3.1, < 5)
90+
mail (~> 2.3)
91+
sinatra (~> 1.2)
92+
skinny (~> 0.2.3)
93+
sqlite3 (~> 1.3)
94+
thin (~> 1.5.0)
7195
mime-types (1.23)
7296
mini_portile (0.5.0)
73-
multi_json (1.7.6)
97+
multi_json (1.7.7)
7498
nokogiri (1.6.0)
7599
mini_portile (~> 0.5.0)
76-
pg (0.12.2)
100+
orm_adapter (0.4.0)
101+
pg (0.15.1)
77102
polyglot (0.3.3)
78103
rack (1.4.5)
79104
rack-cache (1.2)
80105
rack (>= 0.4)
106+
rack-protection (1.5.0)
107+
rack
81108
rack-ssl (1.3.3)
82109
rack
83110
rack-test (0.6.2)
@@ -97,7 +124,7 @@ GEM
97124
rake (>= 0.8.7)
98125
rdoc (~> 3.4)
99126
thor (>= 0.14.6, < 2.0)
100-
rake (10.0.4)
127+
rake (10.1.0)
101128
rdoc (3.12.2)
102129
json (~> 1.4)
103130
rspec (2.13.0)
@@ -128,12 +155,23 @@ GEM
128155
multi_json (~> 1.0)
129156
simplecov-html (~> 0.7.1)
130157
simplecov-html (0.7.1)
158+
sinatra (1.4.3)
159+
rack (~> 1.4)
160+
rack-protection (~> 1.4)
161+
tilt (~> 1.3, >= 1.3.4)
162+
skinny (0.2.3)
163+
eventmachine (~> 1.0.0)
164+
thin (~> 1.5.0)
131165
sprockets (2.2.2)
132166
hike (~> 1.2)
133167
multi_json (~> 1.0)
134168
rack (~> 1.0)
135169
tilt (~> 1.1, != 1.3.0)
136170
sqlite3 (1.3.7)
171+
thin (1.5.1)
172+
daemons (>= 1.0.9)
173+
eventmachine (>= 0.12.6)
174+
rack (>= 1.0.0)
137175
thor (0.18.1)
138176
tilt (1.4.1)
139177
treetop (1.4.14)
@@ -143,6 +181,8 @@ GEM
143181
uglifier (2.1.1)
144182
execjs (>= 0.3.0)
145183
multi_json (~> 1.0, >= 1.0.2)
184+
warden (1.2.1)
185+
rack (>= 1.0)
146186
xpath (2.0.0)
147187
nokogiri (~> 1.3)
148188

@@ -154,8 +194,11 @@ DEPENDENCIES
154194
binding_of_caller
155195
capybara
156196
coffee-rails (~> 3.2.1)
197+
devise
198+
factory_girl_rails (~> 4.0)
157199
fuubar
158200
jquery-rails
201+
mailcatcher
159202
pg
160203
rails (= 3.2.13)
161204
rspec-rails
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class ApplicationController < ActionController::Base
22
protect_from_forgery
3+
4+
before_filter :authenticate_user!
35
end

app/controllers/welcome_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class WelcomeController < ApplicationController
22
protect_from_forgery
3+
before_filter :authenticate_user!, :except => [:index, :about, :contact]
34

45
#layout 'application_black', :only => :black
56

app/models/user.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
class User < ActiveRecord::Base
2+
# Include default devise modules. Others available are:
3+
# :token_authenticatable, :confirmable,
4+
# :lockable, :timeoutable and :omniauthable
5+
devise :database_authenticatable, :registerable,
6+
:recoverable, :rememberable, :trackable, :validatable
7+
8+
# Setup accessible (or protected) attributes for your model
9+
attr_accessible :email, :password, :password_confirmation, :remember_me
210
attr_accessible :name, :email, :age, :gender
311

412
MALE = 1

app/views/layouts/application.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<%= csrf_meta_tags %>
88
</head>
99
<body>
10+
11+
<p class="notice"><%= notice %></p>
12+
<p class="alert"><%= alert %></p>
13+
1014
<%= render :partial => 'shared/menu_top' %>
1115
<div class='container-fluid'>
1216
<div class='row-fluid'>

app/views/shared/_menu_top.html.erb

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
<span class="icon-bar"></span>
77
<span class="icon-bar"></span>
88
</button>
9-
<%= link_to "Project name", index_path, :class => "brand" %>
9+
<%= link_to "Hands On Rails", index_path, :class => "brand" %>
1010
<div class="nav-collapse collapse">
1111
<p class="navbar-text pull-right">
12-
Logged in as <a href="#" class="navbar-link">Username</a>
13-
</p>
12+
<% if user_signed_in? %>
13+
Logged in as <%= link_to current_user.name, edit_user_registration_path(current_user), :class => "navbar-link" %>
14+
<%= link_to "<i class='icon-off icon-white'></i>".html_safe, destroy_user_session_path, :method => :delete %>
15+
<% else %>
16+
<%= link_to "login", new_user_session_path, :class => "btn" %>
17+
<% end %>
18+
</p>
1419
<ul class="nav">
1520
<li class="active"><%= link_to "Home", index_path %></li>
1621
<li><%= link_to "About", about_path %></li>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h2>Resend confirmation instructions</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| %>
4+
<%= devise_error_messages! %>
5+
6+
<div><%= f.label :email %><br />
7+
<%= f.email_field :email, :autofocus => true %></div>
8+
9+
<div><%= f.submit "Resend confirmation instructions" %></div>
10+
<% end %>
11+
12+
<%= render "users/shared/links" %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p>Welcome <%= @email %>!</p>
2+
3+
<p>You can confirm your account email through the link below:</p>
4+
5+
<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<p>Hello <%= @resource.email %>!</p>
2+
3+
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
4+
5+
<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p>
6+
7+
<p>If you didn't request this, please ignore this email.</p>
8+
<p>Your password won't change until you access the link above and create a new one.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>Hello <%= @resource.email %>!</p>
2+
3+
<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>
4+
5+
<p>Click the link below to unlock your account:</p>
6+
7+
<p><%= link_to 'Unlock my account', unlock_url(@resource, :unlock_token => @resource.unlock_token) %></p>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<h2>Change your password</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
4+
<%= devise_error_messages! %>
5+
<%= f.hidden_field :reset_password_token %>
6+
7+
<div><%= f.label :password, "New password" %><br />
8+
<%= f.password_field :password, :autofocus => true %></div>
9+
10+
<div><%= f.label :password_confirmation, "Confirm new password" %><br />
11+
<%= f.password_field :password_confirmation %></div>
12+
13+
<div><%= f.submit "Change my password" %></div>
14+
<% end %>
15+
16+
<%= render "users/shared/links" %>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h2>Forgot your password?</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| %>
4+
<%= devise_error_messages! %>
5+
6+
<div><%= f.label :email %><br />
7+
<%= f.email_field :email, :autofocus => true %></div>
8+
9+
<div><%= f.submit "Send me reset password instructions" %></div>
10+
<% end %>
11+
12+
<%= render "users/shared/links" %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2>Edit <%= resource_name.to_s.humanize %></h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
4+
<%= devise_error_messages! %>
5+
6+
<div><%= f.label :email %><br />
7+
<%= f.email_field :email, :autofocus => true %></div>
8+
9+
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
10+
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
11+
<% end %>
12+
13+
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
14+
<%= f.password_field :password, :autocomplete => "off" %></div>
15+
16+
<div><%= f.label :password_confirmation %><br />
17+
<%= f.password_field :password_confirmation %></div>
18+
19+
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
20+
<%= f.password_field :current_password %></div>
21+
22+
<div><%= f.submit "Update" %></div>
23+
<% end %>
24+
25+
<h3>Cancel my account</h3>
26+
27+
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>
28+
29+
<%= link_to "Back", :back %>
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h2>Sign up</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
4+
<%= devise_error_messages! %>
5+
6+
<div><%= f.label :name %><br />
7+
<%= f.text_field :name, :autofocus => true %></div>
8+
9+
<div><%= f.label :email %><br />
10+
<%= f.email_field :email %></div>
11+
12+
<div><%= f.label :password %><br />
13+
<%= f.password_field :password %></div>
14+
15+
<div><%= f.label :password_confirmation %><br />
16+
<%= f.password_field :password_confirmation %></div>
17+
18+
<div><%= f.submit "Sign up" %></div>
19+
<% end %>
20+
21+
<%= render "users/shared/links" %>

app/views/users/sessions/new.html.erb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<h2>Sign in</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
4+
<div><%= f.label :email %><br />
5+
<%= f.email_field :email, :autofocus => true %></div>
6+
7+
<div><%= f.label :password %><br />
8+
<%= f.password_field :password %></div>
9+
10+
<% if devise_mapping.rememberable? -%>
11+
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
12+
<% end -%>
13+
14+
<div><%= f.submit "Sign in" %></div>
15+
<% end %>
16+
17+
<%= render "users/shared/links" %>

app/views/users/shared/_links.erb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%- if controller_name != 'sessions' %>
2+
<%= link_to "Sign in", new_session_path(resource_name) %><br />
3+
<% end -%>
4+
5+
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
6+
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
7+
<% end -%>
8+
9+
<%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
10+
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
11+
<% end -%>
12+
13+
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
14+
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
15+
<% end -%>
16+
17+
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
18+
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
19+
<% end -%>
20+
21+
<%- if devise_mapping.omniauthable? %>
22+
<%- resource_class.omniauth_providers.each do |provider| %>
23+
<%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
24+
<% end -%>
25+
<% end -%>

app/views/users/unlocks/new.html.erb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h2>Resend unlock instructions</h2>
2+
3+
<%= form_for(resource, :as => resource_name, :url => unlock_path(resource_name), :html => { :method => :post }) do |f| %>
4+
<%= devise_error_messages! %>
5+
6+
<div><%= f.label :email %><br />
7+
<%= f.email_field :email, :autofocus => true %></div>
8+
9+
<div><%= f.submit "Resend unlock instructions" %></div>
10+
<% end %>
11+
12+
<%= render "users/shared/links" %>

config/environments/development.rb

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
# Don't care if the mailer can't send
1717
config.action_mailer.raise_delivery_errors = false
18+
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
19+
config.action_mailer.delivery_method = :smtp
20+
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
1821

1922
# Print deprecation notices to the Rails logger
2023
config.active_support.deprecation = :log

0 commit comments

Comments
 (0)