Skip to content

Commit 75fab8e

Browse files
committed
WIP: Use stricter more idiomatic params parsing
The original version was copied from `Api::ProjectsController#base_params`, but that seems to be an abberation introduced [1] for no obviously discernable reason. [1]: #86
1 parent e8049ce commit 75fab8e

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

app/controllers/api/public_projects_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create
1818
private
1919

2020
def project_params
21-
params.fetch(:project, {}).permit(:identifier, :locale, :project_type, :name)
21+
params.require(:project).permit(:identifier, :locale, :project_type, :name)
2222
end
2323
end
2424
end

app/models/public_project.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class PublicProject # rubocop:disable Lint/EmptyClass
4+
end

spec/features/public_project/creating_a_public_project_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@
3939
)
4040
end
4141

42-
it 'responds 422 Unprocessable Entity when params are invalid' do
42+
it 'responds 400 Bad Request when params are malformed' do
4343
post('/api/public_projects', headers:, params: { project: {} })
44+
expect(response).to have_http_status(:bad_request)
45+
end
46+
47+
it 'responds 422 Unprocessable Entity when params are invalid' do
48+
post('/api/public_projects', headers:, params: { project: { identifier: 'not-empty' } })
4449
expect(response).to have_http_status(:unprocessable_entity)
4550
end
4651

spec/requests/public_projects/create_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RSpec.describe 'Create public project requests' do
66
let(:project) { create(:project) }
77
let(:creator) { build(:user) }
8+
let(:params) { { project: { identifier: 'not-blank' } } }
89

910
context 'when auth is correct' do
1011
let(:headers) { { Authorization: UserProfileMock::TOKEN } }
@@ -19,7 +20,7 @@
1920
end
2021

2122
it 'returns success' do
22-
post('/api/public_projects', headers:)
23+
post('/api/public_projects', headers:, params:)
2324

2425
expect(response).to have_http_status(:created)
2526
end
@@ -35,7 +36,7 @@
3536
end
3637

3738
it 'returns error' do
38-
post('/api/public_projects', headers:)
39+
post('/api/public_projects', headers:, params:)
3940

4041
expect(response).to have_http_status(:unprocessable_entity)
4142
end
@@ -44,7 +45,7 @@
4445

4546
context 'when no token is given' do
4647
it 'returns unauthorized' do
47-
post('/api/public_projects')
48+
post('/api/public_projects', params:)
4849

4950
expect(response).to have_http_status(:unauthorized)
5051
end

0 commit comments

Comments
 (0)