Skip to content

Commit

Permalink
Fix tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Nov 18, 2024
1 parent eae3d8c commit aa64482
Showing 3 changed files with 59 additions and 26 deletions.
19 changes: 12 additions & 7 deletions lib/discourse_activity_pub/auth.rb
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@ def initialize(domain: nil)
end

def verify_client
@client = create_client if !client
auth_error("failed_to_verify_client") unless check_client
create_client if !client
auth_error("failed_to_verify_client") unless client && check_client
end

def auth_type
@@ -28,11 +28,12 @@ def client
def create_client
credentials = register_client
return nil unless credentials
DiscourseActivityPubClient.create!(
auth_type: auth_type,
domain: domain,
credentials: credentials,
)
@client =
DiscourseActivityPubClient.create!(
auth_type: auth_type,
domain: domain,
credentials: credentials,
)
end

def authorization
@@ -43,6 +44,10 @@ def success?
errors.blank?
end

def check_client
raise NotImplementedError
end

def register_client
raise NotImplementedError
end
11 changes: 11 additions & 0 deletions lib/discourse_activity_pub/auth/mastodon.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ class Mastodon < Auth
REDIRECT_PATH = "ap/auth/redirect/mastodon"
AUTHORIZE_PATH = "oauth/authorize"
APP_PATH = "api/v1/apps"
APP_CHECK_PATH = "api/v1/apps/verify_credentials"
TOKEN_PATH = "oauth/token"
ACCOUNT_PATH = "api/v1/accounts/verify_credentials"
SCOPES = "read:accounts"
@@ -14,6 +15,16 @@ def name
"mastodon"
end

def check_client
request(
APP_CHECK_PATH,
verb: :get,
headers: {
"Authorization" => "Bearer #{client.credentials["client_secret"]}",
},
)
end

def register_client
response =
request(
Original file line number Diff line number Diff line change
@@ -100,25 +100,34 @@
)
end

it "returns the domain" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
expect(response.status).to eq(200)
expect(response.parsed_body["domain"]).to eq(external_domain1)
end
context "when the domain verifies the app" do
before do
stub_request(
:get,
"https://#{external_domain1}/#{DiscourseActivityPub::Auth::Mastodon::APP_CHECK_PATH}",
).to_return(status: 200)
end

it "sets the domain as the verified domain in the session" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
expect(read_secure_session[described_class::DOMAIN_SESSION_KEY]).to eq(external_domain1)
end
it "returns the domain" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
expect(response.status).to eq(200)
expect(response.parsed_body["domain"]).to eq(external_domain1)
end

it "creates a client" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
client =
DiscourseActivityPubClient.find_by(
domain: external_domain1,
auth_type: DiscourseActivityPubClient.auth_types[:mastodon],
)
expect(client.credentials["client_id"]).to eq(mastodon_client_id)
it "sets the domain as the verified domain in the session" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
expect(read_secure_session[described_class::DOMAIN_SESSION_KEY]).to eq(external_domain1)
end

it "creates a client" do
post "/ap/auth/verify", params: { domain: external_domain1, auth_type: "mastodon" }
client =
DiscourseActivityPubClient.find_by(
domain: external_domain1,
auth_type: DiscourseActivityPubClient.auth_types[:mastodon],
)
expect(client.credentials["client_id"]).to eq(mastodon_client_id)
end
end
end

@@ -128,13 +137,17 @@
:post,
"https://#{external_domain2}/#{DiscourseActivityPub::Auth::Mastodon::APP_PATH}",
).to_return(status: 400)
stub_request(
:get,
"https://#{external_domain2}/#{DiscourseActivityPub::Auth::Mastodon::APP_CHECK_PATH}",
).to_return(status: 400)
end

it "returns an error" do
post "/ap/auth/verify", params: { domain: external_domain2, auth_type: "mastodon" }
expect(response.status).to eq(422)
expect(response.parsed_body["errors"].first).to eq(
I18n.t("discourse_activity_pub.auth.error.failed_to_register_client"),
I18n.t("discourse_activity_pub.auth.error.failed_to_verify_client"),
)
end

@@ -156,10 +169,14 @@

context "with a successful client registration" do
before do
stub_request(
:head,
"https://#{external_domain1}/#{DiscourseActivityPub::Auth::Discourse::CLIENT_PATH}?client_id=#{DiscourseActivityPubActor.application.ap_id}",
)
stub_request(
:post,
"https://#{external_domain1}/#{DiscourseActivityPub::Auth::Discourse::CLIENT_PATH}",
).to_return(status: 200)
).to_return(body: { "success" => "OK" }.to_json, status: 200)
end

it "returns the domain" do

0 comments on commit aa64482

Please sign in to comment.