FIX: Make avatar-flair component fail gracefully group info missing (#13398)

FIX: Make avatar-flair component fail gracefully group info missing (#13398)

This can happen when an avatar-flair component is rendered to an anonymous user on a login_required site (e.g. when they are redeeming an invite). The lack of group information was causing an error to be raised. With this commit, it now simple skips rendering the flair.

diff --git a/app/assets/javascripts/discourse/app/lib/avatar-flair.js b/app/assets/javascripts/discourse/app/lib/avatar-flair.js
index 5e6ee0e..3a8bb1f 100644
--- a/app/assets/javascripts/discourse/app/lib/avatar-flair.js
+++ b/app/assets/javascripts/discourse/app/lib/avatar-flair.js
@@ -52,7 +52,7 @@ function initializeAutoGroupFlair(site) {
     "trust_level_3",
     "trust_level_4",
   ].forEach((groupName) => {
-    const group = site.groups.findBy("name", groupName);
+    const group = site.groups?.findBy("name", groupName);
     if (group && group.flair_url) {
       _noAutoFlair = false;
       _autoGroupFlair[groupName] = {
diff --git a/app/assets/javascripts/discourse/tests/integration/components/user-avatar-flair-test.js b/app/assets/javascripts/discourse/tests/integration/components/user-avatar-flair-test.js
index 70f22db..bc457a5 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/user-avatar-flair-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/user-avatar-flair-test.js
@@ -147,6 +147,26 @@ discourseModule(
       },
     });
 
+    componentTest("avatar flair for login-required site, before login", {
+      template: hbs`{{user-avatar-flair user=args}}`,
+      beforeEach() {
+        resetFlair();
+        this.set("args", {
+          admin: false,
+          moderator: false,
+          trust_level: 3,
+        });
+        // Groups not serialized for anon on login_required
+        this.site.groups = undefined;
+      },
+      afterEach() {
+        resetFlair();
+      },
+      test(assert) {
+        assert.ok(!exists(".avatar-flair"), "it does not render a flair");
+      },
+    });
+
     componentTest("avatar flair for primary group flair", {
       template: hbs`{{user-avatar-flair user=args}}`,
       beforeEach() {

GitHub sha: 0f9d31a85e8ec645909afd6e237cfde5f5dd8df8

This commit appears in #13398 which was approved by OsamaSayegh. It was merged by davidtaylorhq.