Skip to content

Fix authorization in Api::ProjectsController #553

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/api/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class ProjectsController < ApiController
before_action :authorize_user, only: %i[create update index destroy]
before_action :load_project, only: %i[show update destroy show_context]
before_action :load_projects, only: %i[index]
load_and_authorize_resource
load_resource only: :create
authorize_resource
before_action :verify_lesson_belongs_to_school, only: :create
after_action :pagination_link_header, only: %i[index]

Expand Down Expand Up @@ -73,6 +74,7 @@ def load_project
else
project_loader.load
end
raise ActiveRecord::RecordNotFound if @project.blank?
end

def load_projects
Expand Down
48 changes: 36 additions & 12 deletions spec/features/project/updating_a_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
require 'rails_helper'

RSpec.describe 'Updating a project', type: :request do
before do
authenticated_in_hydra_as(owner)

create(:component, project:, name: 'main', extension: 'py', content: 'print("hi")')
end

let(:headers) { { Authorization: UserProfileMock::TOKEN } }
let!(:project) { create(:project, name: 'Test Project', user_id: owner.id) }
let(:locale) { 'en' }
let!(:project) { create(:project, name: 'Test Project', user_id: owner.id, locale:) }
let(:owner) { create(:owner, school:) }
let(:school) { create(:school) }

Expand All @@ -25,32 +20,61 @@
}
end

before do
authenticated_in_hydra_as(owner)

create(:component, project:, name: 'main', extension: 'py', content: 'print("hi")')
end

it 'responds 200 OK' do
put("/api/projects/#{project.id}", headers:, params:)
put("/api/projects/#{project.identifier}", headers:, params:)
expect(response).to have_http_status(:ok)
end

it 'responds with the project JSON' do
put("/api/projects/#{project.id}", headers:, params:)
put("/api/projects/#{project.identifier}", headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)

expect(data[:name]).to eq('New Name')
end

it 'responds with the components JSON' do
put("/api/projects/#{project.id}", headers:, params:)
put("/api/projects/#{project.identifier}", headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)

expect(data[:components].first[:content]).to eq('print("hello")')
end

it 'responds 422 Unprocessable Entity when params are invalid' do
put("/api/projects/#{project.id}", headers:, params: { project: { components: [{ name: ' ' }] } })
put("/api/projects/#{project.identifier}", headers:, params: { project: { components: [{ name: ' ' }] } })
expect(response).to have_http_status(:unprocessable_entity)
end

it 'responds 401 Unauthorized when no token is given' do
put("/api/projects/#{project.id}", params:)
put("/api/projects/#{project.identifier}", params:)
expect(response).to have_http_status(:unauthorized)
end

context 'when locale is nil, i.e. the other fallback locale in ProjectLoader' do
let(:locale) { nil }

it 'responds 200 OK even though no locale is specified in query string' do
put("/api/projects/#{project.identifier}", headers:, params:)
expect(response).to have_http_status(:ok)
end
end

context "when locale is 'fr', i.e. not a fallback locale in ProjectLoader" do
let(:locale) { 'fr' }

it 'responds 200 OK if locale is specified in query string' do
put("/api/projects/#{project.identifier}?locale=fr", headers:, params:)
expect(response).to have_http_status(:ok)
end

it 'responds 404 Not Found if locale is not specified in query string' do
put("/api/projects/#{project.identifier}", headers:, params:)
expect(response).to have_http_status(:not_found)
end
end
end