mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 01:31:39 -05:00
Update tests for admin flag
This commit is contained in:
parent
66ff0c3bed
commit
2d2eeda9e7
5 changed files with 95 additions and 60 deletions
|
|
@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Admin flag to the database.
|
- Admin flag to the database. Now not only the first user in the system can create new users, but also users with the admin flag set to true. This will make easier introduction of more admin functions in the future.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class ApplicationController < ActionController::Base
|
||||||
def authenticate_admin!
|
def authenticate_admin!
|
||||||
return if current_user.admin?
|
return if current_user.admin?
|
||||||
|
|
||||||
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :unauthorized
|
redirect_to root_path, notice: 'You are not authorized to perform this action.', status: :see_other
|
||||||
end
|
end
|
||||||
|
|
||||||
def authenticate_api_key
|
def authenticate_api_key
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :user do
|
factory :user do
|
||||||
sequence :email do |n|
|
sequence :email do |n|
|
||||||
|
|
@ -5,5 +7,9 @@ FactoryBot.define do
|
||||||
end
|
end
|
||||||
|
|
||||||
password { SecureRandom.hex(8) }
|
password { SecureRandom.hex(8) }
|
||||||
|
|
||||||
|
trait :admin do
|
||||||
|
admin { true }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -17,53 +17,62 @@ RSpec.describe '/settings/background_jobs', type: :request do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when user is authenticated' do
|
context 'when user is authenticated' do
|
||||||
let(:user) { create(:user) }
|
before { sign_in create(:user) }
|
||||||
|
|
||||||
before do
|
context 'when user is not an admin' do
|
||||||
sign_in user
|
it 'redirects to root page' do
|
||||||
end
|
|
||||||
|
|
||||||
describe 'GET /index' do
|
|
||||||
it 'renders a successful response' do
|
|
||||||
get settings_background_jobs_url
|
get settings_background_jobs_url
|
||||||
|
|
||||||
expect(response).to be_successful
|
expect(response).to redirect_to(root_url)
|
||||||
|
expect(flash[:notice]).to eq('You are not authorized to perform this action.')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'POST /create' do
|
context 'when user is an admin' do
|
||||||
let(:params) { { job_name: 'start_reverse_geocoding' } }
|
before { sign_in create(:user, :admin) }
|
||||||
|
|
||||||
context 'with valid parameters' do
|
describe 'GET /index' do
|
||||||
it 'enqueues a new job' do
|
it 'renders a successful response' do
|
||||||
expect do
|
get settings_background_jobs_url
|
||||||
post settings_background_jobs_url, params:
|
|
||||||
end.to have_enqueued_job(EnqueueReverseGeocodingJob)
|
expect(response).to be_successful
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /create' do
|
||||||
|
let(:params) { { job_name: 'start_reverse_geocoding' } }
|
||||||
|
|
||||||
|
context 'with valid parameters' do
|
||||||
|
it 'enqueues a new job' do
|
||||||
|
expect do
|
||||||
|
post settings_background_jobs_url, params:
|
||||||
|
end.to have_enqueued_job(EnqueueReverseGeocodingJob)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to the created settings_background_job' do
|
||||||
|
post(settings_background_jobs_url, params:)
|
||||||
|
|
||||||
|
expect(response).to redirect_to(settings_background_jobs_url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'DELETE /destroy' do
|
||||||
|
it 'clears the Sidekiq queue' do
|
||||||
|
queue = instance_double(Sidekiq::Queue)
|
||||||
|
allow(Sidekiq::Queue).to receive(:new).and_return(queue)
|
||||||
|
|
||||||
|
expect(queue).to receive(:clear)
|
||||||
|
|
||||||
|
delete settings_background_job_url('queue_name')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects to the created settings_background_job' do
|
it 'redirects to the settings_background_jobs list' do
|
||||||
post(settings_background_jobs_url, params:)
|
delete settings_background_job_url('queue_name')
|
||||||
|
|
||||||
expect(response).to redirect_to(settings_background_jobs_url)
|
expect(response).to redirect_to(settings_background_jobs_url)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'DELETE /destroy' do
|
|
||||||
it 'clears the Sidekiq queue' do
|
|
||||||
queue = instance_double(Sidekiq::Queue)
|
|
||||||
allow(Sidekiq::Queue).to receive(:new).and_return(queue)
|
|
||||||
|
|
||||||
expect(queue).to receive(:clear)
|
|
||||||
|
|
||||||
delete settings_background_job_url('queue_name')
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'redirects to the settings_background_jobs list' do
|
|
||||||
delete settings_background_job_url('queue_name')
|
|
||||||
|
|
||||||
expect(response).to redirect_to(settings_background_jobs_url)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,41 +3,61 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe '/settings/users', type: :request do
|
RSpec.describe '/settings/users', type: :request do
|
||||||
before do
|
let(:valid_attributes) { { email: 'user@domain.com' } }
|
||||||
sign_in create(:user)
|
|
||||||
|
context 'when user is not authenticated' do
|
||||||
|
it 'redirects to sign in page' do
|
||||||
|
post settings_users_url, params: { user: valid_attributes }
|
||||||
|
|
||||||
|
expect(response).to redirect_to(new_user_session_url)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'POST /create' do
|
context 'when user is authenticated' do
|
||||||
context 'with valid parameters' do
|
context 'when user is not an admin' do
|
||||||
let(:valid_attributes) { { email: 'user@domain.com' } }
|
before { sign_in create(:user) }
|
||||||
|
|
||||||
it 'creates a new User' do
|
it 'redirects to root page' 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 }
|
post settings_users_url, params: { user: valid_attributes }
|
||||||
|
|
||||||
expect(response).to redirect_to(settings_url)
|
expect(response).to redirect_to(root_url)
|
||||||
expect(flash[:notice]).to eq("User was successfully created, email is #{valid_attributes[:email]}, password is \"password\".")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid parameters' do
|
context 'when user is an admin' do
|
||||||
let(:invalid_attributes) { { email: nil } }
|
before { sign_in create(:user, :admin) }
|
||||||
|
|
||||||
it 'does not create a new User' do
|
describe 'POST /create' do
|
||||||
expect do
|
context 'with valid parameters' do
|
||||||
post settings_users_url, params: { user: invalid_attributes }
|
it 'creates a new User' do
|
||||||
end.to change(User, :count).by(0)
|
expect do
|
||||||
end
|
post settings_users_url, params: { user: valid_attributes }
|
||||||
|
end.to change(User, :count).by(1)
|
||||||
|
end
|
||||||
|
|
||||||
it 'renders a response with 422 status (i.e. to display the "new" template)' do
|
it 'redirects to the created settings_user' do
|
||||||
post settings_users_url, params: { user: invalid_attributes }
|
post settings_users_url, params: { user: valid_attributes }
|
||||||
|
|
||||||
expect(response).to have_http_status(:unprocessable_entity)
|
expect(response).to redirect_to(settings_url)
|
||||||
|
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
|
||||||
|
|
||||||
|
it 'renders a response with 422 status (i.e. to display the "new" template)' do
|
||||||
|
post settings_users_url, params: { user: invalid_attributes }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue