Просмотр исходного кода

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

Mantas Masalskis 4 лет назад
Родитель
Сommit
ddfbf6139e

+ 2 - 1
spec/requests/admin/knowledge_base/public_menu_spec.rb

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

+ 3 - 1
spec/requests/knowledge_base/answer_attachments_cloning_spec.rb

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

+ 75 - 26
spec/requests/knowledge_base/attachments_spec.rb

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

+ 12 - 4
spec/requests/knowledge_base/loading_initial_data_spec.rb

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

+ 21 - 8
spec/requests/knowledge_base/translation_update_spec.rb

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

+ 68 - 0
spec/support/authenticated_as.rb

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

+ 3 - 24
spec/support/capybara/authenticated.rb

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

+ 4 - 8
spec/support/request.rb

@@ -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
-
-    authenticated_as @current_user unless @current_user.nil?
+  config.before(:each, :authenticated_as, type: :request) do |example|
+    user = authenticated_as_get_user example.metadata[:authenticated_as], return_type: :user
+
+    authenticated_as user if user
   end
 end

+ 1 - 1
spec/system/admin/knowledge_base/public_menu_spec.rb

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

+ 1 - 1
spec/system/basic/authentication_spec.rb

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

Некоторые файлы не были показаны из-за большого количества измененных файлов