From b12bbf51dd2f8f60c1370f9f132896ae3d2dc500 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 7 Dec 2018 21:26:35 +0500 Subject: [PATCH] Add actions TelegramChatsController#index, #show --- app/controllers/telegram_chats_controller.rb | 22 ++++++++ app/policies/telegram_chat_policy.rb | 19 +++++++ app/views/telegram_chats/index.html.erb | 52 +++++++++++++++++++ app/views/telegram_chats/show.html.erb | 21 ++++++++ config/routes.rb | 2 + spec/requests/telegram_chats/index_spec.rb | 45 ++++++++++++++++ spec/requests/telegram_chats/show_spec.rb | 44 ++++++++++++++++ .../telegram_chats/updates/create_spec.rb | 40 ++++++++++++++ 8 files changed, 245 insertions(+) create mode 100644 app/controllers/telegram_chats_controller.rb create mode 100644 app/policies/telegram_chat_policy.rb create mode 100644 app/views/telegram_chats/index.html.erb create mode 100644 app/views/telegram_chats/show.html.erb create mode 100644 spec/requests/telegram_chats/index_spec.rb create mode 100644 spec/requests/telegram_chats/show_spec.rb create mode 100644 spec/requests/telegram_chats/updates/create_spec.rb diff --git a/app/controllers/telegram_chats_controller.rb b/app/controllers/telegram_chats_controller.rb new file mode 100644 index 0000000..8c30ee8 --- /dev/null +++ b/app/controllers/telegram_chats_controller.rb @@ -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 diff --git a/app/policies/telegram_chat_policy.rb b/app/policies/telegram_chat_policy.rb new file mode 100644 index 0000000..4d541c0 --- /dev/null +++ b/app/policies/telegram_chat_policy.rb @@ -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 diff --git a/app/views/telegram_chats/index.html.erb b/app/views/telegram_chats/index.html.erb new file mode 100644 index 0000000..d326a5f --- /dev/null +++ b/app/views/telegram_chats/index.html.erb @@ -0,0 +1,52 @@ +
+ + + + + + + + + + + + + + + + <% @telegram_chats.each do |telegram_chat| %> + + + + + + + + + + + <% end %> + +
+ <%= TelegramChat.human_attribute_name :id %> + + <%= TelegramChat.human_attribute_name :remote_id %> + + <%= TelegramChat.human_attribute_name :chat_type %> + + <%= TelegramChat.human_attribute_name :title %> + + <%= TelegramChat.human_attribute_name :username %> + + <%= TelegramChat.human_attribute_name :first_name %> + + <%= TelegramChat.human_attribute_name :last_name %> +
<%= telegram_chat.id %><%= telegram_chat.remote_id %><%= telegram_chat.chat_type %><%= truncate telegram_chat.title, length: 20 %><%= truncate telegram_chat.username, length: 20 %><%= truncate telegram_chat.first_name, length: 20 %><%= truncate telegram_chat.last_name, length: 20 %> + <% if policy(telegram_chat).show? %> + <%= link_to telegram_chat_path(telegram_chat), + role: :button, class: 'btn btn-light btn-sm' do %> + + <% end %> + <% end %> +
+
diff --git a/app/views/telegram_chats/show.html.erb b/app/views/telegram_chats/show.html.erb new file mode 100644 index 0000000..ce01db7 --- /dev/null +++ b/app/views/telegram_chats/show.html.erb @@ -0,0 +1,21 @@ +
+
+
<%= TelegramChat.human_attribute_name :remote_id %>
+
<%= @telegram_chat.remote_id %>
+ +
<%= TelegramChat.human_attribute_name :chat_type %>
+
<%= @telegram_chat.chat_type %>
+ +
<%= TelegramChat.human_attribute_name :title %>
+
<%= truncate @telegram_chat.title %>
+ +
<%= TelegramChat.human_attribute_name :username %>
+
<%= truncate @telegram_chat.username %>
+ +
<%= TelegramChat.human_attribute_name :first_name %>
+
<%= truncate @telegram_chat.first_name %>
+ +
<%= TelegramChat.human_attribute_name :last_name %>
+
<%= truncate @telegram_chat.last_name %>
+
+
diff --git a/config/routes.rb b/config/routes.rb index acd40a2..e22cf62 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/requests/telegram_chats/index_spec.rb b/spec/requests/telegram_chats/index_spec.rb new file mode 100644 index 0000000..652d81e --- /dev/null +++ b/spec/requests/telegram_chats/index_spec.rb @@ -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 diff --git a/spec/requests/telegram_chats/show_spec.rb b/spec/requests/telegram_chats/show_spec.rb new file mode 100644 index 0000000..6ea0443 --- /dev/null +++ b/spec/requests/telegram_chats/show_spec.rb @@ -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 diff --git a/spec/requests/telegram_chats/updates/create_spec.rb b/spec/requests/telegram_chats/updates/create_spec.rb new file mode 100644 index 0000000..77ebe1b --- /dev/null +++ b/spec/requests/telegram_chats/updates/create_spec.rb @@ -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