Skip to content
Browse files

Be able to regenerate sessions

  • Loading branch information...
1 parent db13b25 commit 28772b8781f5cae8058a4e1a779fafe42a776a8c @chischaschos committed Feb 22, 2014
Showing with 18 additions and 12 deletions.
  1. +2 −0 lib/todo/services/session_creator.rb
  2. +7 −5 spec/api/sessions_spec.rb
  3. +9 −7 spec/services/session_creator_spec.rb
View
2 lib/todo/services/session_creator.rb
@@ -28,6 +28,8 @@ def user
end
def create_session
+ fail 'Session could not be destroyed' if @user.session && !@user.session.destroy
+
@session = Models::Session.create user: @user
if @session.saved?
View
12 spec/api/sessions_spec.rb
@@ -18,18 +18,20 @@
end
context 'when a session already exists' do
- before do
+ let!(:previous_access_token) do
session_creator = Todo::Services::SessionCreator.new(params)
expect(session_creator.valid?).to be_true
+ session_creator.access_token
end
- xit 'should delete older session and create a new one' do
+ it 'should delete older session and create a new one' do
post '/api/session', { user: params }
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8'
- expect(last_response.headers['Set-Cookie']).to be_nil
- expect(last_response.body).to have_json_path 'errors/user'
- expect(last_response.status).to eq 404
+ expect(last_response.headers['Set-Cookie']).to match /access_token=#{user.session.access_token}/
+ expect(user.session.access_token).not_to eq previous_access_token
+ expect(last_response.body).to eq ''
+ expect(last_response.status).to eq 200
end
end
end
View
16 spec/services/session_creator_spec.rb
@@ -9,24 +9,26 @@
it 'should create a session' do
session_creator = Todo::Services::SessionCreator.new params
- expect(session_creator.valid?).to be_true
+ expect(session_creator).to be_valid
expect(session_creator.access_token).not_to be_nil
expect(session_creator.h_errors[:errors]).to be_empty
end
end
context 'when a session already exists' do
- before do
+ let!(:previous_access_token) do
Todo::Models::User.create(params)
session_creator = Todo::Services::SessionCreator.new(params)
- expect(session_creator.valid?).to be_true
+ expect(session_creator).to be_valid
+ session_creator.access_token
end
- it 'should not create a session' do
+ it 'should create a new session' do
session_creator = Todo::Services::SessionCreator.new params
- expect(session_creator.valid?).not_to be_true
- expect(session_creator.access_token).to be_nil
- expect(session_creator.h_errors[:errors]).not_to be_empty
+ expect(session_creator).to be_valid
+ expect(session_creator.access_token).not_to be_nil
+ expect(session_creator.access_token).not_to eq previous_access_token
+ expect(session_creator.h_errors[:errors]).to be_empty
end
end
end

0 comments on commit 28772b8

Please sign in to comment.
Something went wrong with that request. Please try again.