Skip to content
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

Section Create/Destroy API route #6873

Merged
merged 9 commits into from
Dec 18, 2023
Merged
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [unreleased]
- Allow inactive groups in the graders table to be toggled for display (#6778)
- Enable plotly rendering for jupyter notebooks (#6871)
- Added new API routes for the `create` and `destroy` actions of SectionsController (#6873)

## [v2.4.1]
- Internal changes only
Expand Down
35 changes: 35 additions & 0 deletions app/controllers/api/sections_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Api
# Api controller responsible for CRUD operations for the Section model
class SectionsController < MainApiController
def create
section = current_course.sections.new(section_params)
if section.save
render 'shared/http_status',
locals: { code: '201', message: HttpStatusHelper::ERROR_CODE['message']['201'] }, status: :created
else
render 'shared/http_status', locals: { code: '422', message:
HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_entity
end
end

def destroy
@section = record

if @section.has_students?
render 'shared/http_status',
locals: { code: :conflict, message: t('sections.destroy.not_empty') },
status: :conflict
else
@section.assessment_section_properties.each(&:destroy)
@section.destroy
render 'shared/http_status',
locals: { code: '200', message: t('sections.destroy.success') },
status: :ok
end
end

def section_params
params.require(:section).permit(:name)
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
get 'download_entries'
end
end
resources :sections, only: [:create, :destroy]
end
# Return a 404 when no route is match
match '*path', controller: 'main_api', action: 'page_not_found', via: :all
Expand Down
51 changes: 51 additions & 0 deletions spec/controllers/api/sections_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
describe Api::SectionsController do
include AutomatedTestsHelper

let(:section) { create :section }
let(:instructor) { create :instructor }
let(:course) { section.course }

context 'An unauthorized attempt' do
it 'fails to delete section' do
delete :destroy, params: { course_id: course.id, id: section }
expect(response).to have_http_status(403)
expect(course.sections.exists?(section.id)).to be_truthy
end
end

context 'An authorized attempt' do
before :each do
instructor.reset_api_key
request.env['HTTP_AUTHORIZATION'] = "MarkUsAuth #{instructor.api_key.strip}"
end

context 'POST create' do
it 'should create a new section when given the correct params' do
post :create, params: { course_id: course.id, section: { name: 'LEC0301' } }
expect(response).to have_http_status(201)
expect(course.sections.find_by(name: 'LEC0301').name).to eq('LEC0301')
end

it 'should throw a 422 error and not create a section with when given an invalid param' do
post :create, params: { course_id: course.id, section: { name: '' } }
expect(response).to have_http_status(422)
expect(course.sections.find_by(name: '')).to be_nil
end
end

context 'DELETE destroy' do
it 'successfully deletes section' do
delete :destroy, params: { course_id: course.id, id: section }
expect(response).to have_http_status(:ok)
expect(course.sections.exists?(section.id)).to be_falsey
david-yz-liu marked this conversation as resolved.
Show resolved Hide resolved
end
it 'does not delete section because the section is non-empty (has students), with 403 code' do
student = create(:student)
section.students = [student]
delete :destroy, params: { course_id: course.id, id: section }
expect(response).to have_http_status(:conflict)
expect(course.sections.exists?(section.id)).to be_truthy
end
end
end
end