Skip to content

Commit

Permalink
Add committee gem and test swagger definition
Browse files Browse the repository at this point in the history
  • Loading branch information
renatolond committed Nov 26, 2024
1 parent d339476 commit 658259d
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ group :test do

gem "database_cleaner-sequel" # Cleans the database between tests

gem "committee", "~> 5.4" # Used to test the swagger documentation in the tests

gem "factory_bot" # makes it easier to create objects for tests
gem "faker" # provides fake data for tests
gem "mocha" # adds mocking capabilities
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ GEM
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.8)
committee (5.5.0)
json_schema (~> 0.14, >= 0.14.3)
openapi_parser (~> 2.0)
rack (>= 1.5, < 3.2)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
console (1.29.0)
Expand Down Expand Up @@ -145,6 +149,7 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.8.2)
json_schema (0.21.0)
jwt (2.9.3)
base64
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -175,6 +180,7 @@ GEM
octokit (7.2.0)
faraday (>= 1, < 3)
sawyer (~> 0.9)
openapi_parser (2.2.2)
openssl (3.2.0)
parallel (1.26.3)
parser (3.3.6.0)
Expand Down Expand Up @@ -297,6 +303,7 @@ DEPENDENCIES
address_composer!
async-http
bcrypt
committee (~> 5.4)
database_cleaner-sequel
debug
dotenv
Expand Down
7 changes: 3 additions & 4 deletions app/api/authenticated/conversations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ def filter_conversation_for_current_profile!(conversation)
present conversation, with: Entities::Conversation
end

params do
requires :conversation_id, type: String, documentation: { format: :uuid }, regexp: Utils::UUID7_RE
end
namespace ":conversation_id" do
params do
requires :conversation_id, type: String, regexp: Utils::UUID7_RE
end

desc "Returns a single conversation for the logged-in user",
success: { model: Entities::Conversation, message: "A conversation" },
failure: Authenticated::FAILURES,
Expand Down
6 changes: 4 additions & 2 deletions app/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Base < Grape::API
error!({ error: "INTERNAL_SERVER_ERROR", with: Entities::Error }, 500)
end

if Environment.development?
if Environment.development? || Environment.test?
add_swagger_documentation \
mount_path: "/swagger_doc",
doc_version: RetroMeet::Version.to_s,
Expand All @@ -45,7 +45,9 @@ class Base < Grape::API
authorizationUrl: "/login"
}
},
security: { jwt_token: [] }
security: { jwt_token: [] },
consumes: ["application/json"],
produces: ["application/json"]
end

route :any, "*path" do
Expand Down
4 changes: 2 additions & 2 deletions app/api/entities/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Conversation < Grape::Entity
expose :id, documentation: { type: String }
expose :other_profile, using: API::Entities::OtherProfileInfo
expose :created_at, format_with: :iso_timestamp, documentation: { type: DateTime }
expose :last_seen_at, format_with: :iso_timestamp, documentation: { type: [NilClass, DateTime] }
expose :new_messages, as: :new_messages_preview, documentation: { type: String }
expose :last_seen_at, format_with: :iso_timestamp, documentation: { type: [DateTime, NilClass] }
expose :new_messages, as: :new_messages_preview, documentation: { type: String }, expose_nil: false
end
end
end
2 changes: 1 addition & 1 deletion app/api/entities/conversations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Entities
# Represents a collection of conversations
class Conversations < Grape::Entity
present_collection true
expose :items, as: "conversations", using: API::Entities::Conversation
expose :items, as: "conversations", using: API::Entities::Conversation, documentation: { is_array: true }
end
end
end
2 changes: 1 addition & 1 deletion app/api/entities/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Entities
# Represents a collection of conversations
class Messages < Grape::Entity
present_collection true
expose :items, as: "messages", using: API::Entities::Message
expose :items, as: "messages", using: API::Entities::Message, documentation: { is_array: true }
end
end
end
2 changes: 1 addition & 1 deletion app/api/entities/other_profile_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class OtherProfileInfo < Grape::Entity
expose :religion, documentation: { type: String }
expose :religion_importance, documentation: { type: String }
expose :location_display_name, documentation: { type: Hash }
expose :location_distance, format_with: :distance_in_km, documentation: { type: Float }
expose :location_distance, format_with: :distance_in_km, documentation: { type: Float }, expose_nil: false
expose :birth_date, format_with: :age_formatter, as: :age, documentation: { type: Integer }
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/api/entities/other_profile_infos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Entities
# represents a collection of other users' profiles
class OtherProfileInfos < Grape::Entity
present_collection true
expose :items, as: "profiles", using: API::Entities::OtherProfileInfo
expose :items, as: "profiles", using: API::Entities::OtherProfileInfo, documentation: { is_array: true }
end
end
end
2 changes: 1 addition & 1 deletion app/api/rodauth_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RodauthMiddleware < Roda
end

route do |r|
unless Environment.development? && r.path == "/api/swagger_doc"
unless (Environment.development? || Environment.test?) && r.path == "/api/swagger_doc"
r.rodauth
rodauth.require_authentication
rodauth.check_active_session
Expand Down
19 changes: 11 additions & 8 deletions test/app/api/authenticated/conversations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "../../../test_helper"

describe API::Authenticated::Conversations do
include RackHelper
include SwaggerHelper::TestMethods
before(:all) do
@login = "[email protected]"
@password = "bogus123"
Expand All @@ -20,7 +20,7 @@

