use of com.google.iam.v1.TestIamPermissionsResponse in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientSnippets method testTopicPermissions.
/** Example of testing whether the caller has the provided permissions on a topic.
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception {
// [START pubsub_test_topic_permissions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.topics.get");
TopicName topicName = TopicName.create(projectId, topicId);
TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(topicName.toString(), permissions);
return testedPermissions;
}
// [END pubsub_test_topic_permissions]
}
use of com.google.iam.v1.TestIamPermissionsResponse in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientTest method testIamPermissionsTest.
@Test
@SuppressWarnings("all")
public void testIamPermissionsTest() {
TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build();
mockIAMPolicy.addResponse(expectedResponse);
String formattedResource = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]").toString();
List<String> permissions = new ArrayList<>();
TestIamPermissionsResponse actualResponse = client.testIamPermissions(formattedResource, permissions);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
Assert.assertEquals(1, actualRequests.size());
TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0);
Assert.assertEquals(formattedResource, actualRequest.getResource());
Assert.assertEquals(permissions, actualRequest.getPermissionsList());
}
use of com.google.iam.v1.TestIamPermissionsResponse in project google-cloud-java by GoogleCloudPlatform.
the class TopicAdminClientTest method testIamPermissionsTest.
@Test
@SuppressWarnings("all")
public void testIamPermissionsTest() {
TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build();
mockIAMPolicy.addResponse(expectedResponse);
String formattedResource = TopicName.create("[PROJECT]", "[TOPIC]").toString();
List<String> permissions = new ArrayList<>();
TestIamPermissionsResponse actualResponse = client.testIamPermissions(formattedResource, permissions);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
Assert.assertEquals(1, actualRequests.size());
TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0);
Assert.assertEquals(formattedResource, actualRequest.getResource());
Assert.assertEquals(permissions, actualRequest.getPermissionsList());
}
use of com.google.iam.v1.TestIamPermissionsResponse in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method testSubscriptionPermissions.
/** Example of testing whether the caller has the provided permissions on a subscription. */
public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId) throws Exception {
// [START pubsub_test_subscription_permissions]
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.subscriptions.get");
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
TestIamPermissionsResponse testedPermissions = topicAdminClient.testIamPermissions(subscriptionName.toString(), permissions);
return testedPermissions;
}
// [END pubsub_test_subscription_permissions]
}
use of com.google.iam.v1.TestIamPermissionsResponse in project google-cloud-java by GoogleCloudPlatform.
the class ITTopicAdminClientSnippets method replaceTopicPolicyAndTestPermissionsIsSuccessful.
@Test
public void replaceTopicPolicyAndTestPermissionsIsSuccessful() throws Exception {
String topicName = topics[0];
topicAdminClientSnippets.createTopic(topicName);
Policy policy = topicAdminClientSnippets.replaceTopicPolicy(topicName);
assertNotNull(policy.getBindingsCount());
assertTrue(policy.getBindings(0).getRole().equalsIgnoreCase(Role.viewer().toString()));
assertTrue(policy.getBindings(0).getMembers(0).equalsIgnoreCase(Identity.allAuthenticatedUsers().toString()));
TestIamPermissionsResponse response = topicAdminClientSnippets.testTopicPermissions(topicName);
assertTrue(response.getPermissionsList().contains("pubsub.topics.get"));
}
Aggregations