trabajo-afectivo/spec/requests/external_credentials_spec.rb

296 lines
12 KiB
Ruby
Raw Normal View History

require 'rails_helper'
RSpec.describe 'External Credentials', type: :request do
let(:admin_user) { create(:admin_user) }
context 'without authentication' do
describe '#index' do
it 'returns 401 unauthorized' do
get '/api/v1/external_credentials', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'authentication failed')
end
end
describe '#app_verify' do
it 'returns 401 unauthorized' do
post '/api/v1/external_credentials/facebook/app_verify', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'authentication failed')
end
end
describe '#link_account' do
it 'returns 401 unauthorized' do
get '/api/v1/external_credentials/facebook/link_account', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'authentication failed')
end
end
describe '#callback' do
it 'returns 401 unauthorized' do
get '/api/v1/external_credentials/facebook/callback', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'authentication failed')
end
end
end
context 'authenticated as admin' do
before { authenticated_as(admin_user) }
describe '#index' do
it 'responds with an array of ExternalCredential records' do
get '/api/v1/external_credentials', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to eq([])
end
context 'with expand=true URL parameters' do
it 'responds with an array of ExternalCredential records and their association data' do
get '/api/v1/external_credentials?expand=true', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to eq([])
end
end
end
context 'for Facebook' do
let(:invalid_credentials) do
{ application_id: 123, application_secret: 123 }
end
describe '#app_verify' do
describe 'failure cases' do
context 'when permission for Facebook channel is deactivated' do
before { Permission.find_by(name: 'admin.channel_facebook').update(active: false) }
it 'returns 401 unauthorized with internal (Zammad) error' do
post '/api/v1/external_credentials/facebook/app_verify', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'Not authorized (user)!')
end
end
context 'with no credentials' do
it 'returns 200 with internal (Zammad) error' do
post '/api/v1/external_credentials/facebook/app_verify', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => 'No application_id param!')
end
end
context 'with invalid credentials, via request params' do
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
it 'returns 200 with remote (Facebook auth) error', :use_vcr do
post '/api/v1/external_credentials/facebook/app_verify', params: invalid_credentials, as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
end
end
context 'with invalid credentials, via ExternalCredential record' do
before { create(:facebook_credential, credentials: invalid_credentials) }
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
it 'returns 200 with remote (Facebook auth) error', :use_vcr do
post '/api/v1/external_credentials/facebook/app_verify', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
end
end
end
end
describe '#link_account' do
describe 'failure cases' do
context 'with no credentials' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/facebook/link_account', as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No facebook app configured!')
end
end
context 'with invalid credentials, via request params' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/facebook/link_account', params: invalid_credentials, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No facebook app configured!')
end
end
context 'with invalid credentials, via ExternalCredential record' do
before { create(:facebook_credential, credentials: invalid_credentials) }
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
it 'returns 500 with remote (Facebook auth) error', :use_vcr do
get '/api/v1/external_credentials/facebook/link_account', as: :json
expect(response).to have_http_status(:internal_server_error)
expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
end
end
end
end
describe '#callback' do
describe 'failure cases' do
context 'with no credentials' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/facebook/callback', as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No facebook app configured!')
end
end
context 'with invalid credentials, via request params' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/facebook/callback', params: invalid_credentials, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No facebook app configured!')
end
end
context 'with invalid credentials, via ExternalCredential record' do
before { create(:facebook_credential, credentials: invalid_credentials) }
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
it 'returns 500 with remote (Facebook auth) error', :use_vcr do
get '/api/v1/external_credentials/facebook/callback', as: :json
expect(response).to have_http_status(:internal_server_error)
expect(json_response).to include('error' => 'type: OAuthException, code: 101, message: Error validating application. Cannot get application info due to a system error. [HTTP 400]')
end
end
end
end
end
context 'for Twitter' do
let(:invalid_credentials) do
{ consumer_key: 123, consumer_secret: 123, oauth_token: 123, oauth_token_secret: 123 }
end
describe '#app_verify' do
describe 'failure cases' do
context 'when permission for Twitter channel is deactivated' do
before { Permission.find_by(name: 'admin.channel_twitter').update(active: false) }
it 'returns 401 unauthorized with internal (Zammad) error' do
post '/api/v1/external_credentials/twitter/app_verify', as: :json
expect(response).to have_http_status(:unauthorized)
expect(json_response).to include('error' => 'Not authorized (user)!')
end
end
context 'with no credentials' do
it 'returns 200 with internal (Zammad) error' do
post '/api/v1/external_credentials/twitter/app_verify', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => 'No consumer_key param!')
end
end
context 'with invalid credentials, via request params' do
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
it 'returns 200 with remote (Twitter auth) error', :use_vcr do
post '/api/v1/external_credentials/twitter/app_verify', params: invalid_credentials, as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => '401 Authorization Required')
end
end
context 'with invalid credentials, via existing ExternalCredential record' do
before { create(:twitter_credential, credentials: invalid_credentials) }
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
it 'returns 200 with remote (Twitter auth) error', :use_vcr do
post '/api/v1/external_credentials/twitter/app_verify', as: :json
expect(response).to have_http_status(:ok)
expect(json_response).to include('error' => '401 Authorization Required')
end
end
end
end
describe '#link_account' do
describe 'failure cases' do
context 'with no credentials' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/twitter/link_account', as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No twitter app configured!')
end
end
context 'with invalid credentials, via request params' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/twitter/link_account', params: invalid_credentials, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No twitter app configured!')
end
end
context 'with invalid credentials, via ExternalCredential record' do
before { create(:twitter_credential, credentials: invalid_credentials) }
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
it 'returns 500 with remote (Twitter auth) error', :use_vcr do
get '/api/v1/external_credentials/twitter/link_account', as: :json
expect(response).to have_http_status(:internal_server_error)
expect(json_response).to include('error' => '401 Authorization Required')
end
end
end
end
describe '#callback' do
describe 'failure cases' do
context 'with no credentials' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/twitter/callback', as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No twitter app configured!')
end
end
context 'with invalid credentials, via request params' do
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/twitter/callback', params: invalid_credentials, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No twitter app configured!')
end
end
context 'with invalid credentials, via ExternalCredential record' do
before { create(:twitter_credential, credentials: invalid_credentials) }
it 'returns 422 unprocessable entity with internal (Zammad) error' do
get '/api/v1/external_credentials/twitter/callback', as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response).to include('error' => 'No request_token for session found!')
end
end
end
end
end
end
end