/
inclusion_connect_controller_spec.rb
169 lines (131 loc) · 7.8 KB
/
inclusion_connect_controller_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# frozen_string_literal: true
describe InclusionConnectController, type: :controller do
let(:base_url) { "https://test.inclusion.connect.fr" }
stub_sentry_events
describe "#callback" do
it "update first_name and last_name of agent" do
now = Time.zone.parse("2022-08-22 11h34")
travel_to(now)
agent = create(:agent, :invitation_not_accepted, first_name: nil, last_name: nil, email: "bob@demo.rdv-solidarites.fr")
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 200, body: { access_token: "zekfjzeklfjl", expires_in: now + 1.week, scopes: "openid" }.to_json, headers: {})
stub_request(:get, "#{base_url}/userinfo?schema=openid").with(
headers: {
"Expect" => "",
"Authorization" => "Bearer zekfjzeklfjl",
"User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus",
}
).to_return(status: 200, body: { email_verified: true, given_name: "Bob", family_name: "Eponge", email: "bob@demo.rdv-solidarites.fr" }.to_json, headers: {})
state = "A STATE"
session[:ic_state] = state
get :callback, params: { state: state, session_state: state, code: "klzefklzejlf" }
agent.reload
expect(agent.first_name).to eq("Bob")
expect(agent.last_name).to eq("Eponge")
expect(agent.confirmed_at).to be_within(10.seconds).of(now)
end
it "returns an error if state doesn't match" do
session[:ic_state] = "AZEERT"
get :callback, params: { state: "zefjzelkf", session_state: "zfjzerklfjz", code: "klzefklzejlf" }
expect(response).to redirect_to(new_agent_session_path)
expect(flash[:error]).to eq("Nous n'avons pas pu vous authentifier. Contacter le support à l'adresse <support@rdv-solidarites.fr> si le problème persiste.")
# Error message is sent to Sentry
expect(sentry_events.last.message).to include("InclusionConnect states do not match")
expect(sentry_events.last.extra.keys).to match_array(%i[params_state session_ic_state])
end
it "uses the current domain's support email address in the error message" do
request.host = "www.rdv-mairie-test.localhost"
get :callback, params: { state: "zefjzelkf", session_state: "zfjzerklfjz", code: "klzefklzejlf" }
expect(flash[:error]).to include("support@rdv-mairie.fr")
end
it "returns an error if token request error" do
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 500, body: { error: "an error occurs" }.to_json, headers: {})
session[:ic_state] = "a state"
get :callback, params: { state: "a state", session_state: "a state", code: "klzefklzejlf" }
expect(response).to redirect_to(new_agent_session_path)
expect(flash[:error]).to eq("Nous n'avons pas pu vous authentifier. Contacter le support à l'adresse <support@rdv-solidarites.fr> si le problème persiste.")
# HTTP request is sent to Sentry as breadcrumbs
expect(sentry_events.last.breadcrumbs.compact.map(&:message)).to eq(["HTTP request", "HTTP response"])
end
it "returns an error if token request doesn't contains token" do
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 200, body: {}.to_json, headers: {})
session[:ic_state] = "a state"
get :callback, params: { state: "a state", session_state: "a state", code: "klzefklzejlf" }
expect(response).to redirect_to(new_agent_session_path)
expect(flash[:error]).to eq("Nous n'avons pas pu vous authentifier. Contacter le support à l'adresse <support@rdv-solidarites.fr> si le problème persiste.")
# HTTP request is sent to Sentry as breadcrumbs
expect(sentry_events.last.breadcrumbs.compact.map(&:message)).to eq(["HTTP request", "HTTP response"])
end
it "returns an error if userinfo request doesnt work" do
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 200, body: { access_token: "zekfjzeklfjl", expires_in: "", scopes: "openid" }.to_json, headers: {})
stub_request(:get, "#{base_url}/userinfo?schema=openid").with(
headers: {
"Expect" => "",
"Authorization" => "Bearer zekfjzeklfjl",
"User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus",
}
).to_return(status: 500, body: "", headers: {})
session[:ic_state] = "a state"
get :callback, params: { state: "a state", session_state: "a state", code: "klzefklzejlf" }
expect(response).to redirect_to(new_agent_session_path)
expect(flash[:error]).to eq("Nous n'avons pas pu vous authentifier. Contacter le support à l'adresse <support@rdv-solidarites.fr> si le problème persiste.")
# HTTP request is sent to Sentry as breadcrumbs
expect(sentry_events.last.breadcrumbs.compact.map(&:message).uniq).to eq(["HTTP request", "HTTP response"])
end
it "returns an error if userinfo's email checked is false" do
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 200, body: { access_token: "zekfjzeklfjl", expires_in: "", scopes: "openid" }.to_json, headers: {})
stub_request(:get, "#{base_url}/userinfo?schema=openid").with(
headers: {
"Expect" => "",
"Authorization" => "Bearer zekfjzeklfjl",
"User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus",
}
).to_return(status: 200, body: { email_verified: false, given_name: "Bob", family_name: "Eponge", email: "bob@demo.rdv-solidarites.fr" }.to_json, headers: {})
session[:ic_state] = "a state"
get :callback, params: { state: "a state", session_state: "a state", code: "klzefklzejlf" }
expect(response).to redirect_to(new_agent_session_path)
expect(flash[:error]).to eq("Nous n'avons pas pu vous authentifier. Contacter le support à l'adresse <support@rdv-solidarites.fr> si le problème persiste.")
# HTTP request is sent to Sentry as breadcrumbs
expect(sentry_events.last.breadcrumbs.compact.map(&:message).uniq).to eq(["HTTP request", "HTTP response"])
end
it "call sentry about authentification failure" do
stub_const("InclusionConnect::IC_CLIENT_ID", "truc")
stub_const("InclusionConnect::IC_CLIENT_SECRET", "truc secret")
stub_const("InclusionConnect::IC_BASE_URL", base_url)
stub_token_request.to_return(status: 500, body: { error: "an error occurs" }.to_json, headers: {})
session[:ic_state] = "a state"
expect(Sentry).to receive(:capture_message).with("Failed to authentify agent with inclusionConnect")
get :callback, params: { state: "a state", session_state: "a state", code: "klzefklzejlf" }
end
end
def stub_token_request
stub_request(:post, "#{base_url}/token").with(
body: {
"client_id" => "truc",
"client_secret" => "truc secret",
"code" => "klzefklzejlf",
"grant_type" => "authorization_code",
"redirect_uri" => inclusion_connect_callback_url,
},
headers: {
"Expect" => "",
"User-Agent" => "Typhoeus - https://github.com/typhoeus/typhoeus",
"Content-Type" => "application/x-www-form-urlencoded",
}
)
end
end