-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API routes for section create/destroy (#6873)
Co-authored-by: Bruce Liu <[email protected]>
- Loading branch information
1 parent
fb73bac
commit c7e8433
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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 |
This file contains 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 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,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 | ||
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 |