-
Notifications
You must be signed in to change notification settings - Fork 4
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 #109 from kortirso/issue_106
IS-106 Users sessions expiration
- Loading branch information
Showing
19 changed files
with
242 additions
and
24 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
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
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module Users | ||
module Sessions | ||
class RemoveExpiredJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform | ||
Users::Session | ||
.where('created_at < ?', DateTime.now - JwtEncoder::EXPIRATION_SECONDS.seconds) | ||
.destroy_all | ||
end | ||
end | ||
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
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
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
13 changes: 13 additions & 0 deletions
13
db/migrate/20230617083913_change_sessions_index_uniqueness.rb
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,13 @@ | ||
class ChangeSessionsIndexUniqueness < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
remove_index :users_sessions, column: :user_id | ||
add_index :users_sessions, :user_id, unique: false, algorithm: :concurrently | ||
end | ||
|
||
def down | ||
remove_index :users_sessions, column: :user_id | ||
add_index :users_sessions, :user_id, unique: true, algorithm: :concurrently | ||
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
105 changes: 105 additions & 0 deletions
105
spec/controllers/users/omniauth_callbacks_controller_spec.rb
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,105 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Users::OmniauthCallbacksController do | ||
describe 'POST#create' do | ||
let(:code) { nil } | ||
let(:request) { post :create, params: { provider: provider, code: code } } | ||
|
||
context 'for unexisting provider' do | ||
let(:provider) { 'unknown' } | ||
|
||
it 'redirects to root path', :aggregate_failures do | ||
expect { request }.not_to change(User, :count) | ||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
context 'for existing provider' do | ||
let(:provider) { Providerable::GITHUB } | ||
|
||
context 'for blank code' do | ||
it 'redirects to root path', :aggregate_failures do | ||
expect { request }.not_to change(User, :count) | ||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
context 'for present code' do | ||
let!(:auth_service) { double } | ||
let(:code) { 'code' } | ||
|
||
before do | ||
allow(Auth::Providers::Github).to receive(:call).and_return(auth_service) | ||
end | ||
|
||
context 'for invalid code' do | ||
before do | ||
allow(auth_service).to receive(:result).and_return(nil) | ||
end | ||
|
||
it 'redirects to root path', :aggregate_failures do | ||
expect { request }.not_to change(User, :count) | ||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
context 'for valid code' do | ||
before do | ||
allow(auth_service).to receive(:result).and_return(auth_payload) | ||
end | ||
|
||
context 'for invalid payload' do | ||
let(:auth_payload) do | ||
{ | ||
uid: '123', | ||
provider: 'github', | ||
login: 'octocat', | ||
email: nil | ||
} | ||
end | ||
|
||
it 'redirects to root path', :aggregate_failures do | ||
expect { request }.not_to change(User, :count) | ||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
context 'for valid payload' do | ||
let(:auth_payload) do | ||
{ | ||
uid: '123', | ||
provider: 'github', | ||
login: 'octocat', | ||
email: 'email@gmail.com' | ||
} | ||
end | ||
|
||
it 'redirects to companies path', :aggregate_failures do | ||
expect { request }.to change(User, :count).by(1) | ||
expect(response).to redirect_to companies_path | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'DELETE#destroy' do | ||
it_behaves_like 'required auth' | ||
|
||
context 'for logged users' do | ||
sign_in_user | ||
|
||
before { create :users_session, user: @current_user } | ||
|
||
it 'destroys session and redirects', :aggregate_failures do | ||
expect { do_request }.to change(Users::Session, :count).by(-1) | ||
expect(response).to redirect_to root_path | ||
end | ||
end | ||
|
||
def do_request | ||
delete :destroy, params: { locale: 'en' } | ||
end | ||
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
describe ApplicationHelper do | ||
describe '#omniauth_link' do | ||
context 'for unexisting provider' do | ||
it 'returns nil' do | ||
expect(helper.omniauth_link(:unknown)).to be_nil | ||
end | ||
end | ||
|
||
context 'for github provider' do | ||
it 'returns github link' do | ||
expect(helper.omniauth_link(:github).include?('https://github.com')).to be_truthy | ||
end | ||
end | ||
|
||
context 'for gitlab provider' do | ||
it 'returns gitlab link' do | ||
expect(helper.omniauth_link(:gitlab).include?('https://gitlab.com')).to be_truthy | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Users::Sessions::RemoveExpiredJob, type: :service do | ||
subject(:job_call) { described_class.perform_now } | ||
|
||
before do | ||
create :users_session, created_at: DateTime.now - JwtEncoder::EXPIRATION_SECONDS.seconds - 10.seconds | ||
create :users_session, created_at: DateTime.now | ||
end | ||
|
||
it 'removes expired sessions' do | ||
expect { job_call }.to change(Users::Session, :count).by(-1) | ||
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
Oops, something went wrong.