Skip to content

Commit

Permalink
chore: get_feedback_metadata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emcelroy committed Feb 3, 2025
1 parent dcc7e17 commit eac0e49
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
16 changes: 8 additions & 8 deletions rails/app/controllers/api/v1/students_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,6 @@ def remove_from_class
end

def get_feedback_metadata
if !current_user || !current_user.portal_student
return error("You must be a student to use this endpoint")
end

platform_id = APP_CONFIG[:site_url]
platform_student_id = current_user.id

url = ENV['FEEDBACK_METADATA_URL']
source = ENV['FEEDBACK_METADATA_SOURCE']
token = ENV['FEEDBACK_METADATA_BEARER_TOKEN']
Expand All @@ -233,10 +226,17 @@ def get_feedback_metadata
if !source
return error("Feedback metadata source not configured")
end
if !source
if !token
return error("Feedback metadata bearer token not configured")
end

if !current_user || !current_visitor.portal_student
return error("You must be a student to use this endpoint")
end

platform_id = APP_CONFIG[:site_url]
platform_student_id = current_user.id

uri = URI.parse(url)
uri.query = URI.encode_www_form({ source: source, platform_id: platform_id, platform_student_id: platform_student_id })

Expand Down
97 changes: 70 additions & 27 deletions rails/spec/controllers/api/v1/students_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'spec_helper'
require 'uri'

RSpec.describe API::V1::StudentsController, type: :controller do
let(:student) { FactoryBot.create(:full_portal_student) }
Expand Down Expand Up @@ -271,32 +272,74 @@
end
end

# describe '#get_feedback_metadata' do
# it 'should fail without a user' do
# get :get_feedback_metadata
# end

# failures:
# no user
# user not student
# missing
# url = ENV['FEEDBACK_METADATA_URL']
# source = ENV['FEEDBACK_METADATA_SOURCE']
# token = ENV['FEEDBACK_METADATA_BEARER_TOKEN']

# it 'returns the full Firebase function result' do
# stub_const("ENV", ENV.to_h.merge("FEEDBACK_METADATA_URL" => "http://example.com"))
#
# stub = WebMock.stub_request(:get, ENV['FEEDBACK_METADATA_URL'])
# .with{ |request|
# doc = JSON.parse(request.body)["doc"]
# doc["learner_id"] == learner.id && doc["permission_forms_id"] == [permission_form.id] }
# .to_return(status: 200, body: "", headers: {})
#
# get :get_feedback_metadata
#
# expect(response).to have_http_status(:bad_request)
# end
# end
describe '#get_feedback_metadata' do
bearer_token = "abc123"

before :each do
student_user.portal_student = student

stub_const("ENV", ENV.to_h.merge(
"FEEDBACK_METADATA_URL" => "http://example.com",
"FEEDBACK_METADATA_SOURCE" => "authoring.example.org",
"FEEDBACK_METADATA_BEARER_TOKEN" => bearer_token
))
end

it 'should fail without a user' do
get :get_feedback_metadata
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq('{"success":false,"response_type":"ERROR","message":"You must be a student to use this endpoint"}')
end

it 'should fail when the user is not a student' do
sign_in teacher_user
get :get_feedback_metadata
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq('{"success":false,"response_type":"ERROR","message":"You must be a student to use this endpoint"}')
end

it 'should fail when the feedback metadata URL is not configured' do
stub_const("ENV", ENV.to_h.merge("FEEDBACK_METADATA_URL" => nil))
sign_in student_user
get :get_feedback_metadata
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq('{"success":false,"response_type":"ERROR","message":"Feedback metadata URL not configured"}')
end

it 'should fail when the feedback metadata source is not configured' do
stub_const("ENV", ENV.to_h.merge("FEEDBACK_METADATA_SOURCE" => nil))
sign_in student_user
get :get_feedback_metadata
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq('{"success":false,"response_type":"ERROR","message":"Feedback metadata source not configured"}')
end

it 'should fail when the feedback metadata bearer token is not configured' do
stub_const("ENV", ENV.to_h.merge("FEEDBACK_METADATA_BEARER_TOKEN" => nil))
sign_in student_user
get :get_feedback_metadata
expect(response).to have_http_status(:bad_request)
expect(response.body).to eq('{"success":false,"response_type":"ERROR","message":"Feedback metadata bearer token not configured"}')
end

it 'returns feedback metadata for signed-in student' do
sign_in student_user

stubbed_response = { "success": true, "result": { "foo": "bar" } }.to_json
expected_query = URI.encode_www_form(
source: ENV['FEEDBACK_METADATA_SOURCE'],
platform_id: APP_CONFIG[:site_url] || "http://learn.concord.org",
platform_student_id: student_user.id
)
stub_request(:get, ENV['FEEDBACK_METADATA_URL'])
.with(query: expected_query, headers: {"Authorization"=>"Bearer #{bearer_token}"})
.to_return(status: 200, body: stubbed_response, headers: { "Content-Type" => "application/json" })

get :get_feedback_metadata
expect(response).to have_http_status(:ok)
expect(response.body).to eq(stubbed_response)
end

end

end

0 comments on commit eac0e49

Please sign in to comment.