From eac0e496b454e11510f88c12c44ff811427ec31c Mon Sep 17 00:00:00 2001 From: Ethan McElroy Date: Mon, 3 Feb 2025 22:56:20 +0000 Subject: [PATCH] chore: get_feedback_metadata tests --- .../controllers/api/v1/students_controller.rb | 16 +-- .../api/v1/students_controller_spec.rb | 97 +++++++++++++------ 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/rails/app/controllers/api/v1/students_controller.rb b/rails/app/controllers/api/v1/students_controller.rb index 0f093208d..109da173b 100644 --- a/rails/app/controllers/api/v1/students_controller.rb +++ b/rails/app/controllers/api/v1/students_controller.rb @@ -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'] @@ -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 }) diff --git a/rails/spec/controllers/api/v1/students_controller_spec.rb b/rails/spec/controllers/api/v1/students_controller_spec.rb index 2b24c25a8..a92c1ece8 100644 --- a/rails/spec/controllers/api/v1/students_controller_spec.rb +++ b/rails/spec/controllers/api/v1/students_controller_spec.rb @@ -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) } @@ -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