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

[Add]: set up flow to add lms to course #41

Merged
merged 34 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
16f6a26
Add models for course_to_lms
Zzz212zzZ Apr 19, 2024
f48fa29
Implement creating association in course_to_lms
Zzz212zzZ Apr 19, 2024
bb98d3d
Test for creating association in course_to_lms
Zzz212zzZ Apr 19, 2024
03264c7
added space in schema for canvas data
ekandell Apr 20, 2024
99ff5e0
Replace the external_course_id from courses table to course_to_lmss t…
Zzz212zzZ Apr 20, 2024
5b4a9e9
Merge branch 'add-canvas-data-to-schema' of https://github.com/cs169/…
Zzz212zzZ Apr 20, 2024
08ed513
Merge branch 'main' of https://github.com/cs169/flextensions into 187…
Zzz212zzZ Apr 20, 2024
6b6f81f
Take external_course_id into consideration
Zzz212zzZ Apr 20, 2024
528292d
test coverage badge update
Apr 12, 2024
b945975
test coverage badge update 2
Apr 12, 2024
b6c16d4
code climate yml file updated
Apr 15, 2024
6f4a173
code climate yml file updated 2
Apr 15, 2024
58108dc
badge fix to linux
Apr 15, 2024
3d49687
added codeclimate test reporter
Apr 18, 2024
ac4ab61
added gems
Apr 18, 2024
c6f0b1a
simple cov version downgraded
Apr 18, 2024
4f94ecd
simplecov v17
Apr 20, 2024
4768889
secret added
Apr 20, 2024
7f47c45
secret added2
Apr 20, 2024
fa42bbd
secret added3
Apr 20, 2024
8c59962
secret added4
Apr 20, 2024
cb90e99
initial changes to create user
Apr 22, 2024
98c5a52
drafted
Apr 22, 2024
4ee7e63
validation email added
Apr 22, 2024
1c03ef8
chnages added: new+save, has many asso for extension
Apr 23, 2024
69eaa49
Merge latest main into feature branch and resolve conflict
Zzz212zzZ Apr 23, 2024
3ba246b
Refactor the controller and add integer validation
Zzz212zzZ Apr 24, 2024
b4216b9
Pull from main and resolve conflict
Zzz212zzZ Apr 24, 2024
cfca6d8
Fix potential issue in lmss_controller
Zzz212zzZ Apr 25, 2024
de66102
Merge conflict from main branch
Zzz212zzZ Apr 25, 2024
adc3bc1
Add validation check for lms
Zzz212zzZ Apr 26, 2024
e94a196
Use validation helper
Zzz212zzZ Apr 26, 2024
0ef28f3
Merge branch 'main' of https://github.com/cs169/flextensions into 187…
Zzz212zzZ Apr 28, 2024
c692f6c
Amend minor comments
Zzz212zzZ Apr 28, 2024
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
70 changes: 54 additions & 16 deletions app/controllers/api/v1/lmss_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
module Api
module V1
class LmssController < BaseController
before_action :validate_name!, only: [:create]

def create
render :json => 'not yet implemented'.to_json, status: 501
end
before_action :validate_ids!, only: [:create]

def index
render :json => 'not yet implemented'.to_json, status: 501
render json: 'not yet implemented', status: 501
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
end

def destroy
render :json => 'not yet implemented'.to_json, status: 501
render json: 'not yet implemented', status: 501
end

# POST /courses/:course_id/lmss
def create
course_id = params[:course_id]
lms_id = params[:lms_id]
external_course_id = params[:external_course_id]

# Ensure that the course and LMS exist
unless Course.exists?(course_id)
render json: { error: 'Course not found' }, status: :not_found
return
end
unless Lms.exists?(lms_id)
render json: { error: 'Lms not found' }, status: :not_found
return
end
# Ensure that the association does not already exist
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
existing_entry = CourseToLms.find_by(course_id: course_id, lms_id: lms_id, external_course_id: external_course_id)
if existing_entry
render json: { message: 'The association between the specified course and LMS already exists.' }, status: :ok
return
end
# Create the association
course_to_lms = CourseToLms.new(
course_id: course_id,
lms_id: lms_id,
external_course_id: external_course_id
)

if course_to_lms.save
render json: course_to_lms, status: :created
else
render json: course_to_lms.errors, status: :unprocessable_entity
end
end

##
# Validator definitions.
# TODO: this should be exported to its own (validator) class.
# TODO: this validation should also check the config file for the name of lms's.
#
def validate_name!
if params['name'].blank?
render :json => 'name parameter is required', status: 401
private

