Add actions TelegramChatsController#index, #show
This commit is contained in:
parent
88e5dd91b7
commit
b12bbf51dd
|
@ -0,0 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TelegramChatsController < ApplicationController
|
||||
before_action :set_telegram_chat, except: :index
|
||||
|
||||
# GET /telegram_chats
|
||||
def index
|
||||
authorize :telegram_chat
|
||||
@telegram_chats = policy_scope(TelegramChat)
|
||||
end
|
||||
|
||||
# GET /telegram_chats/:id
|
||||
def show
|
||||
authorize @telegram_chat
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_telegram_chat
|
||||
@telegram_chat = TelegramChat.find params[:id]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TelegramChatPolicy < ApplicationPolicy
|
||||
def index?
|
||||
context.account&.is_superuser?
|
||||
end
|
||||
|
||||
def show?
|
||||
context.account&.is_superuser?
|
||||
end
|
||||
|
||||
class Scope < Scope
|
||||
def resolve
|
||||
return scope.all if context.account&.is_superuser?
|
||||
|
||||
scope.none
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,52 @@
|
|||
<div class="container">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :id %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :remote_id %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :chat_type %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :title %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :username %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :first_name %>
|
||||
</th>
|
||||
<th scope="col">
|
||||
<%= TelegramChat.human_attribute_name :last_name %>
|
||||
</th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @telegram_chats.each do |telegram_chat| %>
|
||||
<tr>
|
||||
<td scope="row"><%= telegram_chat.id %></td>
|
||||
<td><%= telegram_chat.remote_id %></td>
|
||||
<td><%= telegram_chat.chat_type %></td>
|
||||
<td><%= truncate telegram_chat.title, length: 20 %></td>
|
||||
<td><%= truncate telegram_chat.username, length: 20 %></td>
|
||||
<td><%= truncate telegram_chat.first_name, length: 20 %></td>
|
||||
<td><%= truncate telegram_chat.last_name, length: 20 %></td>
|
||||
<td>
|
||||
<% if policy(telegram_chat).show? %>
|
||||
<%= link_to telegram_chat_path(telegram_chat),
|
||||
role: :button, class: 'btn btn-light btn-sm' do %>
|
||||
<i class="far fa-eye"></i>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
|
@ -0,0 +1,21 @@
|
|||
<div class="container">
|
||||
<dl>
|
||||
<dt><%= TelegramChat.human_attribute_name :remote_id %></dt>
|
||||
<dd><%= @telegram_chat.remote_id %></dd>
|
||||
|
||||
<dt><%= TelegramChat.human_attribute_name :chat_type %></dt>
|
||||
<dd><%= @telegram_chat.chat_type %></dd>
|
||||
|
||||
<dt><%= TelegramChat.human_attribute_name :title %></dt>
|
||||
<dd><%= truncate @telegram_chat.title %></dd>
|
||||
|
||||
<dt><%= TelegramChat.human_attribute_name :username %></dt>
|
||||
<dd><%= truncate @telegram_chat.username %></dd>
|
||||
|
||||
<dt><%= TelegramChat.human_attribute_name :first_name %></dt>
|
||||
<dd><%= truncate @telegram_chat.first_name %></dd>
|
||||
|
||||
<dt><%= TelegramChat.human_attribute_name :last_name %></dt>
|
||||
<dd><%= truncate @telegram_chat.last_name %></dd>
|
||||
</dl>
|
||||
</div>
|
|
@ -23,4 +23,6 @@ Rails.application.routes.draw do
|
|||
resources :telegram_bots, only: %i[index show] do
|
||||
resources :updates, controller: 'telegram_bots/updates', only: :create
|
||||
end
|
||||
|
||||
resources :telegram_chats, only: %i[index show]
|
||||
end
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /telegram_chats' do
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
|
||||
create_list :telegram_chat, 5
|
||||
|
||||
get '/telegram_chats'
|
||||
end
|
||||
|
||||
context 'when no account is authenticated' do
|
||||
let(:current_account) { nil }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when guest account is authenticated' do
|
||||
let(:current_account) { create :guest_account }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when usual account is authenticated' do
|
||||
let(:current_account) { create :account_with_user }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when superuser account is authenticated' do
|
||||
let(:current_account) { create :superuser_account }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'GET /telegram_chats/:id' do
|
||||
let!(:telegram_chat) { create :telegram_chat }
|
||||
|
||||
before do
|
||||
sign_in current_account.user if current_account&.user
|
||||
get "/telegram_chats/#{telegram_chat.id}"
|
||||
end
|
||||
|
||||
context 'when no account is authenticated' do
|
||||
let(:current_account) { nil }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when guest account is authenticated' do
|
||||
let(:current_account) { create :guest_account }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when usual account is authenticated' do
|
||||
let(:current_account) { create :account_with_user }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
context 'when superuser account is authenticated' do
|
||||
let(:current_account) { create :superuser_account }
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :ok
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'POST /telegram_bots/:telegram_bot_id/updates' do
|
||||
let(:telegram_bot) { create :telegram_bot }
|
||||
|
||||
context 'with valid params' do
|
||||
before do
|
||||
post "/telegram_bots/#{telegram_bot.id}/updates",
|
||||
params: { secret: telegram_bot.secret }
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :no_content
|
||||
end
|
||||
end
|
||||
|
||||
context 'when no telegram bot exist' do
|
||||
before do
|
||||
post "/telegram_bots/#{rand(10_000..1_000_000)}/updates",
|
||||
params: { secret: telegram_bot.secret }
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :not_found
|
||||
end
|
||||
end
|
||||
|
||||
context 'when secret is not valid' do
|
||||
before do
|
||||
post "/telegram_bots/#{telegram_bot.id}/updates",
|
||||
params: { secret: SecureRandom.hex }
|
||||
end
|
||||
|
||||
specify do
|
||||
expect(response).to have_http_status :unauthorized
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue