Skip to content

Commit 71bd642

Browse files
committed
✅ Add specs for JSON body support
1 parent beb4e15 commit 71bd642

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

spec/integration/middleware_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,55 @@
6161
end
6262
end
6363

64+
it "POST /auth/ldap accepts JSON-style credentials via Rails env and sets omniauth.auth" do
65+
begin
66+
OmniAuth.config.test_mode = true
67+
OmniAuth.config.mock_auth[:ldap] = OmniAuth::AuthHash.new(provider: "ldap", uid: "json-bob", info: {"name" => "Bob"})
68+
69+
env = {
70+
"CONTENT_TYPE" => "application/json",
71+
"action_dispatch.request.request_parameters" => {"username" => "bob", "password" => "secret"},
72+
}
73+
post "/auth/ldap", nil, env
74+
75+
# Follow redirects to callback
76+
max_redirects = 5
77+
redirects = 0
78+
while last_response.status == 302 && redirects < max_redirects
79+
follow_redirect!
80+
redirects += 1
81+
end
82+
83+
expect(last_response.status).to eq 200
84+
expect(last_response.body).to include("true")
85+
ensure
86+
OmniAuth.config.mock_auth.delete(:ldap)
87+
OmniAuth.config.test_mode = false
88+
end
89+
end
90+
91+
it "POST /auth/ldap/callback with JSON missing username and password redirects with missing_credentials" do
92+
env = {
93+
"CONTENT_TYPE" => "application/json",
94+
"action_dispatch.request.request_parameters" => {},
95+
}
96+
post "/auth/ldap/callback", nil, env
97+
98+
expect(last_response.status).to eq 302
99+
expect(last_response.headers["Location"]).to match(/missing_credentials/)
100+
end
101+
102+
it "POST /auth/ldap/callback with JSON username but missing password redirects with missing_credentials" do
103+
env = {
104+
"CONTENT_TYPE" => "application/json",
105+
"action_dispatch.request.request_parameters" => {"username" => "bob"},
106+
}
107+
post "/auth/ldap/callback", nil, env
108+
109+
expect(last_response.status).to eq 302
110+
expect(last_response.headers["Location"]).to match(/missing_credentials/)
111+
end
112+
64113
it "honors SCRIPT_NAME when mounted under a subdirectory for redirect to callback" do
65114
begin
66115
OmniAuth.config.test_mode = true

spec/omniauth/strategies/ldap_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ def make_env(path = "/auth/ldap", props = {})
8787
expect(last_response.headers["Location"]).to eq "http://example.org/auth/ldap/callback"
8888
end
8989

90+
it "redirects to callback when JSON POST includes username and password (Rails env)" do
91+
env = {
92+
"REQUEST_METHOD" => "POST",
93+
"CONTENT_TYPE" => "application/json",
94+
# Rails places parsed JSON params here
95+
"action_dispatch.request.request_parameters" => {"username" => "json_alice", "password" => "json_secret"},
96+
}
97+
post "/auth/ldap", nil, env
98+
expect(last_response).to be_redirect
99+
expect(last_response.headers["Location"]).to eq "http://example.org/auth/ldap/callback"
100+
end
101+
90102
context "when mounted under a subdirectory" do
91103
let(:sub_env) do
92104
make_env("/auth/ldap", {

0 commit comments

Comments
 (0)