This repository was archived by the owner on Jun 19, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
FEATURE: retrieve user avatar #6
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe DiscourseLoginClientAuthenticator do | ||
let(:authenticator) { described_class.new } | ||
let(:user) { Fabricate(:user) } | ||
|
||
context "with default settings" do | ||
before do | ||
SiteSetting.discourse_login_client_enabled = true | ||
SiteSetting.discourse_login_client_id = "client_id" | ||
SiteSetting.discourse_login_client_secret = "client_secret" | ||
end | ||
|
||
it "has the right name" do | ||
expect(authenticator.name).to eq("discourse_login") | ||
end | ||
|
||
it "can connect to existing user" do | ||
expect(authenticator.can_connect_existing_user?).to eq(true) | ||
end | ||
|
||
it "can be revoked" do | ||
expect(authenticator.can_revoke?).to eq(true) | ||
end | ||
|
||
it "verifies email by default" do | ||
expect(authenticator.primary_email_verified?({})).to eq(true) | ||
end | ||
|
||
it "does not always update user email" do | ||
expect(authenticator.always_update_user_email?).to eq(false) | ||
end | ||
|
||
describe "#enabled?" do | ||
it "is enabled with proper settings" do | ||
expect(authenticator.enabled?).to eq(true) | ||
end | ||
|
||
it "is disabled without client id" do | ||
SiteSetting.discourse_login_client_id = "" | ||
expect(authenticator.enabled?).to eq(false) | ||
end | ||
|
||
it "is disabled without client secret" do | ||
SiteSetting.discourse_login_client_secret = "" | ||
expect(authenticator.enabled?).to eq(false) | ||
end | ||
|
||
it "is disabled when `discourse_login_client_enabled` is false" do | ||
SiteSetting.discourse_login_client_enabled = false | ||
expect(authenticator.enabled?).to eq(false) | ||
end | ||
end | ||
|
||
describe "#base_url" do | ||
it "returns default URL when setting is blank" do | ||
SiteSetting.discourse_login_client_url = "" | ||
expect(authenticator.base_url).to eq("https://logindemo.discourse.group") | ||
end | ||
|
||
it "returns configured URL when setting is present" do | ||
SiteSetting.discourse_login_client_url = "https://custom.example.com" | ||
expect(authenticator.base_url).to eq("https://custom.example.com") | ||
end | ||
end | ||
end | ||
|
||
describe "DiscourseLoginClientStrategy" do | ||
let(:strategy) { DiscourseLoginClientAuthenticator::DiscourseLoginClientStrategy.new({}) } | ||
|
||
it "uses 'discourse_login' name" do | ||
expect(strategy.options.name).to eq("discourse_login") | ||
end | ||
|
||
it "defines client_options" do | ||
client_options = strategy.options.client_options | ||
expect(client_options.authorize_url).to eq("/oauth/authorize") | ||
expect(client_options.token_url).to eq("/oauth/token") | ||
expect(client_options.auth_scheme).to eq(:basic_auth) | ||
end | ||
|
||
it "defines authorize_options" do | ||
expect(strategy.options.authorize_options).to include(:scope) | ||
end | ||
|
||
it "extracts uid from access_token" do | ||
access_token = | ||
instance_double("OAuth2::AccessToken", params: { "info" => { "uuid" => "12345" } }) | ||
allow(strategy).to receive(:access_token).and_return(access_token) | ||
expect(strategy.uid).to eq("12345") | ||
end | ||
|
||
it "extracts user info from access_token" do | ||
access_token = | ||
instance_double( | ||
"OAuth2::AccessToken", | ||
params: { | ||
"info" => { | ||
"username" => "test_user", | ||
"email" => "test@example.com", | ||
"image" => "http://example.com/avatar.png", | ||
}, | ||
}, | ||
) | ||
allow(strategy).to receive(:access_token).and_return(access_token) | ||
|
||
user_info = strategy.info | ||
|
||
expect(user_info[:username]).to eq("test_user") | ||
expect(user_info[:email]).to eq("test@example.com") | ||
expect(user_info[:image]).to eq("http://example.com/avatar.png") | ||
end | ||
|
||
it "defines callback_url" do | ||
expect(strategy.callback_url).to eq("http://test.localhost/auth/discourse_login/callback") | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a fallback mechanism for cases when the 'image' key is missing in access_token.params["info"] to avoid returning nil and potentially causing issues in downstream code.
Copilot uses AI. Check for mistakes.