mirror of
https://github.com/google/nomulus.git
synced 2025-05-06 06:57:50 +02:00
This adds actual subdomain verification via the SafeBrowsing API to the Spec11 pipeline, as well as on-the-fly KMS decryption via the GenerateSpec11Action to securely store our API key in source code. Testing the interaction becomes difficult due to serialization requirements, and will be significantly expanded in the next cl. For now, it verifies basic end-to-end pipeline behavior. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=208092942
100 lines
4 KiB
Java
100 lines
4 KiB
Java
// Copyright 2018 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google.registry.reporting.spec11;
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
|
import static org.mockito.Matchers.any;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import com.google.api.services.dataflow.Dataflow;
|
|
import com.google.api.services.dataflow.Dataflow.Projects;
|
|
import com.google.api.services.dataflow.Dataflow.Projects.Templates;
|
|
import com.google.api.services.dataflow.Dataflow.Projects.Templates.Launch;
|
|
import com.google.api.services.dataflow.model.Job;
|
|
import com.google.api.services.dataflow.model.LaunchTemplateParameters;
|
|
import com.google.api.services.dataflow.model.LaunchTemplateResponse;
|
|
import com.google.api.services.dataflow.model.RuntimeEnvironment;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.net.MediaType;
|
|
import google.registry.testing.FakeResponse;
|
|
import java.io.IOException;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
|
|
/** Unit tests for {@link google.registry.reporting.spec11.GenerateSpec11ReportAction}. */
|
|
@RunWith(JUnit4.class)
|
|
public class GenerateSpec11ReportActionTest {
|
|
|
|
private FakeResponse response;
|
|
private Dataflow dataflow;
|
|
private Projects dataflowProjects;
|
|
private Templates dataflowTemplates;
|
|
private Launch dataflowLaunch;
|
|
|
|
private GenerateSpec11ReportAction action;
|
|
|
|
@Before
|
|
public void setUp() throws IOException {
|
|
response = new FakeResponse();
|
|
dataflow = mock(Dataflow.class);
|
|
|
|
// Establish the Dataflow API call chain
|
|
dataflow = mock(Dataflow.class);
|
|
dataflowProjects = mock(Dataflow.Projects.class);
|
|
dataflowTemplates = mock(Templates.class);
|
|
dataflowLaunch = mock(Launch.class);
|
|
LaunchTemplateResponse launchTemplateResponse = new LaunchTemplateResponse();
|
|
// Ultimately we get back this job response with a given id.
|
|
launchTemplateResponse.setJob(new Job().setReplaceJobId("jobid"));
|
|
when(dataflow.projects()).thenReturn(dataflowProjects);
|
|
when(dataflowProjects.templates()).thenReturn(dataflowTemplates);
|
|
when(dataflowTemplates.launch(any(String.class), any(LaunchTemplateParameters.class)))
|
|
.thenReturn(dataflowLaunch);
|
|
when(dataflowLaunch.setGcsPath(any(String.class))).thenReturn(dataflowLaunch);
|
|
when(dataflowLaunch.execute()).thenReturn(launchTemplateResponse);
|
|
}
|
|
|
|
@Test
|
|
public void testLaunch_success() throws IOException {
|
|
action =
|
|
new GenerateSpec11ReportAction(
|
|
"test",
|
|
"gs://my-bucket-beam",
|
|
"gs://template",
|
|
"us-east1-c",
|
|
"api_key/a",
|
|
response,
|
|
dataflow);
|
|
action.run();
|
|
|
|
LaunchTemplateParameters expectedLaunchTemplateParameters =
|
|
new LaunchTemplateParameters()
|
|
.setJobName("spec11_action")
|
|
.setEnvironment(
|
|
new RuntimeEnvironment()
|
|
.setZone("us-east1-c")
|
|
.setTempLocation("gs://my-bucket-beam/temporary"))
|
|
.setParameters(ImmutableMap.of("safeBrowsingApiKey", "api_key/a"));
|
|
verify(dataflowTemplates).launch("test", expectedLaunchTemplateParameters);
|
|
verify(dataflowLaunch).setGcsPath("gs://template");
|
|
assertThat(response.getStatus()).isEqualTo(200);
|
|
assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
|
|
assertThat(response.getPayload()).isEqualTo("Launched Spec11 dataflow template.");
|
|
}
|
|
}
|