Guest User

Untitled

a guest
Feb 18th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. Failures:
  2.  
  3. 1) UserPages edit page
  4. Failure/Error: it { should have_selector('h1', text: "Update your profile") }
  5. expected css "h1" with text "Update your profile" to return something
  6. # ./spec/requests/user_pages_spec.rb:73:in `block (4 levels) in <top (required)>'
  7.  
  8. 2) UserPages edit page
  9. Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails')}
  10. expected link "change" to return something
  11. # ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'
  12.  
  13. 3) UserPages edit page
  14. Failure/Error: it { should have_selector('title', text: "Edit user") }
  15. expected css "title" with text "Edit user" to return something
  16. # ./spec/requests/user_pages_spec.rb:74:in `block (4 levels) in <top (required)>'
  17.  
  18. 4) UserPages edit with valid information
  19. Failure/Error: fill_in "Name", with: new_name
  20. Capybara::ElementNotFound:
  21. cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
  22. # (eval):2:in `fill_in'
  23. # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
  24.  
  25. 5) UserPages edit with valid information
  26. Failure/Error: fill_in "Name", with: new_name
  27. Capybara::ElementNotFound:
  28. cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
  29. # (eval):2:in `fill_in'
  30. # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
  31.  
  32. 6) UserPages edit with valid information
  33. Failure/Error: fill_in "Name", with: new_name
  34. Capybara::ElementNotFound:
  35. cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
  36. # (eval):2:in `fill_in'
  37. # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
  38.  
  39. 7) UserPages edit with valid information
  40. Failure/Error: fill_in "Name", with: new_name
  41. Capybara::ElementNotFound:
  42. cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
  43. # (eval):2:in `fill_in'
  44. # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
  45.  
  46. 8) UserPages edit with valid information
  47. Failure/Error: fill_in "Name", with: new_name
  48. Capybara::ElementNotFound:
  49. cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
  50. # (eval):2:in `fill_in'
  51. # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
  52.  
  53. 9) UserPages edit with invalid information
  54. Failure/Error: before { click_button "Save changes" }
  55. Capybara::ElementNotFound:
  56. no button with value or id or text 'Save changes' found
  57. # (eval):2:in `click_button'
  58. # ./spec/requests/user_pages_spec.rb:79:in `block (4 levels) in <top (required)>'
  59.  
  60. require 'spec_helper'
  61.  
  62. describe "AuthenticationPages" do
  63.  
  64. subject { page }
  65.  
  66. describe "signin page" do
  67. before { visit signin_path }
  68.  
  69. it { should have_selector('h1', text: 'Sign in') }
  70. it { should have_selector('title', text: 'Sign in') }
  71. end
  72.  
  73. describe "signin" do
  74. before { visit signin_path}
  75.  
  76. describe "with invalid information" do
  77. before { click_button "Sign in" }
  78.  
  79. it { should have_selector('title', text: 'Sign in') }
  80. it { should have_selector('div.alert.alert-error', text: 'Invalid') }
  81.  
  82. describe "after visiting another page" do
  83. before { click_link "Home" }
  84. it { should_not have_selector('div.alert.alert-error') }
  85. end
  86. end
  87.  
  88.  
  89. describe "with valid information" do
  90. let(:user) { FactoryGirl.create(:user) }
  91. before { sign_in user }
  92.  
  93.  
  94. describe "followed by signout" do
  95. before { click_link "Sign out" }
  96. it { should have_link('Sign in')}
  97. end
  98.  
  99. it { should have_selector('title', text: user.name) }
  100. it { should have_link('Profile', href: user_path(user)) }
  101. it { should have_link('Settings', href: edit_user_path(user)) }
  102. it { should have_link('Sign out', href: signout_path) }
  103. it { should_not have_link('Sign in', href: signin_path) }
  104.  
  105. end
  106. end
  107.  
  108. describe "authorization" do
  109.  
  110. describe "for non-signed-in users" do
  111. let(:user) { FactoryGirl.create(:user) }
  112.  
  113. describe "in the Users controller" do
  114.  
  115. describe "visiting the edit page" do
  116. before { visit edit_user_path(user) }
  117. it { should have_selector('title', text: 'Sign in' ) }
  118. end
  119.  
  120. describe "submitting to the update action" do
  121. before { put user_path(user) }
  122. specify { response.should redirect_to(signin_path) }
  123. end
  124. end
  125. end
  126. end
  127. end
  128.  
  129. require 'spec_helper'
  130.  
  131. describe "UserPages" do
  132.  
  133. subject { page }
  134.  
  135. describe "profile page" do
  136. before { visit user_path(user) }
  137. let(:user) {FactoryGirl.create(:user) }
  138.  
  139.  
  140. it { should have_selector('h1', text: user.name)}
  141. it { should have_selector('title', text: user.name) }
  142. end
  143.  
  144. describe "signup page" do
  145. before {visit signup_path}
  146. it { should have_selector('h1', text: 'Sign up') }
  147. it { should have_selector('title', text: full_title('Sign up'))}
  148. end
  149.  
  150. describe "signup" do
  151. before { visit signup_path}
  152. let(:submit) { "Create my account"}
  153.  
  154. describe "with invalid information" do
  155. it "should not create a user" do
  156. expect { click_button submit }.not_to change(User, :count)
  157. end
  158.  
  159. describe "after submission" do
  160. before { click_button submit }
  161.  
  162. it { should have_selector('title', text: 'Sign up') }
  163. it { should have_content('error') }
  164. end
  165. end
  166.  
  167. describe "with valid information" do
  168. before do
  169. fill_in "Name", with: "Exmaple User"
  170. fill_in "Email", with: "user@example.com"
  171. fill_in "Password", with: "foobar"
  172. fill_in "Password confirmation", with: "foobar"
  173. end
  174.  
  175. it "should create a user" do
  176. expect { click_button submit }.to change(User, :count).by(1)
  177. end
  178.  
  179. describe "after saving the user" do
  180. before { click_button submit }
  181. let(:user) { User.find_by_email('user@example.com') }
  182.  
  183. it { should have_selector('title', text: user.name) }
  184. it { should have_selector('div.alert.alert-success', text: 'Welcome') }
  185. it { should have_link('Sign out') }
  186. end
  187. end
  188. end
  189.  
  190.  
  191. describe "edit" do
  192. let(:user) { FactoryGirl.create(:user) }
  193. before do
  194. visit signin_path
  195. sign_in user
  196. visit edit_user_path(user)
  197. end
  198.  
  199.  
  200. describe "page" do
  201. it { should have_selector('h1', text: "Update your profile") }
  202. it { should have_selector('title', text: "Edit user") }
  203. it { should have_link('change', href: 'http://gravatar.com/emails')}
  204. end
  205.  
  206. describe "with invalid information" do
  207. before { click_button "Save changes" }
  208.  
  209. it { should have_content('error') }
  210. end
  211.  
  212. describe "with valid information" do
  213. let(:new_name) { "New Name"}
  214. let(:new_email) {"new@example.com"}
  215. before do
  216. fill_in "Name", with: new_name
  217. fill_in "Email", with: new_email
  218. fill_in "Password", with: user.password
  219. fill_in "Confirm Password", with: user.password
  220. click_button "Save changes"
  221. end
  222.  
  223. it { should have_selector('title', text: new_name) }
  224. it { should have_selector('div.alert.alert-success') }
  225. it { should have_link('Sign out', href: signout_path) }
  226. specify { user.reload.name.should == new_name }
  227. specify { user.reload.email.should == new_email }
  228. end
  229. end
  230. end
  231.  
  232. def sign_in(user)
  233. fill_in "Email", with: user.email
  234. fill_in "Password", with: user.password
  235. click_button "Sign in"
  236. # Sign in when not using Capybara as well.
  237. cookies[:remember_token] = user.remember_token
  238.  
  239. module SessionsHelper
  240.  
  241. def sign_in(user)
  242. cookies.permanent[:remember_token] = user.remember_token
  243. self.current_user = user
  244. end
  245.  
  246. def signed_in?
  247. !current_user.nil?
  248. end
  249.  
  250. def current_user=(user)
  251. @current_user = user
  252. end
  253.  
  254. def current_user
  255. @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  256. end
  257.  
  258. def current_user?(user)
  259. user == current_user
  260. end
  261.  
  262. def sign_out
  263. self.current_user = nil
  264. cookies.delete(:remember_token)
  265. end
  266.  
  267. def redirect_back_or(default)
  268. redirect_to(session[:return_to] || default)
  269. session.delete(:return_to)
  270. end
  271.  
  272. def store_location
  273. session[:return_to] = request.fullpath
  274. end
  275. end
  276.  
  277. class UsersController < ApplicationController
  278. before_filter :signed_in_user, only: [:edit, :update]
  279. before_filter :correct_user, only: [:edit, :update]
  280.  
  281. def show
  282. @user = User.find(params[:id])
  283. end
  284.  
  285. def new
  286. @user = User.new
  287. end
  288.  
  289. def create
  290. @user = User.new(params[:user])
  291. if @user.save
  292. sign_in @user
  293. flash[:success] = "Welcome to the Sample App!"
  294. redirect_to @user
  295. else
  296. render 'new'
  297. end
  298. end
  299.  
  300. def edit
  301. @user = User.find(params[:id])
  302. end
  303.  
  304. def update
  305. @user = User.find(params[:id])
  306. if @user.update_attributes(params[:user])
  307. sign_in @user
  308. flash[:success] = "Profile updated"
  309. redirect_to @user
  310. else
  311. render 'edit'
  312. end
  313. end
  314.  
  315. private
  316. def signed_in_user
  317. unless signed_in?
  318. store_location
  319. redirect_to signin_path, notice: "Please sign in."
  320. end
  321. end
  322.  
  323. def correct_user
  324. @user=User.find(params[:id])
  325. redirect_to(root_path) unless current_user?(@user)
  326. end
  327.  
  328. end
  329.  
  330. <header class="navbar navbar-fixed-top">
  331. <div class="navbar-inner">
  332. <div class="container">
  333. <%= link_to "sample app", root_path, id: "logo" %>
  334. <nav>
  335. <ul class="nav pull-right">
  336. <li><%= link_to "Home", root_path %></li>
  337. <li><%= link_to "Help", help_path %></li>
  338. <% if signed_in? %>
  339. <li><%= link_to "Users", "#" %></li>
  340. <li id="fat-menu" class="dropdown">
  341. <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  342. Account <b class="caret"></b>
  343. </a>
  344. <ul class="dropdown-menu">
  345. <li><%= link_to "Profile", current_user %></li>
  346. <li><%= link_to "Settings", edit_user_path(current_user) %></li>
  347. <li class="divider"></li>
  348. <li>
  349. <%= link_to "Sign out", signout_path, method: "delete" %>
  350. </li>
  351. </ul>
  352. </li>
  353. <% else %>
  354. <li><%= link_to "Sign in", signin_path %></li>
  355. <% end %>
  356. </ul>
  357. </nav>
  358. </div>
  359. </div>
  360.  
  361. class ApplicationController < ActionController::Base
  362. protect_from_forgery
  363. include SessionsHelper
  364.  
  365. private
  366. def current_user
  367. @current_user ||= User.find(session[:user_id]) if session[:user_id]
  368. end
  369.  
  370. def sign_in(user)
  371. visit signin_path
  372. fill_in "Email", with: user.email
  373. fill_in "Password", with: user.password
  374. click_button "Sign in"
  375. # Sign in when not using Capybara as well.
  376. cookies[:remember_token] = user.remember_token
  377. end
Add Comment
Please, Sign In to add comment