trabajo-afectivo/spec/lib/import/exchange/folder_spec.rb

77 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
require 'rails_helper'
require 'viewpoint' # Only load this gem when it is really used.
RSpec.describe Import::Exchange::Folder do
# see https://github.com/zammad/zammad/issues/2152
Refactoring: Automatic RSpec VCR cassette name helper This commit was prepared to support upcoming additions to the test suite (specifically, better coverage for existing Twitter functionality). These upcoming changes will depend heavily on VCR.[0] (VCR is a Ruby gem that makes it easier to write and run tests that call out to external services over HTTP by "recording" HTTP transactions to a YAML file and "replaying" them later.) VCR is widely-used (4600 GitHub stars), but its API is a little clumsy-- You have to manually specify the name of a "cassette" file every time: it 'does something' do VCR.use_cassette('path/to/cassette') do ... end end This commit adds an RSpec metadata config option as a shorthand for the syntax above: it 'does something', :use_vcr do ... end This config option automatically generates a cassette filename based on the description of the example it's applied to. === Analysis of alternative approaches Ideally, these auto-generated cassette filenames should be unique: if filenames collide, multiple examples will share the same cassette. A first attempt generated names based on `example.full_description`, but that led to errors: Errno::ENAMETOOLONG: File name too long @ rb_sysopen - /opt/zammad/test/data/vcr_cassettes/models/ticket/article/ticket_article_callbacks_observers_async_transactions_-_auto-setting_of_outgoing_twitter_article_attributes_via_bg_jobs_when_the_original_channel_specified_in_ticket_preferences_was_deleted_but_a_new_one_with_the_same_screen_name_exists_sets_appropriate_status_attributes_on_the_new_channel.yml Another idea was to use MD5 digests of the above, but in fact both of these approaches share another problem: even minor changes to the description could break tests (unless the committer remembers to rename the cassette file to match): an altered description means VCR will record a new cassette file instead of replaying from the original. (Normally, this would only slow down the test instead of breaking it, but sometimes we modify tests and cassettes after recording them to hide sensitive data like API keys or login credentials.) The approach taken by this commit was to use partial descriptions, combining the parent `describe`/`context` label with the `it` label. This does not guarantee uniqueness-- even in the present refactoring, it produced a filename collision-- but it's a good middle ground. [0]: https://relishapp.com/vcr/vcr/docs
2019-11-12 08:17:21 +00:00
describe '#display_path (#2152)', :use_vcr do
subject(:folder) { described_class.new(ews_connection) }
let(:ews_connection) { Viewpoint::EWSClient.new(endpoint, user, pass) }
let(:endpoint) { 'https://exchange.example.com/EWS/Exchange.asmx' }
let(:user) { 'user@example.com' }
let(:pass) { 'password' }
let(:grandchild_of_root) { ews_connection.get_folder_by_name('Inbox') }
let(:child_of_root) { ews_connection.get_folder(grandchild_of_root.parent_folder_id) }
context 'when server returns valid UTF-8' do
context 'and target folder is in root directory' do
2018-08-23 05:02:09 +00:00
it 'returns the display name of the folder' do
expect(folder.display_path(child_of_root))
.to eq('Top of Information Store')
2018-08-23 05:02:09 +00:00
end
end
context 'and target folder is in subfolder of root' do
2018-08-23 05:02:09 +00:00
it 'returns the full path from root to target' do
expect(folder.display_path(grandchild_of_root))
.to eq('Top of Information Store -> Inbox')
2018-08-23 05:02:09 +00:00
end
end
context 'and walking up directory tree raises EwsError' do
it 'returns the partial path from error to target folder' do
allow(folder)
.to receive(:id_folder_map).with(any_args).and_raise(Viewpoint::EWS::EwsError)
expect(folder.display_path(grandchild_of_root))
.to eq('Inbox')
2018-08-23 05:02:09 +00:00
end
end
end
context 'when server returns invalid UTF-8' do
context 'and target folder is in root directory' do
it 'returns the display name of the folder in valid UTF-8' do
allow(child_of_root)
.to receive(:display_name).and_return('你好'.b)
expect { folder.display_path(child_of_root).to_json }.not_to raise_error
end
end
context 'and target folder is in subfolder of root' do
it 'returns the full path from root to target in valid UTF-8' do
allow(grandchild_of_root)
.to receive(:display_name).and_return('你好'.b)
expect { folder.display_path(grandchild_of_root).to_json }.not_to raise_error
end
end
context 'and walking up directory tree raises EwsError' do
it 'returns the partial path from error to target folder in valid UTF-8' do
allow(grandchild_of_root)
.to receive(:display_name).and_return('你好'.b)
allow(folder)
.to receive(:id_folder_map).with(any_args).and_raise(Viewpoint::EWS::EwsError)
expect { folder.display_path(grandchild_of_root).to_json }.not_to raise_error
end
end
end
end
end