From 6548d4d01624708ad53269088774dcc016c81deb Mon Sep 17 00:00:00 2001 From: eliasjpr Date: Wed, 9 Oct 2024 20:42:47 -0400 Subject: [PATCH] Refactor testing setup for cleaner configuration 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. --- spec/server.cr => server.cr | 3 ++- spec/handlers_spec.cr | 14 +++++++------- spec/settings.cr | 8 ++++++++ spec/spec_helper.cr | 21 ++++++++++++++------- src/authly/handler.cr | 17 ++++++++--------- src/authly/response_type.cr | 1 - 6 files changed, 39 insertions(+), 25 deletions(-) rename spec/server.cr => server.cr (72%) create mode 100644 spec/settings.cr diff --git a/spec/server.cr b/server.cr similarity index 72% rename from spec/server.cr rename to server.cr index f2a9890..b88fb2a 100644 --- a/spec/server.cr +++ b/server.cr @@ -1,5 +1,6 @@ require "http/server" -require "./spec_helper" +require "./src/authly" +require "./spec/settings" server = HTTP::Server.new([ Authly::OAuthHandler.new, diff --git a/spec/handlers_spec.cr b/spec/handlers_spec.cr index a2ffab6..3ab1c6e 100644 --- a/spec/handlers_spec.cr +++ b/spec/handlers_spec.cr @@ -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 @@ -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 @@ -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 diff --git a/spec/settings.cr b/spec/settings.cr new file mode 100644 index 0000000..0538ec6 --- /dev/null +++ b/spec/settings.cr @@ -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") diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index ba85f93..873ffac 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -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 diff --git a/src/authly/handler.cr b/src/authly/handler.cr index ff805ae..b7b1148 100644 --- a/src/authly/handler.cr +++ b/src/authly/handler.cr @@ -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) @@ -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 diff --git a/src/authly/response_type.cr b/src/authly/response_type.cr index 2457aad..c501688 100644 --- a/src/authly/response_type.cr +++ b/src/authly/response_type.cr @@ -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)