# Improved validate_ids! to include integer validation
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
def validate_ids!
required_params = [:course_id, :lms_id, :external_course_id]
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
missing_params = required_params.select { |param| params[param].blank? }

if missing_params.any?
render json: { error: 'course_id and lms_id are required' }, status: :bad_request
return
end

unless params[:course_id].to_s.match?(/\A\d+\z/) && params[:lms_id].to_s.match?(/\A\d+\z/)
Zzz212zzZ marked this conversation as resolved.
Show resolved Hide resolved
render json: { error: 'course_id and lms_id must be integers' }, status: :bad_request
return
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/course_to_lms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class CourseToLms < ApplicationRecord
# Validations
validates :course_id, presence: true
validates :lms_id, presence: true
end
end
5 changes: 5 additions & 0 deletions app/models/lms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# app/models/lms.rb
class Lms < ApplicationRecord


end
97 changes: 82 additions & 15 deletions spec/controllers/api/v1/lmss_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,93 @@
module Api
module V1
describe LmssController do
let(:mock_course_id) { 16 }
let(:mock_course_name) { 'testCourseName' }
describe 'create' do
it 'throws a 501 error' do
post :create, params: {
course_id: :mock_course_id,
name: :mock_course_name,
}
expect(response.status).to eq(501)
def json_response
JSON.parse(response.body)
end

before do
# Manually create a course and LMS in the database
@course = Course.create!(course_name: "Mock CS169 Course")
@lms = Lms.create!(lms_name: "Mock Canvas", use_auth_token: true)
@external_course_id = "mock_external_course_id"
end

after do
# Clean up the specifically created data
CourseToLms.delete_all
Course.delete_all
Lms.delete_all
end

describe 'POST #create' do

context 'when course_id and lms_id are not integers' do
it 'returns status :bad_request' do
post :create, params: { course_id: 'not_an_int', lms_id: 'also_not_an_int', external_course_id: @external_course_id }
expect(response).to have_http_status(:bad_request)
expect(response.body).to include('course_id and lms_id must be integers')
end
end

context 'when lms_id is missing' do
it 'returns status :bad_request' do
post :create, params: { course_id: @course.id, external_course_id: @external_course_id}
expect(response).to have_http_status(:bad_request)
expect(response.body).to include('course_id and lms_id are required')
end
end

it 'throws a 401 error if the name is not specified' do
post :create, params: {
course_id: :mock_course_id,
}
expect(response.status).to eq(401)
expect(response.body).to eq('name parameter is required')
context 'when valid parameters are provided' do
it 'creates a new course_to_lms association and returns status :created' do
post :create, params: { course_id: @course.id, lms_id: @lms.id, external_course_id: @external_course_id}
expect(response).to have_http_status(:created)
expect(json_response['course_id']).to eq(@course.id)
expect(json_response['lms_id']).to eq(@lms.id)
end
end
context 'when course_to_lms fails to save' do
it 'returns status :unprocessable_entity' do
allow_any_instance_of(CourseToLms).to receive(:save).and_return(false)
post :create, params: { course_id: @course.id, lms_id: @lms.id, external_course_id: @external_course_id}
expect(response).to have_http_status(:unprocessable_entity)
end
end

context 'when course does not exist' do
it 'returns status :not_found' do
# Ensure that the course does not exist
selected_course = Course.find_by(id: @course.id)
selected_course.destroy if selected_course
post :create, params: { course_id: @course.id, lms_id: @lms.id, external_course_id: @external_course_id}
expect(response).to have_http_status(:not_found)
expect(response.body).to include('Course not found')
end
end

context 'when lms does not exist' do
it 'returns status :not_found' do
# Ensure that the LMS does not exist
selected_lms = Lms.find_by(id: @lms.id)
selected_lms.destroy if selected_lms

post :create, params: { course_id: @course.id, lms_id: 1, external_course_id: @external_course_id}
expect(response).to have_http_status(:not_found)
expect(response.body).to include('Lms not found')
end
end

context 'when the association already exists' do
it 'returns status :ok' do
CourseToLms.create!(course_id: @course.id, lms_id: @lms.id, external_course_id: @external_course_id)
post :create, params: { course_id: @course.id, lms_id: @lms.id, external_course_id: @external_course_id}
expect(response).to have_http_status(:ok)
expect(response.body).to include('The association between the specified course and LMS already exists.')
end
end
end



describe 'index' do
it 'throws a 501 error' do
get :index, params: { course_id: :mock_course_id }
Expand Down
Loading