mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-15 04:11:41 +00:00
Obtener listado de sitios
This commit is contained in:
parent
bdfd82c6c5
commit
7d7b265610
5 changed files with 31 additions and 3 deletions
|
@ -4,7 +4,11 @@ module Api
|
|||
module V1
|
||||
# API
|
||||
class BaseController < ActionController::Base
|
||||
http_basic_authenticate_with name: ENV['HTTP_BASIC_USER'],
|
||||
password: ENV['HTTP_BASIC_PASSWORD']
|
||||
|
||||
protect_from_forgery with: :null_session
|
||||
respond_to :json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,6 +4,10 @@ module Api
|
|||
module V1
|
||||
# API para sitios
|
||||
class SitesController < BaseController
|
||||
def index
|
||||
render json: Site.all.order(:name).pluck(:name)
|
||||
end
|
||||
|
||||
# Detecta si se puede generar un certificado
|
||||
def allowed
|
||||
name = params[:domain].gsub(/\.#{Site.domain}\Z/, '')
|
||||
|
|
11
config/initializers/hosts.rb
Normal file
11
config/initializers/hosts.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
Rails.application.configure do
|
||||
next if ENV['RAILS_ENV'] == 'test'
|
||||
|
||||
domain = ENV.fetch('SUTTY', 'sutty.nl')
|
||||
|
||||
config.hosts << domain
|
||||
config.hosts << "edit.#{domain}"
|
||||
config.hosts << "api.#{domain}"
|
||||
end
|
|
@ -15,6 +15,7 @@ Rails.application.routes.draw do
|
|||
scope module: 'api' do
|
||||
namespace :v1 do
|
||||
get 'sites/allowed', to: 'sites#allowed'
|
||||
resources :sites, only: %i[index]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,7 +12,8 @@ module Api
|
|||
|
||||
@authorization = {
|
||||
Authorization: ActionController::HttpAuthentication::Basic
|
||||
.encode_credentials(@usuarie.email, @usuarie.password)
|
||||
.encode_credentials(ENV['HTTP_BASIC_USER'],
|
||||
ENV['HTTP_BASIC_PASSWORD'])
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -21,12 +22,19 @@ module Api
|
|||
end
|
||||
|
||||
test 'se puede generar un certificado' do
|
||||
get v1_sites_allowed_url, params: { domain: @site.name }
|
||||
get v1_sites_allowed_url, headers: @authorization,
|
||||
params: { domain: @site.name }
|
||||
assert_response :ok
|
||||
|
||||
get v1_sites_allowed_url, params: { domain: SecureRandom.hex }
|
||||
get v1_sites_allowed_url, headers: @authorization,
|
||||
params: { domain: SecureRandom.hex }
|
||||
assert_response :not_found
|
||||
end
|
||||
|
||||
test 'se puede obtener un listado de todos' do
|
||||
get v1_sites_url, headers: @authorization, as: :json
|
||||
assert_equal [@site.name], JSON.parse(response.body)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue