mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-15 01:41:42 +00:00
API para sitios permitidos
This commit is contained in:
parent
e69a1aceb1
commit
bdfd82c6c5
4 changed files with 70 additions and 0 deletions
10
app/controllers/api/v1/base_controller.rb
Normal file
10
app/controllers/api/v1/base_controller.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
# API
|
||||
class BaseController < ActionController::Base
|
||||
protect_from_forgery with: :null_session
|
||||
end
|
||||
end
|
||||
end
|
20
app/controllers/api/v1/sites_controller.rb
Normal file
20
app/controllers/api/v1/sites_controller.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
# API para sitios
|
||||
class SitesController < BaseController
|
||||
# Detecta si se puede generar un certificado
|
||||
def allowed
|
||||
name = params[:domain].gsub(/\.#{Site.domain}\Z/, '')
|
||||
site = Site.find_by(name: name)
|
||||
|
||||
if site
|
||||
head :ok
|
||||
else
|
||||
head :not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,6 +11,14 @@ Rails.application.routes.draw do
|
|||
# como un objeto válido
|
||||
resources :invitadxs, only: [:create]
|
||||
|
||||
constraints subdomain: 'api' do
|
||||
scope module: 'api' do
|
||||
namespace :v1 do
|
||||
get 'sites/allowed', to: 'sites#allowed'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
resources :sites, constraints: { site_id: %r{[^/]+}, id: %r{[^/]+} } do
|
||||
get 'public/:type/:basename', to: 'sites#send_public_file'
|
||||
|
||||
|
|
32
test/controllers/api/v1/sites_controller_test.rb
Normal file
32
test/controllers/api/v1/sites_controller_test.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class SitesControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@rol = create :rol
|
||||
@site = @rol.site
|
||||
@usuarie = @rol.usuarie
|
||||
|
||||
@authorization = {
|
||||
Authorization: ActionController::HttpAuthentication::Basic
|
||||
.encode_credentials(@usuarie.email, @usuarie.password)
|
||||
}
|
||||
end
|
||||
|
||||
teardown do
|
||||
@site.destroy
|
||||
end
|
||||
|
||||
test 'se puede generar un certificado' do
|
||||
get v1_sites_allowed_url, params: { domain: @site.name }
|
||||
assert_response :ok
|
||||
|
||||
get v1_sites_allowed_url, params: { domain: SecureRandom.hex }
|
||||
assert_response :not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue