1
0
Fork 0

Add action AsymmetricKeysController#index

This commit is contained in:
Alex Kotov 2019-09-14 06:29:13 +05:00
parent df8c5dc7ea
commit 9660c47381
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
12 changed files with 185 additions and 0 deletions

View 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

View file

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

View file

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

View file

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

View 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

View 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>

View 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>

View file

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

View file

@ -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: Сеть контактов

View file

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

View 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

View 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