Guest User

Untitled

a guest
Apr 16th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. # File: features/support/path.rb
  2. when /login/
  3. login_path
  4. when /signup/
  5. signup_path
  6.  
  7. # File: features/support/user_helpers.rb
  8. module UserHelpers
  9. def create_user( options = {} )
  10. args = {
  11. :username => 'subscriber',
  12. :password => 'secret',
  13. :password_confirmation => 'secret',
  14. }.merge( options )
  15. args[:email] ||= "#{args[:username]}@example.com"
  16. user = User.create!(args)
  17. # :create syntax for restful_authentication w/ aasm. Tweak as needed.
  18. # user.activate!
  19. user
  20. end
  21.  
  22. def log_in_as( username )
  23. visit "/login"
  24. fill_in("user_session_username", :with => username)
  25. fill_in("password", :with => 'secret')
  26. click_button("Log in")
  27. end
  28.  
  29. end
  30.  
  31. World(UserHelpers)
  32.  
  33. # File: features/user.feature
  34. Feature: User authentication
  35. In order to access the site
  36. As a user
  37. I must register and log in
  38.  
  39. Scenario: Register as new user
  40. Given I am on the homepage
  41. When I follow "Sign up!"
  42. Then I should be on the signup page
  43. When I fill in the following:
  44. | username | subscriber |
  45. | email | subscriber@example.com |
  46. | password | secret |
  47. | confirm password | secret |
  48. And I press "Sign up"
  49. Then I should be on the homepage
  50. And I should see "Thank you for signing up! You are now logged in."
  51.  
  52. Scenario: Edit profile
  53. Given a user is logged in as "subscriber"
  54. And I am on the homepage
  55. When I follow "My profile"
  56. Then I should see "Show User Profile"
  57. When I follow "Edit Profile"
  58. Then I should see "Edit User Profile"
  59. When I fill in "email" with "changed@example.com"
  60. And I press "Submit"
  61. Then I should be on the homepage
  62. And I should see "Successfully updated user profile."
  63.  
  64. # File: app/layouts/_user_nav.html.erb
  65. <div id="user_nav">
  66. <%= link_to "Home", root_path %> |
  67. <% if current_user %>
  68. <%= link_to "My profile", user_path(:current) %> |
  69. <%= link_to "Logout", logout_path %>
  70. <% else %>
  71. <%= link_to "Sign up", signup_path %> |
  72. <%= link_to "Login", login_path %>
  73. <% end %>
  74. </div>
  75.  
  76. # File: app/controllers/users_controller.rb
  77.  
  78. class UsersController < ApplicationController
  79. before_filter :login_required, :except => [:new, :create]
  80. before_filter :find_user, :except => [:new, :create]
  81.  
  82. def new
  83. @user = User.new
  84. end
  85.  
  86. def create
  87. @user = User.new(params[:user])
  88. if @user.save
  89. flash[:notice] = "Thank you for signing up! You are now logged in."
  90. redirect_to root_url
  91. else
  92. render :action => 'new'
  93. end
  94. end
  95.  
  96. def show
  97. end
  98.  
  99. def edit
  100. end
  101.  
  102. def update
  103. if @user.update_attributes(params[:user])
  104. flash[:notice] = "Successfully updated user profile."
  105. redirect_to root_url
  106. else
  107. render :action => 'edit'
  108. end
  109. end
  110.  
  111. private
  112.  
  113. def find_user
  114. @user = current_user
  115. end
  116.  
  117. end
  118.  
  119. # File: app/views/users/show.html.erb
  120. <% title "Show User Profile" %>
  121. <p>
  122. <strong>Username:</strong>
  123. <%=h @user.username %>
  124. </p>
  125. <p>
  126. <strong>Email:</strong>
  127. <%=h @user.email %>
  128. </p>
  129. <p>
  130. <%= link_to "Edit Profile", edit_user_path(@user) %>
  131. </p>
  132.  
  133. # File: app/views/users/edit.html.erb
  134. <% title "Edit User Profile" %>
  135. <% form_for @user do |f| %>
  136. <%= f.error_messages %>
  137. <p>
  138. <%= f.label :email %><br />
  139. <%= f.text_field :email %>
  140. </p>
  141. <p>
  142. <%= f.label :password %><br />
  143. <%= f.password_field :password, :value => nil, :autocomplete => 'off' %>
  144. </p>
  145. <p>
  146. <%= f.label :password_confirmation %><br />
  147. <%= f.password_field :password_confirmation %>
  148. </p>
  149. <p><%= f.submit "Submit" %></p>
  150. <p><%= link_to "Cancel", root_url %>
  151. <% end %>
Add Comment
Please, Sign In to add comment