Skip to content

Commit

Permalink
fixed institutions_spec and questions
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavya1803 committed Jan 23, 2025
1 parent 53a04d8 commit b38870e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/controllers/api/v1/institutions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Api::V1::InstitutionsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :institution_not_found
def action_allowed?
has_role?('Instructor')
end
# GET /institutions
def index
@institutions = Institution.all
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/api/v1/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Api::V1::QuestionsController < ApplicationController

def action_allowed?
has_role?('Instructor')
end
# Index method returns the list of questions JSON object
# GET on /questions
def index
Expand Down
1 change: 1 addition & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Role < ApplicationRecord
SUPER_ADMINISTRATOR = find_by_name('Super Administrator')
end


def super_administrator?
name['Super Administrator']
end
Expand Down
21 changes: 20 additions & 1 deletion spec/requests/api/v1/institution_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
require 'swagger_helper'

require 'json_web_token'
RSpec.describe 'Institutions API', type: :request do
before(:all) do
# Create roles in hierarchy
@super_admin = Role.find_or_create_by(name: 'Super Administrator')
@admin = Role.find_or_create_by(name: 'Administrator', parent_id: @super_admin.id)
@instructor = Role.find_or_create_by(name: 'Instructor', parent_id: @admin.id)
@ta = Role.find_or_create_by(name: 'Teaching Assistant', parent_id: @instructor.id)
@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(:token) { JsonWebToken.encode({id: prof.id}) }
let(:Authorization) { "Bearer #{token}" }
path '/api/v1/institutions' do
get('list institutions') do
tags 'Institutions'
Expand Down
59 changes: 48 additions & 11 deletions spec/requests/api/v1/questions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
require 'swagger_helper'

require 'json_web_token'
# Rspec tests for questions controller
def setup_instructor
role = Role.find_or_create_by(name: 'Instructor', parent_id: nil)
expect(role).to be_present

instructor = Instructor.create!(
name: 'testinstructor',
email: '[email protected]',
full_name: 'Test Instructor',
password: '123456',
role: role
)
expect(instructor).to be_valid

instructor
end
RSpec.describe 'api/v1/questions', type: :request do
before(:all) do
# Create roles in hierarchy

@super_admin = Role.find_or_create_by(name: 'Super Administrator')
@admin = Role.find_or_create_by(name: 'Administrator', parent_id: @super_admin.id)
@instructor = Role.find_or_create_by(name: 'Instructor', parent_id: @admin.id)
@ta = Role.find_or_create_by(name: 'Teaching Assistant', parent_id: @instructor.id)
@student = Role.find_or_create_by(name: 'Student', parent_id: @ta.id)
end

let(:instructor) { setup_instructor }

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(:Authorization) { "Bearer #{token}" }
path '/api/v1/questions' do
# Creation of dummy objects for the test with the help of let statements
let(:role) { Role.create(name: 'Instructor', parent_id: nil, default_page_id: nil) }
let(:instructor) do
role
Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role)
end
#let(:role) { Role.create(name: 'Instructor', parent_id: nil, default_page_id: nil) }

#let(:instructor) do
# role
# Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role)
#end

let(:questionnaire) do
instructor
Expand Down Expand Up @@ -147,7 +184,7 @@

let(:instructor) do
role
Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role)
Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role)
end

let(:questionnaire) do
Expand Down Expand Up @@ -355,7 +392,7 @@

let(:instructor) do
role
Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role)
Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role)
end

let(:questionnaire) do
Expand Down Expand Up @@ -431,7 +468,7 @@

let(:instructor) do
role
Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role)
Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role)
end

let(:questionnaire) do
Expand Down Expand Up @@ -533,7 +570,7 @@

let(:instructor) do
role
Instructor.create(name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor', password: '123456', role: role)
Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role)
end

let(:questionnaire) do
Expand Down

0 comments on commit b38870e

Please sign in to comment.