Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaelvilleneuve committed Feb 13, 2025
1 parent aa00b8f commit 70d47c3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 80 deletions.
6 changes: 2 additions & 4 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def create
end

def destroy
# On lit la valeur de la session et on prépare la redirection ici parce qu'il y a un `clear_session` ensuite
clear_session
flash[:notice] = "Déconnexion réussie"
sign_out_path = OmniAuth::Strategies::RdvServicePublic.sign_out_path(ENV["RDV_SOLIDARITES_OAUTH_APP_ID"])
redirect_to "#{ENV['RDV_SOLIDARITES_URL']}#{sign_out_path}", allow_other_host: true

clear_session
flash[:notice] = "Déconnexion réussie" # rubocop:disable Rails/ActionControllerFlashBeforeRender
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/rdv_solidarites_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(uid:, client:, access_token:)
end

def valid?
@email.present? || (required_attributes_present? && token_valid?)
required_attributes_present? && token_valid?
end

def email
Expand Down
2 changes: 1 addition & 1 deletion app/views/common/_header_website.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div class="d-flex align-items-center">
<% if !current_agent %>
<div class="mx-3">
<%= form_tag('/auth/rdvservicepublic', method: 'post', data: {turbo: false}) do %>
<%= form_tag("/auth/rdvservicepublic", method: "post", data: { turbo: false }) do %>
<div class="d-flex mb-4 justify-content-center align-items-center">
<button class="btn btn-blue" type='submit'>Connexion agent</button>
</div>
Expand Down
149 changes: 75 additions & 74 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,106 @@
let!(:organisation) { create(:organisation) }
let!(:agent_email) { "[email protected]" }
let!(:agent) { create(:agent, email: agent_email, organisations: [organisation], last_sign_in_at: nil) }
let!(:rdv_solidarites_credentials) { instance_double(RdvSolidaritesCredentials) }
let!(:timestamp) { Time.zone.now }

describe "POST #create" do
context "JSON" do
let(:request_headers) do
{
"omniauth.auth" => {
"credentials" => {
"token" => "some-token"
},
"info" => {
"agent" => {
"email" => agent_email
}
let(:request_headers) do
{
"omniauth.auth" => {
"credentials" => {
"token" => "some-token"
},
"info" => {
"agent" => {
"email" => agent_email
}
}
}
end
}
end

before do
request.headers.merge(request_headers)
end

it "is a success" do
post :create
expect(flash[:success]).to eq("Connexion réussie")
expect(response).to have_http_status(:found)
end

it "marks the agent as logged in" do
post :create
expect(agent.reload.last_sign_in_at).not_to be_nil
end

it "sets a session" do
post :create

expect(request.session[:agent_auth]).to eq(
{
id: agent.id,
created_at: timestamp.to_i,
origin: "sign_in_form",
signature: agent.sign_with(timestamp.to_i)
}
)
end

context "when a redirect path is in the session" do
before do
request.headers.merge(request_headers)
allow(RdvSolidaritesCredentials).to receive(:new)
.and_return(rdv_solidarites_credentials)
allow(rdv_solidarites_credentials).to receive_messages(email: agent_email)
request.session[:agent_return_to] = "/some_path"
end

it "is a success" do
it "deletes the path from the session" do
post :create
expect(response).to have_http_status(:found)
expect(request.session[:agent_return_to]).to be_nil
end

it "marks the agent as logged in" do
it "redirects to the path" do
post :create
expect(agent.reload.last_sign_in_at).not_to be_nil
expect(response.location).to include("/some_path")
end
end

it "sets a session" do
post :create

expect(request.session[:agent_auth]).to eq(
{
id: agent.id,
created_at: timestamp.to_i,
origin: "sign_in_form",
signature: agent.sign_with(timestamp.to_i)
}
)
context "when no redirect path is in the session" do
before do
request.session[:agent_return_to] = nil
end

context "when a redirect path is in the session" do
before do
request.session[:agent_return_to] = "/some_path"
end

it "deletes the path from the session" do
post :create
expect(request.session[:agent_return_to]).to be_nil
end
it "returns the organisations path" do
post :create
expect(response.location).to eq(root_url)
end
end

context "when no redirect path is in the session" do
before do
request.session[:agent_return_to] = nil
end
context "when it fails to retrieve the agent" do
let!(:agent) { create(:agent, email: "[email protected]", organisations: [organisation]) }

it "returns the organisations path" do
post :create
expect(response.location).to eq(root_url)
end
it "is a failure" do
post :create
expect(response).not_to be_successful
expect(flash[:error]).to include(
"L'agent ne fait pas partie d'une organisation sur RDV-Insertion"
)
expect(request.session[:agent_auth]).to be_nil
end
end

context "when it fails to retrieve the agent" do
let!(:agent) { create(:agent, email: "[email protected]", organisations: [organisation]) }

it "is a failure" do
post :create
expect(response).not_to be_successful
expect(flash[:error]).to include(
"L'agent ne fait pas partie d'une organisation sur RDV-Insertion"
)
expect(request.session[:agent_auth]).to be_nil
end
context "when it fails to mark the agent as logged in" do
before do
allow(Agent).to receive(:find_by).and_return(agent)
allow(agent).to receive(:update).and_return(false)
allow(agent).to receive_message_chain(:errors, :full_messages)
.and_return(["Update impossible"])
end

context "when it fails to mark the agent as logged in" do
before do
allow(Agent).to receive(:find_by).and_return(agent)
allow(agent).to receive(:update).and_return(false)
allow(agent).to receive_message_chain(:errors, :full_messages)
.and_return(["Update impossible"])
end

it "is a failure" do
post :create
expect(response).not_to be_successful
expect(flash[:error]).to include("Update impossible")
expect(request.session[:agent_auth]).to be_nil
end
it "is a failure" do
post :create
expect(response).not_to be_successful
expect(flash[:error]).to include("Update impossible")
expect(response).to redirect_to(root_url)
expect(request.session[:agent_auth]).to be_nil
end
end
end
Expand Down

0 comments on commit 70d47c3

Please sign in to comment.