Skip to content

Commit

Permalink
only allow admins to create users
Browse files Browse the repository at this point in the history
  • Loading branch information
lfilmeyer committed Aug 5, 2024
1 parent f2ecd30 commit be02646
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 1 addition & 4 deletions backend/app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# frozen_string_literal: true

class UserPolicy < ApplicationPolicy
attr_reader :user, :record

def create?
# Need to fix authentication issues across all endpoints
true
user&.admin?
end
end
27 changes: 27 additions & 0 deletions backend/spec/requests/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'rails_helper'

RSpec.describe '/admin/users', type: :request do
include Devise::Test::IntegrationHelpers

let(:admin) do
User.create(email: '[email protected]', password: 'password', role: :admin)
end

describe 'POST /create' do
context 'with valid parameters for a surveyor' do
let(:valid_attributes) do
Expand All @@ -21,6 +27,8 @@
}
end

before { sign_in admin }

it 'creates a new user who is a surveyor' do
expect do
post admin_users_url, params: { user: valid_attributes }, as: :json
Expand Down Expand Up @@ -55,6 +63,8 @@
}
end

before { sign_in admin }

it 'creates a new user who is an admin' do
expect do
post admin_users_url, params: { user: valid_attributes }, as: :json
Expand All @@ -79,6 +89,8 @@
}
end

before { sign_in admin }

it 'does not create a new user' do
expect do
post admin_users_url, params: { user: invalid_attributes }, as: :json
Expand All @@ -93,5 +105,20 @@
expect(error_message).to eq("can't be blank")
end
end

context 'as unauthorized user' do
let(:valid_attributes) do
{
email: '[email protected]',
role: 'admin'
}
end

it 'raises an error' do
expect do
post admin_users_url, params: { user: valid_attributes }, as: :json
end.to raise_error(Pundit::NotAuthorizedError)
end
end
end
end

0 comments on commit be02646

Please sign in to comment.