Skip to content

Commit

Permalink
Refactor testing setup for cleaner configuration
Browse files Browse the repository at this point in the history
Renamed and moved server file outside specs for better separation. Introduced a dedicated settings file to manage test configurations, enhancing modularity. Cleaned up whitespace and formatting inconsistencies across handlers to improve readability. Introduced server process management in tests for better isolation and test reliability.
  • Loading branch information
eliasjpr committed Oct 10, 2024
1 parent 9778fa9 commit 6548d4d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
3 changes: 2 additions & 1 deletion spec/server.cr → server.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "http/server"
require "./spec_helper"
require "./src/authly"
require "./spec/settings"

server = HTTP::Server.new([
Authly::OAuthHandler.new,
Expand Down
14 changes: 7 additions & 7 deletions spec/handlers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Authly
it "returns authorization code with valid client_id and redirect_uri" do
response = HTTP::Client.get("http://127.0.0.1:8080/oauth/authorize?client_id=1&redirect_uri=https://www.example.com/callback&response_type=code")
body = response.body
body
body

response.status_code.should eq 302

Expand All @@ -33,12 +33,12 @@ module Authly
})
response.status_code.should eq 200
body = JSON.parse(response.body)
body["access_token"]
body["access_token"]
body["access_token"].should_not be_nil
end

it "returns 400 for unsupported grant type" do
response = HTTP::Client.post("http://127.0.0.1:8080/oauth/token", form: {"grant_type" => "invalid_grant"})
response = HTTP::Client.post("http://127.0.0.1:8080/oauth/token", form: {"grant_type" => "invalid_grant"})
response.status_code.should eq 400
response.body.should eq "Invalid or unknown grant type"
end
Expand All @@ -56,10 +56,10 @@ module Authly
body = JSON.parse(response.body)
body.should eq({
"active" => true,
"scope" => token.scope,
"cid" => token.client_id,
"exp" => token.expires_in,
"sub" => token.sub
"scope" => token.scope,
"cid" => token.client_id,
"exp" => token.expires_in,
"sub" => token.sub,
})
end

Expand Down
8 changes: 8 additions & 0 deletions spec/settings.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
secret_key = "4bce37fbb1542a68dddba2da22635beca9d814cb3424c461fcc8876904ad39c1"
Authly.configure do |config|
config.secret_key = secret_key
config.public_key = secret_key
end

Authly.clients << Authly::Client.new("example", "secret", "https://www.example.com/callback", "1")
Authly.owners << Authly::Owner.new("username", "password")
21 changes: 14 additions & 7 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ require "digest"
require "base64"
require "faker"
require "../src/authly"
require "./settings"

# Configure
secret_key = "4bce37fbb1542a68dddba2da22635beca9d814cb3424c461fcc8876904ad39c1"
Authly.configure do |config|
config.secret_key = secret_key
config.public_key = secret_key
process = nil
Spec.before_suite do
# Start test server
process = Process.new("bin/test_server", output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
# Wait for process to start
sleep 1.seconds
end

Authly.clients << Authly::Client.new("example", "secret", "https://www.example.com/callback", "1")
Authly.owners << Authly::Owner.new("username", "password")
Spec.after_suite do
# Stop test server
if pro = process
pro.terminate
sleep 1.seconds
end
end
17 changes: 8 additions & 9 deletions src/authly/handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ module Authly
introspection_result = Authly.introspect(token)
ResponseHelper.write(context, 200, "application/json", introspection_result.to_json)
else

end
rescue e : Error
ResponseHelper.write(context, e.code, "text/plain", e.message)
Expand All @@ -90,15 +89,15 @@ module Authly

class RevokeHandler
def self.handle(context)
unless context.request.method == "POST"
ResponseHelper.write(context, 405, "text/plain", "Method not allowed")
end
unless context.request.method == "POST"
ResponseHelper.write(context, 405, "text/plain", "Method not allowed")
end

# Extracting request parameters
params = context.request.form_params
token = params["token"]
Authly.revoke(token)
ResponseHelper.write(context, 200, "text/plain", "Token revoked successfully")
# Extracting request parameters
params = context.request.form_params
token = params["token"]
Authly.revoke(token)
ResponseHelper.write(context, 200, "text/plain", "Token revoked successfully")
rescue e : Error
ResponseHelper.write(context, e.code, "text/plain", e.message)
rescue e : KeyError
Expand Down
1 change: 0 additions & 1 deletion src/authly/response_type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module Authly
end

def decode

raise Error.invalid_redirect_uri if redirect_uri.empty?
raise Error.unauthorized_client unless authorize_client(client_id, redirect_uri)

Expand Down

0 comments on commit 6548d4d

Please sign in to comment.