2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ApplicationController do
|
|
|
|
describe '#check_password_expiration' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:controller) { ApplicationController.new }
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'redirects if the user is over their password expiry' do
|
2014-09-02 18:07:02 +05:30
|
|
|
user.password_expires_at = Time.new(2002)
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(user.ldap_user?).to be_falsey
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).to receive(:redirect_to)
|
|
|
|
expect(controller).to receive(:new_profile_password_path)
|
2014-09-02 18:07:02 +05:30
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'does not redirect if the user is under their password expiry' do
|
2014-09-02 18:07:02 +05:30
|
|
|
user.password_expires_at = Time.now + 20010101
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(user.ldap_user?).to be_falsey
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
2014-09-02 18:07:02 +05:30
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'does not redirect if the user is over their password expiry but they are an ldap user' do
|
2014-09-02 18:07:02 +05:30
|
|
|
user.password_expires_at = Time.new(2002)
|
2015-04-26 12:48:37 +05:30
|
|
|
allow(user).to receive(:ldap_user?).and_return(true)
|
|
|
|
allow(controller).to receive(:current_user).and_return(user)
|
|
|
|
expect(controller).not_to receive(:redirect_to)
|
2014-09-02 18:07:02 +05:30
|
|
|
controller.send(:check_password_expiration)
|
|
|
|
end
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
describe "#authenticate_user_from_token!" do
|
|
|
|
describe "authenticating a user from a private token" do
|
|
|
|
controller(ApplicationController) do
|
|
|
|
def index
|
|
|
|
render text: "authenticated"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
context "when the 'private_token' param is populated with the private token" do
|
|
|
|
it "logs the user in" do
|
|
|
|
get :index, private_token: user.private_token
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(response).to have_http_status(200)
|
2016-06-22 15:30:34 +05:30
|
|
|
expect(response.body).to eq("authenticated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the 'PRIVATE-TOKEN' header is populated with the private token" do
|
|
|
|
it "logs the user in" do
|
|
|
|
@request.headers['PRIVATE-TOKEN'] = user.private_token
|
|
|
|
get :index
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(response).to have_http_status(200)
|
2016-06-22 15:30:34 +05:30
|
|
|
expect(response.body).to eq("authenticated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log the user in otherwise" do
|
|
|
|
@request.headers['PRIVATE-TOKEN'] = "token"
|
|
|
|
get :index, private_token: "token", authenticity_token: "token"
|
|
|
|
expect(response.status).not_to eq(200)
|
|
|
|
expect(response.body).not_to eq("authenticated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "authenticating a user from a personal access token" do
|
|
|
|
controller(ApplicationController) do
|
|
|
|
def index
|
|
|
|
render text: 'authenticated'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:personal_access_token) { create(:personal_access_token, user: user) }
|
|
|
|
|
|
|
|
context "when the 'personal_access_token' param is populated with the personal access token" do
|
|
|
|
it "logs the user in" do
|
|
|
|
get :index, private_token: personal_access_token.token
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(response).to have_http_status(200)
|
2016-06-22 15:30:34 +05:30
|
|
|
expect(response.body).to eq('authenticated')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the 'PERSONAL_ACCESS_TOKEN' header is populated with the personal access token" do
|
|
|
|
it "logs the user in" do
|
|
|
|
@request.headers["PRIVATE-TOKEN"] = personal_access_token.token
|
|
|
|
get :index
|
2016-08-24 12:49:21 +05:30
|
|
|
expect(response).to have_http_status(200)
|
2016-06-22 15:30:34 +05:30
|
|
|
expect(response.body).to eq('authenticated')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log the user in otherwise" do
|
|
|
|
get :index, private_token: "token"
|
|
|
|
expect(response.status).not_to eq(200)
|
|
|
|
expect(response.body).not_to eq('authenticated')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|