Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion GemFile
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ gem 'pagy', '~> 6.0'

gem 'devise'

gem 'cancancan'
gem 'cancancan'

gem 'rswag'
17 changes: 17 additions & 0 deletions GemFile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.3)
json-schema (3.0.0)
addressable (>= 2.8)
loofah (2.19.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -211,6 +213,20 @@ GEM
rspec-mocks (~> 3.11)
rspec-support (~> 3.11)
rspec-support (3.12.0)
rswag (2.8.0)
rswag-api (= 2.8.0)
rswag-specs (= 2.8.0)
rswag-ui (= 2.8.0)
rswag-api (2.8.0)
railties (>= 3.1, < 7.1)
rswag-specs (2.8.0)
activesupport (>= 3.1, < 7.1)
json-schema (>= 2.2, < 4.0)
railties (>= 3.1, < 7.1)
rspec-core (>= 2.14)
rswag-ui (2.8.0)
actionpack (>= 3.1, < 7.1)
railties (>= 3.1, < 7.1)
rubocop (1.43.0)
json (~> 2.3)
parallel (~> 1.10)
Expand Down Expand Up @@ -286,6 +302,7 @@ DEPENDENCIES
rails (~> 7.0.4)
rails-controller-testing
rspec-rails
rswag
rubocop (>= 1.0, < 2.0)
selenium-webdriver
sprockets-rails
Expand Down
14 changes: 14 additions & 0 deletions config/initializers/rswag_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Rswag::Api.configure do |c|

# Specify a root folder where Swagger JSON files are located
# This is used by the Swagger middleware to serve requests for API descriptions
# NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
# that it's configured to generate files in the same folder
c.swagger_root = Rails.root.to_s + '/swagger'

# Inject a lambda function to alter the returned Swagger prior to serialization
# The function will have access to the rack env for the current request
# For example, you could leverage this to dynamically assign the "host" property
#
#c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
end
16 changes: 16 additions & 0 deletions config/initializers/rswag_ui.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Rswag::Ui.configure do |c|

# List the Swagger endpoints that you want to be documented through the
# swagger-ui. The first parameter is the path (absolute or relative to the UI
# host) to the corresponding endpoint and the second is a title that will be
# displayed in the document selector.
# NOTE: If you're using rspec-api to expose Swagger files
# (under swagger_root) as JSON or YAML endpoints, then the list below should
# correspond to the relative paths for those endpoints.

c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'

# Add Basic Auth in case your API is private
# c.basic_auth_enabled = true
# c.basic_auth_credentials 'username', 'password'
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
#devise root
devise_for :users
devise_scope :user do
Expand Down
163 changes: 163 additions & 0 deletions spec/requests/api/v1/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
require 'swagger_helper'

RSpec.describe 'api/v1/comments', type: :request do

path '/api/v1/users/{user_id}/posts/{post_id}/comments' do
# You'll want to customize the parameter types...
parameter name: 'user_id', in: :path, type: :string, description: 'user_id'
parameter name: 'post_id', in: :path, type: :string, description: 'post_id'

get('list comments') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

post('create comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end

path '/api/v1/users/{user_id}/posts/{post_id}/comments/new' do
# You'll want to customize the parameter types...
parameter name: 'user_id', in: :path, type: :string, description: 'user_id'
parameter name: 'post_id', in: :path, type: :string, description: 'post_id'

get('new comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end

path '/api/v1/users/{user_id}/posts/{post_id}/comments/{id}/edit' do
# You'll want to customize the parameter types...
parameter name: 'user_id', in: :path, type: :string, description: 'user_id'
parameter name: 'post_id', in: :path, type: :string, description: 'post_id'
parameter name: 'id', in: :path, type: :string, description: 'id'

get('edit comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end

path '/api/v1/users/{user_id}/posts/{post_id}/comments/{id}' do
# You'll want to customize the parameter types...
parameter name: 'user_id', in: :path, type: :string, description: 'user_id'
parameter name: 'post_id', in: :path, type: :string, description: 'post_id'
parameter name: 'id', in: :path, type: :string, description: 'id'

get('show comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

patch('update comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

put('update comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end

delete('delete comment') do
response(200, 'successful') do
let(:user_id) { '123' }
let(:post_id) { '123' }
let(:id) { '123' }

after do |example|
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end
end
end
end
Loading