Skip to content

Commit

Permalink
fix(users): fix pb supression des utilisateurs (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lelievre-david authored Dec 17, 2024
1 parent 4201ad3 commit 325570b
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 158 deletions.
12 changes: 6 additions & 6 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api::V1::UsersController < ApiController
def index
users = policy_scope(User)
users = policy_scope(User).kept
authorize users
render json: apply_fetcheable(users).to_blueprint
end
Expand All @@ -21,18 +21,18 @@ def create
if user.save
render json: user.to_blueprint, status: :created
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
render_validation_error(user)
end
end

def destroy
user = policy_scope(User).find(params[:id])
user = policy_scope(User).kept.find(params[:id])
authorize user

if user.destroy
render json: { message: 'User deleted' }, status: :ok
if current_user.discard
head :no_content
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
render_validation_error(user)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Request < ApplicationRecord

# Associations
belongs_to :handler,
class_name: 'Users::Grower',
class_name: 'User',
inverse_of: :handled_requests,
optional: true

Expand Down
17 changes: 10 additions & 7 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ class User < ApplicationRecord
foreign_key: :resource_owner_id,
dependent: :destroy

has_many :handled_requests,
class_name: 'Request',
foreign_key: 'handler_id',
inverse_of: :handler,
dependent: :destroy

# Callbacks
after_discard :anonymize
after_discard :partial_anonymize

# check password_confirmation before using `update_password` method from Clearance::User
#
Expand All @@ -46,16 +52,13 @@ def update_password!(password:, password_confirmation:)
update_password(password)
end

# Private instance methods
private

def anonymize
def partial_anonymize
self.email = "anonymized_#{id}"
self.encrypted_password = 'anonymized'
self.confirmation_token = nil
self.remember_token = 'anonymized'
self.first_name = nil
self.last_name = nil

save(validate: false)
end
Expand All @@ -70,8 +73,8 @@ def anonymize
# encrypted_password :string(128) not null
# confirmation_token :string(128)
# remember_token :string(128) not null
# first_name :string
# last_name :string
# first_name :string not null
# last_name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# discarded_at :datetime
Expand Down
32 changes: 0 additions & 32 deletions app/models/users/grower.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def show?
true
end

def create?
def update?
true
end

def update?
def create?
true
end

Expand Down
8 changes: 0 additions & 8 deletions config/locales/enumerize.fr.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
fr:
enumerize:
user:
role:
requester: demandeur
grower: serriste
invite:
role:
requester: demandeur
grower: serriste
pot:
shape:
square: carré
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20241217133422_first_name_last_name_not_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class FirstNameLastNameNotNull < ActiveRecord::Migration[7.2]
def up
change_table :users, bulk: true do |t|
t.change :first_name, :string, null: false
t.change :last_name, :string, null: false
end
end

def down
change_table :users, bulk: true do |t|
t.change :first_name, :string, null: true
t.change :last_name, :string, null: true
end
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_12_16_104204) do
ActiveRecord::Schema[7.2].define(version: 2024_12_17_133422) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -152,8 +152,8 @@
t.string "encrypted_password", limit: 128, null: false
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128, null: false
t.string "first_name"
t.string "last_name"
t.string "first_name", null: false
t.string "last_name", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.datetime "discarded_at", precision: nil
Expand Down
4 changes: 2 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
confidential: false
)

