mirror of
https://github.com/discourse/discourse.git
synced 2026-08-11 01:06:53 +08:00
RSpec setup becomes harder to follow at either extreme: trivial fixture wrappers hide lifecycle and intent, while forcing every named operation inline repeats low-level protocol and configuration details. This change documents and applies a test-setup hierarchy: - use `fab!`, `let`, `let!`, `subject`, and inline `Fabricate` according to lifecycle and role; - use a small example-group method when parameterized behavior gives one spec useful vocabulary; - move helpers into auto-loaded `spec/support` only when they are shared across spec files; - use fabricators and page objects for the data shapes and system-test interfaces they own. Core and plugin support files are loaded centrally by `rails_helper`, so plugin-specific support loaders are unnecessary. The migration specs encountered during the sweep are removed according to repository policy; production migrations are unchanged.
119 lines
3.7 KiB
Ruby
Vendored
119 lines
3.7 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
describe "Microsoft OAuth2" do
|
|
let(:access_token) { "microsoft_access_token_448" }
|
|
let(:client_id) { "abcdef11223344" }
|
|
let(:client_secret) { "adddcccdddd99922" }
|
|
let(:temp_code) { "microsoft_temp_code_544254" }
|
|
|
|
fab!(:user1, :user)
|
|
|
|
before do
|
|
SiteSetting.microsoft_auth_enabled = true
|
|
SiteSetting.microsoft_auth_client_id = client_id
|
|
SiteSetting.microsoft_auth_client_secret = client_secret
|
|
|
|
stub_request(:post, "https://login.microsoftonline.com/common/oauth2/v2.0/token").with(
|
|
body:
|
|
hash_including(
|
|
"client_id" => client_id,
|
|
"client_secret" => client_secret,
|
|
"code" => temp_code,
|
|
"grant_type" => "authorization_code",
|
|
"redirect_uri" => "http://test.localhost/auth/microsoft_office365/callback",
|
|
),
|
|
).to_return(
|
|
status: 200,
|
|
body:
|
|
Rack::Utils.build_query(
|
|
access_token: access_token,
|
|
token_type: "Bearer",
|
|
expires_in: 3599,
|
|
scope: "openid email profile https://graph.microsoft.com/User.Read",
|
|
),
|
|
headers: {
|
|
"Content-Type" => "application/x-www-form-urlencoded",
|
|
},
|
|
)
|
|
|
|
stub_request(:get, "https://graph.microsoft.com/v1.0/me").with(
|
|
headers: {
|
|
"Authorization" => "Bearer #{access_token}",
|
|
},
|
|
).to_return(
|
|
status: 200,
|
|
body:
|
|
JSON.dump(
|
|
businessPhones: ["+1 425 555 0109"],
|
|
displayName: "Adele Vance",
|
|
givenName: "Adele",
|
|
jobTitle: "Retail Manager",
|
|
mail: user1.email,
|
|
mobilePhone: "+1 425 555 0109",
|
|
officeLocation: "18/2111",
|
|
preferredLanguage: "en-US",
|
|
surname: "Vance",
|
|
userPrincipalName: user1.email,
|
|
id: "87d349ed-44d7-43e1-9a83-5f2406dee5bd",
|
|
),
|
|
headers: {
|
|
"Content-Type" => "application/json",
|
|
},
|
|
)
|
|
end
|
|
|
|
it "signs in the user whose email matches the email included in the API response from microsoft when `microsoft_auth_email_verified` site setting is true" do
|
|
SiteSetting.microsoft_auth_email_verified = true
|
|
|
|
post "/auth/microsoft_office365"
|
|
|
|
expect(response.status).to eq(302)
|
|
expect(response.location).to start_with(
|
|
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
|
)
|
|
|
|
post "/auth/microsoft_office365/callback",
|
|
params: {
|
|
state: session["omniauth.state"],
|
|
code: temp_code,
|
|
}
|
|
|
|
expect(response.status).to eq(302)
|
|
expect(response.location).to eq("http://test.localhost/")
|
|
expect(session[:current_user_id]).to eq(user1.id)
|
|
end
|
|
|
|
it "does not sign in the user whose email matches the email included in the API response from microsoft when `microsoft_auth_email_verified` site setting is false" do
|
|
SiteSetting.microsoft_auth_email_verified = false
|
|
|
|
post "/auth/microsoft_office365"
|
|
|
|
expect(response.status).to eq(302)
|
|
expect(response.location).to start_with(
|
|
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
|
)
|
|
|
|
post "/auth/microsoft_office365/callback",
|
|
params: {
|
|
state: session["omniauth.state"],
|
|
code: temp_code,
|
|
}
|
|
|
|
expect(response.status).to eq(302)
|
|
expect(response.location).to eq("http://test.localhost/")
|
|
expect(session[:current_user_id]).to eq(nil)
|
|
end
|
|
|
|
context "when configured as single tenant" do
|
|
it "uses the tenant id from the site setting" do
|
|
SiteSetting.microsoft_auth_tenant_id = "my-tenant-id"
|
|
|
|
post "/auth/microsoft_office365"
|
|
|
|
expect(response.status).to eq(302)
|
|
expect(response.location).to start_with(
|
|
"https://login.microsoftonline.com/my-tenant-id/oauth2/v2.0/authorize",
|
|
)
|
|
end
|
|
end
|
|
end
|