From 5d3e5d93129eabc8861d3a448bb217014074d7f7 Mon Sep 17 00:00:00 2001 From: nerix Date: Tue, 20 Jun 2023 19:42:29 +0200 Subject: [PATCH] Fix anonymous users being pinged by `justinfan64537` (#4698) Co-authored-by: Rasmus Karlsson --- CHANGELOG.md | 1 + .../highlights/HighlightController.cpp | 3 +- tests/src/HighlightController.cpp | 87 ++++++++++++++----- 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de429d51..955c3776 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Bugfix: Fix spacing issue with mentions inside RTL text. (#4677) - Bugfix: Fixed a crash when opening and closing a reply thread and switching the user. (#4675) - Bugfix: Fix visual glitches with smooth scrolling. (#4501) +- Bugfix: Fixed pings firing for the "Your username" highlight when not signed in. (#4698) - Dev: Added command to set Qt's logging filter/rules at runtime (`/c2-set-logging-rules`). (#4637) - Dev: Added the ability to see & load custom themes from the Themes directory. No stable promises are made of this feature, changes might be made that breaks custom themes without notice. (#4570) - Dev: Added test cases for emote and tab completion. (#4644) diff --git a/src/controllers/highlights/HighlightController.cpp b/src/controllers/highlights/HighlightController.cpp index ae85fdea..8f7958dc 100644 --- a/src/controllers/highlights/HighlightController.cpp +++ b/src/controllers/highlights/HighlightController.cpp @@ -186,7 +186,8 @@ void rebuildMessageHighlights(Settings &settings, auto currentUser = getIApp()->getAccounts()->twitch.getCurrent(); QString currentUsername = currentUser->getUserName(); - if (settings.enableSelfHighlight && !currentUsername.isEmpty()) + if (settings.enableSelfHighlight && !currentUsername.isEmpty() && + !currentUser->isAnon()) { HighlightPhrase highlight( currentUsername, settings.showSelfHighlightInMentions, diff --git a/tests/src/HighlightController.cpp b/tests/src/HighlightController.cpp index c1a7235d..c129ecfd 100644 --- a/tests/src/HighlightController.cpp +++ b/tests/src/HighlightController.cpp @@ -52,7 +52,7 @@ public: } // namespace -static QString DEFAULT_SETTINGS = R"!( +static QString SETTINGS_DEFAULT = R"!( { "accounts": { "uid117166826": { @@ -157,6 +157,10 @@ static QString DEFAULT_SETTINGS = R"!( } })!"; +static QString SETTINGS_ANON_EMPTY = R"!( +{ +})!"; + struct TestCase { // TODO: create one of these from a raw irc message? hmm xD struct { @@ -176,14 +180,14 @@ struct TestCase { class HighlightControllerTest : public ::testing::Test { protected: - void SetUp() override + void configure(const QString &settings, bool isAnon) { // Write default settings to the mock settings json file this->settingsDir_ = std::make_unique(); QFile settingsFile(this->settingsDir_->filePath("settings.json")); ASSERT_TRUE(settingsFile.open(QIODevice::WriteOnly | QIODevice::Text)); - ASSERT_GT(settingsFile.write(DEFAULT_SETTINGS.toUtf8()), 0); + ASSERT_GT(settingsFile.write(settings.toUtf8()), 0); ASSERT_TRUE(settingsFile.flush()); settingsFile.close(); @@ -192,7 +196,7 @@ protected: initializeHelix(this->mockHelix); EXPECT_CALL(*this->mockHelix, loadBlocks).Times(Exactly(1)); - EXPECT_CALL(*this->mockHelix, update).Times(Exactly(1)); + EXPECT_CALL(*this->mockHelix, update).Times(Exactly(isAnon ? 0 : 1)); this->mockApplication = std::make_unique(); this->settings = std::make_unique(this->settingsDir_->path()); @@ -205,6 +209,23 @@ protected: this->controller->initialize(*this->settings, *this->paths); } + void runTests(const std::vector &tests) + { + for (const auto &[input, expected] : tests) + { + auto [isMatch, matchResult] = this->controller->check( + input.args, input.badges, input.senderName, + input.originalMessage, input.flags); + + EXPECT_EQ(isMatch, expected.state) + << qUtf8Printable(input.senderName) << ": " + << qUtf8Printable(input.originalMessage); + EXPECT_EQ(matchResult, expected.result) + << qUtf8Printable(input.senderName) << ": " + << qUtf8Printable(input.originalMessage); + } + } + void TearDown() override { this->mockApplication.reset(); @@ -229,10 +250,10 @@ protected: mock::Helix *mockHelix; }; -TEST_F(HighlightControllerTest, A) +TEST_F(HighlightControllerTest, LoggedInAndConfigured) { - auto currentUser = - this->mockApplication->getAccounts()->twitch.getCurrent(); + configure(SETTINGS_DEFAULT, false); + std::vector tests{ { { @@ -458,17 +479,43 @@ TEST_F(HighlightControllerTest, A) }, }; - for (const auto &[input, expected] : tests) - { - auto [isMatch, matchResult] = - this->controller->check(input.args, input.badges, input.senderName, - input.originalMessage, input.flags); - - EXPECT_EQ(isMatch, expected.state) - << qUtf8Printable(input.senderName) << ": " - << qUtf8Printable(input.originalMessage); - EXPECT_EQ(matchResult, expected.result) - << qUtf8Printable(input.senderName) << ": " - << qUtf8Printable(input.originalMessage); - } + this->runTests(tests); +} + +TEST_F(HighlightControllerTest, AnonEmpty) +{ + configure(SETTINGS_ANON_EMPTY, true); + + std::vector tests{ + { + { + // input + MessageParseArgs{}, // no special args + {}, // no badges + "pajlada2", // sender name + "hello!", // original message + }, + { + // expected + false, // state + HighlightResult::emptyResult(), // result + }, + }, + { + // anonymous default username + { + MessageParseArgs{}, // no special args + {}, // no badges + "pajlada2", // sender name + "justinfan64537", // original message + }, + { + // expected + false, // state + HighlightResult::emptyResult(), // result + }, + }, + }; + + this->runTests(tests); }