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

feat: Added SecurityLogs Related APIs #223

Merged
merged 4 commits into from
Feb 12, 2024
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
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.crowdin.client.projectsgroups.ProjectsGroupsApi;
import com.crowdin.client.reports.ReportsApi;
import com.crowdin.client.screenshots.ScreenshotsApi;
import com.crowdin.client.securitylogs.SecurityLogsApi;
import com.crowdin.client.sourcefiles.SourceFilesApi;
import com.crowdin.client.sourcestrings.SourceStringsApi;
import com.crowdin.client.storage.StorageApi;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class Client extends CrowdinApi {
private final LabelsApi labelsApi;
private final StringCommentsApi stringCommentsApi;
private final BundlesApi bundlesApi;
private final SecurityLogsApi securityLogsApi;
private final NotificationsApi notificationsApi;
private final ApplicationsApi applicationsApi;
private final ClientsApi clientsApi;
Expand Down Expand Up @@ -95,6 +97,7 @@ public Client(Credentials credentials) {
this.labelsApi = new LabelsApi(credentials);
this.stringCommentsApi = new StringCommentsApi(credentials);
this.bundlesApi = new BundlesApi(credentials);
this.securityLogsApi = new SecurityLogsApi(credentials);
this.notificationsApi = new NotificationsApi(credentials);
this.applicationsApi = new ApplicationsApi(credentials);
this.clientsApi = new ClientsApi(credentials);
Expand Down Expand Up @@ -128,6 +131,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) {
this.labelsApi = new LabelsApi(credentials, clientConfig);
this.stringCommentsApi = new StringCommentsApi(credentials, clientConfig);
this.bundlesApi = new BundlesApi(credentials, clientConfig);
this.securityLogsApi = new SecurityLogsApi(credentials, clientConfig);
this.notificationsApi = new NotificationsApi(credentials, clientConfig);
this.applicationsApi = new ApplicationsApi(credentials, clientConfig);
this.clientsApi = new ClientsApi(credentials, clientConfig);
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/crowdin/client/securitylogs/SecurityLogsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.crowdin.client.securitylogs;

import com.crowdin.client.core.CrowdinApi;
import com.crowdin.client.core.http.HttpRequestConfig;
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
import com.crowdin.client.core.http.exceptions.HttpException;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.securitylogs.model.SecurityLogResource;
import com.crowdin.client.securitylogs.model.SecurityLogResourceObject;
import com.crowdin.client.securitylogs.model.SecurityLogsResourceResponseList;

import java.util.Map;
import java.util.Optional;

public class SecurityLogsApi extends CrowdinApi {

public SecurityLogsApi(Credentials credentials) {
super(credentials);
}

public SecurityLogsApi(Credentials credentials, ClientConfig clientConfig) {
super(credentials, clientConfig);
}

/**
* @param userId user Identifier
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @param event value of event
* @param ipAddress value of ipAddress
* @return list of Security logs
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.getMany" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.users.security-logs.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<SecurityLogResource> listUserSecurityLogs(Long userId, Long limit, Integer offset, String event, String ipAddress) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset),
"event", Optional.ofNullable(event),
"ipAddress", Optional.ofNullable(ipAddress)
);
SecurityLogsResourceResponseList securityLogsResponseList = this.httpClient.get(this.url + "/users/" + userId+ "/security-logs", new HttpRequestConfig(queryParams), SecurityLogsResourceResponseList.class);
return SecurityLogsResourceResponseList.to(securityLogsResponseList);
}

/**
* @param userId user Identifier
* @param securityLogId log Identifier
* @return Security log
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/#operation/api.users.security-logs.get" target="_blank"><b>API Documentation</b></a></li>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.users.security-logs.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<SecurityLogResource> getUserSecurityLog(Long userId, Long securityLogId) throws HttpException, HttpBadRequestException {
SecurityLogResourceObject securityLogResourceObject = this.httpClient.get(this.url + "/users/" + userId+ "/security-logs/" + securityLogId, new HttpRequestConfig(), SecurityLogResourceObject.class);
return ResponseObject.of(securityLogResourceObject.getData());
}

/**
* @param limit maximum number of items to retrieve (default 25)
* @param offset starting offset in the collection (default 0)
* @param event value of event
* @param createdAfter String Date in UTC format
* @param createdBefore String Date in UTC format
* @param ipAddress value of ipAddress
* @param userId user Identifier
* @return list of Security logs
* @see <ul>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.getMany" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseList<SecurityLogResource> listOrganizationSecurityLogs(Long limit, Integer offset, String event, String createdAfter, String createdBefore, String ipAddress,Long userId) throws HttpException, HttpBadRequestException {
Map<String, Optional<Object>> queryParams = HttpRequestConfig.buildUrlParams(
"limit", Optional.ofNullable(limit),
"offset", Optional.ofNullable(offset),
"event", Optional.ofNullable(event),
"createdAfter", Optional.ofNullable(createdAfter),
"createdBefore", Optional.ofNullable(createdBefore),
"ipAddress", Optional.ofNullable(ipAddress),
"userId", Optional.ofNullable(userId)
);
SecurityLogsResourceResponseList securityLogsResponseList = this.httpClient.get(this.url + "/security-logs", new HttpRequestConfig(queryParams), SecurityLogsResourceResponseList.class);
return SecurityLogsResourceResponseList.to(securityLogsResponseList);
}

/**
* @param securityLogId log Identifier
* @return Security log
* @see <ul>
* <li><a href="https://developer.crowdin.com/enterprise/api/v2/#operation/api.security-logs.get" target="_blank"><b>Enterprise API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<SecurityLogResource> getOrganizationSecurityLog(Long securityLogId) throws HttpException, HttpBadRequestException {
SecurityLogResourceObject securityLogResourceObject = this.httpClient.get(this.url + "/security-logs/" + securityLogId, new HttpRequestConfig(), SecurityLogResourceObject.class);
return ResponseObject.of(securityLogResourceObject.getData());
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.crowdin.client.securitylogs.model;

import lombok.Data;

@Data
public class SecurityLogResource {
Long id;
String event;
String info;
Long userId;
String location;
String ipAddress;
String deviceName;
String createdAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crowdin.client.securitylogs.model;

import lombok.Data;

@Data
public class SecurityLogResourceObject {
private SecurityLogResource data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.crowdin.client.securitylogs.model;

import com.crowdin.client.core.model.Pagination;
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class SecurityLogsResourceResponseList {
private List<SecurityLogResourceObject> data;
private Pagination pagination;

public static ResponseList<SecurityLogResource> to(SecurityLogsResourceResponseList securityLogsResourceResponseList) {
return ResponseList.of(
securityLogsResourceResponseList.getData().stream()
.map(SecurityLogResourceObject::getData)
.map(ResponseObject::of)
.collect(Collectors.toList()),
securityLogsResourceResponseList.getPagination()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.crowdin.client.securitylogs;

import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import com.crowdin.client.securitylogs.model.SecurityLogResource;
import org.apache.http.client.methods.HttpGet;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SecurityLogsTest extends TestClient {

private final long userId = 4;
private final long securityLogId = 5;

@Override
public List<RequestMock> getMocks() {
return Arrays.asList(
RequestMock.build(this.url + "/users/" + userId + "/security-logs", HttpGet.METHOD_NAME, "api/securitylogs/listSecurityLogs.json"),
RequestMock.build(this.url + "/users/" + userId + "/security-logs/" + securityLogId, HttpGet.METHOD_NAME, "api/securitylogs/getSecurityLogBylogId.json"),
RequestMock.build(this.url + "/security-logs", HttpGet.METHOD_NAME, "api/securitylogs/listOrganizationLogs.json"),
RequestMock.build(this.url + "/security-logs/" + securityLogId, HttpGet.METHOD_NAME, "api/securitylogs/getOrganizationLogBylogId.json")

);
}

@Test
public void listSecurityLogs(){
ResponseList<SecurityLogResource> securityLogResourceResponseList = this.getSecurityLogsApi().listUserSecurityLogs(userId,null, null, null, null);
assertEquals(securityLogResourceResponseList.getData().size(),2);
assertEquals(securityLogResourceResponseList.getData().get(0).getData().getUserId(),userId);
}

@Test
public void getSecurityLogByLogId(){
ResponseObject<SecurityLogResource> securityLogResourceResponseObject = this.getSecurityLogsApi().getUserSecurityLog(userId,securityLogId);
assertEquals(securityLogResourceResponseObject.getData().getId(), securityLogId);
}

@Test
public void listOrganizationLogs(){
ResponseList<SecurityLogResource> securityLogResourceResponseList = this.getSecurityLogsApi().listOrganizationSecurityLogs(null,null,null,null,null,null,null);
assertEquals(securityLogResourceResponseList.getData().size(), 2);
}

@Test
public void getOrganizationLogbylogId(){
ResponseObject<SecurityLogResource> securityLogResourceResponseObject = this.getSecurityLogsApi().getOrganizationSecurityLog(securityLogId);
assertEquals(securityLogResourceResponseObject.getData().getId(), securityLogId);
}

}
12 changes: 12 additions & 0 deletions src/test/resources/api/securitylogs/getOrganizationLogBylogId.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"id": 5,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-19T15:10:43+00:00"
}
}
12 changes: 12 additions & 0 deletions src/test/resources/api/securitylogs/getSecurityLogBylogId.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"id": 5,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-19T15:10:43+00:00"
}
}
33 changes: 33 additions & 0 deletions src/test/resources/api/securitylogs/listOrganizationLogs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": [
{
"data": {
"id": 2,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-19T15:10:43+00:00"
}
},
{
"data": {
"id": 3,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-20T15:10:43+00:00"
}
}

],
"pagination": {
"offset": 0,
"limit": 25
}
}
33 changes: 33 additions & 0 deletions src/test/resources/api/securitylogs/listSecurityLogs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": [
{
"data": {
"id": 2,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-19T15:10:43+00:00"
}
},
{
"data": {
"id": 3,
"event": "Some event",
"info": "Some info",
"userId": 4,
"location": "USA",
"ipAddress": "127.0.0.1",
"deviceName": "MacOs on MacBook",
"createdAt": "2019-09-20T15:10:43+00:00"
}
}

],
"pagination": {
"offset": 0,
"limit": 25
}
}
Loading