From fd6be349746b85c38c1fe3b2f188dff46b02b862 Mon Sep 17 00:00:00 2001 From: Leonardo Mosquera Date: Thu, 16 Jun 2022 13:12:56 -0300 Subject: [PATCH] FIX: don't send nil user.name for MS Teams (#131) Because Teams rejects the request for having a JSON `null` where a string is expected. --- .../provider/teams/teams_provider.rb | 7 ++++++- .../provider/teams/teams_provider_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/discourse_chat_integration/provider/teams/teams_provider.rb b/lib/discourse_chat_integration/provider/teams/teams_provider.rb index 267719a..1ad42d7 100644 --- a/lib/discourse_chat_integration/provider/teams/teams_provider.rb +++ b/lib/discourse_chat_integration/provider/teams/teams_provider.rb @@ -32,7 +32,12 @@ module DiscourseChatIntegration::Provider::TeamsProvider def self.get_message(post) display_name = "@#{post.user.username}" - full_name = SiteSetting.enable_names ? post.user.name : "" + full_name = + if SiteSetting.enable_names && post.user.name.present? + post.user.name + else + "" + end topic = post.topic diff --git a/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb b/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb index 4ea45e4..450b368 100644 --- a/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb +++ b/spec/lib/discourse_chat_integration/provider/teams/teams_provider_spec.rb @@ -25,6 +25,18 @@ RSpec.describe DiscourseChatIntegration::Provider::TeamsProvider do expect(stub1).to have_been_requested.once end + describe 'with nil user.name' do + before do + post.user.update!(name: nil) + end + + it 'handles nil username correctly' do + message = described_class.get_message(post) + name = message[:sections].first[:facts].first[:name] + expect(name).to eq("") + end + end + end end