Skip to content

Commit

Permalink
Fixing authentication in helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmalick committed Jan 22, 2025
1 parent fb7b800 commit 53a04d8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 53 deletions.
16 changes: 8 additions & 8 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Course < ApplicationRecord
validates :name, presence: true
validates :directory_path, presence: true
has_many :ta_mappings, dependent: :destroy
has_many :tas, through: :ta_mappings
has_many :tas, through: :ta_mappings, source: :ta

# Returns the submission directory for the course
def path
Expand All @@ -16,25 +16,25 @@ def path
def add_ta(user)
if user.nil?
return { success: false, message: "The user with id #{user.id} does not exist" }
elsif TaMapping.exists?(ta_id: user.id, course_id: id)
elsif TaMapping.exists?(user_id: user.id, course_id: id)
return { success: false, message: "The user with id #{user.id} is already a TA for this course." }
else
ta_mapping = TaMapping.create(ta_id: user.id, course_id: id)
ta_mapping = TaMapping.create(user_id: user.id, course_id: id)
user.update(role: Role::TEACHING_ASSISTANT)
if ta_mapping.save
return { success: true, data: ta_mapping.slice(:course_id, :ta_id) }
return { success: true, data: ta_mapping.slice(:course_id, :user_id) }
else
return { success: false, message: ta_mapping.errors }
end
end
end

# Removes Teaching Assistant from the course
def remove_ta(ta_id)
ta_mapping = ta_mappings.find_by(ta_id: ta_id, course_id: :id)
def remove_ta(user_id)
ta_mapping = ta_mappings.find_by(user_id: user_id, course_id: :id)
return { success: false, message: "No TA mapping found for the specified course and TA" } if ta_mapping.nil?
ta = User.find(ta_mapping.ta_id)
ta_count = TaMapping.where(ta_id: ta_id).size - 1
ta = User.find(ta_mapping.user_id)
ta_count = TaMapping.where(user_id: user_id).size - 1
if ta_count.zero?
ta.update(role: Role::STUDENT)
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/ta_mapping.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class TaMapping < ApplicationRecord
belongs_to :course
belongs_to :ta
belongs_to :ta, class_name: 'User', foreign_key: 'user_id'

#Returns course ids of the TA
def self.get_course_ids(user_id)
TaMapping.find_by(ta_id: user_id).course_id
TaMapping.find_by(user_id: user_id).course_id
end

#Returns courses of the TA
Expand Down
110 changes: 67 additions & 43 deletions spec/requests/api/v1/courses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,39 @@
@student = Role.find_or_create_by(name: 'Student', parent_id: @ta.id)
end

let(:prof) { User.create(
name: "profa",
password_digest: "password",
role_id: @instructor.id,
full_name: "Prof A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
) }
let(:prof) {
User.create(
name: "profa",
password_digest: "password",
role_id: @instructor.id,
full_name: "Prof A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
)
}

let(:token) { JsonWebToken.encode({id: prof.id}) }
let(:token) { JsonWebToken.encode({ id: prof.id }) }
let(:Authorization) { "Bearer #{token}" }


# GET /courses/{id}/add_ta/{ta_id}
path '/api/v1/courses/{id}/add_ta/{ta_id}' do
parameter name: :id, in: :path, type: :integer, required: true
parameter name: :ta_id, in: :path, type: :integer, required: true
let(:institution) { Institution.create(name: "NC State") }
let(:prof) { User.create(
name: "profa",
password_digest: "password",
role_id: 4,
fullname: "Prof A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
) }
let(:ta) { User.create(
name: "taa",
password_digest: "password",
role_id: 3,
fullname: "TA A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
) }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:ta) {
User.create(
name: "taa",
password_digest: "password",
role_id: @ta.id,
full_name: "TA A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
)
}
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
let(:ta_id) { ta.id }
get('add_ta course') do
Expand Down Expand Up @@ -82,7 +80,10 @@
path '/api/v1/courses/{id}/tas' do
parameter name: 'id', in: :path, type: :string, description: 'id'
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
get('view_tas course') do
tags 'Courses'
Expand All @@ -104,16 +105,21 @@
parameter name: 'id', in: :path, type: :string, description: 'id'
parameter name: 'ta_id', in: :path, type: :string, description: 'ta_id'
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
let(:ta) { User.create(
name: "taa",
password_digest: "password",
role_id: 3,
fullname: "TA A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
) }
let(:ta) {
User.create(
name: "taa",
password_digest: "password",
role_id: @ta.id,
full_name: "TA A",
email: "[email protected]",
mru_directory_path: "/home/testuser",
)
}
let(:ta_id) { ta.id }
let(:ta_mapping) { TaMapping.create(course_id: course.id, ta_id: ta.id) }
get('remove_ta course') do
Expand All @@ -140,7 +146,10 @@
path '/api/v1/courses/{id}/copy' do
parameter name: 'id', in: :path, type: :string, description: 'id'
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
get('copy course') do
tags 'Courses'
Expand Down Expand Up @@ -190,7 +199,10 @@
}
response(201, 'successful') do
let(:institution) { Institution.create(name: "NC State") }
let(:course) { { institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank' } }
let(:course) {
{ institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank' }
}
after do |example|
example.metadata[:response][:content] = {
'application/json' => {
Expand All @@ -207,7 +219,10 @@
path '/api/v1/courses/{id}' do
parameter name: 'id', in: :path, type: :string, description: 'id'
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
get('show course') do
tags 'Courses'
Expand Down Expand Up @@ -239,7 +254,10 @@
required: %w[]
}
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
response(200, 'successful') do
after do |example|
Expand Down Expand Up @@ -269,7 +287,10 @@
required: %w[]
}
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
response(200, 'successful') do
after do |example|
Expand All @@ -287,7 +308,10 @@
delete('delete course') do
tags 'Courses'
let(:institution) { Institution.create(name: "NC State") }
let(:course) { Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD', info: 'blank') }
let(:course) {
Course.create(institution_id: institution.id, instructor_id: prof.id, directory_path: 'samplepath', name: 'OODD',
info: 'blank')
}
let(:id) { course.id }
response(204, 'successful') do
after do |example|
Expand Down

0 comments on commit 53a04d8

Please sign in to comment.