Skip to content

Commit

Permalink
pr cleanups and add failure test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Tooyosi committed Feb 7, 2024
1 parent 91d871e commit 121cc3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
15 changes: 8 additions & 7 deletions app/controllers/api/v1/project_preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Api::V1::ProjectPreferencesController < Api::ApiController
extra_schema_actions :update_settings
schema_type :json_schema
before_action :find_upp_for_update_settings, only: [:update_settings]
before_action :find_project, only: [:read_settings]

def read_settings
skip_policy_scope
Expand All @@ -20,7 +21,9 @@ def update_settings
read_and_update_settings_response
end

private
def find_project
@project = Project.find(params[:project_id])
end

def find_upp_for_update_settings
@upp = UserProjectPreference.find_by!(
Expand All @@ -35,7 +38,7 @@ def user_allowed?
end

def read_and_update_settings_response
set_last_modified_header if action_name == "update_settings"
set_last_modified_header if action_name == 'update_settings'

render_json_response
end
Expand All @@ -47,14 +50,12 @@ def set_last_modified_header
end

def render_json_response
if action_name == "update_settings"
preferences = if action_name == 'update_settings'
preferences = UserProjectPreference.where(id: @upp.id)
else
project = Project.find_by!(id: params[:project_id])
preferences = project.user_project_preference.where.not(email_communication: nil)
preferences = @project.user_project_preference.where.not(email_communication: nil)
preferences = params[:user_id].present? ? preferences.where(user_id: params[:user_id]) : preferences
end

end
render(
status: :ok,
json_api: serializer.resource({}, preferences, context)
Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Project < ApplicationRecord
has_many :tutorials
has_many :field_guides, dependent: :destroy
belongs_to :organization
has_many :user_project_preference
has_many :user_project_preference, dependent: :destroy
# uses the activated_state enum on the workflow
has_many :workflows,
-> { where(serialize_with_project: true).active},
Expand Down
20 changes: 16 additions & 4 deletions spec/controllers/api/v1/project_preferences_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
let!(:project) { create(:project, owner: authorized_user) }
let!(:upp) { create(:user_project_preference, project: project) }
let(:run_generic_read) { get :read_settings, params: { project_id: project.id, format: :json } }
let(:run_invalid_project) { get :read_settings, params: { project_id: 500, format: :json } }
let(:unauthorised_user) { create(:user) }
let(:run_unauthorised_user_read) { get :read_settings, params: { project_id: project.id, user_id: unauthorised_user.id, format: :json } }

Expand All @@ -214,27 +215,38 @@
it 'responds with a 200' do
expect(response.status).to eq(200)
end

it 'returns the correct response data' do
json_response = JSON.parse(response.body)
expect(json_response["project_preferences"]).to be_a(Array)
expect(json_response["project_preferences"].count).to eq(1)
end
end

describe 'invalid project' do
before do
default_request user_id: authorized_user.id, scopes: scopes
run_invalid_project
end

it 'responds with a 404' do
expect(response.status).to eq(404)
end
end

describe 'user specific preferences' do
before(:each) do
before do
default_request user_id: unauthorised_user.id, scopes: scopes
run_unauthorised_user_read
end

it 'responds with a 200' do
expect(response.status).to eq(200)
end

it 'returns the correct response data' do
json_response = JSON.parse(response.body)
expect(json_response["project_preferences"].count).to eq(0)
expect(json_response['project_preferences'].count).to eq(0)
end
end
end
Expand Down

0 comments on commit 121cc3f

Please sign in to comment.