Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest):new endpoint to delete all license information in admin tab #2060

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion rest/resource-server/src/docs/asciidoc/licenses.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ A `DELETE` request will delete a single license.
include::{snippets}/should_document_delete_license/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_delete_license/http-response.adoc[]
include::{snippets}/should_document_delete_license/http-response.adoc[]

[[delete-all-license-info]]
==== Delete all licenses info

A `DELETE` request will delete all licenses info.

===== Example request
include::{snippets}/should_document_delete_all_license_info/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_delete_all_license_info/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,12 @@ private HalResource<License> createHalLicense(License sw360License) {
}
return halLicense;
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = LICENSES_URL + "/deleteAll", method = RequestMethod.DELETE)
public ResponseEntity deleteAllLicense() throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
licenseService.deleteAllLicenseInfo(sw360User);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
Expand Down Expand Up @@ -75,6 +78,15 @@ public void deleteLicenseById(String licenseId, User user) throws TException {
}
}

public void deleteAllLicenseInfo(User user) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, user)) {
RequestSummary deleteLicenseStatus = sw360LicenseClient.deleteAllLicenseInformation(user);
} else {
throw new HttpMessageNotReadableException("Unable to delete license. User is not admin");
}
}

public License createLicense(License license, User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
license.setId(license.getShortname());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void before() throws TException {
given(this.licenseServiceMock.getLicenses()).willReturn(licenseList);
given(this.licenseServiceMock.getLicenseById(eq(license.getId()))).willReturn(license);
Mockito.doNothing().when(licenseServiceMock).deleteLicenseById(any(), any());
Mockito.doNothing().when(licenseServiceMock).deleteAllLicenseInfo(any());
obligation1 = new Obligation();
obligation1.setId("0001");
obligation1.setTitle("Obligation 1");
Expand Down Expand Up @@ -202,4 +203,14 @@ public void should_document_unlink_obligation() throws Exception {
.header("Authorization", "Bearer " + accessToken))
.andExpect(status().isOk());
}

@Test
public void should_document_delete_all_license_info() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(delete("/api/licenses/" + "/delete")
.header("Authorization", "Bearer " + accessToken)
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk());
}

}