-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathExceptionsApiIntegrationTest.java
More file actions
113 lines (98 loc) · 6.31 KB
/
Copy pathExceptionsApiIntegrationTest.java
File metadata and controls
113 lines (98 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package pl.akmf.ksef.sdk;
import jakarta.xml.bind.JAXBException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.akmf.ksef.sdk.api.builders.permission.entity.GrantEntityPermissionsRequestBuilder;
import pl.akmf.ksef.sdk.client.model.ApiException;
import pl.akmf.ksef.sdk.client.model.ForbiddenApiException;
import pl.akmf.ksef.sdk.client.model.ForbiddenProblemDetails;
import pl.akmf.ksef.sdk.client.model.UnauthorizedApiException;
import pl.akmf.ksef.sdk.client.model.UnauthorizedProblemDetails;
import pl.akmf.ksef.sdk.client.model.permission.OperationResponse;
import pl.akmf.ksef.sdk.client.model.permission.PermissionStatusInfo;
import pl.akmf.ksef.sdk.client.model.permission.entity.EntityPermission;
import pl.akmf.ksef.sdk.client.model.permission.entity.EntityPermissionType;
import pl.akmf.ksef.sdk.client.model.permission.entity.GrantEntityPermissionsRequest;
import pl.akmf.ksef.sdk.client.model.permission.entity.SubjectIdentifier;
import pl.akmf.ksef.sdk.configuration.BaseIntegrationTest;
import pl.akmf.ksef.sdk.util.IdentifierGeneratorUtils;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static pl.akmf.ksef.sdk.api.Url.GRANT_INVOICE_SUBJECT_PERMISSION;
class ExceptionsApiIntegrationTest extends BaseIntegrationTest {
@Test
void forbiddenTest() throws JAXBException, IOException, ApiException {
String contextNip = IdentifierGeneratorUtils.generateRandomNIP();
String subjectNip = IdentifierGeneratorUtils.generateRandomNIP();
String thirdNip = IdentifierGeneratorUtils.generateRandomNIP();
String accessToken = authWithCustomNip(contextNip, contextNip).accessToken();
String grantReferenceNumber = grantPermission(subjectNip, accessToken);
await().pollDelay(Duration.ZERO)
.atMost(30, SECONDS)
.pollInterval(2, SECONDS)
.until(() -> isOperationFinish(grantReferenceNumber, accessToken));
String accessTokenSubject = authWithCustomNip(contextNip, subjectNip).accessToken();
ApiException apiException = assertThrows(ApiException.class, () ->
grantPermission(thirdNip, accessTokenSubject));
Assertions.assertEquals(403, apiException.getCode());
Assertions.assertEquals("POST", apiException.getMethod());
Assertions.assertTrue(apiException.getUrl().contains(GRANT_INVOICE_SUBJECT_PERMISSION.getUrl()));
ForbiddenApiException forbiddenApiException = (ForbiddenApiException) apiException;
Assertions.assertNotNull(forbiddenApiException);
ForbiddenProblemDetails forbiddenProblemDetails = forbiddenApiException.getForbiddenProblemDetails();
Assertions.assertNotNull(forbiddenProblemDetails);
Assertions.assertEquals("Forbidden", forbiddenProblemDetails.getTitle());
Assertions.assertEquals(403, forbiddenProblemDetails.getStatus());
Assertions.assertEquals("missing-permissions", forbiddenProblemDetails.getReasonCode());
Assertions.assertEquals("Brak wymaganych uprawnień do wykonania operacji w bieżącym kontekście.", forbiddenProblemDetails.getDetail());
Assertions.assertNotNull(forbiddenProblemDetails.getInstance());
Assertions.assertNotNull(forbiddenProblemDetails.getTraceId());
Map<String, Object> security = forbiddenProblemDetails.getSecurity();
Assertions.assertNotNull(security);
Assertions.assertTrue(security.get("requiredAnyOfPermissions").toString().contains("CredentialsManage"));
Assertions.assertTrue(security.get("presentPermissions").toString().contains("InvoiceRead"));
Assertions.assertTrue(security.get("presentPermissions").toString().contains("InvoiceWrite"));
}
@Test
void unauthorizedTest() {
String subjectNip = IdentifierGeneratorUtils.generateRandomNIP();
ApiException apiException = assertThrows(ApiException.class, () ->
grantPermission(subjectNip, "invalidToken"));
Assertions.assertEquals(401, apiException.getCode());
Assertions.assertEquals("POST", apiException.getMethod());
Assertions.assertTrue(apiException.getUrl().contains(GRANT_INVOICE_SUBJECT_PERMISSION.getUrl()));
UnauthorizedApiException unauthorizedApiException = (UnauthorizedApiException) apiException;
Assertions.assertNotNull(unauthorizedApiException);
UnauthorizedProblemDetails unauthorizedProblemDetails = unauthorizedApiException.getUnauthorizedProblemDetails();
Assertions.assertNotNull(unauthorizedProblemDetails);
Assertions.assertEquals("Unauthorized", unauthorizedProblemDetails.getTitle());
Assertions.assertEquals(401, unauthorizedProblemDetails.getStatus());
Assertions.assertEquals("Wymagane jest uwierzytelnienie.", unauthorizedProblemDetails.getDetail());
Assertions.assertNotNull(unauthorizedProblemDetails.getInstance());
Assertions.assertNotNull(unauthorizedProblemDetails.getTraceId());
}
private Boolean isOperationFinish(String referenceNumber, String accessToken) throws ApiException {
PermissionStatusInfo operations = ksefClient.permissionOperationStatus(referenceNumber, accessToken);
return operations != null && operations.getStatus().getCode() == 200;
}
private String grantPermission(String targetNip, String accessToken) throws ApiException {
GrantEntityPermissionsRequest request = new GrantEntityPermissionsRequestBuilder()
.withPermissions(List.of(
new EntityPermission(EntityPermissionType.INVOICE_READ, true),
new EntityPermission(EntityPermissionType.INVOICE_WRITE, false)))
.withDescription("description")
.withSubjectIdentifier(new SubjectIdentifier(SubjectIdentifier.IdentifierType.NIP, targetNip))
.withSubjectDetails(
new GrantEntityPermissionsRequest.PermissionsEntitySubjectDetails("Testowo")
)
.build();
OperationResponse response = ksefClient.grantsPermissionEntity(request, accessToken);
Assertions.assertNotNull(response);
return response.getReferenceNumber();
}
}