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.4KB

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