Maintenance: Unified :authenticated and :authenticated_as helpers to just :authenticated_as in system and request specs.

This commit is contained in:
Mantas Masalskis 2020-06-18 13:51:25 +02:00 committed by Thorsten Eckel
parent ae22dfcded
commit ddfbf6139e
27 changed files with 209 additions and 93 deletions

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Admin Knowledge Base Public Menu', type: :request, authenticated_as: :admin_user do
RSpec.describe 'Admin Knowledge Base Public Menu', type: :request, authenticated_as: :current_user do
let(:url) { "/api/v1/knowledge_bases/manage/#{knowledge_base.id}/update_menu_items" }
let(:params) do
{
@ -12,6 +12,7 @@ RSpec.describe 'Admin Knowledge Base Public Menu', type: :request, authenticated
}
end
let(:current_user) { create(:admin) }
let(:menu_item) { create(:knowledge_base_menu_item) }
let(:kb_locale) { menu_item.kb_locale }
let(:knowledge_base) { kb_locale.knowledge_base }

View file

@ -1,12 +1,14 @@
require 'rails_helper'
RSpec.describe 'KnowledgeBase answer attachments cloning', type: :request, authenticated_as: :agent_user do
RSpec.describe 'KnowledgeBase answer attachments cloning', type: :request, authenticated_as: :current_user do
include_context 'basic Knowledge Base' do
before do
published_answer
end
end
let(:current_user) { create(:agent) }
it 'copies to given UploadCache' do
form_id = Random.rand(999..9999)
endpoint = "/api/v1/knowledge_bases/#{knowledge_base.id}/answers/#{published_answer.id}/attachments/clone_to_form"

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'KnowledgeBase attachments', type: :request do
RSpec.describe 'KnowledgeBase attachments', type: :request, authenticated_as: :current_user do
include_context 'basic Knowledge Base'
let(:attachment) do
@ -16,7 +16,8 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
)
end
let(:endpoint) { "/api/v1/attachments/#{attachment.id}" }
let(:endpoint) { "/api/v1/attachments/#{attachment.id}" }
let(:current_user) { create(user_identifier) if defined?(user_identifier) }
describe 'visible when attached to' do
shared_examples 'a visible resource' do
@ -36,15 +37,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'draft answer' do
let(:object) { draft_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-existent resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a visible resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-existent resource'
end
@ -56,15 +63,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'internal answer' do
let(:object) { internal_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a visible resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a visible resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-existent resource'
end
@ -76,15 +89,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'published answer' do
let(:object) { published_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a visible resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a visible resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a visible resource'
end
@ -96,15 +115,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'archived answer' do
let(:object) { archived_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-existent resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a visible resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-existent resource'
end
@ -126,15 +151,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'draft answer' do
let(:object) { draft_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-deletable resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a deletable resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-deletable resource'
end
@ -146,15 +177,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'internal answer' do
let(:object) { internal_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-deletable resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a deletable resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-deletable resource'
end
@ -166,15 +203,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'published answer' do
let(:object) { published_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-deletable resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a deletable resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-deletable resource'
end
@ -186,15 +229,21 @@ RSpec.describe 'KnowledgeBase attachments', type: :request do
describe 'archived answer' do
let(:object) { archived_answer }
describe 'as agent', authenticated_as: :agent_user do
describe 'as agent' do
let(:user_identifier) { :agent }
it_behaves_like 'a non-deletable resource'
end
context 'as admin', authenticated_as: :admin_user do
context 'as admin' do
let(:user_identifier) { :admin }
it_behaves_like 'a deletable resource'
end
context 'as customer', authenticated_as: :customer_user do
context 'as customer' do
let(:user_identifier) { :customer }
it_behaves_like 'a non-deletable resource'
end

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'KnowledgeBase loading initial data', type: :request, searchindex: true do
RSpec.describe 'KnowledgeBase loading initial data', type: :request, searchindex: true, authenticated_as: :current_user do
include_context 'basic Knowledge Base' do
before do
draft_answer
@ -13,12 +13,16 @@ RSpec.describe 'KnowledgeBase loading initial data', type: :request, searchindex
post '/api/v1/knowledge_bases/init'
end
let(:current_user) { create(user_identifier) if defined?(user_identifier) }
shared_examples 'returning valid JSON' do
it { expect(response).to have_http_status(:ok) }
it { expect(json_response).to be_a_kind_of(Hash) }
end
describe 'for admin', authenticated_as: :admin_user do
describe 'for admin' do
let(:user_identifier) { :admin }
it_behaves_like 'returning valid JSON'
it 'returns assets for all KB objects' do
@ -26,7 +30,9 @@ RSpec.describe 'KnowledgeBase loading initial data', type: :request, searchindex
end
end
describe 'for agent', authenticated_as: :agent_user do
describe 'for agent' do
let(:user_identifier) { :agent }
it_behaves_like 'returning valid JSON'
it 'returns assets for all KB objects except drafts' do
@ -36,7 +42,9 @@ RSpec.describe 'KnowledgeBase loading initial data', type: :request, searchindex
end
end
describe 'for customer', authenticated_as: :customer_user do
describe 'for customer' do
let(:user_identifier) { :customer }
it_behaves_like 'returning valid JSON'
it 'only returns assets for KB itself' do

View file

@ -1,9 +1,10 @@
require 'rails_helper'
RSpec.describe 'KnowledgeBase translation update', type: :request, authentication: true do
RSpec.describe 'KnowledgeBase translation update', type: :request, authenticated_as: :current_user do
include_context 'basic Knowledge Base'
let(:new_title) { 'new title for update test' }
let(:new_title) { 'new title for update test' }
let(:current_user) { create(user_identifier) if defined?(user_identifier) }
let(:params_for_updating) do
{
@ -22,19 +23,25 @@ RSpec.describe 'KnowledgeBase translation update', type: :request, authenticatio
end
describe 'changes KB translation title' do
describe 'as editor', authenticated_as: :admin_user do
describe 'as editor' do
let(:user_identifier) { :admin_user }
it 'updates title' do
expect { request }.to change { knowledge_base.reload.translations.first.title }.to(new_title)
end
end
describe 'as reader', authenticated_as: :agent_user do
describe 'as reader' do
let(:user_identifier) { :agent_user }
it 'does not change title' do
expect { request }.not_to change { knowledge_base.reload.translations.first.title }
end
end
describe 'as non-KB user', authenticated_as: :customer do
describe 'as non-KB user' do
let(:user_identifier) { :customer }
it 'does not change title' do
expect { request }.not_to change { knowledge_base.reload.translations.first.title }
end
@ -50,16 +57,22 @@ RSpec.describe 'KnowledgeBase translation update', type: :request, authenticatio
describe 'can make request to KB translation' do
before { request }
describe 'as editor', authenticated_as: :admin_user do
describe 'as editor' do
let(:user_identifier) { :admin_user }
it { expect(response).to have_http_status(:ok) }
it { expect(json_response).to be_a_kind_of(Hash) }
end
describe 'as reader', authenticated_as: :agent_user do
describe 'as reader' do
let(:user_identifier) { :agent_user }
it { expect(response).to have_http_status(:unauthorized) }
end
describe 'as non-KB user', authenticated_as: :customer do
describe 'as non-KB user' do
let(:user_identifier) { :customer }
it { expect(response).to have_http_status(:unauthorized) }
end

View file

@ -0,0 +1,68 @@
module ZammadAuthenticatedAsHelper
# parse authenticated_as params for request and system test helpers
#
# @param input [Any] any to parse, see bellow for options
# @param return_type [Symbol] :credentials or :user
def authenticated_as_get_user(input, return_type:)
case input
when Proc
parse_meta instance_exec(&input), return_type: return_type
when Symbol
parse_meta instance_eval { send(input) }, return_type: return_type
else
parse_meta input, return_type: return_type
end
end
private
def parse_meta(input, return_type:)
case return_type
when :credentials
parse_meta_credentials(input)
when :user
parse_meta_user_object(input)
end
end
def parse_meta_user_object(input)
case input
when User
input
end
end
def parse_meta_credentials(input)
case input
when Hash
input.slice(:username, :password)
when User
parse_meta_user(input)
when true
{
username: 'master@example.com',
password: 'test',
}
end
end
def parse_meta_user(input)
password = input.password_plain
if password.blank?
password = 'automagically set by your friendly capybara helper'
input.update!(password: password)
end
{
username: input.email,
password: password,
}
end
end
RSpec.configure do |config|
%i[request system].each do |type|
config.include ZammadAuthenticatedAsHelper, type: type
end
end

View file

@ -13,30 +13,9 @@ RSpec.configure do |config|
# there is no way to authenticated in a not set up system
next if !example.metadata.fetch(:set_up, true)
# check if authentication should be performed
authenticated = example.metadata.fetch(:authenticated, true)
next if authenticated.blank?
authenticated = example.metadata.fetch(:authenticated_as, true)
credentials = authenticated_as_get_user(authenticated, return_type: :credentials)
if authenticated.is_a?(Proc)
user = instance_exec(&authenticated)
password = user.password_plain
if password.blank?
password = 'automagically set by your friendly capybara helper'
user.update!(password: password)
end
credentials = {
username: user.email,
password: password,
}
else
credentials = {
username: 'master@example.com',
password: 'test',
}
end
login(credentials)
login(credentials) if credentials
end
end

View file

@ -147,13 +147,9 @@ RSpec.configure do |config|
#
# let(:user) { create(:customer_user) }
#
config.before(:each, :authenticated_as) do |example|
@current_user = if example.metadata[:authenticated_as].is_a? Proc
instance_exec(&example.metadata[:authenticated_as])
else
create(*Array(example.metadata[:authenticated_as]))
end
config.before(:each, :authenticated_as, type: :request) do |example|
user = authenticated_as_get_user example.metadata[:authenticated_as], return_type: :user
authenticated_as @current_user unless @current_user.nil?
authenticated_as user if user
end
end

View file

@ -1,7 +1,7 @@
require 'rails_helper'
# https://github.com/zammad/zammad/issues/266
RSpec.describe 'Admin Panel > Knowledge Base > Public Menu', type: :system, authenticated: true do
RSpec.describe 'Admin Panel > Knowledge Base > Public Menu', type: :system, authenticated_as: true do
include_context 'basic Knowledge Base'
include_context 'Knowledge Base menu items'

View file

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe 'Authentication', type: :system do
it 'Login', authenticated: false do
it 'Login', authenticated_as: false do
login(
username: 'master@example.com',
password: 'test',

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Unauthenticated redirect', type: :system, authenticated: false do
RSpec.describe 'Unauthenticated redirect', type: :system, authenticated_as: false do
it 'Sessions' do
visit 'system/sessions'

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Dashboard', type: :system, authenticated: true do
RSpec.describe 'Dashboard', type: :system, authenticated_as: true do
it 'shows default widgets' do
visit 'dashboard'

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'QUnit', type: :system, authenticated: false, set_up: true, websocket: false do
RSpec.describe 'QUnit', type: :system, authenticated_as: false, set_up: true, websocket: false do
def q_unit_tests(test_name)

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Knowledge Base Locale Answer Edit', type: :system, authenticated: true do
RSpec.describe 'Knowledge Base Locale Answer Edit', type: :system, authenticated_as: true do
include_context 'basic Knowledge Base'
before do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base answer', type: :system, authenticated: false do
RSpec.describe 'Public Knowledge Base answer', type: :system, authenticated_as: false do
include_context 'basic Knowledge Base'
context 'video content' do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base for guest search', type: :system, authenticated: true, searchindex: true do
RSpec.describe 'Public Knowledge Base for guest search', type: :system, authenticated_as: true, searchindex: true do
include_context 'basic Knowledge Base'
before do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base for editor', type: :system, authenticated: true do
RSpec.describe 'Public Knowledge Base for editor', type: :system, authenticated_as: true do
include_context 'basic Knowledge Base'
before do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base for guest search', type: :system, authenticated: false, searchindex: true do
RSpec.describe 'Public Knowledge Base for guest search', type: :system, authenticated_as: false, searchindex: true do
include_context 'basic Knowledge Base'
before do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base for guest', type: :system, authenticated: false do
RSpec.describe 'Public Knowledge Base for guest', type: :system, authenticated_as: false do
include_context 'basic Knowledge Base'
before do

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Public Knowledge Base menu items', type: :system, authenticated: false do
RSpec.describe 'Public Knowledge Base menu items', type: :system, authenticated_as: false do
include_context 'basic Knowledge Base'
include_context 'Knowledge Base menu items'

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Login Message', type: :system, authenticated: false do
RSpec.describe 'Login Message', type: :system, authenticated_as: false do
context 'with maintenance_login_message' do
let(:message) { "badum tssss #{rand(99_999)}" }
let(:alt_message) { 'lorem ipsum' }

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'Profile > Devices', type: :system, authenticated: true do
RSpec.describe 'Profile > Devices', type: :system, authenticated_as: true do
subject!(:device) { create(:user_device, user_id: User.find_by(login: 'master@example.com').id) }
it 'allows to remove device' do

View file

@ -9,7 +9,7 @@ RSpec.describe 'System setup process', type: :system, set_up: false do
raise "Unable to get fqdn based on #{app_host}"
end
it 'Setting up a new system', authenticated: false do
it 'Setting up a new system', authenticated_as: false do
if !ENV['MAILBOX_INIT']
skip("NOTICE: Need MAILBOX_INIT as ENV variable like export MAILBOX_INIT='unittest01@znuny.com:somepass'")

View file

@ -10,7 +10,7 @@ RSpec.describe 'Ticket Create', type: :system do
let!(:template) { create(:template, :dummy_data, group: unpermitted_group, owner: agent) }
# Regression test for issue #2424 - Unavailable ticket template attributes get applied
it 'unavailable attributes do not get applied', authenticated: -> { agent } do
it 'unavailable attributes do not get applied', authenticated_as: :agent do
visit 'ticket/create'
use_template(template)
@ -55,7 +55,7 @@ RSpec.describe 'Ticket Create', type: :system do
end
end
context 'private key configured', authenticated: -> { agent } do
context 'private key configured', authenticated_as: :agent do
let!(:template) { create(:template, :dummy_data, group: group, owner: agent, customer: customer) }
let(:system_email_address) { 'smime1@example.com' }

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'inserting Knowledge Base answer', type: :system, authenticated: true, searchindex: true do
RSpec.describe 'inserting Knowledge Base answer', type: :system, authenticated_as: true, searchindex: true do
include_context 'basic Knowledge Base'
let(:field) { find(:richtext) }

View file

@ -1,6 +1,6 @@
require 'rails_helper'
RSpec.describe 'linking Knowledge Base answer', type: :system, authenticated: true, searchindex: true do
RSpec.describe 'linking Knowledge Base answer', type: :system, authenticated_as: true, searchindex: true do
include_context 'basic Knowledge Base'
before do

View file

@ -185,7 +185,7 @@ RSpec.describe 'Ticket zoom', type: :system do
create(:ticket_article, ticket: ticket)
end
it 'ensures that text input opens on multiple replies', authenticated: -> { current_user } do
it 'ensures that text input opens on multiple replies', authenticated_as: :current_user do
visit "ticket/zoom/#{ticket.id}"
2.times do |article_offset|
@ -207,7 +207,7 @@ RSpec.describe 'Ticket zoom', type: :system do
end
end
describe 'delete article', authenticated: -> { user } do
describe 'delete article', authenticated_as: :user do
let(:admin_user) { create :admin, groups: [Group.first] }
let(:agent_user) { create :agent, groups: [Group.first] }
let(:customer_user) { create :customer }
@ -395,7 +395,7 @@ RSpec.describe 'Ticket zoom', type: :system do
end
end
context 'S/MIME active', authenticated: -> { agent } do
context 'S/MIME active', authenticated_as: :agent do
let(:system_email_address) { 'smime1@example.com' }
let(:email_address) { create(:email_address, email: system_email_address) }