Add the ability to provide credential JSON file to the nomulus tool

This allows us to run nomulus tool programmatically on environments that do not
allow the 3-legged OAuth authentication flow.

The provided JSON file corresponds to a service account, which must have
GAE admin permission and whose client ID must be whitelisted in the config
file.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=226008337
This commit is contained in:
jianglai 2018-12-18 09:25:06 -08:00 committed by Michael Muller
parent 40b05ffb3c
commit 27b6231053
5 changed files with 76 additions and 20 deletions

View file

@ -16,6 +16,7 @@ package google.registry.tools;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -32,11 +33,15 @@ import com.google.api.client.util.store.DataStore;
import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -49,6 +54,9 @@ public class AuthModuleTest {
private static final String ACCESS_TOKEN = "FakeAccessToken";
private static final String REFRESH_TOKEN = "FakeReFreshToken";
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
private final Credential fakeCredential =
new Credential.Builder(
new Credential.AccessMethod() {
@ -154,7 +162,8 @@ public class AuthModuleTest {
@Test
public void test_provideLocalCredentialJson() {
String credentialJson = AuthModule.provideLocalCredentialJson(getSecrets(), getCredential());
String credentialJson =
AuthModule.provideLocalCredentialJson(this::getSecrets, this::getCredential, null);
Map<String, String> jsonMap =
new Gson().fromJson(credentialJson, new TypeToken<Map<String, String>>() {}.getType());
assertThat(jsonMap.get("type")).isEqualTo("authorized_user");
@ -163,6 +172,16 @@ public class AuthModuleTest {
assertThat(jsonMap.get("refresh_token")).isEqualTo(REFRESH_TOKEN);
}
@Test
public void test_provideExternalCredentialJson() throws Exception {
File credentialFile = folder.newFile("credential.json");
Files.write(credentialFile.toPath(), "{some_field: some_value}".getBytes(UTF_8));
String credentialJson =
AuthModule.provideLocalCredentialJson(
this::getSecrets, this::getCredential, credentialFile.getCanonicalPath());
assertThat(credentialJson).isEqualTo("{some_field: some_value}");
}
@Test
public void test_provideCredential() {
Credential cred = getCredential();