-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Implement API to GDPR delete users #3224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Janis4411
wants to merge
1
commit into
main
Choose a base branch
from
jv/SODEV-2997-Implement-API-to-GDPR-delete-users
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class ApiController < ActionController::API | ||
| def verify_token! | ||
| token = request.headers['Authorization']&.remove('Bearer ') | ||
|
|
||
| unless ActiveSupport::SecurityUtils.secure_compare( | ||
| token.to_s, | ||
| ENV.fetch('OPENHPI_API_TOKEN', 'test_token') | ||
| ) | ||
| return head :unauthorized | ||
|
Check warning on line 12 in app/controllers/api/api_controller.rb
|
||
| end | ||
| end | ||
| end | ||
| end | ||
25 changes: 25 additions & 0 deletions
25
app/controllers/api/internal/users/deletions_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| module Internal | ||
| module Users | ||
| class DeletionsController < Api::ApiController | ||
| before_action :verify_token! | ||
|
|
||
| def delete | ||
| user = ExternalUser.where(external_id: params[:user_id], consumer_id: 1).first | ||
|
|
||
| if user.nil? | ||
| return head :not_found | ||
| end | ||
|
|
||
| user.soft_delete! | ||
| head :ok | ||
| rescue StandardError => e | ||
| Rails.logger.error("Failed to delete user #{params[:user_id]}: #{e.message}") | ||
| head :internal_server_error | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'DELETE Users API', type: :request do | ||
| include ActiveJob::TestHelper | ||
|
|
||
| let(:user_id) { '123456' } | ||
| let(:token) { 'test_token' } | ||
| let(:consumer) { create(:consumer, id: 1, name: 'openHPI') } | ||
| let!(:user) { create(:external_user, external_id: user_id, consumer_id: consumer.id, name: 'Test User', email: 'testmail@gmail.com') } | ||
|
|
||
| let(:body) do | ||
| {user_id: user_id}.to_json | ||
| end | ||
|
|
||
| let(:headers) do | ||
| { | ||
| 'Authorization' => "Bearer #{token}", | ||
| 'Content-Type' => 'application/json', | ||
| } | ||
| end | ||
|
|
||
| around do |example| | ||
| original_token = ENV.fetch('OPENHPI_API_TOKEN', nil) | ||
|
|
||
| ENV['OPENHPI_API_TOKEN'] = token | ||
|
|
||
| example.run | ||
|
|
||
| ENV['OPENHPI_API_TOKEN'] = original_token | ||
| end | ||
|
|
||
| before do | ||
| # Ensure job queue is clean | ||
| clear_enqueued_jobs | ||
| clear_performed_jobs | ||
| # Stub request.raw_post to return the exact JSON body for signature verification | ||
| allow_any_instance_of(ActionDispatch::Request).to receive(:raw_post).and_return(body) | ||
| end | ||
|
|
||
| describe 'DELETE /api/internal/users/deleted' do | ||
| context 'with valid token and signature' do | ||
| it 'anonymizes the user name to "Deleted User"' do | ||
| expect { delete '/api/internal/users/deleted', params: {user_id: user_id}, headers: headers } | ||
| .to change { user.reload.name }.to('Deleted User').and change { user.reload.email }.to(nil) | ||
| end | ||
|
|
||
| it 'returns 200 OK' do | ||
| delete '/api/internal/users/deleted', | ||
| params: {user_id: user_id}, | ||
| headers: headers | ||
|
|
||
| expect(response).to have_http_status(:ok) | ||
| end | ||
| end | ||
|
|
||
| context 'with invalid token' do | ||
| it 'returns 401 Unauthorized' do | ||
| invalid_headers = headers.merge('Authorization' => 'Bearer invalid_token') | ||
|
|
||
| delete '/api/internal/users/deleted', | ||
| params: {user_id: user_id}, | ||
| headers: invalid_headers | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
|
|
||
| it 'does not anonymize the user' do | ||
| invalid_headers = headers.merge('Authorization' => 'Bearer invalid_token') | ||
|
|
||
| expect { delete '/api/internal/users/deleted', params: {user_id: user_id}, headers: invalid_headers } | ||
| .not_to change { user.reload.name } | ||
| end | ||
| end | ||
|
|
||
| context 'with missing Authorization header' do | ||
| it 'returns 401 Unauthorized' do | ||
| delete '/api/internal/users/deleted', | ||
| params: {user_id: user_id}, | ||
| headers: { | ||
| 'Content-Type' => 'application/json', | ||
| } | ||
|
|
||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
| end | ||
|
|
||
| context 'when soft_delete! fails' do | ||
| it 'returns 500 Internal Server Error' do | ||
| allow_any_instance_of(ExternalUser).to receive(:soft_delete!).and_raise(StandardError, 'Database error') | ||
|
|
||
| delete '/api/internal/users/deleted', | ||
| params: {user_id: user_id}, | ||
| headers: headers | ||
|
|
||
| expect(response).to have_http_status(:internal_server_error) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The user deletion API endpoint always returns a success status, even if the underlying
user.soft_delete!operation fails to update the database.Severity: HIGH
Suggested Fix
Check the boolean return value of
user.soft_delete!. If it returnsfalse, respond with an appropriate error status, such ashead :unprocessable_entity, instead ofhead :ok. This ensures that failures in the soft-deletion process are correctly reported to the API client.Prompt for AI Agent