Skip to content

Commit

Permalink
feat(Devise-related endpoints): Tests and re-generate JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
juanarias93 committed Nov 11, 2023
1 parent 88c8a4c commit 91a0045
Show file tree
Hide file tree
Showing 5 changed files with 614 additions and 96 deletions.
19 changes: 19 additions & 0 deletions api/spec/api/users/confirmations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'swagger_helper'

RSpec.describe 'users::confirmations', type: :request do
# Show
path '/users/confirmation' do
get 'Finds a user with their confirmation token' do
tags 'Devise'
produces 'application/json'
parameter name: :confirmation_token, in: :query, type: :string, value: 'Token'

response '302', 'Found' do
let(:user) { create :user, :with_confirmation_token }
let(:confirmation_token) { user.confirmation_token }

run_test!
end
end
end
end
84 changes: 84 additions & 0 deletions api/spec/api/users/passwords_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'swagger_helper'

RSpec.describe 'users::passwords', type: :request do
# Create
path '/users/password' do
post 'Creates a recover password request' do
tags 'Devise'
consumes 'application/json'
produces 'application/json'
parameter name: :user, in: :body, schema: {
type: :object,
properties: {
email: { type: :string }
},
required: ['user']
}

response '200', 'Success' do
let!(:user_for_reset_password) { create :user }
let(:email) { user_for_reset_password.email }
let(:user) { { user: { email: } } }

generate_swagger_response('create_password_users')
run_test! do |response|
json_response = JSON.parse(response.body)

expect(json_response['message'])
.to include('Si tu correo electrónico existe en nuestra base de datos')
end
end
end
end

# Update
path '/users/password' do
patch 'Updates a user password' do
tags 'Devise'
consumes 'application/json'
produces 'application/json'
parameter name: :user, in: :body, schema: {
type: :object,
properties: {
reset_password_token: { type: :string },
email: { type: :string },
password: { type: :string },
password_confirmation: { type: :string }
},
required: ['user']
}

response '200', 'Success' do
let(:token) { Devise.token_generator.generate(User, :reset_password_token) }
let(:raw_token) { token.first }
let(:hashed_token) { token.last }

let!(:user_for_reset_password) do
create :user, reset_password_token: hashed_token, reset_password_sent_at: Time.now.utc
end

let(:reset_password_token) { raw_token }
let(:email) { user_for_reset_password.email }
let(:password) { 'NewPassword#123' }
let(:password_confirmation) { 'NewPassword#123' }
let(:user) do
{
user: {
reset_password_token:,
email:,
password:,
password_confirmation:
}
}
end

generate_swagger_response('update_password_users')
run_test! do |response|
json_response = JSON.parse(response.body)

expect(json_response['message']).to include('Contraseña reestablecida correctamente')
end
end
end
end
end
37 changes: 37 additions & 0 deletions api/spec/api/users/registrations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'swagger_helper'

RSpec.describe 'users::registrations', type: :request do
# Signup
path '/users/signup' do
post 'Signup of a new user' do
tags 'Devise'
consumes 'application/json'
produces 'application/json'
parameter name: :user, in: :body, schema: {
type: :object,
properties: {
email: { type: :string },
password: { type: :string },
name: { type: :string }
},
required: ['user']
}

response '200', 'Success' do
let(:email) { '[email protected]' }
let(:password) { 'Test#123' }
let(:name) { 'Test User' }
let(:user) { { user: { email:, password:, name: } } }

generate_swagger_response('signup_users')
run_test! do |response|
json_response = JSON.parse(response.body)

expect(json_response['message']).to include('El usuario fue creado correctamente')
expect(json_response['user']['email']).to eq(email)
expect(json_response['user']['name']).to eq(name)
end
end
end
end
end
75 changes: 75 additions & 0 deletions api/spec/api/users/sessions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'swagger_helper'

RSpec.describe 'users::sessions', type: :request do
# Login
path '/users/login' do
post 'Login of a user' do
tags 'Devise'
consumes 'application/json'
produces 'application/json'
parameter name: :user, in: :body, schema: {
type: :object,
properties: {
email: { type: :string },
password: { type: :string }
},
required: ['user']
}

response '200', 'Success' do
let!(:user_to_login) { create :user }
let(:email) { user_to_login.email }
let(:password) { user_to_login.password }
let(:user) { { user: { email:, password: } } }

generate_swagger_response('login_users')
run_test! do |response|
json_response = JSON.parse(response.body)

expect(json_response['message']).to include('Sesión iniciada correctamente')
expect(json_response['user']['id']).to eq(user_to_login.id)
expect(json_response['user']['email']).to eq(email)
expect(json_response['user']['name']).to eq(user_to_login.name)
end
end
end
end

# Logout
path '/users/logout' do
delete 'Logout of a user' do
tags 'Devise'
consumes 'application/json'
produces 'application/json'
parameter name: 'Authorization', in: :header, type: :string, value: 'Bearer <JWT Token>'
parameter name: :user, in: :body, schema: {
type: :object,
properties: {
email: { type: :string },
password: { type: :string }
},
required: ['user']
}

response '200', 'Success' do
let!(:user_to_logout) { create :user }
let(:email) { user_to_logout.email }
let(:password) { user_to_logout.password }
let(:user) { { user: { email:, password: } } }

let('Authorization') do
post user_session_path, params: { user: { email: user_to_logout.email, password: user_to_logout.password } }

response.headers['Authorization']
end

generate_swagger_response('logout_users')
run_test! do |response|
json_response = JSON.parse(response.body)

expect(json_response['message']).to include('Sesión cerrada correctamente')
end
end
end
end
end
Loading

0 comments on commit 91a0045

Please sign in to comment.