describe "get /conversations" do
before do
@endpoint = "/api/conversations/"
@endpoint = "/api/conversations"
@auth = login(login: @login, password: @password)
end
it "gets all conversations" do
Expand All @@ -31,7 +31,6 @@
id: @conversation.id,
created_at: @conversation.created_at.iso8601,
last_seen_at: @conversation.profile1_last_seen_at.iso8601,
new_messages_preview: nil,
other_profile: {
id: other_profile.id,
display_name: other_profile.display_name,
Expand All @@ -52,7 +51,6 @@
religion: other_profile.religion,
religion_importance: other_profile.religion_importance,
location_display_name: other_profile.location.display_name.transform_keys(&:to_sym),
location_distance: nil, # TODO: I think this should display the distance to the logged in user
age: 39 # TODO: calculate this so that this test don't breaks when the profile ages
}
}
Expand All @@ -61,13 +59,14 @@
authorized_get @auth, format(@endpoint)

assert_predicate last_response, :ok?
assert_schema_conform(200)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end

describe "get /conversations/:id" do
before do
@endpoint = "/api/conversations/%<id>s/"
@endpoint = "/api/conversations/%<id>s"
@auth = login(login: @login, password: @password)
end
it "gets a 400 if the id is not valid" do
Expand All @@ -81,6 +80,7 @@
authorized_get @auth, format(@endpoint, id: "boo!")

assert_predicate last_response, :bad_request?
assert_response_schema_confirm(400)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
it "gets the single conversation" do
Expand All @@ -89,7 +89,6 @@
id: @conversation.id,
created_at: @conversation.created_at.iso8601,
last_seen_at: @conversation.profile1_last_seen_at.iso8601,
new_messages_preview: nil,
other_profile: {
id: other_profile.id,
display_name: other_profile.display_name,
Expand All @@ -110,13 +109,13 @@
religion: other_profile.religion,
religion_importance: other_profile.religion_importance,
location_display_name: other_profile.location.display_name.transform_keys(&:to_sym),
location_distance: nil, # TODO: I think this should display the distance to the logged in user
age: 39 # TODO: calculate this so that this test don't breaks when the profile ages
}
}
authorized_get @auth, format(@endpoint, id: @conversation.id)

assert_predicate last_response, :ok?
assert_schema_conform(200)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
it "gets the single conversation with an unseen message" do
Expand Down Expand Up @@ -148,13 +147,13 @@
religion: other_profile.religion,
religion_importance: other_profile.religion_importance,
location_display_name: other_profile.location.display_name.transform_keys(&:to_sym),
location_distance: nil, # TODO: I think this should display the distance to the logged in user
age: 39 # TODO: calculate this so that this test don't breaks when the profile ages
}
}
authorized_get @auth, format(@endpoint, id: @conversation.id)

assert_predicate last_response, :ok?
assert_schema_conform(200)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end
Expand All @@ -168,6 +167,7 @@
authorized_put @auth, format(@endpoint, id: @conversation.id)

assert_predicate last_response, :no_content?
assert_schema_conform(204)
@conversation.reload

assert_operator last_seen_at, :<, @conversation.profile1_last_seen_at
Expand All @@ -191,6 +191,7 @@
authorized_get @auth, "#{format(@endpoint, id: @conversation.id)}?min_id=a&max_id=b"

assert_predicate last_response, :bad_request?
assert_response_schema_confirm(400)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end

Expand All @@ -205,6 +206,7 @@
authorized_get @auth, format(@endpoint, id: @conversation.id)

assert_predicate last_response, :ok?
assert_schema_conform(200)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end
Expand All @@ -229,6 +231,7 @@
expected_response[:sent_at] = @message.sent_at.iso8601

assert_predicate last_response, :created?
assert_schema_conform(201)
assert_equal expected_response, JSON.parse(last_response.body, symbolize_names: true)
end
end
Expand Down
6 changes: 5 additions & 1 deletion test/app/api/authenticated/listing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "../../../test_helper"

describe API::Authenticated::Profile do
include RackHelper
include SwaggerHelper::TestMethods
before(:all) do
# Create a few locations to create accounts around
@schaerbeek = create(:location, latitude: 50.8676041, longitude: 4.3737121, language: "en", name: "Schaerbeek - Schaarbeek, Brussels-Capital, Belgium", country_code: "be", osm_id: 58_260)
Expand Down Expand Up @@ -73,6 +73,7 @@
authorized_get @auth, @endpoint

assert_predicate last_response, :ok?
assert_schema_conform(200)
parsed_response = JSON.parse(last_response.body, symbolize_names: true)

assert_equal 1, parsed_response[:profiles].size
Expand All @@ -84,6 +85,7 @@
authorized_get @auth, @endpoint, { max_distance: 200 }

assert_predicate last_response, :ok?
assert_schema_conform(200)
parsed_response = JSON.parse(last_response.body, symbolize_names: true)

assert_equal 3, parsed_response[:profiles].size
Expand All @@ -95,6 +97,7 @@
authorized_get @auth, @endpoint, { max_distance: 400 }

assert_predicate last_response, :ok?
assert_schema_conform(200)
parsed_response = JSON.parse(last_response.body, symbolize_names: true)

assert_equal 4, parsed_response[:profiles].size
Expand All @@ -105,6 +108,7 @@
authorized_get @auth, @endpoint, { max_distance: 401 }

assert_predicate last_response, :bad_request?
assert_response_schema_confirm(400)

expected_response = {
error: "VALIDATION_ERROR",
Expand Down
Loading

0 comments on commit 658259d

Please sign in to comment.