summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-02-25 00:20:59 -0600
committerHombreLaser <sebastian-440@live.com>2023-02-25 00:20:59 -0600
commit6b49fe21f76883e039a2c149ecd5906c9d038de3 (patch)
tree032dd45d5ddf480ffb0d49708d8e3a47d75c4d1b /spec
parent6f0540d9de018f7dc9f474bdc619c8231282f19d (diff)
Añade spec de creación de usuarios
Diffstat (limited to 'spec')
-rw-r--r--spec/requests/authentications_spec.rb2
-rw-r--r--spec/requests/post_user_accounts_spec.rb21
-rw-r--r--spec/support/sessions_helper.rb12
-rw-r--r--spec/support/shared_examples/requests/post_request.rb4
4 files changed, 36 insertions, 3 deletions
diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb
index 71ffa19..3b1da3e 100644
--- a/spec/requests/authentications_spec.rb
+++ b/spec/requests/authentications_spec.rb
@@ -5,7 +5,7 @@ require 'rails_helper'
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(:expected_error_messages) { ['Credenciales incorrectas'] }
let(:desired_error_status) { 401 }
let(:expected_text) { %w[token refresh] }
let(:user) { create(:user_account) }
diff --git a/spec/requests/post_user_accounts_spec.rb b/spec/requests/post_user_accounts_spec.rb
new file mode 100644
index 0000000..4370af8
--- /dev/null
+++ b/spec/requests/post_user_accounts_spec.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe 'POST /api/user_accounts', type: :request do
+ it_behaves_like 'a POST request' do
+ let(:route) { '/api/user_accounts' }
+ let(:expected_error_messages) do
+ ["Password can't be blank", 'Email is invalid', "First name can't be blank", "Last name can't be blank",
+ "Role can't be blank"]
+ end
+ let(:desired_error_status) { 422 }
+ let(:expected_text) { %w[token refresh] }
+ let(:user) { create(:user_account) }
+ let(:params) do
+ { user_account: JSON.parse(user.to_json).except('session_key', 'created_at', 'updated_at', 'id',
+ 'password_digest').merge({ password: user.password }) }
+ end
+ let(:wrong_params) { { user_account: { email: 'notanemail', password: '' } } }
+ end
+end
diff --git a/spec/support/sessions_helper.rb b/spec/support/sessions_helper.rb
new file mode 100644
index 0000000..eeb12d8
--- /dev/null
+++ b/spec/support/sessions_helper.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# Module to generate JWTs for use in specs.
+module SessionsHelper
+ def jwt(user)
+ user_params = { credentials: { email: user.email, password: user.password } }
+ post('/api/authenticate', user_params.to_json)
+ JSON.parse(response.body)['token']
+ end
+end \ No newline at end of file
diff --git a/spec/support/shared_examples/requests/post_request.rb b/spec/support/shared_examples/requests/post_request.rb
index 6ea2a86..a253281 100644
--- a/spec/support/shared_examples/requests/post_request.rb
+++ b/spec/support/shared_examples/requests/post_request.rb
@@ -11,9 +11,9 @@ RSpec.shared_examples 'a POST request' do
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:)
+ post(route, params: wrong_params.to_json, headers:)
expect(response).to have_http_status(desired_error_status)
- expect(response.body).to include(expected_error_message)
+ expect(response.body).to include_strings(expected_error_messages)
end
end
end