diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/requests/authentications_spec.rb | 28 | ||||
-rw-r--r-- | spec/support/include_strings.rb | 12 | ||||
-rw-r--r-- | spec/support/shared_examples/requests/post_request.rb | 19 |
3 files changed, 40 insertions, 19 deletions
diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb index 2ee8ead..71ffa19 100644 --- a/spec/requests/authentications_spec.rb +++ b/spec/requests/authentications_spec.rb @@ -2,24 +2,14 @@ require 'rails_helper' -RSpec.describe 'Authentications', type: :request do - let(:user) { create(:user_account) } - let(:params) { { credentials: { email: user.email, password: user.password } } } - let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } - - describe 'POST /api/authenticate' do - scenario 'successfully' do - post('/api/authenticate', params: params.to_json, headers:) - expect(response).to have_http_status(200) - expect(response.body).to include('token') - expect(response.body).to include('refresh') - end - - scenario 'unsuccessfully' do - params[:credentials][:password] = 'wrong_password' - post('/api/authenticate', params: params.to_json, headers:) - expect(response).to have_http_status(401) - expect(response.body).to include('Credenciales incorrectas') - end +RSpec.describe 'POST /api/authenticate', type: :request do + it_behaves_like 'a POST request' do + let(:route) { '/api/authenticate' } + let(:expected_error_message) { 'Credenciales incorrectas' } + let(:desired_error_status) { 401 } + let(:expected_text) { %w[token refresh] } + let(:user) { create(:user_account) } + let(:params) { { credentials: { email: user.email, password: user.password } } } + let(:wrong_params) { { credentials: { email: user.email, password: 'wrong_password' } } } end end diff --git a/spec/support/include_strings.rb b/spec/support/include_strings.rb new file mode 100644 index 0000000..bddff1a --- /dev/null +++ b/spec/support/include_strings.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true +require 'rspec/expectations' + +RSpec::Matchers.define :include_strings do |expected| + match do |actual| + includes_strings = true + expected.each do |s| + includes_strings = false unless actual.include?(s) + end + includes_strings + end +end diff --git a/spec/support/shared_examples/requests/post_request.rb b/spec/support/shared_examples/requests/post_request.rb new file mode 100644 index 0000000..6ea2a86 --- /dev/null +++ b/spec/support/shared_examples/requests/post_request.rb @@ -0,0 +1,19 @@ +RSpec.shared_examples 'a POST request' do + let(:headers) { { 'CONTENT_TYPE' => 'application/json' } } + + context 'with correct parameters' do + it 'returns a successful http status and a response to the posted data' do + post(route, params: params.to_json, headers:) + expect(response).to have_http_status(200) + expect(response.body).to include_strings(expected_text) + end + end + + context 'with incorrect parameters' do + it 'returns a client side error http status and a error message' do + post('/api/authenticate', params: wrong_params.to_json, headers:) + expect(response).to have_http_status(desired_error_status) + expect(response.body).to include(expected_error_message) + end + end +end |