Users::Grower.create!(
User.create!(
email: '[email protected]',
password: 'password',
password_confirmation: 'password',
Expand Down Expand Up @@ -130,7 +130,7 @@
requester_last_name: 'Doe',
requester_email: '[email protected]',
laboratory: 'My lab',
handler: Users::Grower.first,
handler: User.first,
plant_stage: Plant.first.plant_stages.last,
name: 'My first request',
plant_name: Plant.first.name,
Expand Down
Binary file modified erd.pdf
Binary file not shown.
8 changes: 5 additions & 3 deletions spec/acceptance/api/v1/me_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'

let!(:user) { users(:user1) }
let!(:user) { users(:user1) }
let!(:user_last_name) { user.last_name }
let!(:user_first_name) { user.first_name }
let!(:user_token) { Doorkeeper::AccessToken.create!(resource_owner_id: user.id) }

get '/api/v1/me' do
Expand Down Expand Up @@ -59,8 +61,8 @@
expect(user.discarded?).to be(true)
expect(user.email).to eq('anonymized_1')
expect(user.encrypted_password).to eq('anonymized')
expect(user.first_name).to be_nil
expect(user.last_name).to be_nil
expect(user.first_name).to eq(user_first_name)
expect(user.last_name).to eq(user_last_name)
end
end
end
7 changes: 3 additions & 4 deletions spec/acceptance/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

expect(status).to eq(200)

expect(response_body).to eq(User.to_blueprint)
expect(JSON.parse(response_body).count).to eq(User.count)
expect(response_body).to eq(User.kept.to_blueprint)
expect(JSON.parse(response_body).count).to eq(User.kept.count)
end
end

Expand Down Expand Up @@ -101,8 +101,7 @@

do_request

expect(status).to eq(200)
expect(JSON.parse(response_body)['message']).to eq('User deleted')
expect(status).to eq(204)
end
end
end
12 changes: 5 additions & 7 deletions spec/requests/api/v1/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
expect(response.parsed_body.count).to eq(2)
expect(response.headers['Pagination-Current-Page']).to eq(1)
expect(response.headers['Pagination-Per']).to eq(2)
expect(response.headers['Pagination-Total-Pages']).to eq(2)
expect(response.headers['Pagination-Total-Count']).to eq(3)
expect(response.headers['Pagination-Total-Pages']).to eq(1)
expect(response.headers['Pagination-Total-Count']).to eq(2)
end
end
end
Expand All @@ -33,7 +33,7 @@
describe 'GET api/v1/users/:id' do
context 'when 404' do
it_behaves_like 'with authenticated grower' do
it 'gets a user' do
it 'cannot get a user' do
get(
'/api/v1/users/0',
headers:
Expand All @@ -45,7 +45,7 @@
end

context 'when 401' do
it 'gets a user' do
it 'cannot get a user' do
get(
'/api/v1/users/0',
headers:
Expand Down Expand Up @@ -127,13 +127,11 @@
context 'when 422' do
it_behaves_like 'with authenticated grower' do
it 'fails to delete user' do
allow_any_instance_of(User).to receive(:destroy).and_return(false)

allow_any_instance_of(User).to receive(:discard).and_return(false)
delete(
'/api/v1/users/1',
headers:
)

expect(status).to eq(422)
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ user3:
encrypted_password: anonymized
confirmation_token:
remember_token: anonymized
last_name:
first_name:
last_name: Anonymized
first_name: Anonymized
created_at: !ruby/object:ActiveSupport::TimeWithZone
utc: &1 2019-10-31 15:40:41.545001000 Z
zone: &2 !ruby/object:ActiveSupport::TimeZone
Expand All @@ -66,8 +66,8 @@ user3:
# encrypted_password :string(128) not null
# confirmation_token :string(128)
# remember_token :string(128) not null
# first_name :string
# last_name :string
# first_name :string not null
# last_name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# discarded_at :datetime
Expand Down
53 changes: 48 additions & 5 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,52 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
# Setup
def setup
@user = users(:user2)
end

# Validations
test 'valid user' do
assert @user.valid?, @user.errors.messages
end

test 'invalid without email' do
@user.email = nil
assert_not @user.valid?
assert_not_empty @user.errors[:email]
end

test 'invalid with existing email' do
@user.email = users(:user1).email
assert_not @user.valid?
assert_not_empty @user.errors[:email]
end

test 'invalid with incorrect email value' do
@user.email = 'foo'
assert_not @user.valid?
assert_not_empty @user.errors[:email]
end

test 'invalid without password' do
@user.password = nil
@user.encrypted_password = nil
assert_not @user.valid?
assert_not_empty @user.errors[:password]
end

test 'invalid without first_name' do
@user.first_name = nil
assert_not @user.valid?
assert_not_empty @user.errors[:first_name]
end

test 'invalid without last_name' do
@user.last_name = nil
assert_not @user.valid?
assert_not_empty @user.errors[:last_name]
end
end

# == Schema Information
Expand All @@ -17,8 +60,8 @@ class UserTest < ActiveSupport::TestCase
# encrypted_password :string(128) not null
# confirmation_token :string(128)
# remember_token :string(128) not null
# first_name :string
# last_name :string
# first_name :string not null
# last_name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# discarded_at :datetime
Expand Down
Loading

0 comments on commit 325570b

Please sign in to comment.