require 'rails_helper'
RSpec.describe "admin user persistence token", type: :request, csrf: false do
let(:user_attributes) do
{
first_name: "System",
last_name: "Administrator",
email: "admin@petition.parliament.uk",
password: "L3tme1n!",
password_confirmation: "L3tme1n!"
}
end
let(:login_params) do
{ email: "admin@petition.parliament.uk", password: "L3tme1n!" }
end
before do
FactoryBot.create(:sysadmin_user, user_attributes)
end
def new_browser
open_session do |s| - new_browser has the variable name 's'
s.reset!
s.host! "moderate.petition.parliament.uk"
s.https!
end
end
context "when a new session is created" do
it "logs out existing sessions" do - context(when a new session is created)::it#logs out existing sessions has a flog score of 70
s1 = new_browser
s1.post "/admin/user_sessions", admin_user_session: login_params
expect(s1.response.status).to eq(302)
expect(s1.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin")
s2 = new_browser
s2.post "/admin/user_sessions", admin_user_session: login_params
expect(s2.response.status).to eq(302)
expect(s2.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin")
s1.get("/admin")
expect(s1.response.status).to eq(302)
expect(s1.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin/login")
end
end
context "when a session is destroyed" do
it "resets the persistence token" do - context(when a session is destroyed)::it#resets the persistence token has a flog score of 73
s1 = new_browser
s1.post "/admin/user_sessions", admin_user_session: login_params
expect(s1.response.status).to eq(302)
expect(s1.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin")
s2 = new_browser
s2.cookies["admin_user_credentials"] = s1.cookies["admin_user_credentials"]
s1.get("/admin/logout")
expect(s1.response.status).to eq(302)
expect(s1.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin/login")
s2.get("/admin")
expect(s2.response.status).to eq(302)
expect(s2.response.headers["Location"]).to eq("https://moderate.petition.parliament.uk/admin/login")
end
end
context "when a session is stale" do
before do
host! "moderate.petition.parliament.uk"
https!
end
it "resets the persistence token" do - context(when a session is stale)::it#resets the persistence token has a flog score of 64
Site.instance.update(login_timeout: 600)
travel_to 5.minutes.ago do -
post "/admin/user_sessions", admin_user_session: login_params
expect(response).to redirect_to("/admin")
end
get "/admin"
expect(response).to be_successful
travel_to 15.minutes.from_now do
get "/admin"
expect(response).to redirect_to("/admin/login")
end
Site.instance.update(login_timeout: 1800)
travel_to 15.minutes.from_now do
get "/admin"
expect(response).to redirect_to("/admin/login")
end
end
end
end