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.

HashActionTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.source.ws;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.component.ComponentDto;
  27. import org.sonar.db.source.FileSourceDto;
  28. import org.sonar.server.component.TestComponentFinder;
  29. import org.sonar.server.exceptions.ForbiddenException;
  30. import org.sonar.server.exceptions.NotFoundException;
  31. import org.sonar.server.tester.UserSessionRule;
  32. import org.sonar.server.ws.TestRequest;
  33. import org.sonar.server.ws.WsActionTester;
  34. import static java.lang.String.format;
  35. import static java.util.Collections.singletonList;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.sonar.api.web.UserRole.USER;
  38. import static org.sonar.db.component.ComponentTesting.newFileDto;
  39. public class HashActionTest {
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. @Rule
  43. public DbTester db = DbTester.create(System2.INSTANCE);
  44. @Rule
  45. public UserSessionRule userSessionRule = UserSessionRule.standalone();
  46. private final WsActionTester tester = new WsActionTester(new HashAction(db.getDbClient(), userSessionRule, TestComponentFinder.from(db)));
  47. @Test
  48. public void show_hashes() {
  49. ComponentDto project = db.components().insertPrivateProject();
  50. ComponentDto file = db.components().insertComponent(newFileDto(project));
  51. FileSourceDto fileSource = db.fileSources().insertFileSource(file, f -> f.setLineHashes(singletonList("ABC")));
  52. loginAsProjectViewer(project);
  53. TestRequest request = tester.newRequest().setParam("key", file.getDbKey());
  54. assertThat(request.execute().getInput()).isEqualTo("ABC");
  55. }
  56. @Test
  57. public void hashes_empty_if_no_source() {
  58. ComponentDto project = db.components().insertPrivateProject();
  59. ComponentDto file = db.components().insertComponent(newFileDto(project));
  60. loginAsProjectViewer(project);
  61. TestRequest request = tester.newRequest().setParam("key", file.getKey());
  62. assertThat(request.execute().getStatus()).isEqualTo(204);
  63. }
  64. @Test
  65. public void fail_to_show_hashes_if_file_does_not_exist() {
  66. expectedException.expect(NotFoundException.class);
  67. tester.newRequest().setParam("key", "unknown").execute();
  68. }
  69. @Test
  70. public void fail_when_using_branch_db_key() {
  71. ComponentDto project = db.components().insertPrivateProject();
  72. ComponentDto branch = db.components().insertProjectBranch(project);
  73. loginAsProjectViewer(project);
  74. expectedException.expect(NotFoundException.class);
  75. expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
  76. tester.newRequest().setParam("key", branch.getDbKey()).execute();
  77. }
  78. @Test
  79. public void fail_on_missing_permission() {
  80. ComponentDto project = db.components().insertPrivateProject();
  81. ComponentDto file = db.components().insertComponent(newFileDto(project));
  82. FileSourceDto fileSource = db.fileSources().insertFileSource(file);
  83. userSessionRule.logIn(db.users().insertUser());
  84. expectedException.expect(ForbiddenException.class);
  85. tester.newRequest().setParam("key", file.getKey()).execute();
  86. }
  87. private void loginAsProjectViewer(ComponentDto project) {
  88. userSessionRule.logIn(db.users().insertUser()).addProjectPermission(USER, project);
  89. }
  90. }