API para sitios permitidos

This commit is contained in:
f 2019-09-05 15:56:24 -03:00
parent e69a1aceb1
commit bdfd82c6c5
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
4 changed files with 70 additions and 0 deletions

View 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

View 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

View file

@ -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'

View 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