From bfe078c520a8dd960e1ab59036d88619a3cc3a9c Mon Sep 17 00:00:00 2001 From: Kelv Date: Thu, 5 Oct 2023 13:08:09 +0800 Subject: [PATCH] FIX: call UserBadge.grant with badgeReason in admin interface (#23753) Regression from https://github.com/discourse/discourse/pull/23668 where we stopped passing in `this.badgeReason` to the badge granting function. This PR fixes that and adds a unit test to cover that code path. --- .../addon/controllers/admin-user-badges.js | 7 ++- .../controllers/admin-user-badges-test.js | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js b/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js index 7082b8abb11..8b7e4eef166 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-user-badges.js @@ -73,8 +73,13 @@ export default class AdminUserBadgesController extends Controller { @action performGrantBadge() { - UserBadge.grant(this.selectedBadgeId, this.get("user.username")).then( + UserBadge.grant( + this.selectedBadgeId, + this.get("user.username"), + this.badgeReason + ).then( (newBadge) => { + this.set("badgeReason", ""); this.userBadges.pushObject(newBadge); next(() => { // Update the selected badge ID after the combobox has re-rendered. diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/admin-user-badges-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/admin-user-badges-test.js index e78d97eddf7..c7f452b7b1c 100644 --- a/app/assets/javascripts/discourse/tests/unit/controllers/admin-user-badges-test.js +++ b/app/assets/javascripts/discourse/tests/unit/controllers/admin-user-badges-test.js @@ -1,6 +1,9 @@ import { module, test } from "qunit"; import { setupTest } from "ember-qunit"; import Badge from "discourse/models/badge"; +import UserBadge from "discourse/models/user-badge"; +import { settled } from "@ember/test-helpers"; +import sinon from "sinon"; module("Unit | Controller | admin-user-badges", function (hooks) { setupTest(hooks); @@ -58,4 +61,55 @@ module("Unit | Controller | admin-user-badges", function (hooks) { ); assert.deepEqual(badgeNames, sortedNames, "sorts badges by name"); }); + + test("performGrantBadge", async function (assert) { + const GrantBadgeStub = sinon.stub(UserBadge, "grant"); + const controller = this.owner.lookup("controller:admin-user-badges"); + const store = this.owner.lookup("service:store"); + + const badgeToGrant = store.createRecord("badge", { + id: 3, + name: "Granted Badge", + enabled: true, + manually_grantable: true, + }); + + const otherBadge = store.createRecord("badge", { + id: 4, + name: "Other Badge", + enabled: true, + manually_grantable: true, + }); + + const badgeReason = "Test Reason"; + + const user = { username: "jb", name: "jack black", id: 42 }; + + controller.setProperties({ + model: [], + adminUser: { model: user }, + badgeReason, + selectedBadgeId: badgeToGrant.id, + badges: [badgeToGrant, otherBadge], + }); + + const newUserBadge = store.createRecord("badge", { + id: 88, + badge_id: badgeToGrant.id, + user_id: user.id, + }); + + GrantBadgeStub.returns(Promise.resolve(newUserBadge)); + controller.performGrantBadge(); + await settled(); + + assert.ok( + GrantBadgeStub.calledWith(badgeToGrant.id, user.username, badgeReason) + ); + + assert.equal(controller.badgeReason, ""); + assert.equal(controller.userBadges.length, 1); + assert.equal(controller.userBadges[0].id, newUserBadge.id); + assert.equal(controller.selectedBadgeId, otherBadge.id); + }); });