Add action AsymmetricKeysController#index
This commit is contained in:
parent
df8c5dc7ea
commit
9660c47381
12 changed files with 185 additions and 0 deletions
9
app/controllers/asymmetric_keys_controller.rb
Normal file
9
app/controllers/asymmetric_keys_controller.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AsymmetricKeysController < ApplicationController
|
||||
# GET /public_keys
|
||||
def index
|
||||
authorize AsymmetricKey
|
||||
@asymmetric_keys = policy_scope(AsymmetricKey).page(params[:page])
|
||||
end
|
||||
end
|
|
@ -44,6 +44,14 @@ class AsymmetricKey < ApplicationRecord
|
|||
# Methods #
|
||||
###########
|
||||
|
||||
def algo_class
|
||||
raise NotImplementedError, "#{self.class}#algo_class"
|
||||
end
|
||||
|
||||
def algo_variant
|
||||
raise NotImplementedError, "#{self.class}#algo_variant"
|
||||
end
|
||||
|
||||
def encrypt_private_key_pem
|
||||
cipher = OpenSSL::Cipher::AES256.new
|
||||
cipher.encrypt
|
||||
|
|
|
@ -10,4 +10,16 @@ class EcurveKey < AsymmetricKey
|
|||
validates :curve, inclusion: { in: CURVES }
|
||||
|
||||
validates :bits, absence: true
|
||||
|
||||
###########
|
||||
# Methods #
|
||||
###########
|
||||
|
||||
def algo_class
|
||||
'Elliptic curve'
|
||||
end
|
||||
|
||||
def algo_variant
|
||||
curve
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,4 +10,16 @@ class RSAKey < AsymmetricKey
|
|||
validates :bits, inclusion: { in: BITS }
|
||||
|
||||
validates :curve, absence: true
|
||||
|
||||
###########
|
||||
# Methods #
|
||||
###########
|
||||
|
||||
def algo_class
|
||||
'RSA'
|
||||
end
|
||||
|
||||
def algo_variant
|
||||
"#{bits} bits"
|
||||
end
|
||||
end
|
||||
|
|
13
app/policies/asymmetric_key_policy.rb
Normal file
13
app/policies/asymmetric_key_policy.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AsymmetricKeyPolicy < ApplicationPolicy
|
||||
def index?
|
||||
true
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
scope.all
|
||||
end
|
||||
end
|
||||
end
|
25
app/views/asymmetric_keys/_table.html.erb
Normal file
25
app/views/asymmetric_keys/_table.html.erb
Normal file
|
@ -0,0 +1,25 @@
|
|||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
<%= AsymmetricKey.human_attribute_name :id %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= AsymmetricKey.human_attribute_name :algo_class %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= AsymmetricKey.human_attribute_name :algo_variant %>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% asymmetric_keys.each do |asymmetric_key| %>
|
||||
<tr>
|
||||
<td scope="row"><%= asymmetric_key.id %></td>
|
||||
<td><%= asymmetric_key.algo_class %></td>
|
||||
<td><%= asymmetric_key.algo_variant %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
7
app/views/asymmetric_keys/index.html.erb
Normal file
7
app/views/asymmetric_keys/index.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
|||
<div class="container">
|
||||
<%= nav_breadcrumb AsymmetricKey.model_name.human count: 0 %>
|
||||
|
||||
<%= render partial: 'table',
|
||||
locals: { asymmetric_keys: @asymmetric_keys } %>
|
||||
<%= pagination @asymmetric_keys %>
|
||||
</div>
|
|
@ -4,6 +4,10 @@ en:
|
|||
account:
|
||||
one: Account
|
||||
many: Accounts
|
||||
asymmetric_key:
|
||||
one: Public key
|
||||
few: Public keys
|
||||
many: Public keys
|
||||
contact:
|
||||
one: Contact
|
||||
few: Contacts
|
||||
|
@ -49,6 +53,11 @@ en:
|
|||
avatar: Avatar
|
||||
person: Person
|
||||
timezone: Timezone
|
||||
asymmetric_key:
|
||||
id: ID
|
||||
algo_class: Algorithm class
|
||||
algo_variant: Variant
|
||||
sha256: SHA-256 fingerprint
|
||||
contact:
|
||||
id: ID
|
||||
contact_network: Contact network
|
||||
|
|
|
@ -4,6 +4,10 @@ ru:
|
|||
account:
|
||||
one: Аккаунт
|
||||
many: Аккаунты
|
||||
asymmetric_key:
|
||||
one: Публичный ключ
|
||||
few: Публичных ключа
|
||||
many: Публичные ключи
|
||||
contact:
|
||||
one: Контакт
|
||||
few: Контакта
|
||||
|
@ -49,6 +53,11 @@ ru:
|
|||
avatar: Аватар
|
||||
person: Человек
|
||||
timezone: Часовой пояс
|
||||
asymmetric_key:
|
||||
id: ID
|
||||
algo_class: Класс алгоритма
|
||||
algo_variant: Вариант
|
||||
sha256: Отпечаток SHA-256
|
||||
contact:
|
||||
id: ID
|
||||
contact_network: Сеть контактов
|
||||
|
|
|
@ -13,6 +13,11 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :federal_subjects, param: :number, only: %i[index show]
|
||||
|
||||
resources :public_keys,
|
||||
as: :asymmetric_key,
|
||||
controller: 'asymmetric_keys',
|
||||
only: :index
|
||||
|
||||
resources :private_keys, only: :show
|
||||
|
||||
###############
|
||||
|
|
7
spec/policies/asymmetric_key_policy_spec.rb
Normal file
7
spec/policies/asymmetric_key_policy_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AsymmetricKeyPolicy do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
69
spec/requests/asymmetric_keys/index_spec.rb
Normal file
69
spec/requests/asymmetric_keys/index_spec.rb
Normal file
|
@ -0,0 +1,69 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /public_keys' do
|
||||
let(:current_account) { create :superuser_account }
|
||||
|
||||
let :asymmetric_keys_count do
|
||||
[0, 1, rand(2..4), rand(5..10), rand(20..40)].sample
|
||||
end
|
||||
|
||||
let(:rsa_keys_count) { rand(1...asymmetric_keys_count) || 0 }
|
||||
let(:ecurve_keys_count) { asymmetric_keys_count - rsa_keys_count }
|
||||
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
|
||||
create_list :rsa_key, rsa_keys_count
|
||||
create_list :ecurve_key, ecurve_keys_count
|
||||
|
||||
get '/public_keys'
|
||||
end
|
||||
|
||||
for_account_types nil, :usual, :superuser do
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are no asymmetric keys' do
|
||||
let(:asymmetric_keys_count) { 0 }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there is one asymmetric key' do
|
||||
let(:asymmetric_keys_count) { 1 }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are few asymmetric keys' do
|
||||
let(:asymmetric_keys_count) { rand 2..4 }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are many asymmetric keys' do
|
||||
let(:asymmetric_keys_count) { rand 5..10 }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are lot of asymmetric keys' do
|
||||
let(:asymmetric_keys_count) { rand 20..40 }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue