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.
This commit is contained in:
Kelv 2023-10-05 13:08:09 +08:00 committed by GitHub
parent 790f6b1121
commit bfe078c520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 1 deletions

View File

@ -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.

View File

@ -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);
});
});