Skip to content

Commit

Permalink
Merge pull request #45 from banyan/cleanup-session-controller-and-add…
Browse files Browse the repository at this point in the history
…-spec

Cleanup session controller and add spec
  • Loading branch information
hsbt committed Mar 16, 2013
2 parents 18666de + de6b65a commit 5ad8f7d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
17 changes: 6 additions & 11 deletions app/controllers/sessions_controller.rb
@@ -1,27 +1,22 @@
# This controller handles the login/logout function of the site.
# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
# render new.rhtml
def new
if Member.count == 0
redirect_to sign_up_path
end
redirect_to sign_up_path if Member.count == 0
end

def create
session[:member_id] = Member.authenticate(params[:username], params[:password])
if logged_in?
redirect_to '/'
flash[:notice] = "Signed in successfully"
redirect_to root_path, notice: "Signed in successfully"
else
flash[:notice] = "Cannot sign in"
render :action => :new
flash[:alert] = "Cannot sign in"
render :new
end
end

def destroy
session[:member_id] = nil if logged_in?
reset_session
flash[:notice] = "You have been signed out."
redirect_to root_url
redirect_to root_url, notice: "You have been signed out."
end
end
5 changes: 5 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -26,6 +26,11 @@
<p style="color: green"><%= flash[:notice] %></p>
</div>
<%- end -%>
<%- if flash[:alert] -%>
<div class="alert" style="text-align:center;background-color:#fdf3eb;padding:0.5em">
<p style="color: #e81a37"><%= flash[:alert] %></p>
</div>
<%- end -%>
<%= yield %>
</div>
</body>
Expand Down
68 changes: 68 additions & 0 deletions spec/controllers/sessions_controller_spec.rb
@@ -0,0 +1,68 @@
require 'spec_helper'

describe SessionsController do
let(:member) {
Factory(:member, password: 'mala', password_confirmation: 'mala')
}

let(:valid_sessions) {
{ member_id: member.id }
}

describe "GET 'new'" do
context "when member doesn't exist" do
it do
get 'new'
response.should redirect_to sign_up_path
end
end

context "when member exists" do
before { member }

it do
get 'new'
response.should be_success
end
end
end

describe "POST 'create'" do
context "when authenticate successfully" do
it "should redirect to root path" do
post :create, { username: member.username, password: member.password }
expect(response).to redirect_to root_path
flash[:notice].should_not be_nil
end
end

context "when authenticate failed" do
it "should re-render new page" do
post :create, { username: "bogus_username", password: "bogus_password" }
response.should render_template("new")
flash[:alert].should_not be_nil
end
end
end

describe "GET 'destroy'" do
before {
member
session[:member_id] = Member.authenticate(member.username, member.password)
}

it "should remove session id" do
expect {
get 'destroy'
}.to change {
session[:member_id]
}.from(member).to(nil)
end

it "should redirect to root path" do
get 'destroy'
expect(response).to redirect_to root_path
flash[:notice].should_not be_nil
end
end
end

0 comments on commit 5ad8f7d

Please sign in to comment.