Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. module SessionsHelper
  2.  
  3. # Logs in the given user.
  4. def log_in(user)
  5. session[:user_id] = user.id
  6. end
  7.  
  8. # Returns the current logged-in user (if any).
  9. def current_user
  10. @current_user ||= User.find_by(id: session[:user_id])
  11. end
  12.  
  13. # Returns true if the user is logged in, false otherwise.
  14. def logged_in?
  15. !current_user.nil?
  16. end
  17.  
  18. # Logs out the current user.
  19. def destroy
  20. session.delete(:user_id)
  21. @current_user = nil
  22. end
  23. end
  24.  
  25. class SessionsController < ApplicationController
  26. def new
  27. end
  28.  
  29. def create
  30. #Gets user from database in lowercase & determines if user is valid
  31. user = User.find_by(email: params[:session][:email].downcase)
  32. if user && user.authenticate(params[:session][:password])
  33. # Log the user in and redirect to the user's show page.
  34. log_in user
  35. redirect_to user
  36. else
  37. # Create an error message.
  38. flash.now[:danger] = 'Invalid email/password combination'
  39. render 'new'
  40. end
  41. end
  42.  
  43. # Logs out the current user.
  44. def destroy
  45. log_out # undefined variable Name error
  46. redirect_to root_url
  47. end
  48. end
  49.  
  50. class ApplicationController < ActionController::Base
  51. protect_from_forgery with: :exception
  52. include SessionsHelper #temporary session cookie, expires automatically upon browser close
  53. end
  54.  
  55. <header class="navbar navbar-fixed-top navbar-inverse">
  56. <div class="container">
  57. <%= link_to image_tag("logo2.png", alt: "CourseBuddies logo"), root_path, id: "logo" %>
  58. <%- # link_to "sample app", '#', id: "logo" %>
  59. <nav>
  60. <ul class="nav navbar-nav navbar-right">
  61. <li><%= link_to "Home", root_path %></li>
  62. <li><%= link_to "Reviews", '#' %></li>
  63. <li><%= link_to "About us", about_path %></li>
  64.  
  65. <%- # LOGIN & SCROLL DOWN BAR %>
  66. <% if logged_in? %>
  67. **<li><%= link_to "Users", users_path %></li>
  68. <li class="dropdown">**
  69. <a href="#" class="dropdown-toggle" data-toggle="dropdown">
  70. Account <b class="caret"></b>
  71. </a>
  72. <ul class="dropdown-menu">
  73. **<li><%= link_to "Profile", current_user %></li>
  74. <li><%= link_to "Settings", '#' %></li>**
  75. <li class="divider"></li>
  76. <li>
  77. **<%= link_to "Log out", logout_path, method: :delete %>**
  78. </li>
  79. </ul>
  80. </li>
  81. **<% else %>
  82. <li><%= link_to "Log in", login_path %></li>
  83. <% end %>**
  84. </ul>
  85. </nav>
  86. </div>
  87. </header>
  88.  
  89. class User < ApplicationRecord
  90. #before saving, make sure the email is in downcase
  91. before_save { self.email = email.downcase }
  92.  
  93. validates :name, presence: true, length: { maximum: 50 }
  94. VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  95. validates :email, presence: true, length: { maximum: 255 },
  96. format: { with: VALID_EMAIL_REGEX },
  97. uniqueness: {case_sensitive: false}
  98.  
  99.  
  100. # Let's you safely store a hashed password_digest to DB,
  101. # gives you password & password_confirmation attributes,
  102. # an authenticate method that returns the user when pw is correct,
  103. # otherwise false.
  104. has_secure_password(validations:false)
  105. validates :password, presence:true, length: { minimum: 6 }
  106.  
  107. # Returns the hash digest of the given string.
  108. def User.digest(string)
  109. cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
  110. BCrypt::Engine.cost
  111. BCrypt::Password.create(string, cost: cost)
  112. end
  113. end
  114.  
  115. if @user.save
  116. log_in @user
  117. flash[:success] = "Welcome to CourseBuddies!"
  118. redirect_to @user
  119.  
  120. else
  121. render 'new'
  122. end
  123.  
  124. ENV['RAILS_ENV'] ||= 'test'
  125. require File.expand_path('../../config/environment', __FILE__)
  126. require 'rails/test_help'
  127. require "minitest/reporters"
  128. Minitest::Reporters.use!
  129.  
  130. class ActiveSupport::TestCase
  131. # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  132. fixtures :all
  133.  
  134. # Returns true if a test user is logged in.
  135. def is_logged_in?
  136. !session[:user_id].nil?
  137. end
  138. end
  139.  
  140. require 'test_helper'
  141.  
  142. class UsersLoginTest < ActionDispatch::IntegrationTest
  143.  
  144. def setup
  145. @user = users(:michael)
  146. end
  147.  
  148. # Visit the login path.
  149. # Verify that the new sessions form renders properly.
  150. # Post to the sessions path with an invalid params hash.
  151. # Verify that the new sessions form gets re-rendered and that a flash message appears.
  152. # Visit another page (such as the Home page).
  153. # Verify that the flash message doesnโ€™t appear on the new page.
  154. test "login with valid information followed by logout" do
  155. get login_path
  156. post login_path, params: { session: { email: @user.email,
  157. password: 'password' } }
  158. assert is_logged_in?
  159. assert_redirected_to @user
  160. follow_redirect!
  161. assert_template 'users/show'
  162. assert_select "a[href=?]", login_path, count: 0
  163. assert_select "a[href=?]", logout_path
  164. assert_select "a[href=?]", user_path(@user)
  165.  
  166. #AFTER ADDING THIS MY TEST FAILED
  167.  
  168. #after logging in, we use delete to issue a DELETE request to the logout path
  169. #(Table 8.1) and verify that the user is logged out and redirected to the root URL
  170. delete logout_path
  171. assert_not is_logged_in?
  172. assert_redirected_to root_url
  173. follow_redirect!
  174. assert_select "a[href=?]", login_path
  175. assert_select "a[href=?]", logout_path, count: 0
  176. assert_select "a[href=?]", user_path(@user), count: 0
  177. end
  178. end
  179.  
  180. Rails.application.routes.draw do
  181.  
  182. get 'sessions/new'
  183.  
  184. #ROOT, first page to show
  185. root 'pages#home'
  186.  
  187. # maps requests for the URL/pages/home to the about us action in the Pages controller.
  188. # By using GET we arrange for the route to respond to a GET request.
  189. # With this we generate a about us action inside the Pages controller, automatically
  190. # get a page at the address /pages/about us
  191. get '/about', to: 'pages#about'
  192. get '/signup', to: 'users#new'
  193. post '/signup', to: 'users#create' #signup route that responds to POST requests.
  194. get '/login', to: 'sessions#new'
  195. post '/login', to: 'sessions#create'
  196. delete '/logout', to: 'sessions#destroy'
  197.  
  198. #resources :sessions
  199. resources :users
  200. # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  201.  
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement