Commit b8de68a4 by liyijie

SimpleController supports rails api authentication

parent 70b1be3a
......@@ -6,19 +6,26 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
class_option :view, type: :string, desc: "View files generate folder"
class_option :model, type: :string, desc: "Model name for extract attributes"
class_option :auth, type: :string, desc: "Authentication model name"
class_option 'auth-only', type: :boolean, desc: "Only generate authentication"
class_option 'no-swagger', type: :boolean, desc: "Do not generate swagger spec file"
def setup
return if options["auth-only"]
@routes = RSpec::Rails::Swagger::RouteParser.new(controller_path.sub(/^\//, '')).routes
p "Warning!! Resource is not exist, CHECK & regenerate after you have configurate the model and routes already" if resource_class&.columns_hash.blank?
end
def create_controller_files
if options["auth-only"]
template_file = "controllers/auth_controller.rb"
else
template_file = "controllers/controller.rb"
end
template template_file, File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
end
def copy_view_files
return if options["auth-only"]
%w(index show _single _simple _detail).each do |view|
filename = filename_with_extensions(view)
template "views/#{filename}", File.join('app/views', view_path, filename)
......@@ -27,7 +34,11 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
def create_swagger_files
return if options["no-swagger"]
if options["auth-only"]
template_file = "specs/auth_spec.rb"
else
template_file = "specs/spec.rb"
end
template template_file, File.join("spec/requests", controller_class_path, "#{controller_file_name}_spec.rb")
end
......
class <%= controller_class_name %>Controller < ApplicationController
<% if auth.present? %>
acts_as_auth_session :<%= auth.downcase %>
<% end -%>
end
require 'swagger_helper'
RSpec.describe '<%= controller_path %>', type: :request, capture_examples: true, tags: ["<%= controller_class_path.join(' ') %>"] do
before :each do
<% if auth.present? -%>
@<%= auth_singular %> = <%= auth %>.register "auth", "password"
@auth_token = <%= auth %>.login "auth", "password"
<% end -%>
end
path '/auth/<%= auth_singular %>/session' do
delete(summary: 'logout') do
produces 'application/json'
consumes 'application/json'
parameter '<%= auth %>-Token', in: :header, type: :string
let('<%= auth %>-Token') { @auth_token.token }
response(200, description: 'successful') do
it {
body = JSON.parse(response.body)
expect(body['message']).to eq('logout successful')
}
end
end
post(summary: 'login') do
produces 'application/json'
consumes 'application/json'
parameter :<%= auth_singular %>, in: :body, schema: {
type: :object, properties: {
<%= auth_singular %>: {
type: :object, properties: {
name: { type: :string },
password: { type: :string },
}
}
}
}
response(200, description: 'successful') do
let(:<%= auth_singular %>) {
{
<%= auth_singular %>: {
name: "auth",
password: "password",
}
}
}
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment