diff --git a/spec/factories/tenant.rb b/spec/factories/tenant.rb index dc795fea..15f43012 100644 --- a/spec/factories/tenant.rb +++ b/spec/factories/tenant.rb @@ -4,5 +4,11 @@ factory :tenant do name { Faker::Creature::Animal.name } secrets { "#{Faker::Crypto.sha256}:#{Faker::Crypto.sha512}" } + lrs_endpoint { nil } + kc_token_url { nil } + kc_client_id { nil } + kc_client_secret { nil } + kc_username { nil } + kc_password { nil } end end diff --git a/spec/requests/bigbluebutton_api_controller_spec.rb b/spec/requests/bigbluebutton_api_controller_spec.rb index 6f2ae5fa..7f769938 100644 --- a/spec/requests/bigbluebutton_api_controller_spec.rb +++ b/spec/requests/bigbluebutton_api_controller_spec.rb @@ -922,6 +922,49 @@ end end end + + context 'secret-lrs-payload' do + before do + tenant.lrs_endpoint = 'https://test.com' + + tenant.save! + end + + it 'makes a call to the LrsPayloadService and sets meta_secret-lrs-payload' do + allow_any_instance_of(LrsPayloadService).to receive(:call).and_return('test-token') + + create_params = { meetingID: "test-meeting-1", moderatorPW: "test-password", voiceBridge: "1234567" } + stub_params = { meetingID: "test-meeting-1", moderatorPW: "test-password", voiceBridge: "1234567", + 'meta_tenant-id': tenant.id, 'meta_secret-lrs-payload': 'test-token' } + + stub_create = stub_request(:get, encode_bbb_uri("create", server.url, server.secret, stub_params)) + .to_return(body: "SUCCESStest-meeting-1 + apmp") + + get bigbluebutton_api_create_url, params: create_params + + response_xml = Nokogiri.XML(response.body) + expect(stub_create).to have_been_requested + expect(response_xml.at_xpath("/response/returncode").text).to eq("SUCCESS") + end + + it 'does not set meta_secret-lrs-payload if the value is nil' do + allow_any_instance_of(LrsPayloadService).to receive(:call).and_return(nil) + + create_params = { meetingID: "test-meeting-1", moderatorPW: "test-password", voiceBridge: "1234567" } + stub_params = { meetingID: "test-meeting-1", moderatorPW: "test-password", voiceBridge: "1234567", 'meta_tenant-id': tenant.id } + + stub_create = stub_request(:get, encode_bbb_uri("create", server.url, server.secret, stub_params)) + .to_return(body: "SUCCESStest-meeting-1 + apmp") + + get bigbluebutton_api_create_url, params: create_params + + response_xml = Nokogiri.XML(response.body) + expect(stub_create).to have_been_requested + expect(response_xml.at_xpath("/response/returncode").text).to eq("SUCCESS") + end + end end end diff --git a/spec/services/lrs_payload_service_spec.rb b/spec/services/lrs_payload_service_spec.rb new file mode 100644 index 00000000..841f47a5 --- /dev/null +++ b/spec/services/lrs_payload_service_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe LrsPayloadService, type: :service do + let!(:tenant) do + create(:tenant, + name: 'bn', + lrs_endpoint: 'https://lrs_endpoint.com', + kc_token_url: 'https://token_url.com/auth/token', + kc_client_id: 'client_id', + kc_client_secret: 'client_secret', + kc_username: 'kc_username', + kc_password: 'kc_password') + end + + describe '#call' do + it 'makes a call to kc_token_url with the correct payload' do + payload = { + client_id: tenant.kc_client_id, + client_secret: tenant.kc_client_secret, + username: tenant.kc_username, + password: tenant.kc_password, + grant_type: 'password' + } + + stub_create = stub_request(:post, tenant.kc_token_url) + .with(body: payload).to_return(body: "kc_access_token") + + described_class.new(tenant: tenant, secret: 'server-secret').call + + expect(stub_create).to have_been_requested + end + + it 'logs a warning and returns nil if kc_token_url returns an error' do + stub_request(:post, tenant.kc_token_url) + .to_return(status: 500, body: 'Internal Server Error', headers: {}) + + expect(Rails.logger).to receive(:warn) + + expect(described_class.new(tenant: tenant, secret: 'server-secret').call).to be_nil + end + end +end