File indexing completed on 2024-12-01 04:35:26
0001 /* 0002 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "rocketchataccountsettingstest.h" 0008 #include "rocketchataccountsettings.h" 0009 #include <QSignalSpy> 0010 #include <QStandardPaths> 0011 #include <QTest> 0012 0013 QTEST_GUILESS_MAIN(RocketChatAccountSettingsTest) 0014 0015 RocketChatAccountSettingsTest::RocketChatAccountSettingsTest(QObject *parent) 0016 : QObject(parent) 0017 { 0018 QStandardPaths::setTestModeEnabled(true); 0019 } 0020 0021 void RocketChatAccountSettingsTest::shouldNotEmitSignalWhenNewServerUrlIsSameAsOld() 0022 { 0023 RocketChatAccountSettings SampleChatAccount; 0024 0025 const QString url = QStringLiteral("some.url.com"); 0026 SampleChatAccount.setServerUrl(url); 0027 0028 QSignalSpy SpyURL(&SampleChatAccount, &RocketChatAccountSettings::serverURLChanged); 0029 SampleChatAccount.setServerUrl(url); 0030 QCOMPARE(SpyURL.count(), 0); 0031 } 0032 0033 void RocketChatAccountSettingsTest::shouldEmitSignalWhenSetServerURLChanged() 0034 { 0035 RocketChatAccountSettings SampleChatAccount; 0036 0037 QSignalSpy SpyURL(&SampleChatAccount, &RocketChatAccountSettings::serverURLChanged); 0038 const QString serverUrlDefault = QStringLiteral("bla bla"); 0039 SampleChatAccount.setServerUrl(serverUrlDefault); 0040 QCOMPARE(SpyURL.count(), 1); 0041 0042 // Test same url 0043 SampleChatAccount.setServerUrl(serverUrlDefault); 0044 QCOMPARE(SpyURL.count(), 1); 0045 0046 // Test Empty url 0047 const QString emptyString; 0048 SampleChatAccount.setServerUrl(emptyString); 0049 QCOMPARE(SpyURL.count(), 2); 0050 0051 SampleChatAccount.setServerUrl(emptyString); 0052 QCOMPARE(SpyURL.count(), 2); 0053 } 0054 0055 void RocketChatAccountSettingsTest::shouldNotEmitSignalWhenNewUsernameIsSameAsOld() 0056 { 0057 RocketChatAccountSettings SampleChat; 0058 0059 const QString username = QStringLiteral("dummyUsername"); 0060 SampleChat.setUserName(username); 0061 0062 QSignalSpy SpyName(&SampleChat, &RocketChatAccountSettings::userNameChanged); 0063 SampleChat.setUserName(username); 0064 QCOMPARE(SpyName.count(), 0); 0065 } 0066 0067 void RocketChatAccountSettingsTest::shouldEmitSignalWhenUserNameChanged() 0068 { 0069 RocketChatAccountSettings SampleChat; 0070 0071 QSignalSpy SpyName(&SampleChat, &RocketChatAccountSettings::userNameChanged); 0072 const QString userNameDefault = QStringLiteral("Donald Knuth"); 0073 SampleChat.setUserName(userNameDefault); 0074 QCOMPARE(SpyName.count(), 1); 0075 0076 // Test same name 0077 SampleChat.setUserName(userNameDefault); 0078 QCOMPARE(SpyName.count(), 1); 0079 0080 // Test empty string 0081 const QString emptyString; 0082 SampleChat.setUserName(emptyString); 0083 QCOMPARE(SpyName.count(), 2); 0084 0085 SampleChat.setUserName(emptyString); 0086 QCOMPARE(SpyName.count(), 2); 0087 } 0088 0089 void RocketChatAccountSettingsTest::shouldEmitSignalWhenUserIDChanged() 0090 { 0091 RocketChatAccountSettings SampleChat1; 0092 0093 QSignalSpy SpyID(&SampleChat1, &RocketChatAccountSettings::userIdChanged); 0094 const QString userId = QStringLiteral("RA15"); 0095 QVERIFY(userId != SampleChat1.userId()); 0096 SampleChat1.setUserId(QStringLiteral("RA15")); 0097 QCOMPARE(SpyID.count(), 1); 0098 } 0099 0100 void RocketChatAccountSettingsTest::shouldEmitSignalWhenLoginStatusChanged() 0101 { 0102 // TODO 0103 } 0104 0105 void RocketChatAccountSettingsTest::shouldLogout() 0106 { 0107 RocketChatAccountSettings SampleChat; 0108 0109 SampleChat.setAuthToken(QStringLiteral("Token305")); 0110 SampleChat.setUserId(QStringLiteral("ECE305")); 0111 SampleChat.setPassword(QStringLiteral("masterPassword")); 0112 // Make sure that values are not null 0113 QVERIFY(!SampleChat.authToken().isEmpty()); 0114 QVERIFY(!SampleChat.userId().isEmpty()); 0115 QVERIFY(!SampleChat.password().isEmpty()); 0116 0117 SampleChat.logout(); 0118 QVERIFY(SampleChat.authToken().isEmpty()); 0119 QVERIFY(SampleChat.userId().isEmpty()); 0120 QVERIFY(!SampleChat.password().isEmpty()); // logout should allow to log back in easily, don't clear the password 0121 } 0122 0123 void RocketChatAccountSettingsTest::shouldSetAccountName() 0124 { 0125 RocketChatAccountSettings sampleChat; 0126 QSignalSpy spy(&sampleChat, &RocketChatAccountSettings::accountNameChanged); 0127 0128 const QString val = QStringLiteral("myAccount#$^56"); 0129 sampleChat.setAccountName(val); 0130 0131 QCOMPARE(val, sampleChat.accountName()); 0132 QCOMPARE(spy.count(), 1); 0133 } 0134 0135 void RocketChatAccountSettingsTest::shouldsetAuthToken() 0136 { 0137 RocketChatAccountSettings sampleChat; 0138 0139 const QString val = QStringLiteral("myAuthToken#$^56"); 0140 sampleChat.setAuthToken(val); 0141 0142 QCOMPARE(val, sampleChat.authToken()); 0143 } 0144 0145 void RocketChatAccountSettingsTest::shouldSetPassword() 0146 { 0147 RocketChatAccountSettings sampleChat; 0148 0149 const QString val = QStringLiteral("myPass#$^56"); 0150 sampleChat.setPassword(val); 0151 0152 QCOMPARE(val, sampleChat.password()); 0153 } 0154 0155 void RocketChatAccountSettingsTest::shouldSetServerUrl() 0156 { 0157 RocketChatAccountSettings sampleChat; 0158 0159 const QString val = QStringLiteral("my.fancy.url"); 0160 sampleChat.setServerUrl(val); 0161 0162 QCOMPARE(val, sampleChat.serverUrl()); 0163 } 0164 0165 void RocketChatAccountSettingsTest::shouldSetUserID() 0166 { 0167 RocketChatAccountSettings sampleChat; 0168 0169 const QString val = QStringLiteral("ECE305"); 0170 sampleChat.setUserId(val); 0171 0172 QCOMPARE(val, sampleChat.userId()); 0173 } 0174 0175 void RocketChatAccountSettingsTest::shouldSetUserName() 0176 { 0177 RocketChatAccountSettings sampleChat; 0178 0179 const QString val = QStringLiteral("Eric Roberts"); 0180 sampleChat.setUserName(val); 0181 0182 QCOMPARE(val, sampleChat.userName()); 0183 } 0184 0185 void RocketChatAccountSettingsTest::shouldHaveDefaultValues() 0186 { 0187 RocketChatAccountSettings chat; 0188 0189 QVERIFY(chat.accountName().isEmpty()); 0190 QVERIFY(chat.authToken().isEmpty()); 0191 QVERIFY(!chat.cacheBasePath().isEmpty()); 0192 QVERIFY(chat.userId().isEmpty()); 0193 QVERIFY(chat.userName().isEmpty()); 0194 QVERIFY(chat.password().isEmpty()); 0195 QVERIFY(!chat.serverUrl().isEmpty()); 0196 QVERIFY(chat.userId().isEmpty()); 0197 QVERIFY(chat.userName().isEmpty()); 0198 QVERIFY(chat.authToken().isEmpty()); 0199 QCOMPARE(chat.serverUrl(), QStringLiteral("open.rocket.chat")); 0200 QVERIFY(chat.twoFactorAuthenticationCode().isEmpty()); 0201 QVERIFY(chat.accountEnabled()); 0202 QVERIFY(chat.displayName().isEmpty()); 0203 } 0204 0205 #include "moc_rocketchataccountsettingstest.cpp"