2024-06-30 06:31:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe '/settings/users', type: :request do
|
2024-07-19 14:37:57 -04:00
|
|
|
let(:valid_attributes) { { email: 'user@domain.com' } }
|
2024-06-30 06:31:21 -04:00
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
context 'when user is not authenticated' do
|
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
|
post settings_users_url, params: { user: valid_attributes }
|
2024-06-30 06:31:21 -04:00
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
expect(response).to redirect_to(new_user_session_url)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when user is authenticated' do
|
|
|
|
|
context 'when user is not an admin' do
|
|
|
|
|
before { sign_in create(:user) }
|
2024-06-30 06:31:21 -04:00
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
it 'redirects to root page' do
|
2024-06-30 06:31:21 -04:00
|
|
|
post settings_users_url, params: { user: valid_attributes }
|
|
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
expect(response).to redirect_to(root_url)
|
2024-06-30 06:31:21 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
context 'when user is an admin' do
|
2024-07-27 07:50:37 -04:00
|
|
|
let!(:admin) { create(:user, :admin) }
|
|
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
describe 'POST /create' do
|
2024-08-14 12:14:53 -04:00
|
|
|
before { sign_in admin }
|
|
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
context 'with valid parameters' do
|
|
|
|
|
it 'creates a new User' do
|
|
|
|
|
expect do
|
|
|
|
|
post settings_users_url, params: { user: valid_attributes }
|
|
|
|
|
end.to change(User, :count).by(1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'redirects to the created settings_user' do
|
|
|
|
|
post settings_users_url, params: { user: valid_attributes }
|
|
|
|
|
|
2024-11-08 15:28:45 -05:00
|
|
|
expect(response).to redirect_to(settings_users_url)
|
2024-07-19 14:37:57 -04:00
|
|
|
expect(flash[:notice]).to eq("User was successfully created, email is #{valid_attributes[:email]}, password is \"password\".")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with invalid parameters' do
|
|
|
|
|
let(:invalid_attributes) { { email: nil } }
|
|
|
|
|
|
|
|
|
|
it 'does not create a new User' do
|
|
|
|
|
expect do
|
|
|
|
|
post settings_users_url, params: { user: invalid_attributes }
|
|
|
|
|
end.to change(User, :count).by(0)
|
|
|
|
|
end
|
2024-06-30 06:31:21 -04:00
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
it 'renders a response with 422 status (i.e. to display the "new" template)' do
|
|
|
|
|
post settings_users_url, params: { user: invalid_attributes }
|
2024-06-30 06:31:21 -04:00
|
|
|
|
2024-07-19 14:37:57 -04:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-30 06:31:21 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|