You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginMessageActionTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.setting.ws;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.property.PropertiesDao;
  26. import org.sonar.db.property.PropertyDto;
  27. import org.sonar.server.loginmessage.LoginMessageFeature;
  28. import org.sonar.server.ws.TestResponse;
  29. import org.sonar.server.ws.WsActionTester;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.mockito.Mockito.doReturn;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.when;
  34. import static org.sonar.core.config.WebConstants.SONAR_LOGIN_DISPLAY_MESSAGE;
  35. import static org.sonar.core.config.WebConstants.SONAR_LOGIN_MESSAGE;
  36. public class LoginMessageActionTest {
  37. private final DbClient dbClient = mock(DbClient.class);
  38. private final LoginMessageFeature loginMessageFeature = mock(LoginMessageFeature.class);
  39. private final LoginMessageAction underTest = new LoginMessageAction(dbClient, loginMessageFeature);
  40. private final WsActionTester ws = new WsActionTester(underTest);
  41. private static final String LOGIN_MESSAGE_TEXT = "test link [SonarQube™ Home Page](https://www.sonarsource.com/products/sonarqube)\n* list 1\n* list 2";
  42. private static final String FORMATTED_LOGIN_MESSAGE_TEXT = "test link \\u003ca href\\u003d\\\"https://www.sonarsource.com/products/sonarqube\\\" target\\u003d\\\"_blank\\\" rel\\u003d\\\"noopener noreferrer\\\"\\u003eSonarQube\\u0026trade; Home Page\\u003c/a\\u003e\\u003cbr/\\u003e\\u003cul\\u003e\\u003cli\\u003elist 1\\u003c/li\\u003e\\n\\u003cli\\u003elist 2\\u003c/li\\u003e\\u003c/ul\\u003e";
  43. private static final String JSON_RESPONSE = "{\"message\":\"" + FORMATTED_LOGIN_MESSAGE_TEXT + "\"}";
  44. private static final String EMPTY_JSON_RESPONSE = "{\"message\":\"\"}";
  45. private PropertiesDao propertiesDao;
  46. @Before
  47. public void setup() {
  48. propertiesDao = mock(PropertiesDao.class);
  49. doReturn(true).when(loginMessageFeature).isAvailable();
  50. doReturn(propertiesDao).when(dbClient).propertiesDao();
  51. }
  52. @Test
  53. public void test_definition() {
  54. WebService.Action action = ws.getDef();
  55. assertThat(action.key()).isEqualTo("login_message");
  56. assertThat(action.isPost()).isFalse();
  57. assertThat(action.isInternal()).isTrue();
  58. assertThat(action.params()).isEmpty();
  59. }
  60. @Test
  61. public void returns_login_formatted_message_json() {
  62. mockProperty(SONAR_LOGIN_DISPLAY_MESSAGE, "true");
  63. mockProperty(SONAR_LOGIN_MESSAGE, LOGIN_MESSAGE_TEXT);
  64. TestResponse response = ws.newRequest().execute();
  65. assertThat(response.getInput()).isEqualTo(JSON_RESPONSE);
  66. }
  67. @Test
  68. public void return_empty_message_when_no_message_saved() {
  69. mockProperty(SONAR_LOGIN_DISPLAY_MESSAGE, "true");
  70. TestResponse response = ws.newRequest().execute();
  71. assertThat(response.getInput()).isEqualTo(EMPTY_JSON_RESPONSE);
  72. }
  73. @Test
  74. public void return_empty_message_when_feature_not_enabled() {
  75. mockProperty(SONAR_LOGIN_DISPLAY_MESSAGE, "true");
  76. mockProperty(SONAR_LOGIN_MESSAGE, LOGIN_MESSAGE_TEXT);
  77. when(loginMessageFeature.isAvailable()).thenReturn(false);
  78. TestResponse response = ws.newRequest().execute();
  79. assertThat(response.getInput()).isEqualTo(EMPTY_JSON_RESPONSE);
  80. }
  81. @Test
  82. public void return_empty_message_when_login_message_flag_is_disabled() {
  83. mockProperty(SONAR_LOGIN_DISPLAY_MESSAGE, "false");
  84. mockProperty(SONAR_LOGIN_MESSAGE, LOGIN_MESSAGE_TEXT);
  85. TestResponse response = ws.newRequest().execute();
  86. assertThat(response.getInput()).isEqualTo(EMPTY_JSON_RESPONSE);
  87. }
  88. private void mockProperty(String key, String value) {
  89. var propertyDto = mock(PropertyDto.class);
  90. doReturn(value).when(propertyDto).getValue();
  91. doReturn(key).when(propertyDto).getKey();
  92. doReturn(propertyDto).when(propertiesDao).selectGlobalProperty(key);
  93. }
  94. }