Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from banyan/cleanup-session-controller-and-add…
…-spec Cleanup session controller and add spec
- Loading branch information
Showing
3 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |