1
0
Fork 0

Add action Settings::TelegramContactsController#index

This commit is contained in:
Alex Kotov 2018-12-12 05:08:00 +05:00
parent f97a0fb25e
commit ed35b805a0
No known key found for this signature in database
GPG Key ID: 4E831250F47DE154
7 changed files with 105 additions and 0 deletions

View File

@ -58,6 +58,7 @@ Style/AndOr:
Style/ClassAndModuleChildren:
Exclude:
- 'app/policies/**/*.rb'
- 'app/controllers/**/*.rb'
Style/Documentation:

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
class Settings::TelegramContactsController < ApplicationController
# GET /settings/telegram_contacts
def index
authorize %i[settings account_telegram_contact]
@telegram_contacts = policy_scope(
current_account.account_telegram_contacts,
policy_scope_class: Settings::AccountTelegramContactPolicy::Scope,
)
end
end

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
class Settings::AccountTelegramContactPolicy < ApplicationPolicy
def index?
!!context.guest_account
end
class Scope < Scope
def resolve
return scope.none if context.guest_account.nil?
scope.where(account: context.guest_account)
end
end
end

View File

@ -25,4 +25,8 @@ Rails.application.routes.draw do
end
resources :telegram_chats, only: %i[index show]
namespace :settings do
resources :telegram_contacts, only: :index
end
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Settings::AccountTelegramContactPolicy do
permissions '.scope' do
pending "add some examples to (or delete) #{__FILE__}"
end
permissions :show? do
pending "add some examples to (or delete) #{__FILE__}"
end
permissions :create? do
pending "add some examples to (or delete) #{__FILE__}"
end
permissions :update? do
pending "add some examples to (or delete) #{__FILE__}"
end
permissions :destroy? do
pending "add some examples to (or delete) #{__FILE__}"
end
end

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'GET /settings/telegram_contacts' do
before do
sign_in current_account.user if current_account&.user
if current_account
create_list :account_telegram_contact, 5, account: current_account
end
get '/settings/telegram_contacts'
end
context 'when no account is authenticated' do
let(:current_account) { nil }
specify do
expect(response).to have_http_status :unauthorized
end
end
xcontext 'when guest account is authenticated' do
let(:current_account) { create :guest_account }
specify do
expect(response).to have_http_status :ok
end
end
context 'when usual account is authenticated' do
let(:current_account) { create :usual_account }
specify do
expect(response).to have_http_status :ok
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