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.

TokenActionTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.badge.ws;
  21. import org.assertj.core.api.Assertions;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.mockito.Mockito;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.component.ComponentDto;
  28. import org.sonar.server.exceptions.ForbiddenException;
  29. import org.sonar.server.tester.UserSessionRule;
  30. import org.sonar.server.usertoken.TokenGenerator;
  31. import org.sonar.server.ws.TestRequest;
  32. import org.sonar.server.ws.TestResponse;
  33. import org.sonar.server.ws.WsActionTester;
  34. import static org.mockito.Mockito.when;
  35. public class TokenActionTest {
  36. @Rule
  37. public DbTester db = DbTester.create();
  38. @Rule
  39. public UserSessionRule userSession = UserSessionRule.standalone();
  40. private final TokenGenerator tokenGenerator = Mockito.mock(TokenGenerator.class);
  41. private final WsActionTester ws = new WsActionTester(
  42. new TokenAction(
  43. db.getDbClient(),
  44. tokenGenerator, userSession));
  45. @Test
  46. public void missing_project_parameter_should_fail() {
  47. TestRequest request = ws.newRequest();
  48. Assertions.assertThatThrownBy(request::execute)
  49. .hasMessage("The 'project' parameter is missing")
  50. .isInstanceOf(IllegalArgumentException.class);
  51. }
  52. @Test
  53. public void missing_project_permission_should_fail() {
  54. ComponentDto project = db.components().insertPrivateProject();
  55. TestRequest request = ws.newRequest().setParam("project", project.getKey());
  56. Assertions.assertThatThrownBy(request::execute)
  57. .hasMessage("Insufficient privileges")
  58. .isInstanceOf(ForbiddenException.class);
  59. }
  60. @Test
  61. public void should_generate_token() {
  62. ComponentDto project = db.components().insertPrivateProject();
  63. userSession.logIn().addProjectPermission(UserRole.USER, project);
  64. when(tokenGenerator.generate()).thenReturn("generated_token");
  65. TestResponse response = ws.newRequest().setParam("project", project.getKey()).execute();
  66. response.assertJson("{\"token\":\"generated_token\"}");
  67. }
  68. @Test
  69. public void should_reuse_generated_token() {
  70. ComponentDto project = db.components().insertPrivateProject();
  71. userSession.logIn().addProjectPermission(UserRole.USER, project);
  72. when(tokenGenerator.generate()).thenReturn("generated_token");
  73. // first call, generating the token
  74. TestResponse firstResponse = ws.newRequest().setParam("project", project.getKey()).execute();
  75. firstResponse.assertJson("{\"token\":\"generated_token\"}");
  76. // 2nd call, reusing the existing token
  77. when(tokenGenerator.generate()).thenReturn("never_generated_token");
  78. TestResponse secondResponse = ws.newRequest().setParam("project", project.getKey()).execute();
  79. secondResponse.assertJson("{\"token\":\"generated_token\"}");
  80. }
  81. }